1 /*
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * and/or other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17 /* No user fns here. Pesch 15apr92. */
18
19 #define _DEFAULT_SOURCE
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 ssize_t
__sread(void * cookie,char * buf,size_t n)32 __sread (
33 void *cookie,
34 char *buf,
35 size_t n)
36 {
37 register FILE *fp = (FILE *) cookie;
38 register ssize_t 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 (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 /* Dummy function used in sscanf/swscanf. */
63 ssize_t
__seofread(void * cookie,char * buf,size_t len)64 __seofread (
65 void *cookie,
66 char *buf,
67 size_t len)
68 {
69 (void) cookie;
70 (void) buf;
71 (void) len;
72 return 0;
73 }
74
75 ssize_t
__swrite(void * cookie,char const * buf,size_t n)76 __swrite (
77 void *cookie,
78 char const *buf,
79 size_t n)
80 {
81 register FILE *fp = (FILE *) cookie;
82 ssize_t w;
83 #ifdef __SCLE
84 int oldmode=0;
85 #endif
86
87 if (fp->_flags & __SAPP)
88 lseek (fp->_file, (_off_t) 0, SEEK_END);
89 fp->_flags &= ~__SOFF; /* in case O_APPEND mode is set */
90
91 #ifdef __SCLE
92 if (fp->_flags & __SCLE)
93 oldmode = setmode (fp->_file, O_BINARY);
94 #endif
95
96 w = write (fp->_file, buf, n);
97
98 #ifdef __SCLE
99 if (oldmode)
100 setmode (fp->_file, oldmode);
101 #endif
102
103 return w;
104 }
105
106 _fpos_t
__sseek(void * cookie,_fpos_t offset,int whence)107 __sseek (
108 void *cookie,
109 _fpos_t offset,
110 int whence)
111 {
112 register FILE *fp = (FILE *) cookie;
113 register _off_t ret;
114
115 ret = lseek (fp->_file, (_off_t) offset, whence);
116 if (ret == -1L)
117 fp->_flags &= ~__SOFF;
118 else
119 {
120 fp->_flags |= __SOFF;
121 fp->_offset = ret;
122 }
123 return ret;
124 }
125
126 int
__sclose(void * cookie)127 __sclose (
128 void *cookie)
129 {
130 FILE *fp = (FILE *) cookie;
131
132 return close (fp->_file);
133 }
134
135 #ifdef __SCLE
136 int
__stextmode(int fd)137 __stextmode (int fd)
138 {
139 return 0;
140 }
141 #endif
142