1 /**
2   * D header file for GNU stdio.
3   *
4   * Copyright: Danny Milosavljevic 2014
5   * License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
6   * Authors: Danny Milosavljevic
7   */
8 module core.sys.linux.stdio;
9 version (CRuntime_Glibc):
10 public import core.sys.posix.stdio;
11 import core.sys.posix.sys.types : ssize_t, off64_t = off_t;
12 import core.sys.linux.config : __USE_FILE_OFFSET64;
13 import core.stdc.stdio : FILE;
14 import core.stdc.stddef : wchar_t;
15 
16 
17 extern(C) nothrow
18 {
19     alias ssize_t function(void *cookie, char *buf, size_t size) cookie_read_function_t;
20     alias ssize_t function(void *cookie, const(char) *buf, size_t size) cookie_write_function_t;
21     alias int function(void *cookie, off64_t *offset, int whence) cookie_seek_function_t;
22     alias int function(void *cookie) cookie_close_function_t;
23 
24     struct cookie_io_functions_t
25     {
26         cookie_read_function_t read;
27         cookie_write_function_t write;
28         cookie_seek_function_t seek;
29         cookie_close_function_t close;
30     }
31     FILE* fopencookie(void* cookie, const(char)* mode, cookie_io_functions_t io_funcs);
32     void setbuffer(FILE *stream, char *buf, size_t size); // note: _DEFAULT_SOURCE
33 }
34 
35 unittest
36 {
37     static int flag = 0;
38     static int written = 0;
39     static int closed = 0;
40     // Note: this test needs to run on both a 32 and a 64 bit machine, both have to pass.
41     import core.stdc.errno : errno, EBADF;
42     //import core.sys.posix.sys.stat : off64_t;
43     import core.stdc.stdio : FILE, fflush, fileno, fprintf, fgetc, EOF, fclose;
44     import core.stdc.string : memcpy, memset;
45     extern (C) ssize_t specialRead(void *cookie, char *buf, size_t size) nothrow
46     {
47         memset(buf, 'a', size);
48         return size;
49     }
50     extern (C) int specialClose(void* cookie) nothrow
51     {
52         ++closed;
53         return 0;
54     }
55     extern (C) ssize_t specialWrite(void *cookie, const(char) *buf, size_t size) nothrow
56     {
57         int* c = cast(int*) cookie;
58         flag = *c;
59         written += size;
60         return size;
61     }
62     int dummy = 42;
63     cookie_io_functions_t fns =
64     {
65         read: &specialRead,
66         write: &specialWrite,
67         close: &specialClose,
68     };
69     FILE* f = fopencookie(&dummy, "r+", fns);
70     assert(f !is null);
71     //File.open();
72     //auto g = File(f);
73     assert(fileno(f) == -1 && errno == EBADF);
74     assert(fprintf(f, "hello") == "hello".length);
75     //assert(errno == EBADF);
76     assert(fflush(f) == 0);
77     assert(written == "hello".length);
78     // Note: do not swap reading and writing here.
79     int c = 0;
80     while ((c = fgetc(f)) != EOF)
81     {
82         assert(c == 'a');
83         break; // endless otherwise
84     }
85     assert(c == 'a');
86     assert(fclose(f) == 0);
87     assert(closed == 1);
88     assert(flag == dummy);
89     //stdin.getFP();
90     //assert(stdin.fileno() == 0);
91 }
92 
93 unittest
94 { /* setbuffer */
95     char buf;
96     int c;
97     byte[10] dummy = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
98     FILE* f = fmemopen(dummy.ptr, 10, "r");
99     assert(f !is null);
100     setbuffer(f, &buf, 1);
101     assert(fgetc(f) == 1);
102     assert(fgetc(f) == 2);
103     assert(fgetc(f) == 3);
104     assert(fgetc(f) == 4);
105     assert(fclose(f) == 0);
106 }