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) && defined (STDIO_PTR_LVAL_NOCHANGE_CNT)
178 #if defined(STDIO_PTR_LVAL_SETS_CNT)
179 assert (FILE_cnt(f) == cnt);
181 Perl_croak(aTHX_ "Cannot set 'cnt' of FILE * on this system when setting 'ptr'");
186 #undef PerlIO_get_cnt
188 PerlIO_get_cnt(PerlIO *f)
194 Perl_croak(aTHX_ "Cannot get 'cnt' of FILE * on this system");
199 #undef PerlIO_get_bufsiz
201 PerlIO_get_bufsiz(PerlIO *f)
204 return FILE_bufsiz(f);
207 Perl_croak(aTHX_ "Cannot get 'bufsiz' of FILE * on this system");
212 #undef PerlIO_get_ptr
214 PerlIO_get_ptr(PerlIO *f)
220 Perl_croak(aTHX_ "Cannot get 'ptr' of FILE * on this system");
225 #undef PerlIO_get_base
227 PerlIO_get_base(PerlIO *f)
233 Perl_croak(aTHX_ "Cannot get 'base' of FILE * on this system");
238 #undef PerlIO_has_base
240 PerlIO_has_base(PerlIO *f)
251 PerlIO_puts(PerlIO *f, const char *s)
258 PerlIO_open(const char *path, const char *mode)
260 return fopen(path,mode);
265 PerlIO_fdopen(int fd, const char *mode)
267 return fdopen(fd,mode);
272 PerlIO_reopen(const char *name, const char *mode, PerlIO *f)
274 return freopen(name,mode,f);
279 PerlIO_close(PerlIO *f)
286 PerlIO_eof(PerlIO *f)
291 #undef PerlIO_getname
293 PerlIO_getname(PerlIO *f, char *buf)
296 return fgetname(f,buf);
299 Perl_croak(aTHX_ "Don't know how to get file name");
306 PerlIO_getc(PerlIO *f)
313 PerlIO_error(PerlIO *f)
318 #undef PerlIO_clearerr
320 PerlIO_clearerr(PerlIO *f)
327 PerlIO_flush(PerlIO *f)
334 PerlIO_fileno(PerlIO *f)
339 #undef PerlIO_setlinebuf
341 PerlIO_setlinebuf(PerlIO *f)
343 #ifdef HAS_SETLINEBUF
346 # ifdef __BORLANDC__ /* Borland doesn't like NULL size for _IOLBF */
347 setvbuf(f, Nullch, _IOLBF, BUFSIZ);
349 setvbuf(f, Nullch, _IOLBF, 0);
356 PerlIO_putc(PerlIO *f, int ch)
363 PerlIO_ungetc(PerlIO *f, int ch)
370 PerlIO_read(PerlIO *f, void *buf, Size_t count)
372 return fread(buf,1,count,f);
377 PerlIO_write(PerlIO *f, const void *buf, Size_t count)
379 return fwrite1(buf,1,count,f);
382 #undef PerlIO_vprintf
384 PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap)
386 return vfprintf(f,fmt,ap);
391 PerlIO_tell(PerlIO *f)
393 #if defined(USE_64_BIT_STDIO) && defined(HAS_FTELLO) && !defined(USE_FTELL64)
402 PerlIO_seek(PerlIO *f, Off_t offset, int whence)
404 #if defined(USE_64_BIT_STDIO) && defined(HAS_FSEEKO) && !defined(USE_FSEEK64)
405 return fseeko(f,offset,whence);
407 return fseek(f,offset,whence);
413 PerlIO_rewind(PerlIO *f)
420 PerlIO_printf(PerlIO *f,const char *fmt,...)
425 result = vfprintf(f,fmt,ap);
430 #undef PerlIO_stdoutf
432 PerlIO_stdoutf(const char *fmt,...)
437 result = PerlIO_vprintf(PerlIO_stdout(),fmt,ap);
442 #undef PerlIO_tmpfile
449 #undef PerlIO_importFILE
451 PerlIO_importFILE(FILE *f, int fl)
456 #undef PerlIO_exportFILE
458 PerlIO_exportFILE(PerlIO *f, int fl)
463 #undef PerlIO_findFILE
465 PerlIO_findFILE(PerlIO *f)
470 #undef PerlIO_releaseFILE
472 PerlIO_releaseFILE(PerlIO *p, FILE *f)
479 /* Does nothing (yet) except force this file to be included
480 in perl binary. That allows this file to force inclusion
481 of other functions that may be required by loadable
482 extensions e.g. for FileHandle::tmpfile
486 #endif /* USE_SFIO */
487 #endif /* PERLIO_IS_STDIO */
493 PerlIO_setpos(PerlIO *f, const Off_t *pos)
495 PerlIO_setpos(PerlIO *f, const Fpos_t *pos)
498 return PerlIO_seek(f,*pos,0);
501 #ifndef PERLIO_IS_STDIO
504 PerlIO_setpos(PerlIO *f, const Fpos_t *pos)
506 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
507 return fsetpos64(f, pos);
509 return fsetpos(f, pos);
519 PerlIO_getpos(PerlIO *f, Off_t *pos)
521 *pos = PerlIO_seek(f,0,0);
525 PerlIO_getpos(PerlIO *f, Fpos_t *pos)
527 *pos = PerlIO_tell(f);
532 #ifndef PERLIO_IS_STDIO
535 PerlIO_getpos(PerlIO *f, Fpos_t *pos)
537 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
538 return fgetpos64(f, pos);
540 return fgetpos(f, pos);
546 #if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF)
549 vprintf(char *pat, char *args)
551 _doprnt(pat, args, stdout);
552 return 0; /* wrong, but perl doesn't use the return value */
556 vfprintf(FILE *fd, char *pat, char *args)
558 _doprnt(pat, args, fd);
559 return 0; /* wrong, but perl doesn't use the return value */
564 #ifndef PerlIO_vsprintf
566 PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap)
568 int val = vsprintf(s, fmt, ap);
571 if (strlen(s) >= (STRLEN)n)
574 PerlIO_puts(Perl_error_log,"panic: sprintf overflow - memory corrupted!\n");
582 #ifndef PerlIO_sprintf
584 PerlIO_sprintf(char *s, int n, const char *fmt,...)
589 result = PerlIO_vsprintf(s, n, fmt, ap);
595 #endif /* !PERL_IMPLICIT_SYS */