1 // ----------------------------------------------------------------------------
3 // ----------------------------------------------------------------------------
4 // Public Domain C Library - http://pdclib.sourceforge.net
5 // This code is Public Domain. Use, modify, and redistribute at will.
6 // ----------------------------------------------------------------------------
8 // ----------------------------------------------------------------------------
11 #define __STDIO_H __STDIO_H
13 // ----------------------------------------------------------------------------
17 #include "__pdc_stdio.h"
19 #define _IOFBF 0 // @see setvbuf()
20 #define _IOLBF 1 // @see setvbuf()
21 #define _IONBF 2 // @see setvbuf()
23 #define SEEK_SET 0 // @see fseek()
24 #define SEEK_CUR 1 // @see fseek()
25 #define SEEK_END 2 // @see fseek()
29 // ----------------------------------------------------------------------------
34 typedef FILE; // file position, buffer pointer, ErrorIndicator, EOFIndicator,
36 typedef fpos_t; // file position
42 // ----------------------------------------------------------------------------
45 /* TABLE OF CONTENTS (in order of appearance)
47 * General File Handling
107 * Binary Read / Write
117 /** File OPEN. Opens a file.
118 @param filename Name of the file.
119 @param mode One of r, w, a, rb, wb, ab, r+, w+, a+, rb+, wb+, ab+,
120 specifying which mode to open the file in.
121 @return A file handle associated with the opened file, NULL if failed.
123 FILE * fopen( const char * restrict filename, const char * restrict mode );
125 /** File REOPEN. Opens the file specified by the given name, associating it
126 with the given file handle. If filename is NULL, it is attempted to change
127 the mode of the already opened file associated with the given file handle.
128 (This function can e.g. be used to reassociate stdin / stdout / stderr with
130 @param filename Name of the file to be opened.
131 @param mode One of r, w, a, rb, wb, ab, r+, w+, a+, rb+, wb+, ab+,
132 specifying which mode to open the file in.
133 @param fh The file handle to associate with the opened file.
134 @return fh if successful, NULL if failed.
136 FILE * freopen( const char * restrict filename, const char * restrict mode, FILE * fh );
138 /** File FLUSH. Flushes any output buffers of a file. If parameter is NULL,
139 flushes output buffers for all file handles. The function is undefined for
140 input streams or update streams when the last operation was input.
141 @param fh The file handle.
142 @return 0 if successful, EOF on write error (setting error indicator).
144 int fflush( FILE * fh );
146 /** File EOF. Tests whether EOF is set for a given file.
147 @param fh The file handle.
148 @return 0 if EOF is not set, non-zero if EOF is set.
150 int feof( FILE * fh );
152 /** File ERROR. Tests whether error indicator is set for a given file.
153 @param fh The file handle.
154 @return 0 if error indicator is not set, non-zero if set.
156 int ferror( FILE * fh );
158 /** CLEAR ERRor. Clears EOF and error indicator of a FILE handle.
159 @param fh The file handle.
161 void clearerr( FILE * fh );
163 /** File CLOSE. Flushes any output buffers, closes the file, frees internal
164 buffers, and discards the file handle.
165 @param fh The file handle.
166 @return 0 if successful, non-zero if failed. (In any case, the file handle
167 is invalid afterwards.)
169 int fclose( FILE * fh );
171 // ----------------------------------------------------------------------------
173 /** RENAME file. Causes a file to be no longer accessible under a given name,
174 but a new name instead.
175 @param filename Name of the file.
176 @param newname New file name.
177 @return 0 if successful, non-zero if failed. (This implementation: INT_MAX
178 if newname already exists; INT_MIN if filename could not be found;
179 EOF if filename is a currently open file.)
181 int rename( const char * filename, const char * newname );
183 /** REMOVE file. Causes a file to be no longer accessible under a given name.
184 @param filename Name of the file.
185 @return 0 if successful, non-zero if failed. (This implementation: INT_MAX
186 if the file is currently open.)
188 int remove( const char * filename );
190 // ----------------------------------------------------------------------------
192 /** TeMPorary FILE. Opens a previously non-existend file in "wb+" mode that
193 will be automatically deleted when closed, or when the program terminates.
194 (This implementation: If program terminates abnormally, file is not
196 @return A file handle for the temporary file. (NULL if opening failed.)
198 FILE * tmpfile( void )
200 /** TeMPorary NAMe. Generates a file name that does not yet exist in the file
201 system, and is different from the last call to the function. Note that a
202 file generated with this name is not "temporary", and must be remove()d
204 @param filename NULL, or a char[ L_tmpnam ] array. (Beware, calling this
205 function with a NULL parameter is not thread-safe.)
206 @return If 'filename' is NULL, a pointer to an internal static buffer
207 holding the generated name. If 'filename' is not null, the
208 generated name is stored in 'filename', and 'filename' is returned.
209 If the filename generation fails, function returns NULL.
211 char * tmpnam( char * filename );
213 // ----------------------------------------------------------------------------
215 /** File SEEK. Sets the current position in a file to the values specified by
217 @param fh The file handle.
218 @param offset The offset from 'start' to position to.
219 @param start The starting point from which to calculate the offset. May be
220 one of SEEK_SET, SEEK_CUR, SEEK_END.
221 @return 0 if successful, non-zero if error encountered.
223 int fseek( FILE * fh, long offset, int start );
225 /** REWIND file. Equivalent to (void) fseek( fh, 0, SEEK_SET ).
226 @param fh The file handle.
228 void rewind( FILE * fh );
230 /** File TELL position. Tells the current offset into a given file.
231 @param fh The file handle.
232 @return The offset into the file.
234 long ftell( FILE * fh );
236 /** File GET POSition. Stores the current state and position in a file.
237 @param fh The file handle.
238 @param pos The object to store the current state in.
239 @return 0 if successful, non-zero if error encountered.
241 int fgetpos( FILE * restrict fh, fpos_t * restrict pos );
243 /** File SET POSition. Sets the current file position to the value stored in a
245 @param fh The file handle.
246 @param pos The fpos_t object.
247 @return 0 if successful, non-zero if error encountered.
249 int fsetpos( FILE * fh, const fpos_t * pos );
251 // ----------------------------------------------------------------------------
253 /** File GET Character. Reads a character from file.
254 @param fh The file handle.
255 @return The next character in the file, as unsigned char converted to int,
256 or EOF if end of file is reached.
258 int fgetc( FILE * fh );
260 /** GET Character. Equivalent to fgetc(), but may be implemented as macro, and
261 is allowed to evaluate its parameter more than once.
262 @param fh The file handle.
263 @return The character read, or EOF if end of file / error encountered.
265 int getc( FILE * fh );
267 /** GET CHARacter. Equivalent to getc( stdin ).
268 @return The character read, or EOF if end of file / error encountered.
272 /** UN-GET Character. Puts a character back into an input stream.
273 @param c The character to put back.
274 @param fh The file handle.
275 @return The character put back, EOF if error encountered.
277 int ungetc( int c, FILE * fh );
279 /** File GET String. Reads a line (terminated by newline character) from file,
280 but reading no more than n characters.
281 @param dest The char array to write into.
282 @param n The maximum number of characters to read.
283 @param fh The file handle.
284 @return 'dest', or NULL if an error occurred.
286 char * fgets( char * restrict dest, int n, FILE * restrict fh );
288 /** GET String. Equivalent to fgets( dest, stdin ).
289 @param dest The character array to write to.
290 @return 'dest', or NULL if an error occurred.
292 char * gets( char * dest );
294 // ----------------------------------------------------------------------------
296 /** File PUT Character. Writes a character to file.
297 @param c The character (when converted to unsigned char) to write.
298 @param fh The file handle.
299 @return 'c', or EOF if an error occurred.
301 int fputc( int c, FILE * fh );
303 /** PUT Character. Equivalent to fputc( c, stdout ), but may be implemented as
304 a macro, and may evaluate the file handle more than once.
305 @param c The character to write.
306 @param fh The file handle.
307 @return The character written, or EOF if error encountered.
309 int putc( int c, FILE * fh );
311 /** PUT CHARacter. Equivalent to putc( c, stdout ).
312 @param c The character to write.
313 @return The character written, or EOF if error encountered.
315 int putchar( int c );
317 /** File PUT String. Writes a C string to file.
318 @param src The string to write.
319 @param fh The file handle.
320 @return >= 0 if successful, or EOF if an error occurred.
322 int fputs( const char * restrict src, FILE * restrict fh );
324 /** PUT String. Write a C string to stdout.
325 @param src The C string to write.
326 @return >= 0 if successful, EOF if error encountered.
328 int puts( const char * src );
330 // ----------------------------------------------------------------------------
332 /** File SCAN Formatted. Reads from given file handle, under control of a
333 formatting string, the values of variables pointed to by 0..n pointers.
334 @param fh The file handle.
335 @param format The formatting string.
336 @param ... A list of 0..n pointers corresponding to placeholders in
338 @return EOF if failed, number of values successfully assigned otherwise.
340 int fscanf( FILE * restrict fh, const char * restrict format, ... );
342 /** SCAN Formatted. Equivalent to fscanf( stdin, format, ... )
343 @param format The formatting string.
344 @param ... A list of 0..n pointers corresponding to placeholders in
346 @return EOF if failed, number of values successfully assigned otherwise.
348 int scanf( const char * restrict format, ... );
350 /** String SCAN Formatted. Equivalent to scanf( format, ... ), but using a C
351 string instead of a file handle for input.
352 @param src The input string.
353 @param format The formatting string.
354 @param ... A list of 0..n pointers corresponding to placeholders in
356 @return EOF if failed, number of values successfully assigned otherwise.
358 int sscanf( const char * restrict src, const char * restrict format, ... );
360 /** Variable File SCAN Formatted. Equivalent to fscanf( fh, format, ... ),
361 with the variable-length parameter list replaced by a va_list, created by
363 @param fh The file handle.
364 @param format The formatting string.
365 @param args The argument list created by the va_start macro.
366 @return Number of characters printed.
368 int vfscanf( FILE * restrict fh, const char * restrict format, va_list args );
370 /** Variable SCAN Formatted. Equivalent to vfscanf( stdin, format, args ).
371 @param format The formatting string.
372 @param args The argument list created by the va_start macro.
373 @return Number of characters printed.
375 int vscanf( const char * restrict format, va_list args );
377 /** Variable String SCAN Formatted. Equivalent to vscanf( format, args ), but
378 reading from a C string instead of stdin.
379 @param src The C string to read from.
380 @param format The formatting string.
381 @param args The argument list created by the va_start macro.
382 @return Number of characters printed.
384 int vsscanf( const char * restrict src, const char * restrict format, va_list ap );
386 // ----------------------------------------------------------------------------
388 /** File PRINT Formatted. Prints to given file handle, under control of a
389 formatting string, the values of 0..n variables.
390 @param fh The file handle.
391 @param format The formatting string.
392 @param ... A list of 0..n variables corresponding to placeholders in
394 @return Number of characters printed, negative value if error occurred.
396 int fprintf( FILE * restrict fh, const char * restrict format, ... );
398 /** PRINT Formatted. Equivalent to fprintf( stdout, format, ... ).
399 @param format The formatting string.
400 @param ... A list of 0..n variables corresponding to placeholders in
402 @return Number of characters printed.
404 int printf( const char * restrict format, ... );
406 /** String PRINT Formatted. Equivalent to printf( format, ... ), but writing
407 to a char array instead of stdout.
408 @param dest The char array to write to.
409 @param format The formatting string.
410 @param ... A list of 0..n variables corresponding to placeholders in
412 @return Number of characters printed.
414 int sprintf( char * restrict dest, const char * restrict format, ... );
416 /** String N PRINT Formatted. Equivalent to sprintf( dest, format, ... ), but
417 will not write more than n characters.
418 @param dest The char array to write to.
419 @param n The maximum number of characters to write.
420 @param format The formatting string.
421 @param ... A list of 0..n variables corresponding to placeholders in
423 @return Number of characters printed.
425 int snprintf( char * restrict s, size_t n, const char * restrict format, ... );
427 /** Variable File PRINT Formatted. Equivalent to fprintf( fh, format, ... ),
428 with the variable-length parameter list replaced by a va_list, created by
430 @param fh The file handle.
431 @param format The formatting string.
432 @param args The argument list created by the va_start macro.
433 @return Number of characters printed.
435 int vfprintf( FILE * restrict fh, const char * restrict format, va_list args );
437 /** Variable PRINT Formatted. Equivalent to vfprintf( stdout, format, args ).
438 @param format The formatting string.
439 @param args The argument list created by the va_start macro.
440 @return Number of characters printed.
442 int vprintf( const char * restrict format, va_list args );
444 /** Variable String PRINT Formatted. Equivalent to vprintf( format, args ), but
445 writing to a char array instead to stdout.
446 @param dest The char array to write to.
447 @param format The formatting string.
448 @param args The argument list created by the va_start macro.
449 @return Number of characters printed.
451 int vsprintf( char * restrict s, const char * restrict format, va_list ap);
453 /** Variable String N PRINT Formatted. Equivalent to vsprintf( dest, format,
454 args ), but will not write more than n characters.
455 @param dest The char array to write to.
456 @param n Maximum number of characters to write.
457 @param format The formatting string.
458 @param args The argument list created by the va_start macro.
459 @return Number of characters printed.
461 int vsnprintf( char * restrict dest, size_t n, const char * restrict format, va_list ap );
463 // ----------------------------------------------------------------------------
466 Equivalent to fprintf( stderr, "%s: %s\n", text, strerror( errno ) ).
467 @param test Text to prepend the error message with.
469 void perror( const char * text );
471 // ----------------------------------------------------------------------------
473 /** File READ. Reads a number of objects of a given size from file, and into
475 @param dest The memory area to write into.
476 @param size The size of one object.
477 @param n The number of objects to read.
478 @param fh The file handle.
479 @return The number of objects successfully read.
481 size_t fread( void * restrict dest, size_t size, size_t n, FILE * restrict fh );
483 /** File WRITE. Writes a number of objects from a memory area to file.
484 @param src The memory area to write from.
485 @param size The size of a single object.
486 @param n The number of objects to write.
487 @param fh The file handle.
488 @return The number of objects successfully written.
490 size_t fwrite( const void * restrict src, size_t size, size_t n, FILE * restrict fh );
492 // ----------------------------------------------------------------------------
494 /** SET Virtual BUFfer. Sets buffering mode and (optionally) the memory used
495 for buffering, for a given file handle.
496 This function must only be called immediately after associating the file
497 handle with a file, before any operations are called on the file handle.
498 @param fh The file handle.
499 @param buf A pointer to the memory area to use for buffering, or NULL to
500 use internally assigned buffer memory.
501 @param mode One of _IOFBF, _IOLBF, _IONBF.
502 @param size Size of the memory area to be used for buffering.
504 int setvbuf( FILE * restrict fh, char * restrict buf, int mode, size_t size );
506 /** SET BUFfer. Equivalent to (void) setvbuf( fh, buf, _IOFBF, BUFSIZ ), or
507 (void) setvbuf( fh, NULL, _IONBF, BUFSIZ ) if buf == NULL.
508 @param fh The file handle to be passed to setvbuf().
509 @param buf The buffer pointer to be passed to setvbuf().
511 void setbuf( FILE * restrict fh, char * restrict buf );
513 // ----------------------------------------------------------------------------
515 /* PDPC code - unreviewed
517 What we have is an internal buffer, which is 8 characters
518 longer than the actually used buffer. E.g. say BUFSIZ is
519 512 bytes, then we actually allocate 520 bytes. The first
520 2 characters will be junk, the next 2 characters set to NUL,
521 for protection against some backward-compares. The fourth-last
522 character is set to '\n', to protect against overscan. The
523 last 3 characters will be junk, to protect against memory
524 violation. intBuffer is the internal buffer, but everyone refers
525 to fbuf, which is actually set to the &intBuffer[4]. Also,
526 szfbuf is the size of the "visible" buffer, not the internal
527 buffer. The reason for the 2 junk characters at the beginning
528 is to align the buffer on a 4-byte boundary.
533 #if (defined(__OS2__) || defined(__32BIT__))
534 unsigned long hfile; /* OS/2 file handle */
536 #if (defined(__MSDOS__) || defined(__DOS__) || defined(__POWERC))
537 int hfile; /* dos file handle */
539 #if (defined(__MVS__))
546 int quickBin; /* 1 = do DosRead NOW!!!! */
547 int quickText; /* 1 = quick text mode */
548 int textMode; /* 1 = text mode, 0 = binary mode */
549 int intFno; /* internal file number */
550 unsigned long bufStartR; /* buffer start represents, e.g. if we
551 have read in 3 buffers, each of 512 bytes, and we are
552 currently reading from the 3rd buffer, then the first
553 character in the buffer would be 1024, so that is what is
555 char *fbuf; /* file buffer - this is what all the routines
557 size_t szfbuf; /* size of file buffer (the one that the routines
558 see, and the user allocates, and what is actually
559 read in from disk) */
560 char *upto; /* what character is next to read from buffer */
561 char *endbuf; /* pointer PAST last character in buffer, ie it
562 points to the '\n' in the internal buffer */
563 int errorInd; /* whether an error has occurred on this file */
564 int eofInd; /* whether EOF has been reached on this file */
565 int ungetCh; /* character pushed back, -1 if none */
566 int bufTech; /* buffering technique, _IOFBF etc */
567 char *intBuffer; /* internal buffer */
568 int noNl; /* When doing gets, we don't copy NL */
569 int mode; /* __WRITE_MODE or __READ_MODE */
570 int update; /* Is file update (read + write)? */
571 int theirBuffer; /* Is the buffer supplied by them? */