summaryrefslogtreecommitdiff
path: root/perlio.c
blob: cf93f9951d10e77cf45e761cd666cd7557239366 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
/*    perlio.c
 *
 *    Copyright (c) 1996-2000, Nick Ing-Simmons
 *
 *    You may distribute under the terms of either the GNU General Public
 *    License or the Artistic License, as specified in the README file.
 *
 */

#define VOIDUSED 1
#ifdef PERL_MICRO
#   include "uconfig.h"
#else
#   include "config.h"
#endif

#define PERLIO_NOT_STDIO 0
#if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
/* #define PerlIO FILE */
#endif
/*
 * This file provides those parts of PerlIO abstraction
 * which are not #defined in iperlsys.h.
 * Which these are depends on various Configure #ifdef's
 */

#include "EXTERN.h"
#define PERL_IN_PERLIO_C
#include "perl.h"

#if !defined(PERL_IMPLICIT_SYS)

#ifdef PERLIO_IS_STDIO

void
PerlIO_init(void)
{
 /* Does nothing (yet) except force this file to be included
    in perl binary. That allows this file to force inclusion
    of other functions that may be required by loadable
    extensions e.g. for FileHandle::tmpfile
 */
}

#undef PerlIO_tmpfile
PerlIO *
PerlIO_tmpfile(void)
{
 return tmpfile();
}

#else /* PERLIO_IS_STDIO */

#ifdef USE_SFIO

#undef HAS_FSETPOS
#undef HAS_FGETPOS

/* This section is just to make sure these functions
   get pulled in from libsfio.a
*/

#undef PerlIO_tmpfile
PerlIO *
PerlIO_tmpfile(void)
{
 return sftmp(0);
}

void
PerlIO_init(void)
{
 /* Force this file to be included  in perl binary. Which allows
  *  this file to force inclusion  of other functions that may be
  *  required by loadable  extensions e.g. for FileHandle::tmpfile
  */

 /* Hack
  * sfio does its own 'autoflush' on stdout in common cases.
  * Flush results in a lot of lseek()s to regular files and
  * lot of small writes to pipes.
  */
 sfset(sfstdout,SF_SHARE,0);
}

#else /* USE_SFIO */

/*======================================================================================*/

/* Implement all the PerlIO interface ourselves.
*/

/* We _MUST_ have <unistd.h> if we are using lseek() and may have large files */
#ifdef I_UNISTD
#include <unistd.h>
#endif

#undef printf
void PerlIO_debug(char *fmt,...) __attribute__((format(printf,1,2)));

void
PerlIO_debug(char *fmt,...)
{
 static int dbg = 0;
 if (!dbg)
  {
   char *s = getenv("PERLIO_DEBUG");
   if (s && *s)
    dbg = open(s,O_WRONLY|O_CREAT|O_APPEND,0666);
   else
    dbg = -1;
  }
 if (dbg > 0)
  {
   dTHX;
   va_list ap;
   SV *sv = newSVpvn("",0);
   char *s;
   STRLEN len;
   va_start(ap,fmt);
   sv_vcatpvf(sv, fmt, &ap);
   s = SvPV(sv,len);
   write(dbg,s,len);
   va_end(ap);
   SvREFCNT_dec(sv);
  }
}

#define PERLIO_F_EOF		0x010000
#define PERLIO_F_ERROR		0x020000
#define PERLIO_F_LINEBUF	0x040000
#define PERLIO_F_TEMP		0x080000
#define PERLIO_F_RDBUF		0x100000
#define PERLIO_F_WRBUF		0x200000
#define PERLIO_F_OPEN		0x400000
#define PERLIO_F_USED		0x800000

struct _PerlIO
{
 IV       flags;      /* Various flags for state */
 IV       fd;         /* Maybe pointer on some OSes */
 int      oflags;     /* open/fcntl flags */
 STDCHAR *buf;        /* Start of buffer */
 STDCHAR *end;        /* End of valid part of buffer */
 STDCHAR *ptr;        /* Current position in buffer */
 Size_t   bufsiz;     /* Size of buffer */
 Off_t    posn;       /* Offset of f->buf into the file */
 int      oneword;    /* An if-all-else-fails area as a buffer */
};

/* Table of pointers to the PerlIO structs (malloc'ed) */
PerlIO **_perlio     = NULL;
int _perlio_size     = 0;

void
PerlIO_alloc_buf(PerlIO *f)
{
 if (!f->bufsiz)
  f->bufsiz = 4096;
 New('B',f->buf,f->bufsiz,char);
 if (!f->buf)
  {
   f->buf = (STDCHAR *)&f->oneword;
   f->bufsiz = sizeof(f->oneword);
  }
 f->ptr = f->buf;
 f->end = f->ptr;
}


/* This "flush" is akin to sfio's sync in that it handles files in either
   read or write state
*/
#undef PerlIO_flush
int
PerlIO_flush(PerlIO *f)
{
 int code = 0;
 if (f)
  {
   PerlIO_debug(__FUNCTION__ " f=%p flags=%08X c=%d buf=%p ptr=%p\n",
                f,f->flags,(f->ptr-f->buf),f->buf,f->ptr);
   if (f->flags & PERLIO_F_WRBUF)
    {
     /* write() the buffer */
     STDCHAR *p = f->buf;
     int count;
     while (p < f->ptr)
      {
       count = write(f->fd,p,f->ptr - p);
       if (count > 0)
        {
         p += count;
        }
       else if (count < 0 && errno != EINTR)
        {
         f->flags |= PERLIO_F_ERROR;
         code = -1;
         break;
        }
      }
     f->posn += (p - f->buf);
    }
   else if (f->flags & PERLIO_F_RDBUF)
    {
     /* Note position change */
     f->posn += (f->ptr - f->buf);
     if (f->ptr < f->end)
      {
       /* We did not consume all of it */
       f->posn = lseek(f->fd,f->posn,SEEK_SET);
      }
    }
   f->ptr = f->end = f->buf;
   f->flags &= ~(PERLIO_F_RDBUF|PERLIO_F_WRBUF);
  }
 else
  {
   int i;
   for (i=_perlio_size; i >= 0; i--)
    {
     if ((f = _perlio[i]))
      {
       if (PerlIO_flush(f) != 0)
        code = -1;
      }
    }
  }
 return code;
}

int
PerlIO_oflags(const char *mode)
{
 int oflags = -1;
 PerlIO_debug(__FUNCTION__ " %s = ",mode);
 switch(*mode)
  {
   case 'r':
    oflags = O_RDONLY;
    if (*++mode == '+')
     {
      oflags = O_RDWR;
      mode++;
     }
    break;

   case 'w':
    oflags = O_CREAT|O_TRUNC;
    if (*++mode == '+')
     {
      oflags |= O_RDWR;
      mode++;
     }
    else
     oflags |= O_WRONLY;
    break;

   case 'a':
    oflags = O_CREAT|O_APPEND;
    if (*++mode == '+')
     {
      oflags |= O_RDWR;
      mode++;
     }
    else
     oflags |= O_WRONLY;
    break;
  }
 if (*mode || oflags == -1)
  {
   errno = EINVAL;
   oflags = -1;
  }
 PerlIO_debug(" %X '%s'\n",oflags,mode);
 return oflags;
}

PerlIO *
PerlIO_allocate(void)
{
 /* Find a free slot in the table, growing table as necessary */
 PerlIO *f;
 int i = 0;
 while (1)
  {
   PerlIO **table = _perlio;
   while (i < _perlio_size)
    {
     f = table[i];
     PerlIO_debug(__FUNCTION__ " try %d %p\n",i,f);
     if (!f)
      {
       Newz('F',f,1,PerlIO);
       if (!f)
        return NULL;
       table[i] = f;
      }
     if (!(f->flags & PERLIO_F_USED))
      {
       Zero(f,1,PerlIO);
       f->flags = PERLIO_F_USED;
       return f;
      }
     i++;
    }
   Newz('I',table,_perlio_size+16,PerlIO *);
   if (!table)
    return NULL;
   Copy(_perlio,table,_perlio_size,PerlIO *);
   if (_perlio)
    Safefree(_perlio);
   _perlio = table;
   _perlio_size += 16;
  }
}

#undef PerlIO_fdopen
PerlIO *
PerlIO_fdopen(int fd, const char *mode)
{
 PerlIO *f = NULL;
 if (fd >= 0)
  {
   if ((f = PerlIO_allocate()))
    {
     f->fd     = fd;
     f->oflags = PerlIO_oflags(mode);
     f->flags  |= (PERLIO_F_OPEN|PERLIO_F_USED);
    }
  }
 PerlIO_debug(__FUNCTION__ " fd=%d m=%s f=%p\n",fd,mode,f);
 return f;
}

#undef PerlIO_fileno
int
PerlIO_fileno(PerlIO *f)
{
 if (f && (f->flags & PERLIO_F_OPEN))
  {
   return f->fd;
  }
 return -1;
}

#undef PerlIO_close
int
PerlIO_close(PerlIO *f)
{
 int code = 0;
 if (f)
  {
   if (PerlIO_flush(f) != 0)
    code = -1;
   while (close(f->fd) != 0)
    {
     if (errno != EINTR)
      {
       code = -1;
       break;
      }
    }
   f->flags &= ~PERLIO_F_OPEN;
   f->fd     = -1;
   if (f->buf && f->buf != (STDCHAR *) &f->oneword)
    {
     Safefree(f->buf);
    }
   f->buf = NULL;
   f->ptr = f->end = f->buf;
   f->flags &= ~(PERLIO_F_USED|PERLIO_F_RDBUF|PERLIO_F_WRBUF);
  }
 return code;
}

void
PerlIO_cleanup(void)
{
 /* Close all the files */
 int i;
 PerlIO_debug(__FUNCTION__ "\n");
 for (i=_perlio_size-1; i >= 0; i--)
  {
   PerlIO *f = _perlio[i];
   if (f)
    {
     PerlIO_close(f);
     Safefree(f);
    }
  }
 if (_perlio)
  Safefree(_perlio);
 _perlio      = NULL;
 _perlio_size = 0;
}

#undef PerlIO_open
PerlIO *
PerlIO_open(const char *path, const char *mode)
{
 PerlIO *f = NULL;
 int oflags = PerlIO_oflags(mode);
 if (oflags != -1)
  {
   int fd = open(path,oflags,0666);
   if (fd >= 0)
    {
     PerlIO_debug(__FUNCTION__ "fd=%d\n",fd);
     f = PerlIO_fdopen(fd,mode);
     if (!f)
      close(fd);
    }
  }
 PerlIO_debug(__FUNCTION__ " path=%s m=%s f=%p\n",path,mode,f);
 return f;
}

#undef PerlIO_reopen
PerlIO *
PerlIO_reopen(const char *path, const char *mode, PerlIO *f)
{
 PerlIO_debug(__FUNCTION__ " n=%s m=%s f=%p\n",path,mode,f);
 if (f)
  {
   int oflags = PerlIO_oflags(mode);
   PerlIO_close(f);
   if (oflags != -1)
    {
     int fd = open(path,oflags,0666);
     if (fd >= 0)
      {
       PerlIO_debug(__FUNCTION__ "fd=%d\n",fd);
       f->oflags = oflags;
       f->flags  |= (PERLIO_F_OPEN|PERLIO_F_USED);
      }
    }
   else
    {
     return NULL;
    }
  }
 return PerlIO_open(path,mode);
}

void
PerlIO_init(void)
{
 if (!_perlio)
  {
   atexit(&PerlIO_cleanup);
   PerlIO_fdopen(0,"r");
   PerlIO_fdopen(1,"w");
   PerlIO_fdopen(2,"w");
  }
 PerlIO_debug(__FUNCTION__ "\n");
}

#undef PerlIO_stdin
PerlIO *
PerlIO_stdin(void)
{
 if (!_perlio)
  PerlIO_init();
 return _perlio[0];
}

#undef PerlIO_stdout
PerlIO *
PerlIO_stdout(void)
{
 if (!_perlio)
  PerlIO_init();
 return _perlio[1];
}

#undef PerlIO_stderr
PerlIO *
PerlIO_stderr(void)
{
 if (!_perlio)
  PerlIO_init();
 return _perlio[2];
}

#undef PerlIO_fast_gets
int
PerlIO_fast_gets(PerlIO *f)
{
 return 1;
}

#undef PerlIO_has_cntptr
int
PerlIO_has_cntptr(PerlIO *f)
{
 return 1;
}

#undef PerlIO_canset_cnt
int
PerlIO_canset_cnt(PerlIO *f)
{
 return 1;
}

#undef PerlIO_set_cnt
void
PerlIO_set_cnt(PerlIO *f, int cnt)
{
 if (f)
  {
   dTHX;
   if (!f->buf)
    PerlIO_alloc_buf(f);
   f->ptr = f->end - cnt;
   assert(f->ptr >= f->buf);
  }
}

#undef PerlIO_get_cnt
int
PerlIO_get_cnt(PerlIO *f)
{
 if (f)
  {
   if (!f->buf)
    PerlIO_alloc_buf(f);
   if (f->flags & PERLIO_F_RDBUF)
    return (f->end - f->ptr);
  }
 return 0;
}

#undef PerlIO_set_ptrcnt
void
PerlIO_set_ptrcnt(PerlIO *f, STDCHAR *ptr, int cnt)
{
 if (f)
  {
   if (!f->buf)
    PerlIO_alloc_buf(f);
   f->ptr = ptr;
   if (PerlIO_get_cnt(f) != cnt || f->ptr < f->buf)
    {
     dTHX;
     assert(PerlIO_get_cnt(f) == cnt);
     assert(f->ptr >= f->buf);
    }
   f->flags |= PERLIO_F_RDBUF;
  }
}

#undef PerlIO_get_bufsiz
int
PerlIO_get_bufsiz(PerlIO *f)
{
 if (f)
  {
   if (!f->buf)
    PerlIO_alloc_buf(f);
   return f->bufsiz;
  }
 return -1;
}

#undef PerlIO_get_ptr
STDCHAR *
PerlIO_get_ptr(PerlIO *f)
{
 if (f)
  {
   if (!f->buf)
    PerlIO_alloc_buf(f);
   return f->ptr;
  }
 return NULL;
}

#undef PerlIO_get_base
STDCHAR *
PerlIO_get_base(PerlIO *f)
{
 if (f)
  {
   if (!f->buf)
    PerlIO_alloc_buf(f);
   return f->buf;
  }
 return NULL;
}

#undef PerlIO_has_base
int
PerlIO_has_base(PerlIO *f)
{
 if (f)
  {
   if (!f->buf)
    PerlIO_alloc_buf(f);
   return f->buf != NULL;
  }
}

#undef PerlIO_puts
int
PerlIO_puts(PerlIO *f, const char *s)
{
 STRLEN len = strlen(s);
 return PerlIO_write(f,s,len);
}

#undef PerlIO_eof
int
PerlIO_eof(PerlIO *f)
{
 if (f)
  {
   return (f->flags & PERLIO_F_EOF) != 0;
  }
 return 1;
}

#undef PerlIO_getname
char *
PerlIO_getname(PerlIO *f, char *buf)
{
 dTHX;
 Perl_croak(aTHX_ "Don't know how to get file name");
 return NULL;
}

#undef PerlIO_ungetc
int
PerlIO_ungetc(PerlIO *f, int ch)
{
 if (f->buf && (f->flags & PERLIO_F_RDBUF) && f->ptr > f->buf)
  {
   *--(f->ptr) = ch;
   return ch;
  }
 PerlIO_debug(__FUNCTION__ " f=%p c=%c - cannot\n",f,ch);
 return -1;
}

#undef PerlIO_read
SSize_t
PerlIO_read(PerlIO *f, void *vbuf, Size_t count)
{
 STDCHAR *buf = (STDCHAR *) vbuf;
 if (f)
  {
   Size_t got = 0;
   if (!f->ptr)
    PerlIO_alloc_buf(f);

   while (count > 0)
    {
     SSize_t avail = (f->end - f->ptr);
     if ((SSize_t) count < avail)
      avail = count;
     if (avail > 0)
      {
       Copy(f->ptr,buf,avail,char);
       got     += avail;
       f->ptr  += avail;
       count   -= avail;
       buf     += avail;
      }
     if (count && (f->ptr >= f->end))
      {
       PerlIO_flush(f);
       f->ptr = f->end = f->buf;
       avail = read(f->fd,f->ptr,f->bufsiz);
       if (avail <= 0)
        {
         if (avail == 0)
          f->flags |= PERLIO_F_EOF;
         else if (errno == EINTR)
          continue;
         else
          f->flags |= PERLIO_F_ERROR;
         break;
        }
       f->end   = f->buf+avail;
       f->flags |= PERLIO_F_RDBUF;
      }
    }
   return got;
  }
 return 0;
}

#undef PerlIO_getc
int
PerlIO_getc(PerlIO *f)
{
 STDCHAR buf;
 int count = PerlIO_read(f,&buf,1);
 if (count == 1)
  return (unsigned char) buf;
 return -1;
}

#undef PerlIO_error
int
PerlIO_error(PerlIO *f)
{
 if (f)
  {
   return f->flags & PERLIO_F_ERROR;
  }
 return 1;
}

#undef PerlIO_clearerr
void
PerlIO_clearerr(PerlIO *f)
{
 if (f)
  {
   f->flags &= ~PERLIO_F_ERROR;
  }
}

#undef PerlIO_setlinebuf
void
PerlIO_setlinebuf(PerlIO *f)
{
 if (f)
  {
   f->flags &= ~PERLIO_F_LINEBUF;
  }
}

#undef PerlIO_write
SSize_t
PerlIO_write(PerlIO *f, const void *vbuf, Size_t count)
{
 const STDCHAR *buf = (const STDCHAR *) vbuf;
 Size_t written = 0;
 PerlIO_debug(__FUNCTION__ " f=%p c=%d\n",f,count);
 if (f)
  {
   if (!f->buf)
    PerlIO_alloc_buf(f);
   while (count > 0)
    {
     SSize_t avail = f->bufsiz - (f->ptr - f->buf);
     if ((SSize_t) count < avail)
      avail = count;
     f->flags |= PERLIO_F_WRBUF;
     if (f->flags & PERLIO_F_LINEBUF)
      {
       while (avail > 0)
        {
         int ch = *buf++;
         *(f->ptr)++ = ch;
         count--;
         avail--;
         written++;
         if (ch == '\n')
          {
           PerlIO_flush(f);
           break;
          }
        }
      }
     else
      {
       if (avail)
        {
         Copy(buf,f->ptr,avail,char);
         count   -= avail;
         buf     += avail;
         written += avail;
         f->ptr  += avail;
        }
      }
     if (f->ptr >= (f->buf + f->bufsiz))
      PerlIO_flush(f);
    }
  }
 return written;
}

#undef PerlIO_putc
int
PerlIO_putc(PerlIO *f, int ch)
{
 STDCHAR buf = ch;
 PerlIO_write(f,&ch,1);
}

#undef PerlIO_tell
Off_t
PerlIO_tell(PerlIO *f)
{
 Off_t posn = f->posn;
 if (f->buf)
  posn += (f->ptr - f->buf);
 PerlIO_debug(__FUNCTION__ " f=%p b=%ld a=%ld\n",f,(long)f->posn,(long)posn);
 return posn;
}

#undef PerlIO_seek
int
PerlIO_seek(PerlIO *f, Off_t offset, int whence)
{
 int code;
 PerlIO_debug(__FUNCTION__ " f=%p i=%ld+%d\n",f,(long)f->posn,(f->ptr-f->buf));
 code = PerlIO_flush(f);
 if (code == 0)
  {
   f->flags &= ~PERLIO_F_EOF;
   f->posn = PerlLIO_lseek(f->fd,offset,whence);
   PerlIO_debug(__FUNCTION__ " f=%p o=%ld w=%d p=%ld\n",
                f,(long)offset,whence,(long)f->posn);
   if (f->posn == (Off_t) -1)
    {
     f->posn = 0;
     code = -1;
    }
  }
 return code;
}

#undef PerlIO_rewind
void
PerlIO_rewind(PerlIO *f)
{
 PerlIO_seek(f,(Off_t)0,SEEK_SET);
}

#undef PerlIO_vprintf
int
PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap)
{
 dTHX;
 SV *sv = newSVpvn("",0);
 char *s;
 STRLEN len;
 sv_vcatpvf(sv, fmt, &ap);
 s = SvPV(sv,len);
 return PerlIO_write(f,s,len);
}

#undef PerlIO_printf
int
PerlIO_printf(PerlIO *f,const char *fmt,...)
{
 va_list ap;
 int result;
 va_start(ap,fmt);
 result = PerlIO_vprintf(f,fmt,ap);
 va_end(ap);
 return result;
}

#undef PerlIO_stdoutf
int
PerlIO_stdoutf(const char *fmt,...)
{
 va_list ap;
 int result;
 va_start(ap,fmt);
 result = PerlIO_vprintf(PerlIO_stdout(),fmt,ap);
 va_end(ap);
 return result;
}

#undef PerlIO_tmpfile
PerlIO *
PerlIO_tmpfile(void)
{
 dTHX;
 /* I have no idea how portable mkstemp() is ... */
 SV *sv = newSVpv("/tmp/PerlIO_XXXXXX",0);
 int fd = mkstemp(SvPVX(sv));
 PerlIO *f = NULL;
 if (fd >= 0)
  {
   f = PerlIO_fdopen(fd,"w+");
   if (f)
    {
     f->flags |= PERLIO_F_TEMP;
    }
   unlink(SvPVX(sv));
   SvREFCNT_dec(sv);
  }
 return f;
}

#undef PerlIO_importFILE
PerlIO *
PerlIO_importFILE(FILE *f, int fl)
{
 int fd = fileno(f);
 /* Should really push stdio discipline when we have them */
 return PerlIO_fdopen(fd,"r+");
}

#undef PerlIO_exportFILE
FILE *
PerlIO_exportFILE(PerlIO *f, int fl)
{
 PerlIO_flush(f);
 /* Should really push stdio discipline when we have them */
 return fdopen(PerlIO_fileno(f),"r+");
}

#undef PerlIO_findFILE
FILE *
PerlIO_findFILE(PerlIO *f)
{
 return PerlIO_exportFILE(f,0);
}

#undef PerlIO_releaseFILE
void
PerlIO_releaseFILE(PerlIO *p, FILE *f)
{
}

#undef HAS_FSETPOS
#undef HAS_FGETPOS

/*======================================================================================*/

#endif /* USE_SFIO */
#endif /* PERLIO_IS_STDIO */

#ifndef HAS_FSETPOS
#undef PerlIO_setpos
int
PerlIO_setpos(PerlIO *f, const Fpos_t *pos)
{
 return PerlIO_seek(f,*pos,0);
}
#else
#ifndef PERLIO_IS_STDIO
#undef PerlIO_setpos
int
PerlIO_setpos(PerlIO *f, const Fpos_t *pos)
{
#if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
 return fsetpos64(f, pos);
#else
 return fsetpos(f, pos);
#endif
}
#endif
#endif

#ifndef HAS_FGETPOS
#undef PerlIO_getpos
int
PerlIO_getpos(PerlIO *f, Fpos_t *pos)
{
 *pos = PerlIO_tell(f);
 return 0;
}
#else
#ifndef PERLIO_IS_STDIO
#undef PerlIO_getpos
int
PerlIO_getpos(PerlIO *f, Fpos_t *pos)
{
#if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
 return fgetpos64(f, pos);
#else
 return fgetpos(f, pos);
#endif
}
#endif
#endif

#if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF)

int
vprintf(char *pat, char *args)
{
    _doprnt(pat, args, stdout);
    return 0;		/* wrong, but perl doesn't use the return value */
}

int
vfprintf(FILE *fd, char *pat, char *args)
{
    _doprnt(pat, args, fd);
    return 0;		/* wrong, but perl doesn't use the return value */
}

#endif

#ifndef PerlIO_vsprintf
int
PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap)
{
 int val = vsprintf(s, fmt, ap);
 if (n >= 0)
  {
   if (strlen(s) >= (STRLEN)n)
    {
     dTHX;
     PerlIO_puts(Perl_error_log,"panic: sprintf overflow - memory corrupted!\n");
     my_exit(1);
    }
  }
 return val;
}
#endif

#ifndef PerlIO_sprintf
int
PerlIO_sprintf(char *s, int n, const char *fmt,...)
{
 va_list ap;
 int result;
 va_start(ap,fmt);
 result = PerlIO_vsprintf(s, n, fmt, ap);
 va_end(ap);
 return result;
}
#endif

#endif /* !PERL_IMPLICIT_SYS */