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