r23779: Change from v2 or later to v3 or later.
[Samba.git] / source / lib / xfile.c
blobc90cf2078fe12df65fdf8d5a5d7a8d89c5ade2e2
1 /*
2 Unix SMB/CIFS implementation.
3 stdio replacement
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, 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.
32 #include "includes.h"
34 #define XBUFSIZE BUFSIZ
36 static XFILE _x_stdin = { 0, NULL, NULL, XBUFSIZE, 0, O_RDONLY, X_IOFBF, 0 };
37 static XFILE _x_stdout = { 1, NULL, NULL, XBUFSIZE, 0, O_WRONLY, X_IOLBF, 0 };
38 static XFILE _x_stderr = { 2, NULL, NULL, 0, 0, O_WRONLY, X_IONBF, 0 };
40 XFILE *x_stdin = &_x_stdin;
41 XFILE *x_stdout = &_x_stdout;
42 XFILE *x_stderr = &_x_stderr;
44 #define X_FLAG_EOF 1
45 #define X_FLAG_ERROR 2
46 #define X_FLAG_EINVAL 3
48 /* simulate setvbuf() */
49 int x_setvbuf(XFILE *f, char *buf, int mode, size_t size)
51 x_fflush(f);
52 if (f->bufused) return -1;
54 /* on files being read full buffering is the only option */
55 if ((f->open_flags & O_ACCMODE) == O_RDONLY) {
56 mode = X_IOFBF;
59 /* destroy any earlier buffer */
60 SAFE_FREE(f->buf);
61 f->buf = 0;
62 f->bufsize = 0;
63 f->next = NULL;
64 f->bufused = 0;
65 f->buftype = mode;
67 if (f->buftype == X_IONBF) return 0;
69 /* if buffering then we need some size */
70 if (size == 0) size = XBUFSIZE;
72 f->bufsize = size;
73 f->bufused = 0;
75 return 0;
78 /* allocate the buffer */
79 static int x_allocate_buffer(XFILE *f)
81 if (f->buf) return 1;
82 if (f->bufsize == 0) return 0;
83 f->buf = (char *)SMB_MALLOC(f->bufsize);
84 if (!f->buf) return 0;
85 f->next = f->buf;
86 return 1;
90 /* this looks more like open() than fopen(), but that is quite deliberate.
91 I want programmers to *think* about O_EXCL, O_CREAT etc not just
92 get them magically added
94 XFILE *x_fopen(const char *fname, int flags, mode_t mode)
96 XFILE *ret;
98 ret = SMB_MALLOC_P(XFILE);
99 if (!ret) {
100 return NULL;
103 memset(ret, 0, sizeof(XFILE));
105 if ((flags & O_ACCMODE) == O_RDWR) {
106 /* we don't support RDWR in XFILE - use file
107 descriptors instead */
108 SAFE_FREE(ret);
109 errno = EINVAL;
110 return NULL;
113 ret->open_flags = flags;
115 ret->fd = sys_open(fname, flags, mode);
116 if (ret->fd == -1) {
117 SAFE_FREE(ret);
118 return NULL;
121 x_setvbuf(ret, NULL, X_IOFBF, XBUFSIZE);
123 return ret;
126 XFILE *x_fdup(const XFILE *f)
128 XFILE *ret;
129 int fd;
131 fd = dup(x_fileno(f));
132 if (fd < 0) {
133 return NULL;
136 ret = SMB_CALLOC_ARRAY(XFILE, 1);
137 if (!ret) {
138 close(fd);
139 return NULL;
142 ret->fd = fd;
143 ret->open_flags = f->open_flags;
144 x_setvbuf(ret, NULL, X_IOFBF, XBUFSIZE);
145 return ret;
148 /* simulate fclose() */
149 int x_fclose(XFILE *f)
151 int ret;
153 /* make sure we flush any buffered data */
154 x_fflush(f);
156 ret = close(f->fd);
157 f->fd = -1;
158 if (f->buf) {
159 /* make sure data can't leak into a later malloc */
160 memset(f->buf, 0, f->bufsize);
161 SAFE_FREE(f->buf);
163 /* check the file descriptor given to the function is NOT one of the static
164 * descriptor of this libreary or we will free unallocated memory
165 * --sss */
166 if (f != x_stdin && f != x_stdout && f != x_stderr) {
167 SAFE_FREE(f);
169 return ret;
172 /* simulate fwrite() */
173 size_t x_fwrite(const void *p, size_t size, size_t nmemb, XFILE *f)
175 ssize_t ret;
176 size_t total=0;
178 /* we might be writing unbuffered */
179 if (f->buftype == X_IONBF ||
180 (!f->buf && !x_allocate_buffer(f))) {
181 ret = write(f->fd, p, size*nmemb);
182 if (ret == -1) return -1;
183 return ret/size;
187 while (total < size*nmemb) {
188 size_t n = f->bufsize - f->bufused;
189 n = MIN(n, (size*nmemb)-total);
191 if (n == 0) {
192 /* it's full, flush it */
193 x_fflush(f);
194 continue;
197 memcpy(f->buf + f->bufused, total+(const char *)p, n);
198 f->bufused += n;
199 total += n;
202 /* when line buffered we need to flush at the last linefeed. This can
203 flush a bit more than necessary, but that is harmless */
204 if (f->buftype == X_IOLBF && f->bufused) {
205 int i;
206 for (i=(size*nmemb)-1; i>=0; i--) {
207 if (*(i+(const char *)p) == '\n') {
208 x_fflush(f);
209 break;
214 return total/size;
217 /* thank goodness for asprintf() */
218 int x_vfprintf(XFILE *f, const char *format, va_list ap)
220 char *p;
221 int len, ret;
222 va_list ap2;
224 VA_COPY(ap2, ap);
226 len = vasprintf(&p, format, ap2);
227 if (len <= 0) return len;
228 ret = x_fwrite(p, 1, len, f);
229 SAFE_FREE(p);
230 return ret;
233 int x_fprintf(XFILE *f, const char *format, ...)
235 va_list ap;
236 int ret;
238 va_start(ap, format);
239 ret = x_vfprintf(f, format, ap);
240 va_end(ap);
241 return ret;
244 /* at least fileno() is simple! */
245 int x_fileno(const XFILE *f)
247 return f->fd;
250 /* simulate fflush() */
251 int x_fflush(XFILE *f)
253 int ret;
255 if (f->flags & X_FLAG_ERROR) return -1;
257 if ((f->open_flags & O_ACCMODE) != O_WRONLY) {
258 errno = EINVAL;
259 return -1;
262 if (f->bufused == 0 || !f->buf) return 0;
264 ret = write(f->fd, f->buf, f->bufused);
265 if (ret == -1) return -1;
267 f->bufused -= ret;
268 if (f->bufused > 0) {
269 f->flags |= X_FLAG_ERROR;
270 memmove(f->buf, ret + (char *)f->buf, f->bufused);
271 return -1;
274 return 0;
277 /* simulate setbuffer() */
278 void x_setbuffer(XFILE *f, char *buf, size_t size)
280 x_setvbuf(f, buf, buf?X_IOFBF:X_IONBF, size);
283 /* simulate setbuf() */
284 void x_setbuf(XFILE *f, char *buf)
286 x_setvbuf(f, buf, buf?X_IOFBF:X_IONBF, XBUFSIZE);
289 /* simulate setlinebuf() */
290 void x_setlinebuf(XFILE *f)
292 x_setvbuf(f, NULL, X_IOLBF, 0);
296 /* simulate feof() */
297 int x_feof(XFILE *f)
299 if (f->flags & X_FLAG_EOF) return 1;
300 return 0;
303 /* simulate ferror() */
304 int x_ferror(XFILE *f)
306 if (f->flags & X_FLAG_ERROR) return 1;
307 return 0;
310 /* fill the read buffer */
311 static void x_fillbuf(XFILE *f)
313 int n;
315 if (f->bufused) return;
317 if (!f->buf && !x_allocate_buffer(f)) return;
319 n = read(f->fd, f->buf, f->bufsize);
320 if (n <= 0) return;
321 f->bufused = n;
322 f->next = f->buf;
325 /* simulate fgetc() */
326 int x_fgetc(XFILE *f)
328 int ret;
330 if (f->flags & (X_FLAG_EOF | X_FLAG_ERROR)) return EOF;
332 if (f->bufused == 0) x_fillbuf(f);
334 if (f->bufused == 0) {
335 f->flags |= X_FLAG_EOF;
336 return EOF;
339 ret = *(unsigned char *)(f->next);
340 f->next++;
341 f->bufused--;
342 return ret;
345 /* simulate fread */
346 size_t x_fread(void *p, size_t size, size_t nmemb, XFILE *f)
348 size_t total = 0;
349 while (total < size*nmemb) {
350 int c = x_fgetc(f);
351 if (c == EOF) break;
352 (total+(char *)p)[0] = (char)c;
353 total++;
355 return total/size;
358 /* simulate fgets() */
359 char *x_fgets(char *s, int size, XFILE *stream)
361 char *s0 = s;
362 int l = size;
363 while (l>1) {
364 int c = x_fgetc(stream);
365 if (c == EOF) break;
366 *s++ = (char)c;
367 l--;
368 if (c == '\n') break;
370 if (l==size || x_ferror(stream)) {
371 return 0;
373 *s = 0;
374 return s0;
377 /* trivial seek, works only for SEEK_SET and SEEK_END if SEEK_CUR is
378 * set then an error is returned */
379 off_t x_tseek(XFILE *f, off_t offset, int whence)
381 if (f->flags & X_FLAG_ERROR)
382 return -1;
384 /* only SEEK_SET and SEEK_END are supported */
385 /* SEEK_CUR needs internal offset counter */
386 if (whence != SEEK_SET && whence != SEEK_END) {
387 f->flags |= X_FLAG_EINVAL;
388 errno = EINVAL;
389 return -1;
392 /* empty the buffer */
393 switch (f->open_flags & O_ACCMODE) {
394 case O_RDONLY:
395 f->bufused = 0;
396 break;
397 case O_WRONLY:
398 if (x_fflush(f) != 0)
399 return -1;
400 break;
401 default:
402 errno = EINVAL;
403 return -1;
406 f->flags &= ~X_FLAG_EOF;
407 return (off_t)sys_lseek(f->fd, offset, whence);