1 /**
2  * D header file for C99.
3  *
4  * Copyright: Public Domain
5  * License:   Public Domain
6  * Authors:   Sean Kelly, Walter Bright
7  * Standards: ISO/IEC 9899:1999 (E)
8  */
9 module tango.stdc.stdio;
10 
11 private
12 {
13     import tango.stdc.stdarg;
14     import tango.stdc.stddef;
15     import tango.stdc.config;
16 }
17 
18 extern (C){
19 
20     version( Win32 )
21     {
22         const int BUFSIZ         = 0x4000;
23         const int EOF            = -1;
24         const int FOPEN_MAX      = 20;
25         const int FILENAME_MAX   = 256; // 255 plus NULL
26         const int TMP_MAX        = 32767;
27         const int _SYS_OPEN      = 20;
28         const int SYS_OPEN       = _SYS_OPEN;
29 
30         const int     _NFILE     = 60;
31         const char[]  _P_tmpdir  = "\\";
32         const wchar[] _wP_tmpdir = "\\";
33         const int     L_tmpnam   = _P_tmpdir.length + 12;
34     }
35     else version( linux )
36     {
37         //const int BUFSIZ      = 0x4000;
38         const int EOF           = -1;
39         const int FOPEN_MAX     = 16;
40         const int FILENAME_MAX  = 4095;
41         const int TMP_MAX       = 238328;
42         const int L_tmpnam      = 20;
43     }
44     else version( darwin )
45     {
46         const int EOF           = -1;
47         const int FOPEN_MAX     = 20;
48         const int FILENAME_MAX  = 1024;
49         const int TMP_MAX       = 308915776;
50         const int L_tmpnam      = 1024;
51 
52         private
53         {
54             struct __sbuf
55             {
56                 ubyte*  _base;
57                 int     _size;
58             }
59 
60             struct __sFILEX
61             {
62 
63             }
64         }
65     }
66     else version (FreeBSD)
67     {
68         const int EOF           = -1;
69         const int FOPEN_MAX     = 20;
70         const int FILENAME_MAX  = 1024;
71         const int TMP_MAX       = 308915776;
72         const int L_tmpnam      = 1024;
73 
74         private
75         {
76             struct __sbuf
77             {
78                 ubyte *_base;
79                 int _size;
80             }
81             struct __sFILEX
82             {
83             }
84         }
85     }
86     else version( solaris )
87     {
88         const int BUFSIZ      = 1024;
89         const int EOF           = -1;
90       version (X86_64) {
91         const int _NFILE        = 60;
92       } else {
93         const int _NFILE        = 20;
94       }
95         const int FOPEN_MAX     = _NFILE;
96     
97         const int FILENAME_MAX  = 1024;
98         const int TMP_MAX       = 17576;
99         const int L_tmpnam      = 25;
100     }
101     else
102     {
103         static assert( false );
104     }
105 
106     enum
107     {
108         SEEK_SET,
109         SEEK_CUR,
110         SEEK_END
111     }
112 
113     struct _iobuf
114     {
115         align (1):
116         version( Win32 )
117         {
118             char* _ptr;
119             int   _cnt;
120             char* _base;
121             int   _flag;
122             int   _file;
123             int   _charbuf;
124             int   _bufsiz;
125             int   __tmpnum;
126         }
127         else version( linux )
128         {
129             char*   _read_ptr;
130             char*   _read_end;
131             char*   _read_base;
132             char*   _write_base;
133             char*   _write_ptr;
134             char*   _write_end;
135             char*   _buf_base;
136             char*   _buf_end;
137             char*   _save_base;
138             char*   _backup_base;
139             char*   _save_end;
140             void*   _markers;
141             _iobuf* _chain;
142             int     _fileno;
143             int     _blksize;
144             int     _old_offset;
145             ushort  _cur_column;
146             byte    _vtable_offset;
147             char[1] _shortbuf;
148             void*   _lock;
149         }
150         else version( darwin )
151         {
152             ubyte*    _p;
153             int       _r;
154             int       _w;
155             short     _flags;
156             short     _file;
157             __sbuf    _bf;
158             int       _lbfsize;
159 
160             int* function(void*)                    _close;
161             int* function(void*, char*, int)        _read;
162             fpos_t* function(void*, fpos_t, int)    _seek;
163             int* function(void*, char *, int)       _write;
164 
165             __sbuf    _ub;
166             __sFILEX* _extra;
167             int       _ur;
168 
169             ubyte[3]  _ubuf;
170             ubyte[1]  _nbuf;
171 
172             __sbuf    _lb;
173 
174             int       _blksize;
175             fpos_t    _offset;
176         }
177         else version( FreeBSD )
178         {
179             ubyte*    _p;
180             int       _r;
181             int       _w;
182             short     _flags;
183             short     _file;
184             __sbuf    _bf;
185             int       _lbfsize;
186 
187             void* function()                        _cookie;
188             int* function(void*)                    _close;
189             int* function(void*, char*, int)        _read;
190             fpos_t* function(void*, fpos_t, int)    _seek;
191             int* function(void*, char *, int)       _write;
192 
193             __sbuf    _ub;
194             __sFILEX* _extra;
195             int       _ur;
196 
197             ubyte[3]  _ubuf;
198             ubyte[1]  _nbuf;
199 
200             __sbuf    _lb;
201 
202             int       _blksize;
203             fpos_t    _offset;
204         }
205         else version( solaris )
206         {
207             // From OpenSolaris <ast/sfio_s.h>
208             ubyte*  _next;  /* next position to read/write from */
209             ubyte*  _endw;  /* end of write buffer          */
210             ubyte*  _endr;  /* end of read buffer           */
211             ubyte*  _endb;  /* end of buffer            */
212             _iobuf* _push;  /* the stream that was pushed on    */
213             ushort  _flags; /* type of stream           */
214             short   _file;  /* file descriptor          */
215             ubyte*  _data;  /* base of data buffer          */
216             ptrdiff_t _size;  /* buffer size              */
217             ptrdiff_t _val;   /* values or string lengths     */
218         
219         //  #ifdef _SFIO_PRIVATE
220             // .. I don't think we really need this in D
221         //  #endif
222         }
223         else
224         {
225             static assert( false );
226         }
227     }
228 
229     alias _iobuf FILE;
230 
231     enum
232     {
233         _F_RDWR = 0x0003,
234         _F_READ = 0x0001,
235         _F_WRIT = 0x0002,
236         _F_BUF  = 0x0004,
237         _F_LBUF = 0x0008,
238         _F_ERR  = 0x0010,
239         _F_EOF  = 0x0020,
240         _F_BIN  = 0x0040,
241         _F_IN   = 0x0080,
242         _F_OUT  = 0x0100,
243         _F_TERM = 0x0200,
244     }
245 }
246 version( Win32 )
247 {
248     enum
249     {
250         _IOFBF   = 0,
251         _IOREAD  = 1,
252         _IOWRT   = 2,
253         _IONBF   = 4,
254         _IOMYBUF = 8,
255         _IOEOF   = 0x10,
256         _IOERR   = 0x20,
257         _IOLBF   = 0x40,
258         _IOSTRG  = 0x40,
259         _IORW    = 0x80,
260         _IOTRAN  = 0x100,
261         _IOAPP   = 0x200,
262     }
263 
264     extern extern(C) void function() _fcloseallp;
265 
266     version (GNU)
267     {
268         extern(C){
269             extern __gshared FILE[_NFILE]* _imp___iob;
270         }
271 
272         __gshared FILE* stdin;
273         __gshared FILE* stdout;
274         __gshared FILE* stderr;
275         __gshared FILE* stdaux;
276         __gshared FILE* stdprn;
277 
278         shared static this()
279         {
280             stdin  = &(*_imp___iob)[0];
281             stdout = &(*_imp___iob)[1];
282             stderr = &(*_imp___iob)[2];
283             stdaux = &(*_imp___iob)[3];
284             stdprn = &(*_imp___iob)[4];
285         }
286     }
287     else version (LDC) {
288         extern(C){
289             extern __gshared FILE[_NFILE]* _imp___iob;
290         }
291 
292         __gshared FILE* stdin;
293         __gshared FILE* stdout;
294         __gshared FILE* stderr;
295         __gshared FILE* stdaux;
296         __gshared FILE* stdprn;
297 
298         shared static this()
299         {
300             stdin  = &(*_imp___iob)[0];
301             stdout = &(*_imp___iob)[1];
302             stderr = &(*_imp___iob)[2];
303             stdaux = &(*_imp___iob)[3];
304             stdprn = &(*_imp___iob)[4];
305         }
306     }
307     else
308     {
309         extern(C){
310             extern __gshared FILE[_NFILE] _iob;
311         }
312 
313        __gshared FILE* stdin  = &_iob[0];
314        __gshared FILE* stdout = &_iob[1];
315        __gshared FILE* stderr = &_iob[2];
316        __gshared FILE* stdaux = &_iob[3];
317        __gshared FILE* stdprn = &_iob[4];
318     }
319 } else version( linux ) {
320     enum
321     {
322         _IOFBF = 0,
323         _IOLBF = 1,
324         _IONBF = 2,
325     }
326 
327     extern(C) extern __gshared FILE* stdin;
328     extern(C) extern __gshared FILE* stdout;
329     extern(C) extern __gshared FILE* stderr;
330 }
331 else version( darwin )
332 {
333     extern(C) extern __gshared FILE* __stdinp;
334     extern(C) extern __gshared FILE* __stdoutp;
335     extern(C) extern __gshared FILE* __stderrp;
336 
337     __gshared FILE* stdin;
338     __gshared FILE* stdout;
339     __gshared FILE* stderr;
340 
341     shared static this()
342     {
343         stdin  = __stdinp;
344         stdout = __stdoutp;
345         stderr = __stderrp;
346     }
347 }
348 else version( FreeBSD )
349 {
350     extern(C) extern __gshared FILE* __stdinp;
351     extern(C) extern __gshared FILE* __stdoutp;
352     extern(C) extern __gshared FILE* __stderrp;
353 
354     __gshared FILE* stdin;
355     __gshared FILE* stdout;
356     __gshared FILE* stderr;
357 
358     shared static this()
359     {
360         stdin  = __stdinp;
361         stdout = __stdoutp;
362         stderr = __stderrp;
363     }
364 }
365 else version( solaris )
366 {
367     extern(C) extern __gshared FILE[_NFILE] __iob;
368     
369     __gshared FILE* stdin  = &__iob[0];
370     __gshared FILE* stdout = &__iob[1];
371     __gshared FILE* stderr = &__iob[2];
372 }
373 else
374 {
375     static assert( false );
376 }
377 
378 alias int fpos_t;
379 
380 extern(C){
381     int remove(in char* filename);
382     int rename(in char* from, in char* to);
383 
384     FILE* tmpfile();
385     char* tmpnam(char* s);
386 
387     int   fclose(FILE* stream);
388     int   fflush(FILE* stream);
389     FILE* fopen(in char* filename, in char* mode);
390     FILE* freopen(in char* filename, in char* mode, FILE* stream);
391 
392     void setbuf(FILE* stream, char* buf);
393     int  setvbuf(FILE* stream, char* buf, int mode, size_t size);
394 
395     int fprintf(FILE* stream, in char* format, ...);
396     int fscanf(FILE* stream, in char* format, ...);
397     int sprintf(char* s, in char* format, ...);
398     int sscanf(in char* s, in char* format, ...);
399     int vfprintf(FILE* stream, in char* format, va_list arg);
400     int vfscanf(FILE* stream, in char* format, va_list arg);
401     int vsprintf(char* s, in char* format, va_list arg);
402     int vsscanf(in char* s, in char* format, va_list arg);
403     int vprintf(in char* format, va_list arg);
404     int vscanf(in char* format, va_list arg);
405     int printf(in char* format, ...);
406     int scanf(in char* format, ...);
407 
408     int fgetc(FILE* stream);
409     int fputc(int c, FILE* stream);
410 
411     char* fgets(char* s, int n, FILE* stream);
412     int   fputs(in char* s, FILE* stream);
413     char* gets(char* s);
414     int   puts(in char* s);
415 }
416 
417 extern (D)
418 {
419     int getchar()                 { return getc(stdin);     }
420     int putchar(int c)            { return putc(c,stdout);  }
421     int getc(FILE* stream)        { return fgetc(stream);   }
422     int putc(int c, FILE* stream) { return fputc(c,stream); }
423 }
424 
425 extern(C) {
426     int ungetc(int c, FILE* stream);
427 
428     size_t fread(void* ptr, size_t size, size_t nmemb, FILE* stream);
429     size_t fwrite(in void* ptr, size_t size, size_t nmemb, FILE* stream);
430 
431     int fgetpos(FILE* stream, fpos_t * pos);
432     int fsetpos(FILE* stream, in fpos_t* pos);
433 
434     int    fseek(FILE* stream, c_long offset, int whence);
435     c_long ftell(FILE* stream);
436 }
437 
438 extern(C):
439 
440 version( Win32 )
441 {
442   extern (D)
443   {
444     void rewind(FILE* stream)   { fseek(stream,0L,SEEK_SET); stream._flag&=~_IOERR; }
445     void clearerr(FILE* stream) { stream._flag &= ~(_IOERR|_IOEOF);                 }
446     int  feof(FILE* stream)     { return stream._flag&_IOEOF;                       }
447     int  ferror(FILE* stream)   { return stream._flag&_IOERR;                       }
448   }
449     int   _snprintf(char* s, size_t n, in char* fmt, ...);
450     alias _snprintf snprintf;
451 
452     int   _vsnprintf(char* s, size_t n, in char* format, va_list arg);
453     alias _vsnprintf vsnprintf;
454 }
455 else version( linux )
456 {
457     void rewind(FILE* stream);
458     void clearerr(FILE* stream);
459     int  feof(FILE* stream);
460     int  ferror(FILE* stream);
461     int  fileno(FILE *);
462 
463     int  snprintf(char* s, size_t n, in char* format, ...);
464     int  vsnprintf(char* s, size_t n, in char* format, va_list arg);
465 }
466 else version( darwin )
467 {
468     void rewind(FILE*);
469     void clearerr(FILE*);
470     int  feof(FILE*);
471     int  ferror(FILE*);
472     int  fileno(FILE*);
473 
474     int  snprintf(char* s, size_t n, in char* format, ...);
475     int  vsnprintf(char* s, size_t n, in char* format, va_list arg);
476 }
477 else version( FreeBSD )
478 {
479     void rewind(FILE*);
480     void clearerr(FILE*);
481     int  feof(FILE*);
482     int  ferror(FILE*);
483     int  fileno(FILE*);
484 
485     int  snprintf(char* s, size_t n, in char* format, ...);
486     int  vsnprintf(char* s, size_t n, in char* format, va_list arg);
487 }
488 else version( solaris )
489 {
490     void rewind(FILE*);
491     void clearerr(FILE*);
492     int  feof(FILE*);
493     int  ferror(FILE*);
494     int  fileno(FILE*);
495 
496     int  snprintf(char* s, size_t n, in char* format, ...);
497     int  vsnprintf(char* s, size_t n, in char* format, va_list arg);    
498 }
499 else
500 {
501     static assert( false );
502 }
503 
504 void perror(in char* s);
505 
506 int fwprintf(FILE* stream, in wchar_t* format, ...);
507 int fwscanf(FILE* stream, in wchar_t* format, ...);
508 int swprintf(wchar_t* s, size_t n, in wchar_t* format, ...);
509 int swscanf(in wchar_t* s, in wchar_t* format, ...);
510 int vfwprintf(FILE* stream, in wchar_t* format, va_list arg);
511 int vfwscanf(FILE* stream, in wchar_t* format, va_list arg);
512 int vswprintf(wchar_t* s, size_t n, in wchar_t* format, va_list arg);
513 int vswscanf(in wchar_t* s, in wchar_t* format, va_list arg);
514 int vwprintf(in wchar_t* format, va_list arg);
515 int vwscanf(in wchar_t* format, va_list arg);
516 int wprintf(in wchar_t* format, ...);
517 int wscanf(in wchar_t* format, ...);
518 
519 wint_t fgetwc(FILE* stream);
520 wint_t fputwc(wchar_t c, FILE* stream);
521 
522 wchar_t* fgetws(wchar_t* s, int n, FILE* stream);
523 int      fputws(in wchar_t* s, FILE* stream);
524 
525 extern (D)
526 {
527     wint_t getwchar()                     { return fgetwc(stdin);     }
528     wint_t putwchar(wchar_t c)            { return fputwc(c,stdout);  }
529     wint_t getwc(FILE* stream)            { return fgetwc(stream);    }
530     wint_t putwc(wchar_t c, FILE* stream) { return fputwc(c, stream); }
531 }
532 
533 wint_t ungetwc(wint_t c, FILE* stream);
534 int    fwide(FILE* stream, int mode);