summaryrefslogtreecommitdiff
path: root/libphobos/libdruntime/core/sys/posix/stdio.d
blob: 077838d50aaf64cb6de6c49dc102da8aa5290b24 (plain)
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
/**
 * D header file for POSIX.
 *
 * Copyright: Copyright Sean Kelly 2005 - 2009.
 * License:   $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
 * Authors:   Sean Kelly
 * Standards: The Open Group Base Specifications Issue 6, IEEE Std 1003.1, 2004 Edition
 */

/*          Copyright Sean Kelly 2005 - 2009.
 * Distributed under the Boost Software License, Version 1.0.
 *    (See accompanying file LICENSE or copy at
 *          http://www.boost.org/LICENSE_1_0.txt)
 */
module core.sys.posix.stdio;

import core.sys.posix.config;
public import core.stdc.stdio;
public import core.sys.posix.sys.types; // for off_t

version (OSX)
    version = Darwin;
else version (iOS)
    version = Darwin;
else version (TVOS)
    version = Darwin;
else version (WatchOS)
    version = Darwin;

version (Posix):
extern (C):

nothrow:
@nogc:
@system:

//
// Required (defined in core.stdc.stdio)
//
/*
BUFSIZ
_IOFBF
_IOLBF
_IONBF
L_tmpnam
SEEK_CUR
SEEK_END
SEEK_SET
FILENAME_MAX
FOPEN_MAX
TMP_MAX
EOF
NULL
stderr
stdin
stdout
FILE
fpos_t
size_t

void   clearerr(FILE*);
int    fclose(FILE*);
int    feof(FILE*);
int    ferror(FILE*);
int    fflush(FILE*);
int    fgetc(FILE*);
int    fgetpos(FILE*, fpos_t *);
char*  fgets(char*, int, FILE*);
FILE*  fopen(const scope char*, const scope char*);
int    fprintf(FILE*, const scope char*, ...);
int    fputc(int, FILE*);
int    fputs(const scope char*, FILE*);
size_t fread(void *, size_t, size_t, FILE*);
FILE*  freopen(const scope char*, const scope char*, FILE*);
int    fscanf(FILE*, const scope char*, ...);
int    fseek(FILE*, c_long, int);
int    fsetpos(FILE*, const scope fpos_t*);
c_long ftell(FILE*);
size_t fwrite(in void *, size_t, size_t, FILE*);
int    getc(FILE*);
int    getchar();
char*  gets(char*);
void   perror(const scope char*);
int    printf(const scope char*, ...);
int    putc(int, FILE*);
int    putchar(int);
int    puts(const scope char*);
int    remove(const scope char*);
int    rename(const scope char*, const scope char*);
void   rewind(FILE*);
int    scanf(const scope char*, ...);
void   setbuf(FILE*, char*);
int    setvbuf(FILE*, char*, int, size_t);
int    snprintf(char*, size_t, const scope char*, ...);
int    sprintf(char*, const scope char*, ...);
int    sscanf(const scope char*, const scope char*, int ...);
FILE*  tmpfile();
char*  tmpnam(char*);
int    ungetc(int, FILE*);
int    vfprintf(FILE*, const scope char*, va_list);
int    vfscanf(FILE*, const scope char*, va_list);
int    vprintf(const scope char*, va_list);
int    vscanf(const scope char*, va_list);
int    vsnprintf(char*, size_t, const scope char*, va_list);
int    vsprintf(char*, const scope char*, va_list);
int    vsscanf(const scope char*, const scope char*, va_list arg);
*/

version (CRuntime_Glibc)
{
    /*
     * actually, if __USE_FILE_OFFSET64 && !_LARGEFILE64_SOURCE
     * the *64 functions shouldn't be visible, but the aliases should
     * still be supported
     */
    static if ( __USE_FILE_OFFSET64 )
    {
        int   fgetpos64(FILE*, fpos_t *);
        alias fgetpos64 fgetpos;

        FILE* fopen64(const scope char*, const scope char*);
        alias fopen64 fopen;

        FILE* freopen64(const scope char*, const scope char*, FILE*);
        alias freopen64 freopen;

        int   fseek(FILE*, c_long, int);

        int   fsetpos64(FILE*, const scope fpos_t*);
        alias fsetpos64 fsetpos;

        FILE* tmpfile64();
        alias tmpfile64 tmpfile;
    }
    else
    {
        int   fgetpos(FILE*, fpos_t *);
        FILE* fopen(const scope char*, const scope char*);
        FILE* freopen(const scope char*, const scope char*, FILE*);
        int   fseek(FILE*, c_long, int);
        int   fsetpos(FILE*, const scope fpos_t*);
        FILE* tmpfile();
    }
}
else version (CRuntime_Bionic)
{
    int   fgetpos(FILE*, fpos_t *);
    FILE* fopen(const scope char*, const scope char*);
    FILE* freopen(const scope char*, const scope char*, FILE*);
    int   fseek(FILE*, c_long, int);
    int   fsetpos(FILE*, const scope fpos_t*);
}
else version (CRuntime_UClibc)
{
    static if ( __USE_FILE_OFFSET64 )
    {
        int   fgetpos64(FILE*, fpos_t *);
        alias fgetpos64 fgetpos;

        FILE* fopen64(const scope char*, const scope char*);
        alias fopen64 fopen;

        FILE* freopen64(const scope char*, const scope char*, FILE*);
        alias freopen64 freopen;

        int   fseek(FILE*, c_long, int);

        int   fsetpos64(FILE*, const scope fpos_t*);
        alias fsetpos64 fsetpos;

        FILE* tmpfile64();
        alias tmpfile64 tmpfile;
    }
    else
    {
        int   fgetpos(FILE*, fpos_t *);
        FILE* fopen(const scope char*, const scope char*);
        FILE* freopen(const scope char*, const scope char*, FILE*);
        int   fseek(FILE*, c_long, int);
        int   fsetpos(FILE*, const scope fpos_t*);
        FILE* tmpfile();
    }
}
else version (CRuntime_Musl)
{
    static if ( __USE_FILE_OFFSET64 )
    {
        int   fgetpos64(FILE*, fpos_t *);
        alias fgetpos64 fgetpos;

        FILE* fopen64(const scope char*, const scope char*);
        alias fopen64 fopen;

        FILE* freopen64(const scope char*, const scope char*, FILE*);
        alias freopen64 freopen;

        int   fseek(FILE*, c_long, int);

        int   fsetpos64(FILE*, const scope fpos_t*);
        alias fsetpos64 fsetpos;

        FILE* tmpfile64();
        alias tmpfile64 tmpfile;
    }
    else
    {
        int   fgetpos(FILE*, fpos_t *);
        FILE* fopen(const scope char*, const scope char*);
        FILE* freopen(const scope char*, const scope char*, FILE*);
        int   fseek(FILE*, c_long, int);
        int   fsetpos(FILE*, const scope fpos_t*);
        FILE* tmpfile();
    }
}
else version (Solaris)
{
    static if (__USE_FILE_OFFSET64 && __WORDSIZE != 64)
    {
        int   fgetpos64(FILE*, fpos_t *);
        alias fgetpos = fgetpos64;

        FILE* fopen64(const scope char*, const scope char*);
        alias fopen = fopen64;

        FILE* freopen64(const scope char*, const scope char*, FILE*);
        alias freopen = freopen64;

        int   fseek(FILE*, c_long, int);

        int   fsetpos64(FILE*, const scope fpos_t*);
        alias fsetpos = fsetpos64;

        FILE* tmpfile64();
        alias tmpfile = tmpfile64;
    }
    else
    {
        int   fgetpos(FILE*, fpos_t *);
        FILE* fopen(const scope char*, const scope char*);
        FILE* freopen(const scope char*, const scope char*, FILE*);
        int   fseek(FILE*, c_long, int);
        int   fsetpos(FILE*, const scope fpos_t*);
        FILE* tmpfile();
    }
}

//
// C Extension (CX)
//
/*
L_ctermid

char*   ctermid(char*);
FILE*   fdopen(int, const scope char*);
int     fileno(FILE*);
int     fseeko(FILE*, off_t, int);
off_t   ftello(FILE*);
ssize_t getdelim(char**, size_t*, int, FILE*);
ssize_t getline(char**, size_t*, FILE*);
char*   gets(char*);
int     pclose(FILE*);
FILE*   popen(const scope char*, const scope char*);
*/

version (CRuntime_Glibc)
{
    enum L_ctermid = 9;

    static if ( __USE_FILE_OFFSET64 )
    {
        int   fseeko64(FILE*, off_t, int);
        alias fseeko64 fseeko;
    }
    else
    {
        int   fseeko(FILE*, off_t, int);
    }

    static if ( __USE_FILE_OFFSET64 )
    {
        off_t ftello64(FILE*);
        alias ftello64 ftello;
    }
    else
    {
        off_t ftello(FILE*);
    }

    ssize_t getdelim(char**, size_t*, int, FILE*);
    ssize_t getline(char**, size_t*, FILE*);
}
else version (CRuntime_UClibc)
{
    enum L_ctermid = 9;
    enum L_cuserid = 9;

    static if ( __USE_FILE_OFFSET64 )
    {
        int   fseeko64(FILE*, off_t, int);
        alias fseeko64 fseeko;
    }
    else
    {
        int   fseeko(FILE*, off_t, int);
    }

    static if ( __USE_FILE_OFFSET64 )
    {
        off_t ftello64(FILE*);
        alias ftello64 ftello;
    }
    else
    {
        off_t ftello(FILE*);
    }

    ssize_t getdelim(char**, size_t*, int, FILE*);
    ssize_t getline(char**, size_t*, FILE*);
}
else version (CRuntime_Musl)
{
    enum L_ctermid = 20;

    static if ( __USE_FILE_OFFSET64 )
    {
        int   fseeko64(FILE*, off_t, int);
        alias fseeko64 fseeko;
    }
    else
    {
        int   fseeko(FILE*, off_t, int);
    }

    static if ( __USE_FILE_OFFSET64 )
    {
        off_t ftello64(FILE*);
        alias ftello64 ftello;
    }
    else
    {
        off_t ftello(FILE*);
    }

    ssize_t getdelim(char**, size_t*, int, FILE*);
    ssize_t getline(char**, size_t*, FILE*);
}
else version (CRuntime_Bionic)
{
    enum L_ctermid = 1024;

    static if ( __USE_FILE_OFFSET64 )
    {
        int   fseeko64(FILE*, off_t, int);
        alias fseeko64 fseeko;
    }
    else
    {
        int   fseeko(FILE*, off_t, int);
    }

    static if ( __USE_FILE_OFFSET64 )
    {
        off_t ftello64(FILE*);
        alias ftello64 ftello;
    }
    else
    {
        off_t ftello(FILE*);
    }

    ssize_t getdelim(char**, size_t*, int, FILE*);
    ssize_t getline(char**, size_t*, FILE*);
}
else version (Darwin)
{
    enum L_ctermid = 1024;

    int   fseeko(FILE*, off_t, int);
    off_t ftello(FILE*);

    ssize_t getdelim(char**, size_t*, int, FILE*);
    ssize_t getline(char**, size_t*, FILE*);
}
else version (FreeBSD)
{
    import core.sys.freebsd.config;

    enum L_ctermid = 1024;

    int   fseeko(FILE*, off_t, int);
    off_t ftello(FILE*);

    static if (__FreeBSD_version >= 800000)
    {
        ssize_t getdelim(char**, size_t*, int, FILE*);
        ssize_t getline(char**, size_t*, FILE*);
    }
}
else version (NetBSD)
{
    enum L_ctermid = 1024;

    int   fseeko(FILE*, off_t, int);
    off_t ftello(FILE*);

    ssize_t getdelim(char**, size_t*, int, FILE*);
    ssize_t getline(char**, size_t*, FILE*);
}
else version (OpenBSD)
{
    enum L_ctermid = 1024;

    int   fseeko(FILE*, off_t, int);
    off_t ftello(FILE*);

    ssize_t getdelim(char**, size_t*, int, FILE*);
    ssize_t getline(char**, size_t*, FILE*);
}
else version (DragonFlyBSD)
{
    enum L_ctermid = 1024;

    int   fseeko(FILE*, off_t, int);
    off_t ftello(FILE*);

    ssize_t getdelim(char**, size_t*, int, FILE*);
    ssize_t getline(char**, size_t*, FILE*);
}
else version (Solaris)
{
    enum L_ctermid = 9;
    enum L_cuserid = 9;

    static if (__USE_FILE_OFFSET64 && __WORDSIZE != 64)
    {
        int   fseeko64(FILE*, off_t, int);
        alias fseeko = fseeko64;
    }
    else
    {
        int   fseeko(FILE*, off_t, int);
    }

    static if (__USE_FILE_OFFSET64 && __WORDSIZE != 64)
    {
        off_t ftello64(FILE*);
        alias ftello = ftello64;
    }
    else
    {
        off_t ftello(FILE*);
    }

    ssize_t getdelim(char**, size_t*, int, FILE*);
    ssize_t getline(char**, size_t*, FILE*);
}
else version (Posix)
{
    int   fseeko(FILE*, off_t, int);
    off_t ftello(FILE*);
}

char*  ctermid(char*);
FILE*  fdopen(int, const scope char*);
int    fileno(FILE*);
char*  gets(char*);
int    pclose(FILE*);
FILE*  popen(const scope char*, const scope char*);


// memstream functions are conforming to POSIX.1-2008.  These functions are
// not specified in POSIX.1-2001 and are not widely available on other
// systems.
version (CRuntime_Glibc)                     // as of glibc 1.0x
    version = HaveMemstream;
else version (FreeBSD)                      // as of FreeBSD 9.2
    version = HaveMemstream;
else version (DragonFlyBSD)                 // for DragonFlyBSD
    version = HaveMemstream;
else version (OpenBSD)                      // as of OpenBSD 5.4
    version = HaveMemstream;
else version (CRuntime_UClibc)
    version = HaveMemstream;
// http://git.musl-libc.org/cgit/musl/commit/src/stdio/open_memstream.c?id=b158b32a44d56ef20407d4285b58180447ffff1f
else version (CRuntime_Musl)
    version = HaveMemstream;

version (HaveMemstream)
{
    FILE*  fmemopen(const scope void* buf, in size_t size, const scope char* mode);
    FILE*  open_memstream(char** ptr, size_t* sizeloc);
    version (CRuntime_UClibc) {} else
    FILE*  open_wmemstream(wchar_t** ptr, size_t* sizeloc);
}

//
// Thread-Safe Functions (TSF)
//
/*
void   flockfile(FILE*);
int    ftrylockfile(FILE*);
void   funlockfile(FILE*);
int    getc_unlocked(FILE*);
int    getchar_unlocked();
int    putc_unlocked(int, FILE*);
int    putchar_unlocked(int);
*/

version (CRuntime_Glibc)
{
    void   flockfile(FILE*);
    int    ftrylockfile(FILE*);
    void   funlockfile(FILE*);
    int    getc_unlocked(FILE*);
    int    getchar_unlocked();
    int    putc_unlocked(int, FILE*);
    int    putchar_unlocked(int);
}
else version (CRuntime_Musl)
{
    void   flockfile(FILE*);
    int    ftrylockfile(FILE*);
    void   funlockfile(FILE*);
    int    getc_unlocked(FILE*);
    int    getchar_unlocked();
    int    putc_unlocked(int, FILE*);
    int    putchar_unlocked(int);
}
else version (CRuntime_Bionic)
{
    void   flockfile(FILE*);
    int    ftrylockfile(FILE*);
    void   funlockfile(FILE*);
    int    getc_unlocked(FILE*);
    int    getchar_unlocked();
    int    putc_unlocked(int, FILE*);
    int    putchar_unlocked(int);
}
else version (Darwin)
{
    void   flockfile(FILE*);
    int    ftrylockfile(FILE*);
    void   funlockfile(FILE*);
    int    getc_unlocked(FILE*);
    int    getchar_unlocked();
    int    putc_unlocked(int, FILE*);
    int    putchar_unlocked(int);
}
else version (FreeBSD)
{
    void   flockfile(FILE*);
    int    ftrylockfile(FILE*);
    void   funlockfile(FILE*);
    int    getc_unlocked(FILE*);
    int    getchar_unlocked();
    int    putc_unlocked(int, FILE*);
    int    putchar_unlocked(int);
}
else version (NetBSD)
{
    void   flockfile(FILE*);
    int    ftrylockfile(FILE*);
    void   funlockfile(FILE*);
    int    getc_unlocked(FILE*);
    int    getchar_unlocked();
    int    putc_unlocked(int, FILE*);
    int    putchar_unlocked(int);
}
else version (OpenBSD)
{
    void   flockfile(FILE*);
    int    ftrylockfile(FILE*);
    void   funlockfile(FILE*);
    int    getc_unlocked(FILE*);
    int    getchar_unlocked();
    int    putc_unlocked(int, FILE*);
    int    putchar_unlocked(int);
}
else version (DragonFlyBSD)
{
    void   flockfile(FILE*);
    int    ftrylockfile(FILE*);
    void   funlockfile(FILE*);
    int    getc_unlocked(FILE*);
    int    getchar_unlocked();
    int    putc_unlocked(int, FILE*);
    int    putchar_unlocked(int);
}
else version (Solaris)
{
    void   flockfile(FILE*);
    int    ftrylockfile(FILE*);
    void   funlockfile(FILE*);
    int    getc_unlocked(FILE*);
    int    getchar_unlocked();
    int    putc_unlocked(int, FILE*);
    int    putchar_unlocked(int);
}
else version (CRuntime_UClibc)
{
    void   flockfile(FILE*);
    int    ftrylockfile(FILE*);
    void   funlockfile(FILE*);
    int    getc_unlocked(FILE*);
    int    getchar_unlocked();
    int    putc_unlocked(int, FILE*);
    int    putchar_unlocked(int);
}

//
// XOpen (XSI)
//
/*
P_tmpdir
va_list (defined in core.stdc.stdarg)

char*  tempnam(const scope char*, const scope char*);
*/

char*  tempnam(const scope char*, const scope char*);

version (CRuntime_Glibc)
{
    enum P_tmpdir  = "/tmp";
}
else version (CRuntime_Musl)
{
    enum P_tmpdir  = "/tmp";
}
else version (Darwin)
{
    enum P_tmpdir  = "/var/tmp";
}
else version (FreeBSD)
{
    enum P_tmpdir  = "/var/tmp/";
}
else version (NetBSD)
{
    enum P_tmpdir  = "/var/tmp/";
}
else version (OpenBSD)
{
    enum P_tmpdir  = "/tmp/";
}
else version (DragonFlyBSD)
{
    enum P_tmpdir  = "/var/tmp/";
}
else version (Solaris)
{
    enum P_tmpdir  = "/var/tmp/";
}
else version (CRuntime_UClibc)
{
    enum P_tmpdir  = "/tmp";
}

version (HaveMemstream)
unittest
{ /* fmemopen */
    import core.stdc.string : memcmp;
    byte[10] buf;
    auto f = fmemopen(buf.ptr, 10, "w");
    assert(f !is null);
    assert(fprintf(f, "hello") == "hello".length);
    assert(fflush(f) == 0);
    assert(memcmp(buf.ptr, "hello".ptr, "hello".length) == 0);
    //assert(buf
    assert(fclose(f) == 0);
}

version (HaveMemstream)
unittest
{ /* Note: open_memstream is only useful for writing */
    import core.stdc.string : memcmp;
    char* ptr = null;
    char[6] testdata = ['h', 'e', 'l', 'l', 'o', 0];
    size_t sz = 0;
    auto f = open_memstream(&ptr, &sz);
    assert(f !is null);
    assert(fprintf(f, "%s", testdata.ptr) == 5);
    assert(fflush(f) == 0);
    assert(memcmp(ptr, testdata.ptr, testdata.length) == 0);
    assert(fclose(f) == 0);
}

version (CRuntime_UClibc) {} else
version (HaveMemstream)
unittest
{ /* Note: open_wmemstream is only useful for writing */
    import core.stdc.string : memcmp;
    import core.stdc.wchar_ : fwprintf;
    wchar_t* ptr = null;
    wchar_t[6] testdata = ['h', 'e', 'l', 'l', 'o', 0];
    size_t sz = 0;
    auto f = open_wmemstream(&ptr, &sz);
    assert(f !is null);
    assert(fwprintf(f, testdata.ptr) == 5);
    assert(fflush(f) == 0);
    assert(memcmp(ptr, testdata.ptr, testdata.length*wchar_t.sizeof) == 0);
    assert(fclose(f) == 0);
}