Fixed two long-standing locale bugs.
[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
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
29void
8ac85365 30PerlIO_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
40PerlIO *
8ac85365 41PerlIO_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
58PerlIO *
c78749f2 59PerlIO_tmpfile(void)
760ac839 60{
61 return sftmp(0);
62}
63
64void
c78749f2 65PerlIO_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
87PerlIO *
c78749f2 88PerlIO_stderr(void)
760ac839 89{
90 return (PerlIO *) stderr;
91}
92
93#undef PerlIO_stdin
94PerlIO *
c78749f2 95PerlIO_stdin(void)
760ac839 96{
97 return (PerlIO *) stdin;
98}
99
100#undef PerlIO_stdout
101PerlIO *
c78749f2 102PerlIO_stdout(void)
760ac839 103{
104 return (PerlIO *) stdout;
105}
106
760ac839 107#undef PerlIO_fast_gets
108int
c78749f2 109PerlIO_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
119int
c78749f2 120PerlIO_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
130int
c78749f2 131PerlIO_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
141void
c78749f2 142PerlIO_set_cnt(PerlIO *f, int cnt)
760ac839 143{
159bba36 144 if (cnt < -1)
cea2e8a9 145 Perl_warn(aTHX_ "Setting cnt to %d\n",cnt);
760ac839 146#if defined(USE_STDIO_PTR) && defined(STDIO_CNT_LVALUE)
147 FILE_cnt(f) = cnt;
148#else
cea2e8a9 149 Perl_croak(aTHX_ "Cannot set 'cnt' of FILE * on this system");
760ac839 150#endif
151}
152
153#undef PerlIO_set_ptrcnt
154void
c78749f2 155PerlIO_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)
cea2e8a9 161 Perl_warn(aTHX_ "Setting ptr %p > end+1 %p\n", ptr, e + 1);
760ac839 162 if (cnt != ec)
cea2e8a9 163 Perl_warn(aTHX_ "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
cea2e8a9 168 Perl_croak(aTHX_ "Cannot set 'ptr' of FILE * on this system");
760ac839 169#endif
170#if defined(USE_STDIO_PTR) && defined(STDIO_CNT_LVALUE)
171 FILE_cnt(f) = cnt;
172#else
cea2e8a9 173 Perl_croak(aTHX_ "Cannot set 'cnt' of FILE * on this system");
760ac839 174#endif
175}
176
177#undef PerlIO_get_cnt
178int
c78749f2 179PerlIO_get_cnt(PerlIO *f)
760ac839 180{
181#ifdef FILE_cnt
182 return FILE_cnt(f);
183#else
cea2e8a9 184 Perl_croak(aTHX_ "Cannot get 'cnt' of FILE * on this system");
760ac839 185 return -1;
186#endif
187}
188
189#undef PerlIO_get_bufsiz
190int
c78749f2 191PerlIO_get_bufsiz(PerlIO *f)
760ac839 192{
193#ifdef FILE_bufsiz
194 return FILE_bufsiz(f);
195#else
cea2e8a9 196 Perl_croak(aTHX_ "Cannot get 'bufsiz' of FILE * on this system");
760ac839 197 return -1;
198#endif
199}
200
201#undef PerlIO_get_ptr
888911fc 202STDCHAR *
c78749f2 203PerlIO_get_ptr(PerlIO *f)
760ac839 204{
205#ifdef FILE_ptr
888911fc 206 return FILE_ptr(f);
760ac839 207#else
cea2e8a9 208 Perl_croak(aTHX_ "Cannot get 'ptr' of FILE * on this system");
760ac839 209 return NULL;
210#endif
211}
212
213#undef PerlIO_get_base
888911fc 214STDCHAR *
c78749f2 215PerlIO_get_base(PerlIO *f)
760ac839 216{
217#ifdef FILE_base
888911fc 218 return FILE_base(f);
760ac839 219#else
cea2e8a9 220 Perl_croak(aTHX_ "Cannot get 'base' of FILE * on this system");
760ac839 221 return NULL;
222#endif
223}
224
225#undef PerlIO_has_base
226int
c78749f2 227PerlIO_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
237int
c78749f2 238PerlIO_puts(PerlIO *f, const char *s)
760ac839 239{
240 return fputs(s,f);
241}
242
243#undef PerlIO_open
244PerlIO *
c78749f2 245PerlIO_open(const char *path, const char *mode)
760ac839 246{
247 return fopen(path,mode);
248}
249
250#undef PerlIO_fdopen
251PerlIO *
c78749f2 252PerlIO_fdopen(int fd, const char *mode)
760ac839 253{
254 return fdopen(fd,mode);
255}
256
8c86a920 257#undef PerlIO_reopen
258PerlIO *
c78749f2 259PerlIO_reopen(const char *name, const char *mode, PerlIO *f)
8c86a920 260{
261 return freopen(name,mode,f);
262}
760ac839 263
264#undef PerlIO_close
265int
c78749f2 266PerlIO_close(PerlIO *f)
760ac839 267{
268 return fclose(f);
269}
270
271#undef PerlIO_eof
272int
c78749f2 273PerlIO_eof(PerlIO *f)
760ac839 274{
275 return feof(f);
276}
277
8c86a920 278#undef PerlIO_getname
279char *
c78749f2 280PerlIO_getname(PerlIO *f, char *buf)
8c86a920 281{
282#ifdef VMS
283 return fgetname(f,buf);
284#else
cea2e8a9 285 Perl_croak(aTHX_ "Don't know how to get file name");
c64afb19 286 return NULL;
8c86a920 287#endif
288}
289
760ac839 290#undef PerlIO_getc
291int
c78749f2 292PerlIO_getc(PerlIO *f)
760ac839 293{
294 return fgetc(f);
295}
296
297#undef PerlIO_error
298int
c78749f2 299PerlIO_error(PerlIO *f)
760ac839 300{
301 return ferror(f);
302}
303
304#undef PerlIO_clearerr
305void
c78749f2 306PerlIO_clearerr(PerlIO *f)
760ac839 307{
308 clearerr(f);
309}
310
311#undef PerlIO_flush
312int
c78749f2 313PerlIO_flush(PerlIO *f)
760ac839 314{
315 return Fflush(f);
316}
317
318#undef PerlIO_fileno
319int
c78749f2 320PerlIO_fileno(PerlIO *f)
760ac839 321{
322 return fileno(f);
323}
324
325#undef PerlIO_setlinebuf
326void
c78749f2 327PerlIO_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
341int
c78749f2 342PerlIO_putc(PerlIO *f, int ch)
760ac839 343{
9010f3dd 344 return putc(ch,f);
760ac839 345}
346
347#undef PerlIO_ungetc
348int
c78749f2 349PerlIO_ungetc(PerlIO *f, int ch)
760ac839 350{
9010f3dd 351 return ungetc(ch,f);
760ac839 352}
353
354#undef PerlIO_read
5b54f415 355SSize_t
c78749f2 356PerlIO_read(PerlIO *f, void *buf, Size_t count)
760ac839 357{
358 return fread(buf,1,count,f);
359}
360
361#undef PerlIO_write
5b54f415 362SSize_t
c78749f2 363PerlIO_write(PerlIO *f, const void *buf, Size_t count)
760ac839 364{
365 return fwrite1(buf,1,count,f);
366}
367
368#undef PerlIO_vprintf
369int
c78749f2 370PerlIO_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 376Off_t
c78749f2 377PerlIO_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
387int
c78749f2 388PerlIO_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
398void
c78749f2 399PerlIO_rewind(PerlIO *f)
760ac839 400{
401 rewind(f);
402}
403
404#undef PerlIO_printf
405int
760ac839 406PerlIO_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
417int
760ac839 418PerlIO_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
429PerlIO *
c78749f2 430PerlIO_tmpfile(void)
760ac839 431{
432 return tmpfile();
433}
434
435#undef PerlIO_importFILE
436PerlIO *
c78749f2 437PerlIO_importFILE(FILE *f, int fl)
760ac839 438{
439 return f;
440}
441
442#undef PerlIO_exportFILE
443FILE *
c78749f2 444PerlIO_exportFILE(PerlIO *f, int fl)
760ac839 445{
446 return f;
447}
448
449#undef PerlIO_findFILE
450FILE *
c78749f2 451PerlIO_findFILE(PerlIO *f)
760ac839 452{
453 return f;
454}
455
456#undef PerlIO_releaseFILE
457void
c78749f2 458PerlIO_releaseFILE(PerlIO *p, FILE *f)
760ac839 459{
460}
461
462void
c78749f2 463PerlIO_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
477int
c78749f2 478PerlIO_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
485int
c78749f2 486PerlIO_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
495int
c78749f2 496PerlIO_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
504int
c78749f2 505PerlIO_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
514int
c78749f2 515vprintf(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
521int
c78749f2 522vfprintf(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
531int
8ac85365 532PerlIO_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");
cea2e8a9 540 {
541 dTHX;
542 my_exit(1);
543 }
760ac839 544 }
545 }
546 return val;
547}
548#endif
549
550#ifndef PerlIO_sprintf
551int
760ac839 552PerlIO_sprintf(char *s, int n, const char *fmt,...)
760ac839 553{
554 va_list ap;
555 int result;
760ac839 556 va_start(ap,fmt);
760ac839 557 result = PerlIO_vsprintf(s, n, fmt, ap);
558 va_end(ap);
559 return result;
560}
561#endif
562