2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2001
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 stdio is very convenient, but on some systems the file descriptor
23 in FILE* is 8 bits, so it fails when more than 255 files are open.
25 XFILE replaces stdio. It is less efficient, but at least it works
26 when you have lots of files open
28 The main restriction on XFILE is that it doesn't support seeking,
29 and doesn't support O_RDWR. That keeps the code simple.
33 #include "system/filesys.h"
35 #define XBUFSIZE BUFSIZ
37 static XFILE _x_stdin
= { 0, NULL
, NULL
, XBUFSIZE
, 0, O_RDONLY
, X_IOFBF
, 0 };
38 static XFILE _x_stdout
= { 1, NULL
, NULL
, XBUFSIZE
, 0, O_WRONLY
, X_IOLBF
, 0 };
39 static XFILE _x_stderr
= { 2, NULL
, NULL
, 0, 0, O_WRONLY
, X_IONBF
, 0 };
41 XFILE
*x_stdin
= &_x_stdin
;
42 XFILE
*x_stdout
= &_x_stdout
;
43 XFILE
*x_stderr
= &_x_stderr
;
46 #define X_FLAG_ERROR 2
47 #define X_FLAG_EINVAL 3
49 /* simulate setvbuf() */
50 int x_setvbuf(XFILE
*f
, char *buf
, int mode
, size_t size
)
53 if (f
->bufused
) return -1;
55 /* on files being read full buffering is the only option */
56 if ((f
->open_flags
& O_ACCMODE
) == O_RDONLY
) {
60 /* destroy any earlier buffer */
68 if (f
->buftype
== X_IONBF
) return 0;
70 /* if buffering then we need some size */
71 if (size
== 0) size
= XBUFSIZE
;
79 /* allocate the buffer */
80 static int x_allocate_buffer(XFILE
*f
)
83 if (f
->bufsize
== 0) return 0;
84 f
->buf
= malloc(f
->bufsize
);
85 if (!f
->buf
) return 0;
91 /* this looks more like open() than fopen(), but that is quite deliberate.
92 I want programmers to *think* about O_EXCL, O_CREAT etc not just
93 get them magically added
95 XFILE
*x_fopen(const char *fname
, int flags
, mode_t mode
)
99 ret
= malloc_p(XFILE
);
100 if (!ret
) return NULL
;
102 memset(ret
, 0, sizeof(XFILE
));
104 if ((flags
& O_ACCMODE
) == O_RDWR
) {
105 /* we don't support RDWR in XFILE - use file
106 descriptors instead */
111 ret
->open_flags
= flags
;
113 ret
->fd
= open(fname
, flags
, mode
);
119 x_setvbuf(ret
, NULL
, X_IOFBF
, XBUFSIZE
);
124 /* simulate fclose() */
125 int x_fclose(XFILE
*f
)
129 /* make sure we flush any buffered data */
135 /* make sure data can't leak into a later malloc */
136 memset(f
->buf
, 0, f
->bufsize
);
139 /* check the file descriptor given to the function is NOT one of the static
140 * descriptor of this libreary or we will free unallocated memory
142 if (f
!= x_stdin
&& f
!= x_stdout
&& f
!= x_stderr
) {
148 /* simulate fwrite() */
149 size_t x_fwrite(const void *p
, size_t size
, size_t nmemb
, XFILE
*f
)
154 /* we might be writing unbuffered */
155 if (f
->buftype
== X_IONBF
||
156 (!f
->buf
&& !x_allocate_buffer(f
))) {
157 ret
= write(f
->fd
, p
, size
*nmemb
);
158 if (ret
== -1) return -1;
163 while (total
< size
*nmemb
) {
164 size_t n
= f
->bufsize
- f
->bufused
;
165 n
= MIN(n
, (size
*nmemb
)-total
);
168 /* it's full, flush it */
173 memcpy(f
->buf
+ f
->bufused
, total
+(const char *)p
, n
);
178 /* when line buffered we need to flush at the last linefeed. This can
179 flush a bit more than necessary, but that is harmless */
180 if (f
->buftype
== X_IOLBF
&& f
->bufused
) {
182 for (i
=(size
*nmemb
)-1; i
>=0; i
--) {
183 if (*(i
+(const char *)p
) == '\n') {
193 /* thank goodness for asprintf() */
194 int x_vfprintf(XFILE
*f
, const char *format
, va_list ap
)
202 len
= vasprintf(&p
, format
, ap2
);
203 if (len
<= 0) return len
;
204 ret
= x_fwrite(p
, 1, len
, f
);
209 int x_fprintf(XFILE
*f
, const char *format
, ...)
214 va_start(ap
, format
);
215 ret
= x_vfprintf(f
, format
, ap
);
220 /* at least fileno() is simple! */
221 int x_fileno(XFILE
*f
)
226 /* simulate fflush() */
227 int x_fflush(XFILE
*f
)
231 if (f
->flags
& X_FLAG_ERROR
) return -1;
233 if ((f
->open_flags
& O_ACCMODE
) != O_WRONLY
) {
238 if (f
->bufused
== 0) return 0;
240 ret
= write(f
->fd
, f
->buf
, f
->bufused
);
241 if (ret
== -1) return -1;
244 if (f
->bufused
> 0) {
245 f
->flags
|= X_FLAG_ERROR
;
246 memmove(f
->buf
, ret
+ (char *)f
->buf
, f
->bufused
);
253 /* simulate setbuffer() */
254 void x_setbuffer(XFILE
*f
, char *buf
, size_t size
)
256 x_setvbuf(f
, buf
, buf
?X_IOFBF
:X_IONBF
, size
);
259 /* simulate setbuf() */
260 void x_setbuf(XFILE
*f
, char *buf
)
262 x_setvbuf(f
, buf
, buf
?X_IOFBF
:X_IONBF
, XBUFSIZE
);
265 /* simulate setlinebuf() */
266 void x_setlinebuf(XFILE
*f
)
268 x_setvbuf(f
, NULL
, X_IOLBF
, 0);
272 /* simulate feof() */
275 if (f
->flags
& X_FLAG_EOF
) return 1;
279 /* simulate ferror() */
280 int x_ferror(XFILE
*f
)
282 if (f
->flags
& X_FLAG_ERROR
) return 1;
286 /* fill the read buffer */
287 static void x_fillbuf(XFILE
*f
)
291 if (f
->bufused
) return;
293 if (!f
->buf
&& !x_allocate_buffer(f
)) return;
295 n
= read(f
->fd
, f
->buf
, f
->bufsize
);
301 /* simulate fgetc() */
302 int x_fgetc(XFILE
*f
)
306 if (f
->flags
& (X_FLAG_EOF
| X_FLAG_ERROR
)) return EOF
;
308 if (f
->bufused
== 0) x_fillbuf(f
);
310 if (f
->bufused
== 0) {
311 f
->flags
|= X_FLAG_EOF
;
315 ret
= *(uint8_t *)(f
->next
);
322 size_t x_fread(void *p
, size_t size
, size_t nmemb
, XFILE
*f
)
325 while (total
< size
*nmemb
) {
328 (total
+(char *)p
)[0] = (char)c
;
334 /* simulate fgets() */
335 char *x_fgets(char *s
, int size
, XFILE
*stream
)
340 int c
= x_fgetc(stream
);
344 if (c
== '\n') break;
346 if (l
==size
|| x_ferror(stream
)) {
353 /* trivial seek, works only for SEEK_SET and SEEK_END if SEEK_CUR is
354 * set then an error is returned */
355 off_t
x_tseek(XFILE
*f
, off_t offset
, int whence
)
357 if (f
->flags
& X_FLAG_ERROR
)
360 /* only SEEK_SET and SEEK_END are supported */
361 /* SEEK_CUR needs internal offset counter */
362 if (whence
!= SEEK_SET
&& whence
!= SEEK_END
) {
363 f
->flags
|= X_FLAG_EINVAL
;
368 /* empty the buffer */
369 switch (f
->open_flags
& O_ACCMODE
) {
374 if (x_fflush(f
) != 0)
382 f
->flags
&= ~X_FLAG_EOF
;
383 return lseek(f
->fd
, offset
, whence
);