1=head1 NAME 2 3perlapio - perl's IO abstraction interface. 4 5=head1 SYNOPSIS 6 7 #define PERLIO_NOT_STDIO 0 /* For co-existence with stdio only */ 8 #include <perlio.h> /* Usually via #include <perl.h> */ 9 10 PerlIO *PerlIO_stdin(void); 11 PerlIO *PerlIO_stdout(void); 12 PerlIO *PerlIO_stderr(void); 13 14 PerlIO *PerlIO_open(const char *path,const char *mode); 15 PerlIO *PerlIO_fdopen(int fd, const char *mode); 16 PerlIO *PerlIO_reopen(const char *path, const char *mode, PerlIO *old); /* deprecated */ 17 int PerlIO_close(PerlIO *f); 18 19 int PerlIO_stdoutf(const char *fmt,...) 20 int PerlIO_puts(PerlIO *f,const char *string); 21 int PerlIO_putc(PerlIO *f,int ch); 22 SSize_t PerlIO_write(PerlIO *f,const void *buf,size_t numbytes); 23 int PerlIO_printf(PerlIO *f, const char *fmt,...); 24 int PerlIO_vprintf(PerlIO *f, const char *fmt, va_list args); 25 int PerlIO_flush(PerlIO *f); 26 27 int PerlIO_eof(PerlIO *f); 28 int PerlIO_error(PerlIO *f); 29 void PerlIO_clearerr(PerlIO *f); 30 31 int PerlIO_getc(PerlIO *d); 32 int PerlIO_ungetc(PerlIO *f,int ch); 33 SSize_t PerlIO_read(PerlIO *f, void *buf, size_t numbytes); 34 35 int PerlIO_fileno(PerlIO *f); 36 37 void PerlIO_setlinebuf(PerlIO *f); 38 39 Off_t PerlIO_tell(PerlIO *f); 40 int PerlIO_seek(PerlIO *f, Off_t offset, int whence); 41 void PerlIO_rewind(PerlIO *f); 42 43 int PerlIO_getpos(PerlIO *f, SV *save); /* prototype changed */ 44 int PerlIO_setpos(PerlIO *f, SV *saved); /* prototype changed */ 45 46 int PerlIO_fast_gets(PerlIO *f); 47 int PerlIO_has_cntptr(PerlIO *f); 48 SSize_t PerlIO_get_cnt(PerlIO *f); 49 char *PerlIO_get_ptr(PerlIO *f); 50 void PerlIO_set_ptrcnt(PerlIO *f, char *ptr, SSize_t count); 51 52 int PerlIO_canset_cnt(PerlIO *f); /* deprecated */ 53 void PerlIO_set_cnt(PerlIO *f, int count); /* deprecated */ 54 55 int PerlIO_has_base(PerlIO *f); 56 char *PerlIO_get_base(PerlIO *f); 57 SSize_t PerlIO_get_bufsiz(PerlIO *f); 58 59 PerlIO *PerlIO_importFILE(FILE *stdio, const char *mode); 60 FILE *PerlIO_exportFILE(PerlIO *f, int flags); 61 FILE *PerlIO_findFILE(PerlIO *f); 62 void PerlIO_releaseFILE(PerlIO *f,FILE *stdio); 63 64 int PerlIO_apply_layers(PerlIO *f, const char *mode, const char *layers); 65 int PerlIO_binmode(PerlIO *f, int ptype, int imode, const char *layers); 66 void PerlIO_debug(const char *fmt,...) 67 68=head1 DESCRIPTION 69 70Perl's source code, and extensions that want maximum portability, 71should use the above functions instead of those defined in ANSI C's 72I<stdio.h>. The perl headers (in particular "perlio.h") will 73C<#define> them to the I/O mechanism selected at Configure time. 74 75The functions are modeled on those in I<stdio.h>, but parameter order 76has been "tidied up a little". 77 78C<PerlIO *> takes the place of FILE *. Like FILE * it should be 79treated as opaque (it is probably safe to assume it is a pointer to 80something). 81 82There are currently three implementations: 83 84=over 4 85 86=item 1. USE_STDIO 87 88All above are #define'd to stdio functions or are trivial wrapper 89functions which call stdio. In this case I<only> PerlIO * is a FILE *. 90This has been the default implementation since the abstraction was 91introduced in perl5.003_02. 92 93=item 2. USE_PERLIO 94 95Introduced just after perl5.7.0, this is a re-implementation of the 96above abstraction which allows perl more control over how IO is done 97as it decouples IO from the way the operating system and C library 98choose to do things. For USE_PERLIO PerlIO * has an extra layer of 99indirection - it is a pointer-to-a-pointer. This allows the PerlIO * 100to remain with a known value while swapping the implementation around 101underneath I<at run time>. In this case all the above are true (but 102very simple) functions which call the underlying implementation. 103 104This is the only implementation for which C<PerlIO_apply_layers()> 105does anything "interesting". 106 107The USE_PERLIO implementation is described in L<perliol>. 108 109=back 110 111Because "perlio.h" is a thin layer (for efficiency) the semantics of 112these functions are somewhat dependent on the underlying implementation. 113Where these variations are understood they are noted below. 114 115Unless otherwise noted, functions return 0 on success, or a negative 116value (usually C<EOF> which is usually -1) and set C<errno> on error. 117 118=over 4 119 120=item B<PerlIO_stdin()>, B<PerlIO_stdout()>, B<PerlIO_stderr()> 121 122Use these rather than C<stdin>, C<stdout>, C<stderr>. They are written 123to look like "function calls" rather than variables because this makes 124it easier to I<make them> function calls if platform cannot export data 125to loaded modules, or if (say) different "threads" might have different 126values. 127 128=item B<PerlIO_open(path, mode)>, B<PerlIO_fdopen(fd,mode)> 129 130These correspond to fopen()/fdopen() and the arguments are the same. 131Return C<NULL> and set C<errno> if there is an error. There may be an 132implementation limit on the number of open handles, which may be lower 133than the limit on the number of open files - C<errno> may not be set 134when C<NULL> is returned if this limit is exceeded. 135 136=item B<PerlIO_reopen(path,mode,f)> 137 138While this currently exists in all three implementations perl itself 139does not use it. I<As perl does not use it, it is not well tested.> 140 141Perl prefers to C<dup> the new low-level descriptor to the descriptor 142used by the existing PerlIO. This may become the behaviour of this 143function in the future. 144 145=item B<PerlIO_printf(f,fmt,...)>, B<PerlIO_vprintf(f,fmt,a)> 146 147These are fprintf()/vfprintf() equivalents. 148 149=item B<PerlIO_stdoutf(fmt,...)> 150 151This is printf() equivalent. printf is #defined to this function, 152so it is (currently) legal to use C<printf(fmt,...)> in perl sources. 153 154=item B<PerlIO_read(f,buf,count)>, B<PerlIO_write(f,buf,count)> 155 156These correspond functionally to fread() and fwrite() but the 157arguments and return values are different. The PerlIO_read() and 158PerlIO_write() signatures have been modeled on the more sane low level 159read() and write() functions instead: The "file" argument is passed 160first, there is only one "count", and the return value can distinguish 161between error and C<EOF>. 162 163Returns a byte count if successful (which may be zero or 164positive), returns negative value and sets C<errno> on error. 165Depending on implementation C<errno> may be C<EINTR> if operation was 166interrupted by a signal. 167 168=item B<PerlIO_close(f)> 169 170Depending on implementation C<errno> may be C<EINTR> if operation was 171interrupted by a signal. 172 173=item B<PerlIO_puts(f,s)>, B<PerlIO_putc(f,c)> 174 175These correspond to fputs() and fputc(). 176Note that arguments have been revised to have "file" first. 177 178=item B<PerlIO_ungetc(f,c)> 179 180This corresponds to ungetc(). Note that arguments have been revised 181to have "file" first. Arranges that next read operation will return 182the byte B<c>. Despite the implied "character" in the name only 183values in the range 0..0xFF are defined. Returns the byte B<c> on 184success or -1 (C<EOF>) on error. The number of bytes that can be 185"pushed back" may vary, only 1 character is certain, and then only if 186it is the last character that was read from the handle. 187 188=item B<PerlIO_getc(f)> 189 190This corresponds to getc(). 191Despite the c in the name only byte range 0..0xFF is supported. 192Returns the character read or -1 (C<EOF>) on error. 193 194=item B<PerlIO_eof(f)> 195 196This corresponds to feof(). Returns a true/false indication of 197whether the handle is at end of file. For terminal devices this may 198or may not be "sticky" depending on the implementation. The flag is 199cleared by PerlIO_seek(), or PerlIO_rewind(). 200 201=item B<PerlIO_error(f)> 202 203This corresponds to ferror(). Returns a true/false indication of 204whether there has been an IO error on the handle. 205 206=item B<PerlIO_fileno(f)> 207 208This corresponds to fileno(), note that on some platforms, the meaning 209of "fileno" may not match Unix. Returns -1 if the handle has no open 210descriptor associated with it. 211 212=item B<PerlIO_clearerr(f)> 213 214This corresponds to clearerr(), i.e., clears 'error' and (usually) 215'eof' flags for the "stream". Does not return a value. 216 217=item B<PerlIO_flush(f)> 218 219This corresponds to fflush(). Sends any buffered write data to the 220underlying file. If called with C<NULL> this may flush all open 221streams (or core dump with some USE_STDIO implementations). Calling 222on a handle open for read only, or on which last operation was a read 223of some kind may lead to undefined behaviour on some USE_STDIO 224implementations. The USE_PERLIO (layers) implementation tries to 225behave better: it flushes all open streams when passed C<NULL>, and 226attempts to retain data on read streams either in the buffer or by 227seeking the handle to the current logical position. 228 229=item B<PerlIO_seek(f,offset,whence)> 230 231This corresponds to fseek(). Sends buffered write data to the 232underlying file, or discards any buffered read data, then positions 233the file descriptor as specified by B<offset> and B<whence> (sic). 234This is the correct thing to do when switching between read and write 235on the same handle (see issues with PerlIO_flush() above). Offset is 236of type C<Off_t> which is a perl Configure value which may not be same 237as stdio's C<off_t>. 238 239=item B<PerlIO_tell(f)> 240 241This corresponds to ftell(). Returns the current file position, or 242(Off_t) -1 on error. May just return value system "knows" without 243making a system call or checking the underlying file descriptor (so 244use on shared file descriptors is not safe without a 245PerlIO_seek()). Return value is of type C<Off_t> which is a perl 246Configure value which may not be same as stdio's C<off_t>. 247 248=item B<PerlIO_getpos(f,p)>, B<PerlIO_setpos(f,p)> 249 250These correspond (loosely) to fgetpos() and fsetpos(). Rather than 251stdio's Fpos_t they expect a "Perl Scalar Value" to be passed. What is 252stored there should be considered opaque. The layout of the data may 253vary from handle to handle. When not using stdio or if platform does 254not have the stdio calls then they are implemented in terms of 255PerlIO_tell() and PerlIO_seek(). 256 257=item B<PerlIO_rewind(f)> 258 259This corresponds to rewind(). It is usually defined as being 260 261 PerlIO_seek(f,(Off_t)0L, SEEK_SET); 262 PerlIO_clearerr(f); 263 264=item B<PerlIO_tmpfile()> 265 266This corresponds to tmpfile(), i.e., returns an anonymous PerlIO or 267NULL on error. The system will attempt to automatically delete the 268file when closed. On Unix the file is usually C<unlink>-ed just after 269it is created so it does not matter how it gets closed. On other 270systems the file may only be deleted if closed via PerlIO_close() 271and/or the program exits via C<exit>. Depending on the implementation 272there may be "race conditions" which allow other processes access to 273the file, though in general it will be safer in this regard than 274ad. hoc. schemes. 275 276=item B<PerlIO_setlinebuf(f)> 277 278This corresponds to setlinebuf(). Does not return a value. What 279constitutes a "line" is implementation dependent but usually means 280that writing "\n" flushes the buffer. What happens with things like 281"this\nthat" is uncertain. (Perl core uses it I<only> when "dumping"; 282it has nothing to do with $| auto-flush.) 283 284=back 285 286=head2 Co-existence with stdio 287 288There is outline support for co-existence of PerlIO with stdio. 289Obviously if PerlIO is implemented in terms of stdio there is no 290problem. However in other cases then mechanisms must exist to create a 291FILE * which can be passed to library code which is going to use stdio 292calls. 293 294The first step is to add this line: 295 296 #define PERLIO_NOT_STDIO 0 297 298I<before> including any perl header files. (This will probably become 299the default at some point). That prevents "perlio.h" from attempting 300to #define stdio functions onto PerlIO functions. 301 302XS code is probably better using "typemap" if it expects FILE * 303arguments. The standard typemap will be adjusted to comprehend any 304changes in this area. 305 306=over 4 307 308=item B<PerlIO_importFILE(f,mode)> 309 310Used to get a PerlIO * from a FILE *. 311 312The mode argument should be a string as would be passed to 313fopen/PerlIO_open. If it is NULL then - for legacy support - the code 314will (depending upon the platform and the implementation) either 315attempt to empirically determine the mode in which I<f> is open, or 316use "r+" to indicate a read/write stream. 317 318Once called the FILE * should I<ONLY> be closed by calling 319C<PerlIO_close()> on the returned PerlIO *. 320 321The PerlIO is set to textmode. Use PerlIO_binmode if this is 322not the desired mode. 323 324This is B<not> the reverse of PerlIO_exportFILE(). 325 326=item B<PerlIO_exportFILE(f,mode)> 327 328Given a PerlIO * create a 'native' FILE * suitable for passing to code 329expecting to be compiled and linked with ANSI C I<stdio.h>. The mode 330argument should be a string as would be passed to fopen/PerlIO_open. 331If it is NULL then - for legacy support - the FILE * is opened in same 332mode as the PerlIO *. 333 334The fact that such a FILE * has been 'exported' is recorded, (normally 335by pushing a new :stdio "layer" onto the PerlIO *), which may affect 336future PerlIO operations on the original PerlIO *. You should not 337call C<fclose()> on the file unless you call C<PerlIO_releaseFILE()> 338to disassociate it from the PerlIO *. (Do not use PerlIO_importFILE() 339for doing the disassociation.) 340 341Calling this function repeatedly will create a FILE * on each call 342(and will push an :stdio layer each time as well). 343 344=item B<PerlIO_releaseFILE(p,f)> 345 346Calling PerlIO_releaseFILE informs PerlIO that all use of FILE * is 347complete. It is removed from the list of 'exported' FILE *s, and the 348associated PerlIO * should revert to its original behaviour. 349 350Use this to disassociate a file from a PerlIO * that was associated 351using PerlIO_exportFILE(). 352 353=item B<PerlIO_findFILE(f)> 354 355Returns a native FILE * used by a stdio layer. If there is none, it 356will create one with PerlIO_exportFILE. In either case the FILE * 357should be considered as belonging to PerlIO subsystem and should 358only be closed by calling C<PerlIO_close()>. 359 360 361=back 362 363=head2 "Fast gets" Functions 364 365In addition to standard-like API defined so far above there is an 366"implementation" interface which allows perl to get at internals of 367PerlIO. The following calls correspond to the various FILE_xxx macros 368determined by Configure - or their equivalent in other 369implementations. This section is really of interest to only those 370concerned with detailed perl-core behaviour, implementing a PerlIO 371mapping or writing code which can make use of the "read ahead" that 372has been done by the IO system in the same way perl does. Note that 373any code that uses these interfaces must be prepared to do things the 374traditional way if a handle does not support them. 375 376=over 4 377 378=item B<PerlIO_fast_gets(f)> 379 380Returns true if implementation has all the interfaces required to 381allow perl's C<sv_gets> to "bypass" normal IO mechanism. This can 382vary from handle to handle. 383 384 PerlIO_fast_gets(f) = PerlIO_has_cntptr(f) && \ 385 PerlIO_canset_cnt(f) && \ 386 'Can set pointer into buffer' 387 388=item B<PerlIO_has_cntptr(f)> 389 390Implementation can return pointer to current position in the "buffer" 391and a count of bytes available in the buffer. Do not use this - use 392PerlIO_fast_gets. 393 394=item B<PerlIO_get_cnt(f)> 395 396Return count of readable bytes in the buffer. Zero or negative return 397means no more bytes available. 398 399=item B<PerlIO_get_ptr(f)> 400 401Return pointer to next readable byte in buffer, accessing via the 402pointer (dereferencing) is only safe if PerlIO_get_cnt() has returned 403a positive value. Only positive offsets up to value returned by 404PerlIO_get_cnt() are allowed. 405 406=item B<PerlIO_set_ptrcnt(f,p,c)> 407 408Set pointer into buffer, and a count of bytes still in the 409buffer. Should be used only to set pointer to within range implied by 410previous calls to C<PerlIO_get_ptr> and C<PerlIO_get_cnt>. The two 411values I<must> be consistent with each other (implementation may only 412use one or the other or may require both). 413 414=item B<PerlIO_canset_cnt(f)> 415 416Implementation can adjust its idea of number of bytes in the buffer. 417Do not use this - use PerlIO_fast_gets. 418 419=item B<PerlIO_set_cnt(f,c)> 420 421Obscure - set count of bytes in the buffer. Deprecated. Only usable 422if PerlIO_canset_cnt() returns true. Currently used in only doio.c to 423force count less than -1 to -1. Perhaps should be PerlIO_set_empty or 424similar. This call may actually do nothing if "count" is deduced from 425pointer and a "limit". Do not use this - use PerlIO_set_ptrcnt(). 426 427=item B<PerlIO_has_base(f)> 428 429Returns true if implementation has a buffer, and can return pointer 430to whole buffer and its size. Used by perl for B<-T> / B<-B> tests. 431Other uses would be very obscure... 432 433=item B<PerlIO_get_base(f)> 434 435Return I<start> of buffer. Access only positive offsets in the buffer 436up to the value returned by PerlIO_get_bufsiz(). 437 438=item B<PerlIO_get_bufsiz(f)> 439 440Return the I<total number of bytes> in the buffer, this is neither the 441number that can be read, nor the amount of memory allocated to the 442buffer. Rather it is what the operating system and/or implementation 443happened to C<read()> (or whatever) last time IO was requested. 444 445=back 446 447=head2 Other Functions 448 449=over 4 450 451=item PerlIO_apply_layers(f,mode,layers) 452 453The new interface to the USE_PERLIO implementation. The layers ":crlf" 454and ":raw" are only ones allowed for other implementations and those 455are silently ignored. (As of perl5.8 ":raw" is deprecated.) Use 456PerlIO_binmode() below for the portable case. 457 458=item PerlIO_binmode(f,ptype,imode,layers) 459 460The hook used by perl's C<binmode> operator. 461B<ptype> is perl's character for the kind of IO: 462 463=over 8 464 465=item 'E<lt>' read 466 467=item 'E<gt>' write 468 469=item '+' read/write 470 471=back 472 473B<imode> is C<O_BINARY> or C<O_TEXT>. 474 475B<layers> is a string of layers to apply, only ":crlf" makes sense in 476the non USE_PERLIO case. (As of perl5.8 ":raw" is deprecated in favour 477of passing NULL.) 478 479Portable cases are: 480 481 PerlIO_binmode(f,ptype,O_BINARY,NULL); 482and 483 PerlIO_binmode(f,ptype,O_TEXT,":crlf"); 484 485On Unix these calls probably have no effect whatsoever. Elsewhere 486they alter "\n" to CR,LF translation and possibly cause a special text 487"end of file" indicator to be written or honoured on read. The effect 488of making the call after doing any IO to the handle depends on the 489implementation. (It may be ignored, affect any data which is already 490buffered as well, or only apply to subsequent data.) 491 492=item PerlIO_debug(fmt,...) 493 494PerlIO_debug is a printf()-like function which can be used for 495debugging. No return value. Its main use is inside PerlIO where using 496real printf, warn() etc. would recursively call PerlIO and be a 497problem. 498 499PerlIO_debug writes to the file named by $ENV{'PERLIO_DEBUG'} typical 500use might be 501 502 Bourne shells (sh, ksh, bash, zsh, ash, ...): 503 PERLIO_DEBUG=/dev/tty ./perl somescript some args 504 505 Csh/Tcsh: 506 setenv PERLIO_DEBUG /dev/tty 507 ./perl somescript some args 508 509 If you have the "env" utility: 510 env PERLIO_DEBUG=/dev/tty ./perl somescript some args 511 512 Win32: 513 set PERLIO_DEBUG=CON 514 perl somescript some args 515 516If $ENV{'PERLIO_DEBUG'} is not set PerlIO_debug() is a no-op. 517 518=back 519