perl5.002beta3
[p5sagit/p5-mst-13.2.git] / ext / FileHandle / FileHandle.xs
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include <stdio.h>
5
6 typedef int SysRet;
7 typedef FILE * InputStream;
8 typedef FILE * OutputStream;
9
10 static bool
11 constant(name, pval)
12 char *name;
13 IV *pval;
14 {
15     switch (*name) {
16     case '_':
17         if (strEQ(name, "_IOFBF"))
18 #ifdef _IOFBF
19             { *pval = _IOFBF; return TRUE; }
20 #else
21             return FALSE;
22 #endif
23         if (strEQ(name, "_IOLBF"))
24 #ifdef _IOLBF
25             { *pval = _IOLBF; return TRUE; }
26 #else
27             return FALSE;
28 #endif
29         if (strEQ(name, "_IONBF"))
30 #ifdef _IONBF
31             { *pval = _IONBF; return TRUE; }
32 #else
33             return FALSE;
34 #endif
35         break;
36     }
37
38     return FALSE;
39 }
40
41
42 MODULE = FileHandle     PACKAGE = FileHandle    PREFIX = f
43
44 SV *
45 constant(name)
46         char *          name
47     CODE:
48         IV i;
49         if (constant(name, &i))
50             RETVAL = newSViv(i);
51         else
52             RETVAL = &sv_undef;
53     OUTPUT:
54         RETVAL
55
56 SV *
57 fgetpos(handle)
58         InputStream     handle
59     CODE:
60         if (handle) {
61             Fpos_t pos;
62             fgetpos(handle, &pos);
63             ST(0) = sv_2mortal(newSVpv((char*)&pos, sizeof(Fpos_t)));
64         }
65         else {
66             ST(0) = &sv_undef;
67             errno = EINVAL;
68         }
69
70 SysRet
71 fsetpos(handle, pos)
72         InputStream     handle
73         SV *            pos
74     CODE:
75         if (handle)
76             RETVAL = fsetpos(handle, (Fpos_t*)SvPVX(pos));
77         else {
78             RETVAL = -1;
79             errno = EINVAL;
80         }
81     OUTPUT:
82         RETVAL
83
84 int
85 ungetc(handle, c)
86         InputStream     handle
87         int             c
88     CODE:
89         if (handle)
90             RETVAL = ungetc(c, handle);
91         else {
92             RETVAL = -1;
93             errno = EINVAL;
94         }
95     OUTPUT:
96         RETVAL
97
98 OutputStream
99 new_tmpfile(packname = "FileHandle")
100     char *              packname
101     CODE:
102         RETVAL = tmpfile();
103     OUTPUT:
104         RETVAL
105
106 int
107 ferror(handle)
108         InputStream     handle
109     CODE:
110         if (handle)
111             RETVAL = ferror(handle);
112         else {
113             RETVAL = -1;
114             errno = EINVAL;
115         }
116     OUTPUT:
117         RETVAL
118
119 SysRet
120 fflush(handle)
121         OutputStream    handle
122     CODE:
123         if (handle)
124             RETVAL = fflush(handle);
125         else {
126             RETVAL = -1;
127             errno = EINVAL;
128         }
129     OUTPUT:
130         RETVAL
131
132 void
133 setbuf(handle, buf)
134         OutputStream    handle
135         char *          buf = SvPOK(ST(1)) ? sv_grow(ST(1), BUFSIZ) : 0;
136     CODE:
137         if (handle)
138             setbuf(handle, buf);
139
140
141 #ifdef _IOFBF
142
143 SysRet
144 setvbuf(handle, buf, type, size)
145         OutputStream    handle
146         char *          buf = SvPOK(ST(1)) ? sv_grow(ST(1), SvIV(ST(3))) : 0;
147         int             type
148         int             size
149     CODE:
150         if (handle)
151             RETVAL = setvbuf(handle, buf, type, size);
152         else {
153             RETVAL = -1;
154             errno = EINVAL;
155         }
156     OUTPUT:
157         RETVAL
158
159 #endif /* _IOFBF */