1 /* No user fns here. Pesch 15apr92. */
2
3 /*
4 * Copyright (c) 1990 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by the University of California, Berkeley. The name of the
13 * University may not be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18 */
19
20 #include <stdio.h>
21 #include <sys/types.h>
22 #include <fcntl.h>
23 #include <sys/unistd.h>
24 #include "local.h"
25
26 /*
27 * Small standard I/O/seek/close functions.
28 * These maintain the `known seek offset' for seek optimisation.
29 */
30
31 _READ_WRITE_RETURN_TYPE
__sread(cookie,buf,n)32 __sread (cookie, buf, n)
33 _PTR cookie;
34 char *buf;
35 int n;
36 {
37 register FILE *fp = (FILE *) cookie;
38 register int ret;
39
40 #ifdef __SCLE
41 int oldmode = 0;
42 if (fp->_flags & __SCLE)
43 oldmode = setmode(fp->_file, O_BINARY);
44 #endif
45
46 ret = _read_r (_REENT, fp->_file, buf, n);
47
48 #ifdef __SCLE
49 if (oldmode)
50 setmode(fp->_file, oldmode);
51 #endif
52
53 /* If the read succeeded, update the current offset. */
54
55 if (ret >= 0)
56 fp->_offset += ret;
57 else
58 fp->_flags &= ~__SOFF; /* paranoia */
59 return ret;
60 }
61
62 _READ_WRITE_RETURN_TYPE
__swrite(cookie,buf,n)63 __swrite (cookie, buf, n)
64 _PTR cookie;
65 char _CONST *buf;
66 int n;
67 {
68 register FILE *fp = (FILE *) cookie;
69 int w;
70 #ifdef __SCLE
71 int oldmode=0;
72 #endif
73
74 if (fp->_flags & __SAPP)
75 (void) _lseek_r (_REENT, fp->_file, (_off_t) 0, SEEK_END);
76 fp->_flags &= ~__SOFF; /* in case O_APPEND mode is set */
77
78 #ifdef __SCLE
79 if (fp->_flags & __SCLE)
80 oldmode = setmode(fp->_file, O_BINARY);
81 #endif
82
83 w = _write_r (_REENT, fp->_file, buf, n);
84
85 #ifdef __SCLE
86 if (oldmode)
87 setmode(fp->_file, oldmode);
88 #endif
89
90 return w;
91 }
92
93 _fpos_t
__sseek(cookie,offset,whence)94 __sseek (cookie, offset, whence)
95 _PTR cookie;
96 _fpos_t offset;
97 int whence;
98 {
99 register FILE *fp = (FILE *) cookie;
100 register _off_t ret;
101
102 ret = _lseek_r (_REENT, fp->_file, (_off_t) offset, whence);
103 if (ret == -1L)
104 fp->_flags &= ~__SOFF;
105 else
106 {
107 fp->_flags |= __SOFF;
108 fp->_offset = ret;
109 }
110 return ret;
111 }
112
113 int
__sclose(cookie)114 __sclose (cookie)
115 _PTR cookie;
116 {
117 FILE *fp = (FILE *) cookie;
118
119 return _close_r (_REENT, fp->_file);
120 }
121
122 #ifdef __SCLE
123 int
__stextmode(int fd)124 __stextmode (int fd)
125 {
126 #ifdef __CYGWIN__
127 return _cygwin_istext_for_stdio (fd);
128 #else
129 return 0;
130 #endif
131 }
132 #endif
133