This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
/
stdio.d
434 lines (380 loc) · 9.91 KB
/
stdio.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/**
* D header file for C99.
*
* Copyright: Copyright Sean Kelly 2005 - 2009.
* License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
* Authors: Sean Kelly
* Standards: ISO/IEC 9899:1999 (E)
*/
/* Copyright Sean Kelly 2005 - 2009.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
module core.stdc.stdio;
private
{
import core.stdc.config;
import core.stdc.stddef; // for size_t
import core.stdc.stdarg; // for va_list
version (FreeBSD)
{
import core.sys.posix.sys.types;
}
}
extern (C):
nothrow:
version( Windows )
{
enum
{
BUFSIZ = 0x4000,
EOF = -1,
FOPEN_MAX = 20,
FILENAME_MAX = 256, // 255 plus NULL
TMP_MAX = 32767,
SYS_OPEN = 20, // non-standard
}
enum int _NFILE = 60; // non-standard
enum string _P_tmpdir = "\\"; // non-standard
enum wstring _wP_tmpdir = "\\"; // non-standard
enum int L_tmpnam = _P_tmpdir.length + 12;
}
else version( linux )
{
enum
{
BUFSIZ = 8192,
EOF = -1,
FOPEN_MAX = 16,
FILENAME_MAX = 4095,
TMP_MAX = 238328,
L_tmpnam = 20
}
}
else version( OSX )
{
enum
{
BUFSIZ = 1024,
EOF = -1,
FOPEN_MAX = 20,
FILENAME_MAX = 1024,
TMP_MAX = 308915776,
L_tmpnam = 1024,
}
private
{
struct __sbuf
{
ubyte* _base;
int _size;
}
struct __sFILEX
{
}
}
}
else version ( FreeBSD )
{
enum
{
EOF = -1,
FOPEN_MAX = 20,
FILENAME_MAX = 1024,
TMP_MAX = 308915776,
L_tmpnam = 1024
}
struct __sbuf
{
ubyte *_base;
int _size;
}
alias _iobuf __sFILE;
union __mbstate_t // <sys/_types.h>
{
char[128] _mbstate8;
long _mbstateL;
}
}
else
{
static assert( false, "Unsupported platform" );
}
enum
{
SEEK_SET,
SEEK_CUR,
SEEK_END
}
struct _iobuf
{
align (1):
version( Windows )
{
char* _ptr;
int _cnt;
char* _base;
int _flag;
int _file;
int _charbuf;
int _bufsiz;
int __tmpnum;
}
else version( linux )
{
int _flags;
char* _read_ptr;
char* _read_end;
char* _read_base;
char* _write_base;
char* _write_ptr;
char* _write_end;
char* _buf_base;
char* _buf_end;
char* _save_base;
char* _backup_base;
char* _save_end;
void* _markers;
_iobuf* _chain;
int _fileno;
int _blksize;
int _old_offset;
ushort _cur_column;
byte _vtable_offset;
char[1] _shortbuf;
void* _lock;
}
else version( OSX )
{
ubyte* _p;
int _r;
int _w;
short _flags;
short _file;
__sbuf _bf;
int _lbfsize;
int* function(void*) _close;
int* function(void*, char*, int) _read;
fpos_t* function(void*, fpos_t, int) _seek;
int* function(void*, char *, int) _write;
__sbuf _ub;
__sFILEX* _extra;
int _ur;
ubyte[3] _ubuf;
ubyte[1] _nbuf;
__sbuf _lb;
int _blksize;
fpos_t _offset;
}
else version( FreeBSD )
{
ubyte* _p;
int _r;
int _w;
short _flags;
short _file;
__sbuf _bf;
int _lbfsize;
void* _cookie;
int function(void*) _close;
int function(void*, char*, int) _read;
fpos_t function(void*, fpos_t, int) _seek;
int function(void*, in char*, int) _write;
__sbuf _ub;
ubyte* _up;
int _ur;
ubyte[3] _ubuf;
ubyte[1] _nbuf;
__sbuf _lb;
int _blksize;
fpos_t _offset;
pthread_mutex_t _fl_mutex;
pthread_t _fl_owner;
int _fl_count;
int _orientation;
__mbstate_t _mbstate;
}
else
{
static assert( false, "Unsupported platform" );
}
}
alias shared(_iobuf) FILE;
enum
{
_F_RDWR = 0x0003, // non-standard
_F_READ = 0x0001, // non-standard
_F_WRIT = 0x0002, // non-standard
_F_BUF = 0x0004, // non-standard
_F_LBUF = 0x0008, // non-standard
_F_ERR = 0x0010, // non-standard
_F_EOF = 0x0020, // non-standard
_F_BIN = 0x0040, // non-standard
_F_IN = 0x0080, // non-standard
_F_OUT = 0x0100, // non-standard
_F_TERM = 0x0200, // non-standard
}
version( Windows )
{
enum
{
_IOFBF = 0,
_IOLBF = 0x40,
_IONBF = 4,
_IOREAD = 1, // non-standard
_IOWRT = 2, // non-standard
_IOMYBUF = 8, // non-standard
_IOEOF = 0x10, // non-standard
_IOERR = 0x20, // non-standard
_IOSTRG = 0x40, // non-standard
_IORW = 0x80, // non-standard
_IOTRAN = 0x100, // non-standard
_IOAPP = 0x200, // non-standard
}
extern shared void function() _fcloseallp;
private extern shared FILE[_NFILE] _iob;
shared stdin = &_iob[0];
shared stdout = &_iob[1];
shared stderr = &_iob[2];
shared stdaux = &_iob[3];
shared stdprn = &_iob[4];
}
else version( linux )
{
enum
{
_IOFBF = 0,
_IOLBF = 1,
_IONBF = 2,
}
extern shared FILE* stdin;
extern shared FILE* stdout;
extern shared FILE* stderr;
}
else version( OSX )
{
enum
{
_IOFBF = 0,
_IOLBF = 1,
_IONBF = 2,
}
private extern shared FILE* __stdinp;
private extern shared FILE* __stdoutp;
private extern shared FILE* __stderrp;
alias __stdinp stdin;
alias __stdoutp stdout;
alias __stderrp stderr;
}
else version( FreeBSD )
{
enum
{
_IOFBF = 0,
_IOLBF = 1,
_IONBF = 2,
}
private extern shared FILE* __stdinp;
private extern shared FILE* __stdoutp;
private extern shared FILE* __stderrp;
alias __stdinp stdin;
alias __stdoutp stdout;
alias __stderrp stderr;
}
else
{
static assert( false, "Unsupported platform" );
}
alias int fpos_t;
int remove(in char* filename);
int rename(in char* from, in char* to);
FILE* tmpfile();
char* tmpnam(char* s);
int fclose(FILE* stream);
int fflush(FILE* stream);
FILE* fopen(in char* filename, in char* mode);
FILE* freopen(in char* filename, in char* mode, FILE* stream);
void setbuf(FILE* stream, char* buf);
int setvbuf(FILE* stream, char* buf, int mode, size_t size);
int fprintf(FILE* stream, in char* format, ...);
int fscanf(FILE* stream, in char* format, ...);
int sprintf(char* s, in char* format, ...);
int sscanf(in char* s, in char* format, ...);
int vfprintf(FILE* stream, in char* format, va_list arg);
int vfscanf(FILE* stream, in char* format, va_list arg);
int vsprintf(char* s, in char* format, va_list arg);
int vsscanf(in char* s, in char* format, va_list arg);
int vprintf(in char* format, va_list arg);
int vscanf(in char* format, va_list arg);
int printf(in char* format, ...);
int scanf(in char* format, ...);
int fgetc(FILE* stream);
int fputc(int c, FILE* stream);
char* fgets(char* s, int n, FILE* stream);
int fputs(in char* s, FILE* stream);
char* gets(char* s);
int puts(in char* s);
extern (D)
{
int getchar() { return getc(stdin); }
int putchar(int c) { return putc(c,stdout); }
int getc(FILE* stream) { return fgetc(stream); }
int putc(int c, FILE* stream) { return fputc(c,stream); }
}
int ungetc(int c, FILE* stream);
size_t fread(void* ptr, size_t size, size_t nmemb, FILE* stream);
size_t fwrite(in void* ptr, size_t size, size_t nmemb, FILE* stream);
int fgetpos(FILE* stream, fpos_t * pos);
int fsetpos(FILE* stream, in fpos_t* pos);
int fseek(FILE* stream, c_long offset, int whence);
c_long ftell(FILE* stream);
version( Windows )
{
extern (D)
{
void rewind(FILE* stream) { fseek(stream,0L,SEEK_SET); stream._flag&=~_IOERR; }
void clearerr(FILE* stream) { stream._flag &= ~(_IOERR|_IOEOF); }
int feof(FILE* stream) { return stream._flag&_IOEOF; }
int ferror(FILE* stream) { return stream._flag&_IOERR; }
}
int _snprintf(char* s, size_t n, in char* fmt, ...);
alias _snprintf snprintf;
int _vsnprintf(char* s, size_t n, in char* format, va_list arg);
alias _vsnprintf vsnprintf;
}
else version( linux )
{
void rewind(FILE* stream);
void clearerr(FILE* stream);
int feof(FILE* stream);
int ferror(FILE* stream);
int fileno(FILE *);
int snprintf(char* s, size_t n, in char* format, ...);
int vsnprintf(char* s, size_t n, in char* format, va_list arg);
}
else version( OSX )
{
void rewind(FILE*);
void clearerr(FILE*);
int feof(FILE*);
int ferror(FILE*);
int fileno(FILE*);
int snprintf(char* s, size_t n, in char* format, ...);
int vsnprintf(char* s, size_t n, in char* format, va_list arg);
}
else version( FreeBSD )
{
void rewind(FILE*);
void clearerr(FILE*);
int feof(FILE*);
int ferror(FILE*);
int fileno(FILE*);
int snprintf(char* s, size_t n, in char* format, ...);
int vsnprintf(char* s, size_t n, in char* format, va_list arg);
}
else
{
static assert( false, "Unsupported platform" );
}
void perror(in char* s);