/* Input/output <stdio.h>12This file is part of the Public Domain C Library (PDCLib).3Permission is granted to use, modify, and / or redistribute at will.4*/56#ifndef _PDCLIB_STDIO_H7#define _PDCLIB_STDIO_H _PDCLIB_STDIO_H8#include "_PDCLIB_int.h"910#ifdef __cplusplus11extern "C" {12#endif1314#ifndef _PDCLIB_SIZE_T_DEFINED15#define _PDCLIB_SIZE_T_DEFINED _PDCLIB_SIZE_T_DEFINED16typedef _PDCLIB_size_t size_t;17#endif1819#ifndef _PDCLIB_NULL_DEFINED20#define _PDCLIB_NULL_DEFINED _PDCLIB_NULL_DEFINED21#define NULL _PDCLIB_NULL22#endif2324/* See setvbuf(), third argument */25#define _IOFBF 126#define _IOLBF 227#define _IONBF 42829/* The following are platform-dependant, and defined in _PDCLIB_config.h. */30typedef _PDCLIB_fpos_t fpos_t;31typedef _PDCLIB_file_t FILE;32#define EOF -133#define BUFSIZ _PDCLIB_BUFSIZ34#define FOPEN_MAX _PDCLIB_FOPEN_MAX35#define FILENAME_MAX _PDCLIB_FILENAME_MAX36#define L_tmpnam _PDCLIB_L_tmpnam37#define TMP_MAX _PDCLIB_TMP_MAX3839/* See fseek(), third argument40*41* Some system headers (e.g. windows) also define the SEEK_* values. Check for42* this and validate that they're the same value43*/44#if !defined(SEEK_CUR)45#define SEEK_CUR _PDCLIB_SEEK_CUR46#elif SEEK_CUR != _PDCLIB_SEEK_CUR47#error SEEK_CUR != _PDCLIB_SEEK_CUR48#endif4950#if !defined(SEEK_END)51#define SEEK_END _PDCLIB_SEEK_END52#elif SEEK_END != _PDCLIB_SEEK_END53#error SEEK_END != _PDCLIB_SEEK_END54#endif5556#if !defined(SEEK_SET)57#define SEEK_SET _PDCLIB_SEEK_SET58#elif SEEK_SET != _PDCLIB_SEEK_SET59#error SEEK_SET != _PDCLIB_SEEK_SET60#endif6162extern FILE * stdin;63extern FILE * stdout;64extern FILE * stderr;6566/* Operations on files */6768/* Remove the given file.69Returns zero if successful, non-zero otherwise.70This implementation does detect if a file of that name is currently open,71and fails the remove in this case. This does not detect two distinct names72that merely result in the same file (e.g. "/home/user/foo" vs. "~/foo").73*/74int remove( const char * filename ) _PDCLIB_nothrow;7576/* Rename the given old file to the given new name.77Returns zero if successful, non-zero otherwise.78This implementation does detect if the old filename corresponds to an open79file, and fails the rename in this case.80If there already is a file with the new filename, behaviour is defined by81the glue code (see functions/_PDCLIB/rename.c).82*/83int rename( const char * old, const char * newn ) _PDCLIB_nothrow;8485/* Open a temporary file with mode "wb+", i.e. binary-update. Remove the file86automatically if it is closed or the program exits normally (by returning87from main() or calling exit()).88Returns a pointer to a FILE handle for this file.89This implementation does not remove temporary files if the process aborts90abnormally (e.g. abort()).91*/92FILE * tmpfile( void ) _PDCLIB_nothrow;9394/* Generate a file name that is not equal to any existing filename AT THE TIME95OF GENERATION. Generate a different name each time it is called.96Returns a pointer to an internal static buffer containing the filename if s97is a NULL pointer. (This is not thread-safe!)98Returns s if it is not a NULL pointer (s is then assumed to point to an array99of at least L_tmpnam characters).100Returns NULL if unable to generate a suitable name (because all possible101names already exist, or the function has been called TMP_MAX times already).102Note that this implementation cannot guarantee a file of the name generated103is not generated between the call to this function and a subsequent fopen().104*/105char * tmpnam( char * s ) _PDCLIB_nothrow;106107/* File access functions */108109/* Close the file associated with the given stream (after flushing its buffers).110Returns zero if successful, EOF if any errors occur.111*/112int fclose( FILE * stream ) _PDCLIB_nothrow;113114/* Flush the buffers of the given output stream. If the stream is an input115stream, or an update stream with the last operation being an input operation,116behaviour is undefined.117If stream is a NULL pointer, perform the buffer flushing for all applicable118streams.119Returns zero if successful, EOF if a write error occurs.120Sets the error indicator of the stream if a write error occurs.121*/122int fflush( FILE * stream ) _PDCLIB_nothrow;123124/* Open the file with the given filename in the given mode, and return a stream125handle for it in which error and end-of-file indicator are cleared. Defined126values for mode are:127128READ MODES129text files binary files130without update "r" "rb"131with update "r+" "rb+" or "r+b"132133Opening in read mode fails if no file with the given filename exists, or if134cannot be read.135136WRITE MODES137text files binary files138without update "w" "wb"139with update "w+" "wb+" or "w+b"140141With write modes, if a file with the given filename already exists, it is142truncated to zero length.143144APPEND MODES145text files binary files146without update "a" "ab"147with update "a+" "ab+" or "a+b"148149With update modes, if a file with the given filename already exists, it is150not truncated to zero length, but all writes are forced to end-of-file (this151regardless to fseek() calls). Note that binary files opened in append mode152might have their end-of-file padded with '\0' characters.153154Update modes mean that both input and output functions can be performed on155the stream, but output must be terminated with a call to either fflush(),156fseek(), fsetpos(), or rewind() before input is performed, and input must157be terminated with a call to either fseek(), fsetpos(), or rewind() before158output is performed, unless input encountered end-of-file.159160If a text file is opened with update mode, the implementation is at liberty161to open a binary stream instead. This implementation honors the exact mode162given.163164The stream is fully buffered if and only if it can be determined not to165refer to an interactive device.166167If the mode string begins with but is longer than one of the above sequences168the implementation is at liberty to ignore the additional characters, or do169implementation-defined things. This implementation only accepts the exact170modes above.171172Returns a pointer to the stream handle if successfull, NULL otherwise.173*/174FILE * fopen( const char * _PDCLIB_restrict filename,175const char * _PDCLIB_restrict mode ) _PDCLIB_nothrow;176177/* Creates a stream connected to the file descriptor \p fd with mode \p mode.178Mode must match the mode with which the file descriptor was opened.179*/180FILE * _PDCLIB_fvopen( _PDCLIB_fd_t fd, const _PDCLIB_fileops_t * ops,181int mode, const char * filename ) _PDCLIB_nothrow;182183/* Close any file currently associated with the given stream. Open the file184identified by the given filename with the given mode (equivalent to fopen()),185and associate it with the given stream. If filename is a NULL pointer,186attempt to change the mode of the given stream.187This implementation allows any mode changes on "real" files, and associating188of the standard streams with files. It does *not* support mode changes on189standard streams.190(Primary use of this function is to redirect stdin, stdout, and stderr.)191*/192FILE * freopen( const char * _PDCLIB_restrict filename, const char * _PDCLIB_restrict mode, FILE * _PDCLIB_restrict stream ) _PDCLIB_nothrow;193194/* If buf is a NULL pointer, call setvbuf( stream, NULL, _IONBF, BUFSIZ ).195If buf is not a NULL pointer, call setvbuf( stream, buf, _IOFBF, BUFSIZ ).196*/197void setbuf( FILE * _PDCLIB_restrict stream, char * _PDCLIB_restrict buf ) _PDCLIB_nothrow;198199/* Set the given stream to the given buffering mode. If buf is not a NULL200pointer, use buf as file buffer (of given size). If buf is a NULL pointer,201use a buffer of given size allocated internally. _IONBF causes unbuffered202behaviour, _IOLBF causes line-buffered behaviour, _IOFBF causes fully203buffered behaviour. Calling this function is only valid right after a file is204opened, and before any other operation (except for any unsuccessful calls to205setvbuf()) has been performed.206Returns zero if successful, nonzero otherwise.207*/208int setvbuf( FILE * _PDCLIB_restrict stream, char * _PDCLIB_restrict buf, int mode, size_t size ) _PDCLIB_nothrow;209210/* Formatted input/output functions */211212/*213Write output to the given stream, as defined by the given format string and2140..n subsequent arguments (the argument stack).215216The format string is written to the given stream verbatim, except for any217conversion specifiers included, which start with the letter '%' and are218documented below. If the given conversion specifiers require more arguments219from the argument stack than provided, behaviour is undefined. Additional220arguments not required by conversion specifiers are evaluated but otherwise221ignored.222223(The standard specifies the format string is allowed to contain multibyte224character sequences as long as it starts and ends in initial shift state,225but this is not yet supported by this implementation, which interprets the226format string as sequence of char.)227TODO: Add multibyte support to printf() functions.228229A conversion specifier consists of:230- Zero or more flags (one of the characters "-+ #0").231- Optional minimum field width as decimal integer. Default is padding to the232left, using spaces. Note that 0 is taken as a flag, not the beginning of a233field width. Note also that a small field width will not result in the234truncation of a value.235- Optional precision (given as ".#" with # being a decimal integer),236specifying:237- the min. number of digits to appear (diouxX),238- the max. number of digits after the decimal point (aAeEfF),239- the max. number of significant digits (gG),240- the max. number of bytes to be written (s).241- behaviour with other conversion specifiers is undefined.242- Optional length modifier specifying the size of the argument (one of "hh",243"ll", or one of the characters "hljztL").244- Conversion specifier character specifying the type of conversion to be245applied (and the type of the next argument from the argument stack). One246of the characters "diouxXfFeEgGaAcspn%".247248Minimum field width and/or precision may be given as asterisk ('*') instead249of a decimal integer. In this case, the next argument from the argument250stack is assumed to be an int value specifying the width / precision. A251negative field width is interpreted as flag '-' followed by a positive field252width. A negative precision is interpreted as if no precision was given.253254FLAGS255- Left-justify the conversion result within its field width.256+ Prefix a '+' on positive signed conversion results. Prefix a '-' on257floating conversions resulting in negative zero, or negative values258rounding to zero.259space Prefix a space on positive signed conversion results, or if a signed260conversion results in no characters. If both '+' and ' ' are given,261' ' is ignored.262# Use an "alternative form" for263- 'o' conversion, increasing precision until the first digit of the264result is a zero;265- 'x' or 'X' conversion, prefixing "0x" or "0X" to nonzero results;266- "aAeEfF" conversions, always printing a decimal point even if no267digits are following;268- 'g' or 'G' conversions, always printing a decimal point even if no269digits are following, and not removing trailing zeroes.270- behaviour for other conversions is unspecified.2710 Use leading zeroes instead of spaces for field width padding. If both272'-' and '0' are given, '0' is ignored. If a precision is specified for273any of the "diouxX" conversions, '0' is ignored. Behaviour is only274defined for "diouxXaAeEfFgG".275276LENGTH MODIFIERS277hh For "diouxX" conversions, the argument from the argument stack is278assumed to be of char width. (It will have been subject to integer279promotion but will be converted back.) For 'n' conversions, the argument280is assumed to be a pointer to signed char.281h For "diouxX" conversions, the argument from the argument stack is282assumed to be of short int width. (It will have been subject to integer283promotion but will be converted back.) For 'n' conversions, the argument284is assumed to be a pointer to short int.285l For "diouxX" conversions, the argument from the argument stack is286assumed to be of long int width. For 'n' conversions, the argument is287assumed to be a pointer to short int. For 'c' conversions, the argument288is assumed to be a wint_t. For 's' conversions, the argument is assumed289to be a pointer to wchar_t. No effect on "aAeEfFgG" conversions.290ll For "diouxX" conversions, the argument from the argument stack is291assumed to be of long long int width. For 'n' conversions, the argument292is assumed to be a pointer to long long int.293j For "diouxX" conversions, the argument from the argument stack is294assumed to be of intmax_t width. For 'n' conversions, the argument is295assumed to be a pointer to intmax_t.296z For "diouxX" conversions, the argument from the argument stack is297assumed to be of size_t width. For 'n' conversions, the argument is298assumed to be a pointer to size_t.299t For "diouxX" conversions, the argument from the argument stack is300assumed to be of ptrdiff_t width. For 'n' conversions, the argument is301assumed to be a pointer to ptrdiff_t.302L For "aAeEfFgG" conversions, the argument from the argument stack is303assumed to be a long double.304Length modifiers appearing for any conversions not mentioned above will have305undefined behaviour.306If a length modifier appears with any conversion specifier other than as307specified above, the behavior is undefined.308309CONVERSION SPECIFIERS310d,i The argument from the argument stack is assumed to be of type int, and311is converted to a signed decimal value with a minimum number of digits312as specified by the precision (default 1), padded with leading zeroes.313A zero value converted with precision zero yields no output.314o The argument from the argument stack is assumed to be of type unsigned315int, and is converted to an unsigned octal value, other behaviour being316as above.317u The argument from the argument stack is assumed to be of type unsigned318int, and converted to an unsigned decimal value, other behaviour being319as above.320x,X The argument from the argument stack is assumed to be of type unsigned321int, and converted to an unsigned hexadecimal value, using lowercase322"abcdef" for 'x' and uppercase "ABCDEF" for 'X' conversion, other323behaviour being as above.324f,F The argument from the argument stack is assumed to be of type double,325and converted to a decimal floating point in decimal-point notation,326with the number of digits after the decimal point as specified by the327precision (default 6) and the value being rounded appropriately. If328precision is zero (and the '#' flag is not given), no decimal point is329printed. At least one digit is always printed before the decimal point.330For 'f' conversions, an infinity value is printed as either [-]inf or331[-]infinity (, depending on the configuration of this implementation. A332NaN value is printed as [-]nan. For 'F' conversions uppercase characters333are used for these special values. The flags '-', '+' and ' ' apply as334usual to these special values, '#' and '0' have no effect.335e,E The argument from the argument stack is assumed to be of type double,336and converted to a decimal floating point in normalized exponential337notation ([?]d.ddd edd). "Normalized" means one nonzero digit before338the decimal point, unless the value is zero. The number of digits after339the decimal point is specified by the precision (default 6), the value340being rounded appropriately. If precision is zero (and the '#' flag is341not given), no decimal point is printed. The exponent has at least two342digits, and not more than necessary to represent the exponent. If the343value is zero, the exponent is zero. The 'e' written to indicate the344exponend is uppercase for 'E' conversions.345Infinity or NaN values are represented as for 'f' and 'F' conversions,346respectively.347g,G The argument from the argument stack is assumed to be of type double,348and converted according to either 'f' or 'e' format for 'g' conversions,349or 'F' or 'E' format for 'G' conversions, respectively, with the actual350conversion chosen depending on the value. 'e' / 'E' conversion is chosen351if the resulting exponent is < -4 or >= the precision (default 1).352Trailing zeroes are removed (unless the '#' flag is given). A decimal353point appears only if followed by a digit.354Infinity or NaN values are represented as for 'f' and 'F' conversions,355respectively.356a,A The argument from the argument stack is assumed to be of type double,357and converted to a floating point hexadecimal notation ([?]0xh.hhhh pd)358with one hexadecimal digit (being nonzero if the value is normalized,359and otherwise unspecified) before the decimal point, and the number of360digits after the decimal point being specified by the precision. If no361precision is given, the default is to print as many digits as nevessary362to give an exact representation of the value (if FLT_RADIX is a power of3632). If no precision is given and FLT_RADIX is not a power of 2, the364default is to print as many digits to distinguish values of type double365(possibly omitting trailing zeroes). (A precision p is sufficient to366distinguish values of the source type if 16^p-1 > b^n where b is367FLT_RADIX and n is the number of digits in the significand (to base b)368of the source type. A smaller p might suffice depending on the369implementation's scheme for determining the digit to the left of the370decimal point.) The error has the correct sign for the current rounding371direction.372Unless the '#' flag is given, no decimal-point is given for zero373precision.374The 'a' conversion uses lowercase "abcdef", "0x" and 'p', the 'A'375conversion uppercase "ABCDEF", "0X" and 'P'.376The exponent always has at least one digit, and not more than necessary377to represent the decimal exponent of 2. If the value is zero, the378exponent is zero.379Infinity or NaN values are represented as for 'f' and 'F' conversions,380respectively.381Binary implementations are at liberty to chose the hexadecimal digit to382the left of the decimal point so that subsequent digits align to nibble383boundaries.384c The argument from the argument stack is assumed to be of type int, and385converted to a character after the value has been cast to unsigned char.386If the 'l' length modifier is given, the argument is assumed to be of387type wint_t, and converted as by a "%ls" conversion with no precision388and a pointer to a two-element wchar_t array, with the first element389being the wint_t argument and the second a '\0' wide character.390s The argument from the argument stack is assumed to be a char array (i.e.391pointer to char). Characters from that array are printed until a zero392byte is encountered or as many bytes as specified by a given precision393have been written.394If the l length modifier is given, the argument from the argument stack395is assumed to be a wchar_t array (i.e. pointer to wchar_t). Wide396characters from that array are converted to multibyte characters as by397calls to wcrtomb() (using a mbstate_t object initialized to zero prior398to the first conversion), up to and including the terminating null wide399character. The resulting multibyte character sequence is then printed up400to but not including the terminating null character. If a precision is401given, it specifies the maximum number of bytes to be written (including402shift sequences). If the given precision would require access to a wide403character one past the end of the array, the array shall contain a '\0'404wide character. In no case is a partial multibyte character written.405Redundant shift sequences may result if the multibyte characters have a406state-dependent encoding.407TODO: Clarify these statements regarding %ls.408p The argument from the argument stack is assumed to be a void pointer,409and converted to a sequence of printing characters in an implementation-410defined manner.411This implementation casts the pointer to type intptr_t, and prints the412value as if a %#x conversion specifier was given.413n The argument from the argument stack is assumed to be a pointer to a414signed integer, into which the number of characters written so far by415this call to fprintf is stored. The behaviour, should any flags, field416widths, or precisions be given is undefined.417% A verbatim '%' character is written. No argument is taken from the418argument stack.419420Returns the number of characters written if successful, a negative value421otherwise.422*/423int fprintf( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict format, ... ) _PDCLIB_nothrow;424425/* TODO: fscanf() documentation */426/*427Read input from a given stream, as defined by the given format string, and428store converted input in the objects pointed to by 0..n subsequent arguments429(the argument stack).430431The format string contains a sequence of directives that are expected to432match the input. If such a directive fails to match, the function returns433(matching error). It also returns if an input error occurs (input error).434435Directives can be:436- one or more whitespaces, matching any number of whitespaces in the input;437- printing characters, matching the input verbatim;438- conversion specifications, which convert an input sequence into a value as439defined by the individual specifier, and store that value in a memory440location pointed to by the next pointer on the argument stack. Details are441documented below. If there is an insufficient number of pointers on the442argument stack, behaviour is undefined. Additional arguments not required443by any conversion specifications are evaluated, but otherwise ignored.444445(The standard specifies the format string is allowed to contain multibyte446character sequences as long as it starts and ends in initial shift state,447but this is not yet supported by this implementation, which interprets the448format string as sequence of char.)449TODO: Add multibyte support to scanf() functions.450451A conversion specifier consists of:452- Optional assignment-suppressing character ('*') that makes the conversion453read input as usual, but does not assign the conversion result.454- Optional maximum field width as decimal integer.455- Optional length modifier specifying the size of the argument (one of "hh",456"ll", or one of the characters "hljztL").457- Conversion specifier character specifying the type of conversion to be458applied (and the type of the next argument from the argument stack). One459of the characters "diouxXaAeEfFgGcs[pn%".460461LENGTH MODIFIERS462hh For "diouxXn" conversions, the next pointer from the argument stack is463assumed to point to a variable of of char width.464h For "diouxXn" conversions, the next pointer from the argument stack is465assumed to point to a variable of short int width.466l For "diouxXn" conversions, the next pointer from the argument stack is467assumed to point to a variable of long int width.468For "aAeEfFgG" conversions, it is assumed to point to a variable of type469double.470For "cs[" conversions, it is assumed to point to a variable of type471wchar_t.472ll For "diouxXn" conversions, the next pointer from the argument stack is473assumed to point to a variable of long long int width.474j For "diouxXn" conversions, the next pointer from the argument stack is475assumed to point to a variable of intmax_t width.476z For "diouxXn" conversions, the next pointer from the argument stack is477assumed to point to a variable of size_t width.478t For "diouxXn" conversions, the next pointer from the argument stack is479assumed to point to a variable of ptrdiff_t width.480L For "aAeEfFgG" conversions, the next pointer from the argument stack is481assumed to point to a variable of type long double.482Length modifiers appearing for any conversions not mentioned above will have483undefined behaviour.484If a length modifier appears with any conversion specifier other than as485specified above, the behavior is undefined.486487CONVERSION SPECIFIERS488d Matches an (optionally signed) decimal integer of the format expected489by strtol() with base 10. The next pointer from the argument stack is490assumed to point to a signed integer.491i Matches an (optionally signed) integer of the format expected by492strtol() with base 0. The next pointer from the argument stack is493assumed to point to a signed integer.494o Matches an (optionally signed) octal integer of the format expected by495strtoul() with base 8. The next pointer from the argument stack is496assumed to point to an unsigned integer.497u Matches an (optionally signed) decimal integer of the format expected498by strtoul() with base 10. The next pointer from the argument stack is499assumed to point to an unsigned integer.500x Matches an (optionally signed) hexadecimal integer of the format501expected by strtoul() with base 16. The next pointer from the argument502stack is assumed to point to an unsigned integer.503aefg Matches an (optionally signed) floating point number, infinity, or not-504a-number-value of the format expected by strtod(). The next pointer505from the argument stack is assumed to point to a float.506c Matches a number of characters as specified by the field width (default5071). The next pointer from the argument stack is assumed to point to a508character array large enough to hold that many characters.509If the 'l' length modifier is given, the input is assumed to match a510sequence of multibyte characters (starting in the initial shift state),511which will be converted to a wide character sequence as by successive512calls to mbrtowc() with a mbstate_t object initialized to zero prior to513the first conversion. The next pointer from the argument stack is514assumed to point to a wchar_t array large enough to hold that many515characters.516In either case, note that no '\0' character is added to terminate the517sequence.518s Matches a sequence of non-white-space characters. The next pointer from519the argument stack is assumed to point to a character array large520enough to hold the sequence including terminating '\0' character.521If the 'l' length modifier is given, the input is assumed to match a522sequence of multibyte characters (starting in the initial shift state),523which will be converted to a wide character sequence as by a call to524mbrtowc() with a mbstate_t object initialized to zero prior to the525first conversion. The next pointer from the argument stack is assumed526to point to a wchar_t array large enough to hold the sequence including527terminating '\0' character.528[ Matches a nonempty sequence consisting of any of those characters529specified between itself and a corresponding closing bracket (']').530If the first character in the list is a circumflex ('^'), this matches531a nonempty sequence consisting of any characters NOT specified. If the532closing bracket appears as the first character in the scanset ("[]" or533"[^]", it is assumed to belong to the scanset, which then ends with the534NEXT closing bracket.535If there is a '-' character in the scanset which is not the first after536the opening bracket (or the circumflex, see above) or the last in the537scanset, behaviour is implementation-defined. This implementation538handles this character like any other.539540The extend of the input field is determined byte-by-byte for the above541conversions ('c', 's', '['), with no special provisions being made for542multibyte characters. The resulting field is nevertheless a multibyte543sequence begining in intial shift state.544545p Matches a sequence of characters as produced by the printf() "%p"546conversion. The next pointer from the argument stack is assumed to547point to a void pointer, which will be filled with the same location548as the pointer used in the printf() statement. Note that behaviour is549undefined if the input value is not the result of an earlier printf()550call.551n Does not read input. The next pointer from the argument stack is552assumed to point to a signed integer, into which the number of553characters read from input so far by this call to fscanf() is stored.554This does not affect the return value of fscanf(). The behaviour,555should an assignment-supressing character of field width be given,556is undefined.557This can be used to test the success of literal matches and suppressed558assignments.559% Matches a single, verbatim '%' character.560561A, E, F, G and X are valid, and equivalent to their lowercase counterparts.562563All conversions except [, c, or n imply that whitespace characters from the564input stream are consumed until a non-whitespace character is encountered.565Such whitespaces do not count against a maximum field width.566567Conversions push at most one character back into the input stream. That568implies that some character sequences converted by the strtol() and strtod()569function families are not converted identically by the scnaf() function570family.571572Returns the number of input items successfully assigned. This can be zero if573an early mismatch occurs. Returns EOF if an input failure occurs before the574first conversion.575*/576int fscanf( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict format, ... ) _PDCLIB_nothrow;577578/* Equivalent to fprintf( stdout, format, ... ). */579int printf( const char * _PDCLIB_restrict format, ... ) _PDCLIB_nothrow;580581/* Equivalent to fscanf( stdin, format, ... ). */582int scanf( const char * _PDCLIB_restrict format, ... ) _PDCLIB_nothrow;583584/* Equivalent to fprintf( stdout, format, ... ), except that the result is585written into the buffer pointed to by s, instead of stdout, and that any586characters beyond the (n-1)th are discarded. The (n)th character is587replaced by a '\0' character in this case.588Returns the number of characters that would have been written (not counting589the terminating '\0' character) if n had been sufficiently large, if590successful, and a negative number if an encoding error ocurred.591*/592int snprintf( char * _PDCLIB_restrict s, size_t n, const char * _PDCLIB_restrict format, ... ) _PDCLIB_nothrow;593594/* Equivalent to fprintf( stdout, format, ... ), except that the result is595written into the buffer pointed to by s, instead of stdout.596*/597int sprintf( char * _PDCLIB_restrict s, const char * _PDCLIB_restrict format, ... ) _PDCLIB_nothrow;598599/* Equivalent to fscanf( stdin, format, ... ), except that the input is read600from the buffer pointed to by s, instead of stdin.601*/602int sscanf( const char * _PDCLIB_restrict s, const char * _PDCLIB_restrict format, ... ) _PDCLIB_nothrow;603604/* Equivalent to fprintf( stream, format, ... ), except that the argument stack605is passed as va_list parameter. Note that va_list is not declared by606<stdio.h>.607*/608int vfprintf( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict format, _PDCLIB_va_list arg ) _PDCLIB_nothrow;609610/* Equivalent to fscanf( stream, format, ... ), except that the argument stack611is passed as va_list parameter. Note that va_list is not declared by612<stdio.h>.613*/614int vfscanf( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict format, _PDCLIB_va_list arg ) _PDCLIB_nothrow;615616/* Equivalent to fprintf( stdout, format, ... ), except that the argument stack617is passed as va_list parameter. Note that va_list is not declared by618<stdio.h>.619*/620int vprintf( const char * _PDCLIB_restrict format, _PDCLIB_va_list arg ) _PDCLIB_nothrow;621622/* Equivalent to fscanf( stdin, format, ... ), except that the argument stack623is passed as va_list parameter. Note that va_list is not declared by624<stdio.h>.625*/626int vscanf( const char * _PDCLIB_restrict format, _PDCLIB_va_list arg ) _PDCLIB_nothrow;627628/* Equivalent to snprintf( s, n, format, ... ), except that the argument stack629is passed as va_list parameter. Note that va_list is not declared by630<stdio.h>.631*/632int vsnprintf( char * _PDCLIB_restrict s, size_t n, const char * _PDCLIB_restrict format, _PDCLIB_va_list arg ) _PDCLIB_nothrow;633634/* Equivalent to fprintf( stdout, format, ... ), except that the argument stack635is passed as va_list parameter, and the result is written to the buffer636pointed to by s, instead of stdout. Note that va_list is not declared by637<stdio.h>.638*/639int vsprintf( char * _PDCLIB_restrict s, const char * _PDCLIB_restrict format, _PDCLIB_va_list arg ) _PDCLIB_nothrow;640641/* Equivalent to fscanf( stdin, format, ... ), except that the argument stack642is passed as va_list parameter, and the input is read from the buffer643pointed to by s, instead of stdin. Note that va_list is not declared by644<stdio.h>.645*/646int vsscanf( const char * _PDCLIB_restrict s, const char * _PDCLIB_restrict format, _PDCLIB_va_list arg ) _PDCLIB_nothrow;647648int asprintf(char **strp, const char * _PDCLIB_restrict fmt, ...) _PDCLIB_nothrow;649int vasprintf(char **strp, const char * _PDCLIB_restrict fmt, _PDCLIB_va_list arg) _PDCLIB_nothrow;650651/* Character input/output functions */652653/* Retrieve the next character from given stream.654Returns the character, EOF otherwise.655If end-of-file is reached, the EOF indicator of the stream is set.656If a read error occurs, the error indicator of the stream is set.657*/658int fgetc( FILE * stream ) _PDCLIB_nothrow;659660/* Read at most n-1 characters from given stream into the array s, stopping at661\n or EOF. Terminate the read string with \n. If EOF is encountered before662any characters are read, leave the contents of s unchanged.663Returns s if successful, NULL otherwise.664If a read error occurs, the error indicator of the stream is set. In this665case, the contents of s are indeterminate.666*/667char * fgets( char * _PDCLIB_restrict s, int n, FILE * _PDCLIB_restrict stream ) _PDCLIB_nothrow;668669/* Write the value c (cast to unsigned char) to the given stream.670Returns c if successful, EOF otherwise.671If a write error occurs, sets the error indicator of the stream is set.672*/673int fputc( int c, FILE * stream ) _PDCLIB_nothrow;674675/* Write the string s (not including the terminating \0) to the given stream.676Returns a value >=0 if successful, EOF otherwise.677This implementation does set the error indicator of the stream if a write678error occurs.679*/680int fputs( const char * _PDCLIB_restrict s, FILE * _PDCLIB_restrict stream ) _PDCLIB_nothrow;681682/* Equivalent to fgetc( stream ), but may be overloaded by a macro that683evaluates its parameter more than once.684*/685int getc( FILE * stream ) _PDCLIB_nothrow;686687/* Equivalent to fgetc( stdin ). */688int getchar( void ) _PDCLIB_nothrow;689690#if _PDCLIB_C_MAX(1999)691/* Read characters from given stream into the array s, stopping at \n or EOF.692The string read is terminated with \0. Returns s if successful. If EOF is693encountered before any characters are read, the contents of s are unchanged,694and NULL is returned. If a read error occurs, the contents of s are indeter-695minate, and NULL is returned.696697This function is dangerous and has been a great source of security698vulnerabilities. Do not use it. It was removed by C11.699*/700char * gets( char * s ) _PDCLIB_DEPRECATED _PDCLIB_nothrow;701#endif702703/* Equivalent to fputc( c, stream ), but may be overloaded by a macro that704evaluates its parameter more than once.705*/706int putc( int c, FILE * stream ) _PDCLIB_nothrow;707708/* Equivalent to fputc( c, stdout ), but may be overloaded by a macro that709evaluates its parameter more than once.710*/711int putchar( int c ) _PDCLIB_nothrow;712713/* Write the string s (not including the terminating \0) to stdout, and append714a newline to the output. Returns a value >= 0 when successful, EOF if a715write error occurred.716*/717int puts( const char * s ) _PDCLIB_nothrow;718719/* Push the value c (cast to unsigned char) back onto the given (input) stream.720A character pushed back in this way will be delivered by subsequent read721operations (and skipped by subsequent file positioning operations) as if it722has not been read. The external representation of the stream is unaffected723by this pushback (it is a buffer operation). One character of pushback is724guaranteed, further pushbacks may fail. EOF as value for c does not change725the input stream and results in failure of the function.726For text files, the file position indicator is indeterminate until all727pushed-back characters are read. For binary files, the file position728indicator is decremented by each successful call of ungetc(). If the file729position indicator for a binary file was zero before the call of ungetc(),730behaviour is undefined. (Older versions of the library allowed such a call.)731Returns the pushed-back character if successful, EOF if it fails.732*/733int ungetc( int c, FILE * stream ) _PDCLIB_nothrow;734735/* Direct input/output functions */736737/* Read up to nmemb elements of given size from given stream into the buffer738pointed to by ptr. Returns the number of elements successfully read, which739may be less than nmemb if a read error or EOF is encountered. If a read740error is encountered, the value of the file position indicator is741indeterminate. If a partial element is read, its value is indeterminate.742If size or nmemb are zero, the function does nothing and returns zero.743*/744size_t fread( void * _PDCLIB_restrict ptr, size_t size, size_t nmemb, FILE * _PDCLIB_restrict stream ) _PDCLIB_nothrow;745746/* Write up to nmemb elements of given size from buffer pointed to by ptr to747the given stream. Returns the number of elements successfully written, which748will be less than nmemb only if a write error is encountered. If a write749error is encountered, the value of the file position indicator is750indeterminate. If size or nmemb are zero, the function does nothing and751returns zero.752*/753size_t fwrite( const void * _PDCLIB_restrict ptr, size_t size, size_t nmemb, FILE * _PDCLIB_restrict stream ) _PDCLIB_nothrow;754755/* File positioning functions */756757/* Store the current position indicator (and, where appropriate, the current758mbstate_t status object) for the given stream into the given pos object. The759actual contents of the object are unspecified, but it can be used as second760parameter to fsetpos() to reposition the stream to the exact position and761parse state at the time fgetpos() was called.762Returns zero if successful, nonzero otherwise.763TODO: Implementation-defined errno setting for fgetpos().764*/765int fgetpos( FILE * _PDCLIB_restrict stream, fpos_t * _PDCLIB_restrict pos ) _PDCLIB_nothrow;766767/* Set the position indicator for the given stream to the given offset from:768- the beginning of the file if whence is SEEK_SET,769- the current value of the position indicator if whence is SEEK_CUR,770- end-of-file if whence is SEEK_END.771On text streams, non-zero offsets are only allowed with SEEK_SET, and must772have been returned by ftell() for the same file.773Any characters buffered by ungetc() are dropped, the end-of-file indicator774for the stream is cleared. If the given stream is an update stream, the next775operation after a successful fseek() may be either input or output.776Returns zero if successful, nonzero otherwise. If a read/write error occurs,777the error indicator for the given stream is set.778*/779int fseek( FILE * stream, long int offset, int whence ) _PDCLIB_nothrow;780781/* Set the position indicator (and, where appropriate the mbstate_t status782object) for the given stream to the given pos object (created by an earlier783call to fgetpos() on the same file).784Any characters buffered by ungetc() are dropped, the end-of-file indicator785for the stream is cleared. If the given stream is an update stream, the next786operation after a successful fsetpos() may be either input or output.787Returns zero if successful, nonzero otherwise. If a read/write error occurs,788the error indicator for the given stream is set.789TODO: Implementation-defined errno setting for fsetpos().790*/791int fsetpos( FILE * stream, const fpos_t * pos ) _PDCLIB_nothrow;792793/* Return the current offset of the given stream from the beginning of the794associated file. For text streams, the exact value returned is unspecified795(and may not be equal to the number of characters), but may be used in796subsequent calls to fseek().797Returns -1L if unsuccessful.798TODO: Implementation-defined errno setting for ftell().799*/800long int ftell( FILE * stream ) _PDCLIB_nothrow;801802/* Equivalent to (void)fseek( stream, 0L, SEEK_SET ), except that the error803indicator for the stream is also cleared.804*/805void rewind( FILE * stream ) _PDCLIB_nothrow;806807/* Error-handling functions */808809/* Clear the end-of-file and error indicators for the given stream. */810void clearerr( FILE * stream ) _PDCLIB_nothrow;811812/* Return zero if the end-of-file indicator for the given stream is not set,813nonzero otherwise.814*/815int feof( FILE * stream ) _PDCLIB_nothrow;816817/* Return zero if the error indicator for the given stream is not set, nonzero818otherwise.819*/820int ferror( FILE * stream ) _PDCLIB_nothrow;821822/* If s is neither a NULL pointer nor an empty string, print the string to823stderr (with appended colon (':') and a space) first. In any case, print an824error message depending on the current value of errno (being the same as if825strerror( errno ) had been called).826*/827void perror( const char * s ) _PDCLIB_nothrow;828829/* Unlocked I/O830*831* Since threading was introduced in C11, FILE objects have had implicit locks832* to prevent data races and inconsistent output.833*834* PDCLib provides these functions from POSIX as an extension in order to enable835* users to access the underlying unlocked functions.836*837* For each function defined in C11 where an _unlocked variant is defined below,838* the behaviour of the _unlocked variant is the same except that it will not839* take the lock associated with the stream.840*841* flockfile, ftrylockfile and funlockfile can be used to manually manipulate842* the stream locks. The behaviour of the _unlocked functions if called when the843* stream isn't locked by the calling thread is implementation defined.844*/845#if _PDCLIB_POSIX_MIN(200112L) || _PDCLIB_BSD_SOURCE || _PDCLIB_SVID_SOURCE846void flockfile(FILE *file) _PDCLIB_nothrow;847int ftrylockfile(FILE *file) _PDCLIB_nothrow;848void funlockfile(FILE *file) _PDCLIB_nothrow;849850int getc_unlocked(FILE *stream) _PDCLIB_nothrow;851int getchar_unlocked(void) _PDCLIB_nothrow;852int putc_unlocked(int c, FILE *stream) _PDCLIB_nothrow;853int putchar_unlocked(int c) _PDCLIB_nothrow;854#endif855856#if _PDCLIB_BSD_SOURCE || _PDCLIB_SVID_SOURCE857void clearerr_unlocked(FILE *stream) _PDCLIB_nothrow;858int feof_unlocked(FILE *stream) _PDCLIB_nothrow;859int ferror_unlocked(FILE *stream) _PDCLIB_nothrow;860int fflush_unlocked(FILE *stream) _PDCLIB_nothrow;861int fgetc_unlocked(FILE *stream) _PDCLIB_nothrow;862int fputc_unlocked(int c, FILE *stream) _PDCLIB_nothrow;863size_t fread_unlocked(void *ptr, size_t size, size_t n, FILE *stream) _PDCLIB_nothrow;864size_t fwrite_unlocked(const void *ptr, size_t size, size_t n, FILE *stream) _PDCLIB_nothrow;865#endif866867#if _PDCLIB_GNU_SOURCE868char *fgets_unlocked(char *s, int n, FILE *stream) _PDCLIB_nothrow;869int fputs_unlocked(const char *s, FILE *stream) _PDCLIB_nothrow;870#endif871872#if _PDCLIB_EXTENSIONS873int _vcbprintf(874void *p,875_PDCLIB_size_t ( *cb ) ( void *p, const char *buf, _PDCLIB_size_t size ),876const char *format,877_PDCLIB_va_list arg );878879int _cbprintf(880void *p,881size_t ( *cb ) ( void *p, const char *buf, size_t size ),882const char *format,883... );884885int fgetpos_unlocked( FILE * _PDCLIB_restrict stream, fpos_t * _PDCLIB_restrict pos ) _PDCLIB_nothrow;886int fsetpos_unlocked( FILE * stream, const fpos_t * pos ) _PDCLIB_nothrow;887long int ftell_unlocked( FILE * stream ) _PDCLIB_nothrow;888int fseek_unlocked( FILE * stream, long int offset, int whence ) _PDCLIB_nothrow;889void rewind_unlocked( FILE * stream ) _PDCLIB_nothrow;890891int puts_unlocked( const char * s ) _PDCLIB_nothrow;892int ungetc_unlocked( int c, FILE * stream ) _PDCLIB_nothrow;893894int printf_unlocked( const char * _PDCLIB_restrict format, ... ) _PDCLIB_nothrow;895int vprintf_unlocked( const char * _PDCLIB_restrict format, _PDCLIB_va_list arg ) _PDCLIB_nothrow;896int fprintf_unlocked( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict format, ... ) _PDCLIB_nothrow;897int vfprintf_unlocked( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict format, _PDCLIB_va_list arg ) _PDCLIB_nothrow;898int scanf_unlocked( const char * _PDCLIB_restrict format, ... ) _PDCLIB_nothrow;899int vscanf_unlocked( const char * _PDCLIB_restrict format, _PDCLIB_va_list arg ) _PDCLIB_nothrow;900int fscanf_unlocked( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict format, ... ) _PDCLIB_nothrow;901int vfscanf_unlocked( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict format, _PDCLIB_va_list arg ) _PDCLIB_nothrow;902903// Todo: remove prefix?904_PDCLIB_uint_fast64_t _PDCLIB_ftell64( FILE * stream ) _PDCLIB_nothrow;905_PDCLIB_uint_fast64_t _PDCLIB_ftell64_unlocked( FILE * stream ) _PDCLIB_nothrow;906#endif907908#ifdef __cplusplus909}910#endif911912#endif913914915