3 * Copyright (c) 1996-2000, Nick Ing-Simmons
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.
14 #define PERLIO_NOT_STDIO 0
15 #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
19 * This file provides those parts of PerlIO abstraction
20 * which are not #defined in iperlsys.h.
21 * Which these are depends on various Configure #ifdef's
25 #define PERL_IN_PERLIO_C
28 #if !defined(PERL_IMPLICIT_SYS)
30 #ifdef PERLIO_IS_STDIO
35 /* Does nothing (yet) except force this file to be included
36 in perl binary. That allows this file to force inclusion
37 of other functions that may be required by loadable
38 extensions e.g. for FileHandle::tmpfile
49 #else /* PERLIO_IS_STDIO */
56 /* This section is just to make sure these functions
57 get pulled in from libsfio.a
70 /* Force this file to be included in perl binary. Which allows
71 * this file to force inclusion of other functions that may be
72 * required by loadable extensions e.g. for FileHandle::tmpfile
76 * sfio does its own 'autoflush' on stdout in common cases.
77 * Flush results in a lot of lseek()s to regular files and
78 * lot of small writes to pipes.
80 sfset(sfstdout,SF_SHARE,0);
85 /* Implement all the PerlIO interface using stdio.
86 - this should be only file to include <stdio.h>
93 return (PerlIO *) stderr;
100 return (PerlIO *) stdin;
107 return (PerlIO *) stdout;
110 #undef PerlIO_fast_gets
112 PerlIO_fast_gets(PerlIO *f)
114 #if defined(USE_STDIO_PTR) && defined(STDIO_PTR_LVALUE) && defined(STDIO_CNT_LVALUE)
121 #undef PerlIO_has_cntptr
123 PerlIO_has_cntptr(PerlIO *f)
125 #if defined(USE_STDIO_PTR)
132 #undef PerlIO_canset_cnt
134 PerlIO_canset_cnt(PerlIO *f)
136 #if defined(USE_STDIO_PTR) && defined(STDIO_CNT_LVALUE)
143 #undef PerlIO_set_cnt
145 PerlIO_set_cnt(PerlIO *f, int cnt)
148 if (cnt < -1 && ckWARN_d(WARN_INTERNAL))
149 Perl_warner(aTHX_ WARN_INTERNAL, "Setting cnt to %d\n",cnt);
150 #if defined(USE_STDIO_PTR) && defined(STDIO_CNT_LVALUE)
153 Perl_croak(aTHX_ "Cannot set 'cnt' of FILE * on this system");
157 #undef PerlIO_set_ptrcnt
159 PerlIO_set_ptrcnt(PerlIO *f, STDCHAR *ptr, int cnt)
163 STDCHAR *e = FILE_base(f) + FILE_bufsiz(f);
165 if (ptr > e + 1 && ckWARN_d(WARN_INTERNAL))
166 Perl_warner(aTHX_ WARN_INTERNAL, "Setting ptr %p > end+1 %p\n", ptr, e + 1);
167 if (cnt != ec && ckWARN_d(WARN_INTERNAL))
168 Perl_warner(aTHX_ WARN_INTERNAL, "Setting cnt to %d, ptr implies %d\n",cnt,ec);
170 #if defined(USE_STDIO_PTR) && defined(STDIO_PTR_LVALUE)
173 Perl_croak(aTHX_ "Cannot set 'ptr' of FILE * on this system");
175 #if defined(USE_STDIO_PTR) && defined(STDIO_CNT_LVALUE)
178 Perl_croak(aTHX_ "Cannot set 'cnt' of FILE * on this system");
182 #undef PerlIO_get_cnt
184 PerlIO_get_cnt(PerlIO *f)
190 Perl_croak(aTHX_ "Cannot get 'cnt' of FILE * on this system");
195 #undef PerlIO_get_bufsiz
197 PerlIO_get_bufsiz(PerlIO *f)
200 return FILE_bufsiz(f);
203 Perl_croak(aTHX_ "Cannot get 'bufsiz' of FILE * on this system");
208 #undef PerlIO_get_ptr
210 PerlIO_get_ptr(PerlIO *f)
216 Perl_croak(aTHX_ "Cannot get 'ptr' of FILE * on this system");
221 #undef PerlIO_get_base
223 PerlIO_get_base(PerlIO *f)
229 Perl_croak(aTHX_ "Cannot get 'base' of FILE * on this system");
234 #undef PerlIO_has_base
236 PerlIO_has_base(PerlIO *f)
247 PerlIO_puts(PerlIO *f, const char *s)
254 PerlIO_open(const char *path, const char *mode)
256 return fopen(path,mode);
261 PerlIO_fdopen(int fd, const char *mode)
263 return fdopen(fd,mode);
268 PerlIO_reopen(const char *name, const char *mode, PerlIO *f)
270 return freopen(name,mode,f);
275 PerlIO_close(PerlIO *f)
282 PerlIO_eof(PerlIO *f)
287 #undef PerlIO_getname
289 PerlIO_getname(PerlIO *f, char *buf)
292 return fgetname(f,buf);
295 Perl_croak(aTHX_ "Don't know how to get file name");
302 PerlIO_getc(PerlIO *f)
309 PerlIO_error(PerlIO *f)
314 #undef PerlIO_clearerr
316 PerlIO_clearerr(PerlIO *f)
323 PerlIO_flush(PerlIO *f)
330 PerlIO_fileno(PerlIO *f)
335 #undef PerlIO_setlinebuf
337 PerlIO_setlinebuf(PerlIO *f)
339 #ifdef HAS_SETLINEBUF
342 # ifdef __BORLANDC__ /* Borland doesn't like NULL size for _IOLBF */
343 setvbuf(f, Nullch, _IOLBF, BUFSIZ);
345 setvbuf(f, Nullch, _IOLBF, 0);
352 PerlIO_putc(PerlIO *f, int ch)
359 PerlIO_ungetc(PerlIO *f, int ch)
366 PerlIO_read(PerlIO *f, void *buf, Size_t count)
368 return fread(buf,1,count,f);
373 PerlIO_write(PerlIO *f, const void *buf, Size_t count)
375 return fwrite1(buf,1,count,f);
378 #undef PerlIO_vprintf
380 PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap)
382 return vfprintf(f,fmt,ap);
387 PerlIO_tell(PerlIO *f)
389 #if defined(USE_64_BIT_STDIO) && defined(HAS_FTELLO) && !defined(USE_FTELL64)
398 PerlIO_seek(PerlIO *f, Off_t offset, int whence)
400 #if defined(USE_64_BIT_STDIO) && defined(HAS_FSEEKO) && !defined(USE_FSEEK64)
401 return fseeko(f,offset,whence);
403 return fseek(f,offset,whence);
409 PerlIO_rewind(PerlIO *f)
416 PerlIO_printf(PerlIO *f,const char *fmt,...)
421 result = vfprintf(f,fmt,ap);
426 #undef PerlIO_stdoutf
428 PerlIO_stdoutf(const char *fmt,...)
433 result = PerlIO_vprintf(PerlIO_stdout(),fmt,ap);
438 #undef PerlIO_tmpfile
445 #undef PerlIO_importFILE
447 PerlIO_importFILE(FILE *f, int fl)
452 #undef PerlIO_exportFILE
454 PerlIO_exportFILE(PerlIO *f, int fl)
459 #undef PerlIO_findFILE
461 PerlIO_findFILE(PerlIO *f)
466 #undef PerlIO_releaseFILE
468 PerlIO_releaseFILE(PerlIO *p, FILE *f)
475 /* Does nothing (yet) except force this file to be included
476 in perl binary. That allows this file to force inclusion
477 of other functions that may be required by loadable
478 extensions e.g. for FileHandle::tmpfile
482 #endif /* USE_SFIO */
483 #endif /* PERLIO_IS_STDIO */
488 PerlIO_setpos(PerlIO *f, const Fpos_t *pos)
490 return PerlIO_seek(f,*pos,0);
493 #ifndef PERLIO_IS_STDIO
496 PerlIO_setpos(PerlIO *f, const Fpos_t *pos)
498 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
499 return fsetpos64(f, pos);
501 return fsetpos(f, pos);
510 PerlIO_getpos(PerlIO *f, Fpos_t *pos)
512 *pos = PerlIO_tell(f);
516 #ifndef PERLIO_IS_STDIO
519 PerlIO_getpos(PerlIO *f, Fpos_t *pos)
521 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
522 return fgetpos64(f, pos);
524 return fgetpos(f, pos);
530 #if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF)
533 vprintf(char *pat, char *args)
535 _doprnt(pat, args, stdout);
536 return 0; /* wrong, but perl doesn't use the return value */
540 vfprintf(FILE *fd, char *pat, char *args)
542 _doprnt(pat, args, fd);
543 return 0; /* wrong, but perl doesn't use the return value */
548 #ifndef PerlIO_vsprintf
550 PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap)
552 int val = vsprintf(s, fmt, ap);
555 if (strlen(s) >= (STRLEN)n)
558 PerlIO_puts(Perl_error_log,"panic: sprintf overflow - memory corrupted!\n");
566 #ifndef PerlIO_sprintf
568 PerlIO_sprintf(char *s, int n, const char *fmt,...)
573 result = PerlIO_vsprintf(s, n, fmt, ap);
579 #endif /* !PERL_IMPLICIT_SYS */