Commit | Line | Data |
760ac839 |
1 | /* perlio.c |
2 | * |
4eb8286e |
3 | * Copyright (c) 1996-1999, Nick Ing-Simmons |
760ac839 |
4 | * |
5 | * You may distribute under the terms of either the GNU General Public |
6 | * License or the Artistic License, as specified in the README file. |
7 | * |
8 | */ |
9 | |
10 | #define VOIDUSED 1 |
11 | #include "config.h" |
12 | |
13 | #define PERLIO_NOT_STDIO 0 |
14 | #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO) |
15 | #define PerlIO FILE |
16 | #endif |
17 | /* |
18 | * This file provides those parts of PerlIO abstraction |
0f4eea8f |
19 | * which are not #defined in iperlsys.h. |
760ac839 |
20 | * Which these are depends on various Configure #ifdef's |
21 | */ |
22 | |
23 | #include "EXTERN.h" |
864dbfa3 |
24 | #define PERL_IN_PERLIO_C |
760ac839 |
25 | #include "perl.h" |
26 | |
27 | #ifdef PERLIO_IS_STDIO |
28 | |
29 | void |
8ac85365 |
30 | PerlIO_init(void) |
760ac839 |
31 | { |
32 | /* Does nothing (yet) except force this file to be included |
33 | in perl binary. That allows this file to force inclusion |
34 | of other functions that may be required by loadable |
35 | extensions e.g. for FileHandle::tmpfile |
36 | */ |
37 | } |
38 | |
33dcbb9a |
39 | #undef PerlIO_tmpfile |
40 | PerlIO * |
8ac85365 |
41 | PerlIO_tmpfile(void) |
33dcbb9a |
42 | { |
43 | return tmpfile(); |
44 | } |
45 | |
760ac839 |
46 | #else /* PERLIO_IS_STDIO */ |
47 | |
48 | #ifdef USE_SFIO |
49 | |
50 | #undef HAS_FSETPOS |
51 | #undef HAS_FGETPOS |
52 | |
53 | /* This section is just to make sure these functions |
54 | get pulled in from libsfio.a |
55 | */ |
56 | |
57 | #undef PerlIO_tmpfile |
58 | PerlIO * |
c78749f2 |
59 | PerlIO_tmpfile(void) |
760ac839 |
60 | { |
61 | return sftmp(0); |
62 | } |
63 | |
64 | void |
c78749f2 |
65 | PerlIO_init(void) |
760ac839 |
66 | { |
67 | /* Force this file to be included in perl binary. Which allows |
68 | * this file to force inclusion of other functions that may be |
69 | * required by loadable extensions e.g. for FileHandle::tmpfile |
70 | */ |
71 | |
72 | /* Hack |
73 | * sfio does its own 'autoflush' on stdout in common cases. |
74 | * Flush results in a lot of lseek()s to regular files and |
75 | * lot of small writes to pipes. |
76 | */ |
77 | sfset(sfstdout,SF_SHARE,0); |
78 | } |
79 | |
17c3b450 |
80 | #else /* USE_SFIO */ |
760ac839 |
81 | |
82 | /* Implement all the PerlIO interface using stdio. |
83 | - this should be only file to include <stdio.h> |
84 | */ |
85 | |
86 | #undef PerlIO_stderr |
87 | PerlIO * |
c78749f2 |
88 | PerlIO_stderr(void) |
760ac839 |
89 | { |
90 | return (PerlIO *) stderr; |
91 | } |
92 | |
93 | #undef PerlIO_stdin |
94 | PerlIO * |
c78749f2 |
95 | PerlIO_stdin(void) |
760ac839 |
96 | { |
97 | return (PerlIO *) stdin; |
98 | } |
99 | |
100 | #undef PerlIO_stdout |
101 | PerlIO * |
c78749f2 |
102 | PerlIO_stdout(void) |
760ac839 |
103 | { |
104 | return (PerlIO *) stdout; |
105 | } |
106 | |
760ac839 |
107 | #undef PerlIO_fast_gets |
108 | int |
c78749f2 |
109 | PerlIO_fast_gets(PerlIO *f) |
760ac839 |
110 | { |
111 | #if defined(USE_STDIO_PTR) && defined(STDIO_PTR_LVALUE) && defined(STDIO_CNT_LVALUE) |
112 | return 1; |
113 | #else |
114 | return 0; |
115 | #endif |
116 | } |
117 | |
118 | #undef PerlIO_has_cntptr |
119 | int |
c78749f2 |
120 | PerlIO_has_cntptr(PerlIO *f) |
760ac839 |
121 | { |
122 | #if defined(USE_STDIO_PTR) |
123 | return 1; |
124 | #else |
125 | return 0; |
126 | #endif |
127 | } |
128 | |
129 | #undef PerlIO_canset_cnt |
130 | int |
c78749f2 |
131 | PerlIO_canset_cnt(PerlIO *f) |
760ac839 |
132 | { |
133 | #if defined(USE_STDIO_PTR) && defined(STDIO_CNT_LVALUE) |
134 | return 1; |
135 | #else |
136 | return 0; |
137 | #endif |
138 | } |
139 | |
140 | #undef PerlIO_set_cnt |
141 | void |
c78749f2 |
142 | PerlIO_set_cnt(PerlIO *f, int cnt) |
760ac839 |
143 | { |
159bba36 |
144 | if (cnt < -1) |
760ac839 |
145 | warn("Setting cnt to %d\n",cnt); |
146 | #if defined(USE_STDIO_PTR) && defined(STDIO_CNT_LVALUE) |
147 | FILE_cnt(f) = cnt; |
148 | #else |
149 | croak("Cannot set 'cnt' of FILE * on this system"); |
150 | #endif |
151 | } |
152 | |
153 | #undef PerlIO_set_ptrcnt |
154 | void |
c78749f2 |
155 | PerlIO_set_ptrcnt(PerlIO *f, STDCHAR *ptr, int cnt) |
760ac839 |
156 | { |
33dcbb9a |
157 | #ifdef FILE_bufsiz |
888911fc |
158 | STDCHAR *e = FILE_base(f) + FILE_bufsiz(f); |
159 | int ec = e - ptr; |
159bba36 |
160 | if (ptr > e + 1) |
161 | warn("Setting ptr %p > end+1 %p\n", ptr, e + 1); |
760ac839 |
162 | if (cnt != ec) |
163 | warn("Setting cnt to %d, ptr implies %d\n",cnt,ec); |
33dcbb9a |
164 | #endif |
760ac839 |
165 | #if defined(USE_STDIO_PTR) && defined(STDIO_PTR_LVALUE) |
888911fc |
166 | FILE_ptr(f) = ptr; |
760ac839 |
167 | #else |
168 | croak("Cannot set 'ptr' of FILE * on this system"); |
169 | #endif |
170 | #if defined(USE_STDIO_PTR) && defined(STDIO_CNT_LVALUE) |
171 | FILE_cnt(f) = cnt; |
172 | #else |
173 | croak("Cannot set 'cnt' of FILE * on this system"); |
174 | #endif |
175 | } |
176 | |
177 | #undef PerlIO_get_cnt |
178 | int |
c78749f2 |
179 | PerlIO_get_cnt(PerlIO *f) |
760ac839 |
180 | { |
181 | #ifdef FILE_cnt |
182 | return FILE_cnt(f); |
183 | #else |
184 | croak("Cannot get 'cnt' of FILE * on this system"); |
185 | return -1; |
186 | #endif |
187 | } |
188 | |
189 | #undef PerlIO_get_bufsiz |
190 | int |
c78749f2 |
191 | PerlIO_get_bufsiz(PerlIO *f) |
760ac839 |
192 | { |
193 | #ifdef FILE_bufsiz |
194 | return FILE_bufsiz(f); |
195 | #else |
196 | croak("Cannot get 'bufsiz' of FILE * on this system"); |
197 | return -1; |
198 | #endif |
199 | } |
200 | |
201 | #undef PerlIO_get_ptr |
888911fc |
202 | STDCHAR * |
c78749f2 |
203 | PerlIO_get_ptr(PerlIO *f) |
760ac839 |
204 | { |
205 | #ifdef FILE_ptr |
888911fc |
206 | return FILE_ptr(f); |
760ac839 |
207 | #else |
208 | croak("Cannot get 'ptr' of FILE * on this system"); |
209 | return NULL; |
210 | #endif |
211 | } |
212 | |
213 | #undef PerlIO_get_base |
888911fc |
214 | STDCHAR * |
c78749f2 |
215 | PerlIO_get_base(PerlIO *f) |
760ac839 |
216 | { |
217 | #ifdef FILE_base |
888911fc |
218 | return FILE_base(f); |
760ac839 |
219 | #else |
220 | croak("Cannot get 'base' of FILE * on this system"); |
221 | return NULL; |
222 | #endif |
223 | } |
224 | |
225 | #undef PerlIO_has_base |
226 | int |
c78749f2 |
227 | PerlIO_has_base(PerlIO *f) |
760ac839 |
228 | { |
229 | #ifdef FILE_base |
230 | return 1; |
231 | #else |
232 | return 0; |
233 | #endif |
234 | } |
235 | |
236 | #undef PerlIO_puts |
237 | int |
c78749f2 |
238 | PerlIO_puts(PerlIO *f, const char *s) |
760ac839 |
239 | { |
240 | return fputs(s,f); |
241 | } |
242 | |
243 | #undef PerlIO_open |
244 | PerlIO * |
c78749f2 |
245 | PerlIO_open(const char *path, const char *mode) |
760ac839 |
246 | { |
247 | return fopen(path,mode); |
248 | } |
249 | |
250 | #undef PerlIO_fdopen |
251 | PerlIO * |
c78749f2 |
252 | PerlIO_fdopen(int fd, const char *mode) |
760ac839 |
253 | { |
254 | return fdopen(fd,mode); |
255 | } |
256 | |
8c86a920 |
257 | #undef PerlIO_reopen |
258 | PerlIO * |
c78749f2 |
259 | PerlIO_reopen(const char *name, const char *mode, PerlIO *f) |
8c86a920 |
260 | { |
261 | return freopen(name,mode,f); |
262 | } |
760ac839 |
263 | |
264 | #undef PerlIO_close |
265 | int |
c78749f2 |
266 | PerlIO_close(PerlIO *f) |
760ac839 |
267 | { |
268 | return fclose(f); |
269 | } |
270 | |
271 | #undef PerlIO_eof |
272 | int |
c78749f2 |
273 | PerlIO_eof(PerlIO *f) |
760ac839 |
274 | { |
275 | return feof(f); |
276 | } |
277 | |
8c86a920 |
278 | #undef PerlIO_getname |
279 | char * |
c78749f2 |
280 | PerlIO_getname(PerlIO *f, char *buf) |
8c86a920 |
281 | { |
282 | #ifdef VMS |
283 | return fgetname(f,buf); |
284 | #else |
285 | croak("Don't know how to get file name"); |
c64afb19 |
286 | return NULL; |
8c86a920 |
287 | #endif |
288 | } |
289 | |
760ac839 |
290 | #undef PerlIO_getc |
291 | int |
c78749f2 |
292 | PerlIO_getc(PerlIO *f) |
760ac839 |
293 | { |
294 | return fgetc(f); |
295 | } |
296 | |
297 | #undef PerlIO_error |
298 | int |
c78749f2 |
299 | PerlIO_error(PerlIO *f) |
760ac839 |
300 | { |
301 | return ferror(f); |
302 | } |
303 | |
304 | #undef PerlIO_clearerr |
305 | void |
c78749f2 |
306 | PerlIO_clearerr(PerlIO *f) |
760ac839 |
307 | { |
308 | clearerr(f); |
309 | } |
310 | |
311 | #undef PerlIO_flush |
312 | int |
c78749f2 |
313 | PerlIO_flush(PerlIO *f) |
760ac839 |
314 | { |
315 | return Fflush(f); |
316 | } |
317 | |
318 | #undef PerlIO_fileno |
319 | int |
c78749f2 |
320 | PerlIO_fileno(PerlIO *f) |
760ac839 |
321 | { |
322 | return fileno(f); |
323 | } |
324 | |
325 | #undef PerlIO_setlinebuf |
326 | void |
c78749f2 |
327 | PerlIO_setlinebuf(PerlIO *f) |
760ac839 |
328 | { |
329 | #ifdef HAS_SETLINEBUF |
330 | setlinebuf(f); |
331 | #else |
3e3baf6d |
332 | # ifdef __BORLANDC__ /* Borland doesn't like NULL size for _IOLBF */ |
333 | setvbuf(f, Nullch, _IOLBF, BUFSIZ); |
334 | # else |
760ac839 |
335 | setvbuf(f, Nullch, _IOLBF, 0); |
3e3baf6d |
336 | # endif |
760ac839 |
337 | #endif |
338 | } |
339 | |
340 | #undef PerlIO_putc |
341 | int |
c78749f2 |
342 | PerlIO_putc(PerlIO *f, int ch) |
760ac839 |
343 | { |
9010f3dd |
344 | return putc(ch,f); |
760ac839 |
345 | } |
346 | |
347 | #undef PerlIO_ungetc |
348 | int |
c78749f2 |
349 | PerlIO_ungetc(PerlIO *f, int ch) |
760ac839 |
350 | { |
9010f3dd |
351 | return ungetc(ch,f); |
760ac839 |
352 | } |
353 | |
354 | #undef PerlIO_read |
5b54f415 |
355 | SSize_t |
c78749f2 |
356 | PerlIO_read(PerlIO *f, void *buf, Size_t count) |
760ac839 |
357 | { |
358 | return fread(buf,1,count,f); |
359 | } |
360 | |
361 | #undef PerlIO_write |
5b54f415 |
362 | SSize_t |
c78749f2 |
363 | PerlIO_write(PerlIO *f, const void *buf, Size_t count) |
760ac839 |
364 | { |
365 | return fwrite1(buf,1,count,f); |
366 | } |
367 | |
368 | #undef PerlIO_vprintf |
369 | int |
c78749f2 |
370 | PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap) |
760ac839 |
371 | { |
372 | return vfprintf(f,fmt,ap); |
373 | } |
374 | |
760ac839 |
375 | #undef PerlIO_tell |
5ff3f7a4 |
376 | Off_t |
c78749f2 |
377 | PerlIO_tell(PerlIO *f) |
760ac839 |
378 | { |
5ff3f7a4 |
379 | #ifdef HAS_FTELLO |
380 | return ftello(f); |
381 | #else |
760ac839 |
382 | return ftell(f); |
5ff3f7a4 |
383 | #endif |
760ac839 |
384 | } |
385 | |
386 | #undef PerlIO_seek |
387 | int |
c78749f2 |
388 | PerlIO_seek(PerlIO *f, Off_t offset, int whence) |
760ac839 |
389 | { |
5ff3f7a4 |
390 | #ifdef HAS_FSEEKO |
391 | return fseeko(f,offset,whence); |
392 | #else |
760ac839 |
393 | return fseek(f,offset,whence); |
5ff3f7a4 |
394 | #endif |
760ac839 |
395 | } |
396 | |
397 | #undef PerlIO_rewind |
398 | void |
c78749f2 |
399 | PerlIO_rewind(PerlIO *f) |
760ac839 |
400 | { |
401 | rewind(f); |
402 | } |
403 | |
404 | #undef PerlIO_printf |
405 | int |
760ac839 |
406 | PerlIO_printf(PerlIO *f,const char *fmt,...) |
760ac839 |
407 | { |
408 | va_list ap; |
409 | int result; |
760ac839 |
410 | va_start(ap,fmt); |
760ac839 |
411 | result = vfprintf(f,fmt,ap); |
412 | va_end(ap); |
413 | return result; |
414 | } |
415 | |
416 | #undef PerlIO_stdoutf |
417 | int |
760ac839 |
418 | PerlIO_stdoutf(const char *fmt,...) |
760ac839 |
419 | { |
420 | va_list ap; |
421 | int result; |
760ac839 |
422 | va_start(ap,fmt); |
760ac839 |
423 | result = PerlIO_vprintf(PerlIO_stdout(),fmt,ap); |
424 | va_end(ap); |
425 | return result; |
426 | } |
427 | |
428 | #undef PerlIO_tmpfile |
429 | PerlIO * |
c78749f2 |
430 | PerlIO_tmpfile(void) |
760ac839 |
431 | { |
432 | return tmpfile(); |
433 | } |
434 | |
435 | #undef PerlIO_importFILE |
436 | PerlIO * |
c78749f2 |
437 | PerlIO_importFILE(FILE *f, int fl) |
760ac839 |
438 | { |
439 | return f; |
440 | } |
441 | |
442 | #undef PerlIO_exportFILE |
443 | FILE * |
c78749f2 |
444 | PerlIO_exportFILE(PerlIO *f, int fl) |
760ac839 |
445 | { |
446 | return f; |
447 | } |
448 | |
449 | #undef PerlIO_findFILE |
450 | FILE * |
c78749f2 |
451 | PerlIO_findFILE(PerlIO *f) |
760ac839 |
452 | { |
453 | return f; |
454 | } |
455 | |
456 | #undef PerlIO_releaseFILE |
457 | void |
c78749f2 |
458 | PerlIO_releaseFILE(PerlIO *p, FILE *f) |
760ac839 |
459 | { |
460 | } |
461 | |
462 | void |
c78749f2 |
463 | PerlIO_init(void) |
760ac839 |
464 | { |
465 | /* Does nothing (yet) except force this file to be included |
466 | in perl binary. That allows this file to force inclusion |
467 | of other functions that may be required by loadable |
468 | extensions e.g. for FileHandle::tmpfile |
469 | */ |
470 | } |
471 | |
472 | #endif /* USE_SFIO */ |
473 | #endif /* PERLIO_IS_STDIO */ |
474 | |
475 | #ifndef HAS_FSETPOS |
476 | #undef PerlIO_setpos |
477 | int |
c78749f2 |
478 | PerlIO_setpos(PerlIO *f, const Fpos_t *pos) |
760ac839 |
479 | { |
480 | return PerlIO_seek(f,*pos,0); |
481 | } |
c411622e |
482 | #else |
483 | #ifndef PERLIO_IS_STDIO |
484 | #undef PerlIO_setpos |
485 | int |
c78749f2 |
486 | PerlIO_setpos(PerlIO *f, const Fpos_t *pos) |
c411622e |
487 | { |
488 | return fsetpos(f, pos); |
489 | } |
490 | #endif |
760ac839 |
491 | #endif |
492 | |
493 | #ifndef HAS_FGETPOS |
494 | #undef PerlIO_getpos |
495 | int |
c78749f2 |
496 | PerlIO_getpos(PerlIO *f, Fpos_t *pos) |
760ac839 |
497 | { |
498 | *pos = PerlIO_tell(f); |
499 | return 0; |
500 | } |
c411622e |
501 | #else |
502 | #ifndef PERLIO_IS_STDIO |
503 | #undef PerlIO_getpos |
504 | int |
c78749f2 |
505 | PerlIO_getpos(PerlIO *f, Fpos_t *pos) |
c411622e |
506 | { |
507 | return fgetpos(f, pos); |
508 | } |
509 | #endif |
760ac839 |
510 | #endif |
511 | |
512 | #if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF) |
513 | |
514 | int |
c78749f2 |
515 | vprintf(char *pat, char *args) |
662a7e3f |
516 | { |
517 | _doprnt(pat, args, stdout); |
518 | return 0; /* wrong, but perl doesn't use the return value */ |
519 | } |
520 | |
521 | int |
c78749f2 |
522 | vfprintf(FILE *fd, char *pat, char *args) |
760ac839 |
523 | { |
524 | _doprnt(pat, args, fd); |
525 | return 0; /* wrong, but perl doesn't use the return value */ |
526 | } |
527 | |
528 | #endif |
529 | |
530 | #ifndef PerlIO_vsprintf |
531 | int |
8ac85365 |
532 | PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap) |
760ac839 |
533 | { |
534 | int val = vsprintf(s, fmt, ap); |
535 | if (n >= 0) |
536 | { |
8c86a920 |
537 | if (strlen(s) >= (STRLEN)n) |
760ac839 |
538 | { |
539 | PerlIO_puts(PerlIO_stderr(),"panic: sprintf overflow - memory corrupted!\n"); |
540 | my_exit(1); |
541 | } |
542 | } |
543 | return val; |
544 | } |
545 | #endif |
546 | |
547 | #ifndef PerlIO_sprintf |
548 | int |
760ac839 |
549 | PerlIO_sprintf(char *s, int n, const char *fmt,...) |
760ac839 |
550 | { |
551 | va_list ap; |
552 | int result; |
760ac839 |
553 | va_start(ap,fmt); |
760ac839 |
554 | result = PerlIO_vsprintf(s, n, fmt, ap); |
555 | va_end(ap); |
556 | return result; |
557 | } |
558 | #endif |
559 | |