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 3 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, see <http://www.gnu.org/licenses/>.
21 stdio is very convenient, but on some systems the file descriptor
22 in FILE* is 8 bits, so it fails when more than 255 files are open.
24 XFILE replaces stdio. It is less efficient, but at least it works
25 when you have lots of files open
27 The main restriction on XFILE is that it doesn't support seeking,
28 and doesn't support O_RDWR. That keeps the code simple.
33 #define XBUFSIZE BUFSIZ
35 static XFILE _x_stdin
= { 0, NULL
, NULL
, XBUFSIZE
, 0, O_RDONLY
, X_IOFBF
, 0 };
36 static XFILE _x_stdout
= { 1, NULL
, NULL
, XBUFSIZE
, 0, O_WRONLY
, X_IOLBF
, 0 };
37 static XFILE _x_stderr
= { 2, NULL
, NULL
, 0, 0, O_WRONLY
, X_IONBF
, 0 };
39 XFILE
*x_stdin
= &_x_stdin
;
40 XFILE
*x_stdout
= &_x_stdout
;
41 XFILE
*x_stderr
= &_x_stderr
;
44 #define X_FLAG_ERROR 2
45 #define X_FLAG_EINVAL 3
47 /* simulate setvbuf() */
48 int x_setvbuf(XFILE
*f
, char *buf
, int mode
, size_t size
)
51 if (f
->bufused
) return -1;
53 /* on files being read full buffering is the only option */
54 if ((f
->open_flags
& O_ACCMODE
) == O_RDONLY
) {
58 /* destroy any earlier buffer */
66 if (f
->buftype
== X_IONBF
) return 0;
68 /* if buffering then we need some size */
69 if (size
== 0) size
= XBUFSIZE
;
77 /* allocate the buffer */
78 static int x_allocate_buffer(XFILE
*f
)
81 if (f
->bufsize
== 0) return 0;
82 f
->buf
= (char *)SMB_MALLOC(f
->bufsize
);
83 if (!f
->buf
) return 0;
89 /* this looks more like open() than fopen(), but that is quite deliberate.
90 I want programmers to *think* about O_EXCL, O_CREAT etc not just
91 get them magically added
93 XFILE
*x_fopen(const char *fname
, int flags
, mode_t mode
)
97 ret
= SMB_MALLOC_P(XFILE
);
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 */
112 ret
->open_flags
= flags
;
114 ret
->fd
= sys_open(fname
, flags
, mode
);
120 x_setvbuf(ret
, NULL
, X_IOFBF
, XBUFSIZE
);
125 XFILE
*x_fdup(const XFILE
*f
)
130 fd
= dup(x_fileno(f
));
135 ret
= SMB_CALLOC_ARRAY(XFILE
, 1);
142 ret
->open_flags
= f
->open_flags
;
143 x_setvbuf(ret
, NULL
, X_IOFBF
, XBUFSIZE
);
147 /* simulate fclose() */
148 int x_fclose(XFILE
*f
)
152 /* make sure we flush any buffered data */
158 /* make sure data can't leak into a later malloc */
159 memset(f
->buf
, 0, f
->bufsize
);
162 /* check the file descriptor given to the function is NOT one of the static
163 * descriptor of this libreary or we will free unallocated memory
165 if (f
!= x_stdin
&& f
!= x_stdout
&& f
!= x_stderr
) {
171 /* simulate fwrite() */
172 size_t x_fwrite(const void *p
, size_t size
, size_t nmemb
, XFILE
*f
)
177 /* we might be writing unbuffered */
178 if (f
->buftype
== X_IONBF
||
179 (!f
->buf
&& !x_allocate_buffer(f
))) {
180 ret
= write(f
->fd
, p
, size
*nmemb
);
181 if (ret
== -1) return -1;
186 while (total
< size
*nmemb
) {
187 size_t n
= f
->bufsize
- f
->bufused
;
188 n
= MIN(n
, (size
*nmemb
)-total
);
191 /* it's full, flush it */
196 memcpy(f
->buf
+ f
->bufused
, total
+(const char *)p
, n
);
201 /* when line buffered we need to flush at the last linefeed. This can
202 flush a bit more than necessary, but that is harmless */
203 if (f
->buftype
== X_IOLBF
&& f
->bufused
) {
205 for (i
=(size
*nmemb
)-1; i
>=0; i
--) {
206 if (*(i
+(const char *)p
) == '\n') {
216 /* thank goodness for asprintf() */
217 int x_vfprintf(XFILE
*f
, const char *format
, va_list ap
)
225 len
= vasprintf(&p
, format
, ap2
);
226 if (len
<= 0) return len
;
227 ret
= x_fwrite(p
, 1, len
, f
);
232 int x_fprintf(XFILE
*f
, const char *format
, ...)
237 va_start(ap
, format
);
238 ret
= x_vfprintf(f
, format
, ap
);
243 /* at least fileno() is simple! */
244 int x_fileno(const XFILE
*f
)
249 /* simulate fflush() */
250 int x_fflush(XFILE
*f
)
254 if (f
->flags
& X_FLAG_ERROR
) return -1;
256 if ((f
->open_flags
& O_ACCMODE
) != O_WRONLY
) {
261 if (f
->bufused
== 0 || !f
->buf
) return 0;
263 ret
= write(f
->fd
, f
->buf
, f
->bufused
);
264 if (ret
== -1) return -1;
267 if (f
->bufused
> 0) {
268 f
->flags
|= X_FLAG_ERROR
;
269 memmove(f
->buf
, ret
+ (char *)f
->buf
, f
->bufused
);
276 /* simulate setbuffer() */
277 void x_setbuffer(XFILE
*f
, char *buf
, size_t size
)
279 x_setvbuf(f
, buf
, buf
?X_IOFBF
:X_IONBF
, size
);
282 /* simulate setbuf() */
283 void x_setbuf(XFILE
*f
, char *buf
)
285 x_setvbuf(f
, buf
, buf
?X_IOFBF
:X_IONBF
, XBUFSIZE
);
288 /* simulate setlinebuf() */
289 void x_setlinebuf(XFILE
*f
)
291 x_setvbuf(f
, NULL
, X_IOLBF
, 0);
295 /* simulate feof() */
298 if (f
->flags
& X_FLAG_EOF
) return 1;
302 /* simulate ferror() */
303 int x_ferror(XFILE
*f
)
305 if (f
->flags
& X_FLAG_ERROR
) return 1;
309 /* fill the read buffer */
310 static void x_fillbuf(XFILE
*f
)
314 if (f
->bufused
) return;
316 if (!f
->buf
&& !x_allocate_buffer(f
)) return;
318 n
= read(f
->fd
, f
->buf
, f
->bufsize
);
324 /* simulate fgetc() */
325 int x_fgetc(XFILE
*f
)
329 if (f
->flags
& (X_FLAG_EOF
| X_FLAG_ERROR
)) return EOF
;
331 if (f
->bufused
== 0) x_fillbuf(f
);
333 if (f
->bufused
== 0) {
334 f
->flags
|= X_FLAG_EOF
;
338 ret
= *(unsigned char *)(f
->next
);
345 size_t x_fread(void *p
, size_t size
, size_t nmemb
, XFILE
*f
)
348 while (total
< size
*nmemb
) {
351 (total
+(char *)p
)[0] = (char)c
;
357 /* simulate fgets() */
358 char *x_fgets(char *s
, int size
, XFILE
*stream
)
363 int c
= x_fgetc(stream
);
367 if (c
== '\n') break;
369 if (l
==size
|| x_ferror(stream
)) {
376 /* trivial seek, works only for SEEK_SET and SEEK_END if SEEK_CUR is
377 * set then an error is returned */
378 off_t
x_tseek(XFILE
*f
, off_t offset
, int whence
)
380 if (f
->flags
& X_FLAG_ERROR
)
383 /* only SEEK_SET and SEEK_END are supported */
384 /* SEEK_CUR needs internal offset counter */
385 if (whence
!= SEEK_SET
&& whence
!= SEEK_END
) {
386 f
->flags
|= X_FLAG_EINVAL
;
391 /* empty the buffer */
392 switch (f
->open_flags
& O_ACCMODE
) {
397 if (x_fflush(f
) != 0)
405 f
->flags
&= ~X_FLAG_EOF
;
406 return (off_t
)sys_lseek(f
->fd
, offset
, whence
);