Xdlibs.a and extended.lib are the two standard libraries of SOZOBON C. There are some libraries like float.a aesfast.a, etc. with own docs. XDLIBS standard library for SOZOBON C, based on dlibs by Dale Schumacher. Some special multitasking related functions will be implemented next months according to MiNT docs. A checking for MiNT's presence will be done there. contents: 1. HEADER FILES 2. STDLIB 2.1 PROCESS CONTROL 2.2 MEMORY MANAGEMENT 2.3 TYPE CONVERSION 2.4 MISCELLANEOUS FUNCTIONS 2.5 STANDARD INTERNALS 3. STDIO 3.1 FILE TO STREAM OPERATIONS 3.2 PROCESS COMMUNICATION 3.3 FORMATTED I/O 3.4 FORMATTING I/O 3.5 CHARACTER I/O 3.6 I/O of non char OBJECTS 3.7 POSITIONING in STREAMS 3.8 STD I/O ERROR HANDLING 3.9 UTILITIES 3.10 EXTENSIONS AND INTERNALS 4. CHARACTER TYPE FUNCTIONS 5. STRING FUNCTIONS 5.1 STRING LIB 5.2 FORMATTING/TYPE CONVERSION 6. ASSERT AND ERRORHANDLING 7. VARIABLE ARGUMENT LISTS 8. GLOBAL JUMPS 9. DATE/TIME FUNCTIONS 10. UNISTD, STAT AND FCNTL 10.1 FILE HANDLING 10.2 INPUT/OUTPUT FUNCTIONS 10.3 PROCESS CONTROL 10.4 CREATE CHILD PROCESSES 11. MISCELLANEOUS FUNCTIONS 11.1 MALLOC 11.2 SEARCHING AND SORTING 12. TOS 12.1 TIMER 12.2 PROCESS CONTROL 13. DEVICE I/O ----------------- 1. HEADER FILES The first two files are included by stdlib.h and stdio.h. Thy contain basic typedefs and macros. stddef.h This is the root header file which should be included in all programs. It defines FALSE, TRUE, NULL, size_t, ptrdiff_t and the offsetof() macro. The expected version number of the XDLIBS library is defined in macro _XDLIBS. To test write a program containing something like: assert( _xdlibs < _XDLIBS) types.h This file contains typedefs for many special and/or non-standard type names. Many of the typedefs in this file define type names which are ANSI and/or System V standard. This file is included by several other header files, and includes . The next ones are standard header files, to find in every standard C-system. stdlib.h This file is defines the dynamic memory managment functions, malloc(), et. al., string to number functions, environment access and basic process control functions. It should be present in every source code you write. It includes types.h. stdio.h This header file should be present in nearly all C programs. It contains defines the FILE struct, EOF and contains extern declarations for standard i/o and other commonly used functions. ~ For convenience, the constants TRUE, FALSE and ERROR are also ~ defined in this file, although this is somewhat non-standard. string.h This file defines aliases for string function names (since some string functions have various names on different systems) and extern declarations for all string functions. The number to string functions are there to find, too. ctype.h The isxxxx() macros and toxxxx() macros are define in this file. errno.h This file defines the error code constants, the errno variable and related error handling functions. limits.h Various maximum and minimum values are defined by this file. Among these are PATHSIZE and MAXINT. time.h This file defines time related constants and structures, and extern declarations for the time functions. assert.h This header defines the assert() run-time condition checking macro. stdarg.h This header defines the type and macros needed for variable argument list processing. setjmp.h This file defines the buffer needed to save your context when using setjmp()/longjmp() or catch()/throw(). Now follow some non standard header files. They are provided for compatibility and easy ports. macros.h This file contains a few useful macros including min(), max(), swap() and other. unistd.h The standard operating system calls in C are the POSIX system calls. This file contains function declarations and macros for translating this standardized functions to Atari TOS functions. fcntl.h This file defines the O_xxxx constants used by open(). stat.h The struct stat and the file mode flag constants are defined in this file. Some extensions to standard libraries and direct TOS access is to find in next files: malloc.h Some functions dealing with memory block of malloc(), alloca() for allocating memory from stack, ... . atari.h For direct TOS access include this file. It will include the following ones and errno.h. Some OS structures are declarated here. osbind.h Mnemonics for bios(), xbios() and gemdos() calls are defined in this file. basepage.h The BASEPAGE struct and the _base variable, which is initialized to point to the current process basepage, are defined in this file. osheader.h The TOS header struct is in here. 2. STDLIB The standard library. Include stdlib.h for access to these functions. int _xdlibs; This is the version number of your library 'xdlibs.a'. Highbyte is version, lowbyte is revision. 2.1 PROCESS CONTROL: global variables: generally no write access at runtime! some are from startup codes, others are to find in the libraries ATARI specific: int _app is TRUE if its an application, and FALSE for accessories int _mint If it is 0 MiNT is not running. If -1 you will have to test this, any other value is the active's MiNT version number. Using a '*_s' startup code will always include a MiNT test, it is done in _main() function. With other startup codes either test with 'void GetMiNT()' if (__mint == -1) GetMiNT(); or like this if (__mint == -1) { long val; if (Getcookie('MiNT', &val)) __mint = (int)val; else __mint = 0; } Now you can use the flag. if (__mint) { } void GetMiNT() is a dummy function with the standard stdio startup codes ('*_s.o'), and never called, in the above example. BASEPAGE *_base pointer to process' basepage char *_envp pointer to begin of current environment; it may be changed by putenv() int errno holds the error number of last error occured in a library function (from stdlib.h, stdio.h unidstd.h) char *etext; char *edata; char *end; These variables point to the first byte beyond the end of the text, data and BSS segments respectively. They are provided for compatibility. long _STKSIZ = 4096L; This variable defines the amount of run-time stack space to be reserved. The default value is 4K, which is more than enough for most applications. Since dynamic memory is NOT allocated from the stack, this value need only be large enough to handle local variables and deep recursion. If you will need a larger stack put it in your source; if not, forget about it. int main(int argc, char *argv[]) or: int main(int argc, char *argv[], char *envp) This function is not actually in the standard libraries, but must be present somewhere in the user's code. The parameters passed to it by _main() are the number of arguments in the command line, a pointer to a list of pointers to arguments, and a pointer to the initial environment string. The return value from main() is passed, by _main(), to exit(). Therefore, you should always return() from main(). You may call exit() directly in any function, too. A return value of 0 means okay, a positiv value up to 127 means something happened. Negative values are reserved for OS errors and signals. void exit(int status) Normal program termination. Calls all atexit functions (first the last stored), flushes and closes all open streams. Returns value to the operating system. A return value of 0 means okay, a positiv value up to 127 means something happened. Negative values are reserved for OS errors and signals. void abort() Prints the message "Abnormal program termination" to stderr and calls _exit() with a status code of 3. int atexit( void (*fnc)() ) Stores fnc() so that this function is called by exit or if main() returns. If it returns not 0 the function couldn't be stored (there is space for 32 function pointers reserved). char *getenv(char *var) Search for in the environment. If is found, a pointer to it's value is returned. NULL is returned if is not found. WARNING: The returned pointer points into the environment and must not be modified! The returned string maybe empty! int putenv(char *entry) Add to the environment. can be any of the following forms: = = The first form removes from the environment. getenv() will return NULL if looking for this variable. The second form adds to the environment, with a null value. getenv() will return a pointer to a '\0' character if looking for this variable. Many environment handlers don't support such "tag variables", so their use is not recommended. The final form is the most common. is installed (or replaced) with the value . It should be noted that the putenv() function itself is not supported in many systems and therefore may not be portable. This function uses malloc() if there is not enough space in the last allocated environment block. It returns TRUE for success or FALSE for failure. Failure maybe ENSMEM or you tried to remove a nonexisting variable. int system(char *command) If is NULL it returns TRUE if a shell is to find, if not FALSE. If command has a value it attempts to pass to the sytem, this is the standard shell, known from environment. If MiNT is not present and "_shell_p" is valid the program pointed to by the system variable "_shell_p" is used. If a valid shell can't be found there, the "SHELL" environment variable is searched for. If it exists and is not empty, it will be the name of the shell program to execute the given command. The string is parsed to arguments using usual quote chars (', `, "). If "SHELL" is not valid, it should return ERROR, but for backword compatibility It doesn't: The "PATH" variable is used as a list of directories to search for the program name which is the first token of the command. The extensions tried (if none is specified) with MiNT running are ".TTP", ".TOS"; else all usuall extensions are tested. Of course it uses the ARGV scheme, which is ATARI's standard convention for calling programs. 2.2 MEMORY MANAGEMENT: long _BLKSIZ = 65536; This variable controls the granularity of system memory allocation used by malloc(), et. al. This is the amount of memory that is requested from the system each time a new "heap" is needed to fill dynamic memory requests. To help avoid a GEMDOS bug, only 16 heaps can be active at a time. Therefore, (16 * _BLKSIZ) defines the maximum amount of memory which can be managed by these routines under normal circumstances. The exception is if any single request for memory exceeds _BLKSIZ. In this case, a heap will be allocated larger than _BLKSIZ. (This will change next time.) Note that _BLKSIZ should not be too small (8K is the minimum if Memory Protection with the 030's PMMU is active under MiNT), to avoid too many blocks, and not too large, cause this process will waste memory, which is not available for other processes. void *malloc(size_t size) Allocate at least bytes of memory. A pointer to the requested block is returned, or NULL if there is not enough free memory available. void *calloc(unsigned int n, size_t size) Allocate space for an array of element of bytes each. If the storage can be allocated, it is initialized to all zero. NULL is returned is there is not enough free memory. void *lalloc(long size) Allocate at least bytes of memory. A pointer to the requested block is returned, or NULL if there is not enough free memory available. void *realloc(char *addr, size_t size) Attempt to change the memory block at to the new . Making a block smaller will always work, but making it larger may fail if there is not enough free memory. If there is not enough memory, NULL is returned and the block will still reside at . If realloc() succeeds, a pointer to the (possibly moved) new block will be returned. If is NULL, realloc() will simply act like malloc(). If is 0, and is not NULL, realloc() will work like free(). void free(void *addr) Release the memory block at back into the free memory pool. WARNING: Unlike some implementations, you may not realloc() a block which has been free()ed. 2.3 TYPE CONVERSION: long atol(char *number) Convert the string to a long value. Leading whitespace is ignored, a leading +/- is optional. Characters are processed until a non-digit is reached. Return value is undefined in an overflow situation. int atoi(char *number) Convert the string to an int value. Leading whitespace is ignored, a leading +/- is optional. Characters are processed until a non-digit is reached. Return value is undefined in an overflow situation. long strtol(char *number, char **nptr, int base) Convert the string to a long value of base . Bases from 0 to 36 are allowed. Leading whitespace is ignored, and a leading +/- is optional. If the is 0, a leading '0' indicates base 8 and a leading "0x" or "0X" indicates base 16. Characters are processed until a character is found which is not in the specified base. If is non-NULL, it will be set to point to the character which terminated the translation in . Return value is undefined in an overflow situation. unsigned long strtoul(char *number, char **nptr, int base) Convert the string to an unsigned long value of base . Bases from 0 to 36 are allowed. Leading whitespace is ignored. If the is 0, a leading '0' indicates base 8 and a leading "0x" or "0X" indicates base 16. Characters are processed until a character is found which is not in the specified base. If is non-NULL, it will be set to point to the character which terminated the translation in . Return value is undefined in an overflow situation. 2.4 MISCELLANEOUS FUNCTIONS: int rand() Return a pseudo random number in the range 0..32767. This function uses the system random number generator, but grabs it's value out of the middle to avoid the exactly 50% behavior of the lowest order bit. Source code is also provided, though commented out, showing how to generate your own random number sequence if the system random numbers aren't sufficient or for porting these routines to another machine. void srand(unsigned int seed) Seed the random number generator. This function is #defined as a comment, since no seeding is possible for this implementation of rand(). MACRO abs(x) Return the absolute value of . This macro evalutes it's argument twice. ((x)<0?(-(x)):(x)) MACRO labs(x) see abs(); void *bsearch(char *key, char *base, int num, int size, int (*cmp)()) Perform a binary search for on the sorted data at . , and are like the corresponding parameters to qsort(). A pointer to the matching element is returned for success, or NULL for failure. The global variable "_bsearch" will contain the index of either the matching element, or the index of the element that the value should be inserted after. The use of "_bsearch" is not supported by most implementations of bsearch(). void qsort(char *base, int num, int size, int (*cmp)()) Perform a recursive quick-sort on an array starting at containing elements of bytes each. The function pointed to by is used to compare elements. Pointers to two items in the array are passed to the function, which must return a number representing their relationship as follows: negative item1 < item2 0 item1 == item2 positive item1 > item2 This routine is optimized to avoid N*N sort times for ordered data. In fact, performance on sorted or reverse-sorted data is actually "best case" with this algorithm, rather than "worst case" as with most qsort() implementations. Almost standard is this function, still not tested - jerry -. declarated in unistd.h int getopt(int argc, char **argv, char *optstring) This function eases the processing of the command line. Each call returns a character from , with optarg set to a parameter if one is required; or a '?' indicating that an invalid option was found; or EOF indicating that all options have been processed. The and parameters are (of course) the argc and argv values passed to main(). The is a string of option characters. If an option takes a parameter, it is followed by a ':' in , and the char *optarg variable (global) will be set to point to the parameter string from the command line. For example, "bno:v" defines the valid option characters as 'b', 'n', 'o' and 'v', and 'o' takes a parameter. All options must be preceeded (in the command line) by a '-' character. A single '-' character is taken to indicate stdin as a file, and terminates the argument processing. A "--" string indicated the end of options, and is skipped over. When option processing is successfully completed, the global variable optind will contain the index into argv[] of the next argument to be processed. Subsequent arguments should be processed with a loop like this: while(optind < argc) process(argv[optind++]); If an error occurs during argument process, an error message is written to stderr and '?' is returned by getopt(). Your program should then give a usage message and abort. If the global variable opterr is set to zero, no error message will be given, but '?' will still be returned. Note that command lines accepted by getopt() are quite flexible. The command lines "-b -v -o filename -- - file", "-vbofilename - file", and "-ofilename -bv - file" all will return the 'b', 'v', and 'o' options with the parameter to 'o' set to "filename" and leave the arguments "-" and "file2" for further processing. Please examine the sample program "echo.c" for an example of getopt() usage. 2.5 STANDARD INTERNALS Here follows the documentation of some internal xdlibs functions. You shouldn't use them at all in you programs!! They are called and linked automatically. void _initargs(char *cmdline, int cmdlen) Process the command arguments, either parsing the command line or retrieving the ARGV environment strings. It understands the new ARGV scheme of ATARI including empty arguments and quoted arguments. Never call this function. It manipulates heap/stack area. This function is called from the startup module very early on. Values to be passed to the user's main() function are stored in the global variables "_argc" and "_argv". The startup module initializes these variables to indicate that no arguments are available, and sets "_envp" from the value in the basepage. If a program doesn't use arguments, use the 'min_s.o' startup code. to make the program smaller. int _main() This function defines the standard streams, check to see which of them are devices, and calls the user's main() function. It sets up some other values, like _mint flag and _starttime (for clock()). The startup module calls this function to start the C program. The following standard streams are initialized by _main(): stdin Standard input, may be redirected stdout Standard output, may be redirected stderr Usually the system console's device (environment STDERR is checked, so it maybe file handle 2) Atari special: stdprn The standard printer (output only) (device file prn:) stdaux The communication port (input/output) (device file aux:) The global variables "_argc", "_argv" and "_envp" are used to store the values to be passed to the user's main(). If main() returns, exit() is called, with the return value as status. If a program doesn't use ANY standard I/O, you can use an other startup code, which doesn't call _main() function. But you won't get arguments, if you do so! If you want to get arguments passed, use the _main() function from 'minimum.h'. If you don't need argv[0] and expect only less arguments you can use the getcmdln() function from minimum.h. void _exit(int status) Exits the program immediately, returning to the OS. 3. STDIO Standard input/ouput library. Include stdio.h to use this functions. Portability is guaranteed. 3.1 FILE to STREAM OPERATIONS FILE *fopen(char *filename, char *mode) Open as a stream file. This is the normal open way to open a file. The is a string specifying the mode(s) that are relevent to the file open. Valid characters are: r read mode w write mode a append mode b binary mode t text (translated) mode At least one of "r", "w" or "a" must be specified. "t" is assumed and indicates that is translated to on output and vica-versa on input. If the stream is a character device, the translation is slightly different. The output translation is the same, but on input and both become in all cases. The "b", for binary mode, overides "t" and indicated that characters are not translated during i/o operations. "a" represents append mode and means that the file pointer will initially be placed at the end of the file. "w" mode will create a file if it doesn't exists, or zero an existing file. If "r" is the only mode specified, the file must already exist. A (FILE *) is returned if the open succeeds, or NULL if it fails. FILE *freopen(char *filename, char *mode, FILE *fp) Closes the file associated with and opens the new file as with fopen(), except that a new FILE structure is not created. The existing FILE structure pointed to by is re-initialized with the new file information. This is typically used to redirect i/o to standard streams stdin, stdout, stderr, stdprn, stdaux. is returned for success, or NULL for failure. int fflush(FILE *fp) Flush the file i/o buffer of the stream . The buffer is automatically flushed when it is full, the stream is closed, or the program terminates through exit(). This function has no effect if the stream in unbuffered. Call this function before switching between reading and writing on a stream which is opened for both. int fclose(FILE *fp) Close the stream , flushing the buffer. Returns 0 on success. int remove(char *filename) Delete , if it exists. Return 0 for success, or a negative error code. int rename(char *oldname, char *newname) Change the name of file to . You may use this function to move files from one directory (pathname) to another, but not from one drive to another. Return 0 for success, or a negative error code. FILE *tmpfile() sorry - missing function available soon. char *tmpnam(char *s) Generate a unique filename to be used for a temporary file. The filename will have the form "TEMP$nnn.TMP" where "nnn" is a sequence of numeric digits. The name will unique in the current working directory. If is NULL, space will be obtained from malloc() to store the filename and thus must be free()'d by the caller. If is not NULL, the filename will be copied into the buffer provided. This function returns NULL for failure, or a pointer to the filename for success. void setbuf(FILE *fp, char *buf) If is NULL, make unbuffered; else points to a buffer of BUFSIZ characters to be used as the stream buffer for . void setvbuf(FILE *fp, char *buf, int bmode, int size) If is NULL or is _IONBF, make unbuffered; otherwise points to a buffer of characters to be used as the stream buffer for . The variable indicates the type of buffering desired, as follows: _IONBF No buffering _IOFBF Full buffering (normal) _IOLBF Line buffering (not supported, same as _IOFBF) 3.2 PROCESS COMMUNICATION Not provided as standard but it is some kind of standard on multitasking systems. So I will implement this soon (MiNT). FILE *popen(char *command, char *type, FILE *fp) still missing int pclose(FILE *fp) still missing 3.3 FORMATTED I/O int fprintf(FILE *fp, char *fmt[, arg1, ..., argN]) Formatted output to the stream . See the _printf() function for a description of the formatting string. int printf(char *fmt[, arg1, ..., argN]) Formatted output to the stdout stream. See the _printf() function for a description of the formatting string. int sprintf(char *buf, char *fmt[, arg1, ..., argN]) Formatted output to the string . See the _printf() function for a description of the formatting string. int vfprintf(FILE *fp, char *fmt, va_list ap) Formatted output to the stream with a variable argument list. See _printf() for formatting and va_start() for stdarg explaination. int vprintf(FILE *fp, va_list ap) Formatted output to the stdout stream with a variable argument list. See _printf() for formatting and va_start() for stdarg explaination. int vsprintf(char *buf, char *fmt, va_list ap) Formatted outout to the string with a variable argument list. See _printf() for formatting and va_start() for stdarg explaination. int fscanf(FILE *fp, char *fmt[, arg1, ..., argN]) Formatted input from the stream . See the _scanf() function for a description of the formatting string. int scanf(char *fmt[, arg1, ..., argN]) Formatted input from the stdin stream. See the _scanf() function for a description of the formatting string. int sscanf(char *buf, char *fmt[, arg1, ..., argN]) Formatted input from the string . See the _scanf() function for a description of the formatting string. 3.4 FORMATTING I/O: These functions are internal to XDLIBS. They document the formatting strings. int _printf(char *op, int (*put)(), char *fmt, int *args) This function does all the work for printf(), et al. Many systems don't provide direct access to this function (or it's equivalent), but it is useful for writing your own printf()-like functions. Since this is a non-standard interface, and v[sf]print() is now available, you should probably use the stdarg functions instead. points to a format control string. pointers to a list of arguments. The format string is used to create and output stream with the arguments. The function is used to output each character. The parameter is given to the function to specify the output stream. Calls to are of the form: "(*put)(c, op);" where is the character to output. The format string is composed of characters and format specifications. The '%' character introduces a format specifier. The general form of a format specifier is: %[-][ |+][0][|*][.[|*]][l]{d|i|u|o|x|p|b|c|s} The '-' specifies left justification. The ' ' or '+' specifies the character which preceeds positive numeric values. The '0' specifies that numeric fields will be padded with '0' rather than ' '. The field is a numeric value specifying a minimum field width. The field is a numeric value specifying the maximum number of data characters to display. If '*' is specified for the width or the precision, an "int" value is taken from the argument list and used for that value. If no width is specified, the field width varies according to the data width. If no precision is specified, all data characters are included in the data width. If the data width exceeds the field width, the field width will expand to allow all data characters to be printed. Including the 'l' or capitalizing the trailing character specifies that the associated value is a "long" type. The trailing character specifies the format type, as follows: d Signed decimal integer i same as 'd' u Unsigned decimal integer o Unsigned octal integer x Unsigned hexadecimal integer b Unsigned binary integer p Pointer (displayed in %06.8lX format) c Character s String If the character following the '%' is not recognized, it is simply passed along to the output stream, thus "%%" is used to print a single '%' character. int _scanf(char *ip, int (*get)(), int (*unget)(), char *fmt, char **args) This function does all the work for scanf(), et al. Many systems don't provide direct access to this function (or it's equivalent), but it is useful for writing your own scanf()-like functions. points to a format control string. pointers to a list of arguments, each of which is the address of a variable in which input data may be stored. The format string is used to control reading of characters from the function. As each character is needed is called in the form "c = (*get)(ip);" where is the character read (negative for errors) and is the auxiliary pointer specified by the parameter. If a character needs to be un-gotten, a call to of the form "(*unget)(c, ip);" is made. The format string is composed of characters and format specifications. Any characters in , except whitespace characters, which are not part of a format specifier are expected to be matched one-to-one by characters in the input stream. Scanning terminates if a mismatch occurs or if any call to results in an error. Whitespace characters match 0 or more whitespace characters in the input stream. The '%' character introduces a format specifier. The general form of a format specifier is: %[*][][l|h]{d|u|o|x|b|i|c|s} The '*' specifies that a field is to be scanned by not stored. No variable pointer should be provided for non-stored format specs. The field specifies that maximum number of characters to be process to fill the given format type. Less than characters will be processed if the field ends before characters have been processed. A field ends when either a whitespace character, or a character which does not fit the specified format, is read. The preceding 'l' (or capitalizing the conversion character) specifies that the associated variable is a "long" type. The trailing character specifies the format type, as follows: d Signed decimal integer u Unsigned decimal integer o Unsigned octal integer x Unsigned hexadecimal integer b Unsigned binary integer i Unsigned decimal/octal/hexadecimal/binary integer c Character s String If a is specified with the 'c' format, exactly characters (including whitespace) are read from the input stream, and written to a string. No '\0' character is added If the character following the '%' is not recognized, it is expected to match the input stream as a non-format character, thus "%%" is used to match a single '%' character. One additional conversion is the brace-format. Shown as "%[...]", the '...' represent a list of characters. If the first character in the list is a '^', the field contains any characters -not- in the list (starting with the 1st character after the '^'). If the first character of the list is not a '^', then the field will only contain those characters found in the list. A right brace character (']') can be included as one of the list of characters by placing it as the first character in the list. If the '^' negation character is the first character, the included brace should be the next character after the '^'. For maximum portability, a range should be explicitly given (a good example would be "%[0123456789]"), but to allow for porting from systems with smarter scanf functions, this version of scanf also supports ranges represented using a - form (eg: "%[0-9]"). To use the first-last form, the character must be lexically less than or equal to the character . If this rule is violated, or if the hyphen is the first or last character of the list, the hyphen will be assumed to be just another character in the list and no range expansion will be done. The resulting string containing the characters in (or not in) the list will be null terminated. It should be noted that, unlike most of the other formats, this conversion does allow the programmer to specify that whitespace characters will be included in the resulting string. int _tttty(FILE *fp) "Teeny Tiny TTY" driver function. This function is internal to dLibs, but it's name is documented to allow you to replace it with a tty driver of your own. It's operation can be best understood by reading and UNDERSTANDING the code in the routine provided. In brief, this function is supposed to read from , up to a newline character, putting the character in the FILE buffer, and return the number of characters read (similar to _fillbuf() in some systems). If the stream is in binary mode, a full buffer is to be read, with no translation. If the stream is unbuffered, characters are also untranslated, but ^C on input is checked for, however, the code which calls this function will translate carriage return characters into newlines and ^Z will cause EOF. Effectively, this means that line editing is not allowed if the stream in unbuffered, but most translation is done. Note that this mode of operation is the least likely to produce unix-like results, particularly in the way some control characters are echoed. It is recommended that either binary mode, or buffered and translated mode be used. 3.5 CHARACTER I/O: int fgetc(FILE *fp) Get a character from . Returns the character or EOF. char *fgets(char *data, int limit, FILE *fp) Get data from and puts it in the buffer. At most, -1 characters will be read. Input will also be terminated when a newline is read. will be '\0' terminated and the newline, if read, will be included. A pointer to the start of is returned, or NULL for EOF. int fputc(char c, FILE *fp) Put the character to the stream . void fputs(char *data, FILE *fp) Write the characters in to the stream . A newline WILL NOT be added. int getc(FILE *fp) Same as fgetc() but implemented as a macro. int getchar() Same as "fgetc(stdin)". char *gets(char *data) Get data from stdin and puts it in the buffer. Input is terminated when a newline is read. The newline will be replaced by a '\0' to terminate the string. A backspace character will remove the preceeding character from the buffer, but will not backspace past the start of the buffer. A pointer to the start of is returned, or NULL for EOF. int putc(char c, FILE *fp) Same as fputc() but implemented as a macro. int putchar(char c) Same as "fputc(c, stdin)". void puts(char *data) Write the characters in to stdout. A newline WILL be written after the data. int ungetc(char c, FILE *fp) Same as fungetc() but implemented as a macro. 3.6 I/O of non char OBJECTS int fread(char *data, int size, int count, FILE *fp) Read items of characters each from stream . Data is stored in the buffer pointed to by . The number of full items actually read is returned, or a negative error code. This call DOES NOT translate characters, even if the stream is opened in translate mode. int fwrite(char *data, int size, int count, FILE *fp) Write items of characters each to stream . Data is read from the buffer pointed to by . The number of full items actually written is returned, or a negative error code. This call DOES NOT translate characters, even if the stream is opened in translate mode. These functions should use size_t as provided by standard - instead of int values. This will be fixed soon. 3.7 POSITIONING in STREAMS int fseek(FILE *fp, long offset, int origin) Operates like lseek(), except it works on streams. Note that stream file positions may be misleading due to translation of characters during i/o. ftell() may be used reliably with fseek() to reposition a file to a prior location. WARNING: fseek() returns 0 for success, non-zero for failure, according to the ANSI standard. Some implementations use 0 for failure. This function is maintained for compatibility with old programs. fsetpos() should be used in new code. (cf: fsetpos) long ftell(FILE *fp) Operates like tell(), except it works on streams. Note that stream file positions may be misleading due to translation of characters during i/o. This function is maintained for compatibility with old programs. fsetpos() should be used in new code. (cf: fsetpos) void rewind(FILE *fp) Operates like fseek(fp, 0L, SEEK_SET), except it also clears the end-of-file and error flags for . There is no return value. int fgetpos(FILE *fp, fpos_t *pos) Get the position of the stream and store it at the location pointed to be . This is the new X3J11 function to replace ftell(). Returns 0 for success and ERROR for failure. int fsetpos(FILE *fp, fpos_t *pos) Set the position of the stream to the valued stored at the location pointed to be . Note that this function is only required to work properly for a value which was previously obtained by fgetpos() on the same stream. This is the new X3J11 function to replace fseek(). Returns 0 for success and ERROR for failure. 3.8 I/O ERROR HANDLING void clearerr(FILE *fp) Clear the error flag on . int feof(FILE *fp) Return non-zero if is at end of file. int ferror(FILE *fp) Return non-zero if and error has occurred on . void perror(char *msg) Write, to stream stderr, (if non-null and non-empty), followed by ": " and a system error messaged derived from the value of errno. 3.9 UTILITY functions dealing with PATHS and FILES. Not guaranteed but almost standard. I didn't test them explicitly, until now. -jerry- char *tempnam(char *dir, *pfx) This function is similar to tmpnam(), but is somewhat more flexible. specifies the directory part of the filename. If is NULL, the TMPDIR environment variable will be used. If TMPDIR is not found, the current directory is used. specifies the filename prefix (up to 5 characters). The form of the filename will be "dir\pfxnnn.TMP", similar to tmpnam(). The storage for the filename is always obtained from malloc(), so the caller must free() it after use. This function returns NULL for failure, or a pointer to the fully expanded filename for success. char *fullpath(char *full, *part) is a (possibly) ambiguous file/path specification. A non-ambiguous file/path spec is created which includes a drive letter and all intermediate sub-directories. If the partial specification is not valid, NULL is returned, otherwise a pointer to is returned. If NULL is specified for , an internal buffer is used and a pointer to it is returned. char *findfile(char *afn[, *ext]) Return full file spec for if found. If has no extension, extensions from are tried until a match is found, or the list ends. is a list of extensions separated by '\0' characters and ending with an additional '\0', ie. ".ttp\0.tos\0.prg\0" (note that the final null is added by the compiler to any string constant. If already has an extension, is not used. If no matching files are found, NULL is returned. The pointer returned when a match is found points to a buffer which is internal to fullpath(). If you want to save the value returned, you must make a copy before the buffer is overwritten by subsequent calls. Note: several dLibs functions call filefind(), so don't make too many assumptions about how long the internal buffer is going to stay valid. char *pfindfile(char *path, *afn[, *ext]) Like findfile() but search all directories (separated by ',' or ';') in . If is NULL, the "PATH" environment variable is used instead. If specifies a drive or directory, is not used. The internal buffer for findfile() is used by pfindfile(). The fowllowing ones are non standard. Taken from Dlibs without test and changes. char *wildcard(char *pathname) Return matches for a wildcard filename. If is not NULL, the first file which matches will be returned. The may contain wildcards only in the filename portion, not in any sub-directories. Subsequent calls to wildcard() with a NULL argument return the next matching filename. NULL is returned when no more files match. Note: the pointer returned points to an internal buffer which is overwritten with each call. It should not be modified, and should be copied into a safe place if you want to save the value. char *_splitpath(char *src, *drive, *path, *file, *ext) Parse the filename into component parts. Returns . If any of the component pointers is NULL, that component will be parsed, but not saved. If a given component does not exists int the string, the component will be empty, (ie.: ""). The component will be a drive letter followed by a colon, (ie.: "a:"). The component will be the subdirectory names leading up to the filename, but will not include a trailing '\' unless the path the simply the root path "\", and there will only be a leading '\' if the path is fully qualified, or "rooted", ie.: "\this\is\a\path\name". The component is the base filename without any extension, ie.: "filename". The component is the file extention with no leading '.', ie.: "txt". Recommended sizes for the components is Drive[4], Path[128], File[10], Ext[4]. char *_makepath(char *dst, *drive, *path, *file, *ext) Build the filename from component parts. Returns . This function is basically in inverse of _splitpath(), and will accept the components parsed by _splitpath() as input. It will also allow a little more flexibility in that it will treat any component which is a NULL pointer as an empty field, and the component may optionally have a trailing '\'. 3.10 EXTENSIONS AND INTERNALS The following functions are non standard but usefull sometimes. Some are more internal dLib's functions. int fileno(FILE *fp) Return the file handle associated with the stream . int fungetc(char c, FILE *fp) Push the character back to be gotten by the next fgetc() call on . Only 1 character may be ungotten at a time on each stream. Subsequent calls to fungetc() will write over the currently saved character. int ungetchar(char c) Same as "fungetc(c, stdin)". Note that this name will conflict with any function beginning "ungetch..." due to having only 7 significant characters in external identifiers. int getw(FILE *fp) Get a 2-byte value from the stream . The high-order byte is read first. Use feof() to test for end-of-file. int putw(int n, FILE *fp) Put the 2-byte value to the stream . The high-order byte is written first. int getl(FILE *fp) Get a 4-byte value from the stream . The high-order byte is read first. Use feof() to test for end-of-file. int putl(long n, FILE *fp) Put the 4-byte value to the stream . The high-order byte is written first. FILE *fdopen(int h, char *mode) Associates a stream with the already open file . The values are the same as for fopen(), but MUST be compatible with the mode in which was open()ed. This functions allows a file opened with the low level open()/creat() calls to be used as a buffered/translated stream. A pointer to a FILE struct is returned, or NULL for errors. ????????????????????????????? FILE *fopenp(char *filename, char *mode) Find somewhere on the PATH and open it with . Refer to the fopen() function for valid values. If you want to use a search path other than the PATH environment variable, use the pfindfile() function to locate the file, and pass that filename to fopen(). (cf: pfindfile, fopen) 4. CHARACTER TYPE FUNCTIONS: Standard character type functions. Include ctype.h to use them. The macros use a table from the xdlibs.a library To use the functions in this section, you must include in your source file. Please note that the isxxxx() functions, except isascii(), only have defined results if isascii() is true. (ie. they only work properly on values 0x00 through 0x7F) int toupper(int c) Convert to upper case, if alphabetic. This is implemeted as a macro and also as a function. You may force use of the function version rather than the macro (which evaluates its argument twice) by using the "#undef toupper" directive. int tolower(int c) Convert to lower case, if alphabetic. This is implemeted as a macro and also as a function. You may force use of the function version rather than the macro (which evaluates its argument twice) by using the "#undef tolower" directive. MACRO _toupper(int c) This macro should be used only if is known to be lower case. It converts to upper case. Results are undefined if converting a character which is not lower case. MACRO _tolower(int c) This macro should be used only if is known to be upper case. It converts to lower case. Results are undefined if converting a character which is not upper case. MACRO toascii(int c) Convert to 7-bit ascii, putting it into the range 0x00..0x7F. MACRO isalnum(int c) Return non-zero if is '0'..'9','A'..'Z','a'..'z'. MACRO isalpha(int c) Return non-zero if is 'A'..'Z','a'..'z'. MACRO isascii(int c) Return non-zero if is 0x00..0x7F. MACRO iscntrl(int c) Return non-zero if is 0x00..0x1F,0x7F. MACRO isdigit(int c) Return non-zero if is '0'..'9'. MACRO isgraph(int c) Return non-zero if is 0x21..0x7E. MACRO islower(int c) Return non-zero if is 'a'..'z'. MACRO isprint(int c) Return non-zero if is 0x20..0x7E. MACRO ispunct(int c) Return non-zero if is not iscntrl(), isalnum() or isspace(). MACRO isspace(int c) Return non-zero if is 0x09..0x0D,0x20. MACRO isupper(int c) Return non-zero if is 'A'..'Z'. MACRO isxdigit(int c) Return non-zero if is '0'..'9','A'..'F','a'..'f'. 5.STRING FUNCTIONS: String related functions. Some conversion functions are put in here too. Include string.h for using them. STRING MANIPULATION: int strlen(char *string) Returns the number of characters in a string, not including the terminating '\0'. char *strcpy(char *dest, char *source) Copies the string to the including the '\0'. A pointer to the start of is returned. char *strncpy(char *dest, char *source, int limit) Copies the string to the . At most, characters are copied. If ends before characters have been copied, the '\0' is copied, otherwise is not terminated by the copy. char *strpcpy(char *dest, char *start, char *stop) Copies characters from up to into . The character pointed to by is not copied, and MUST be in the same string as . The pointer is returned. char *strdup(char *string) Create a copy of and return a pointer to the copy. Storage for the copy is obtained from malloc(). char *strset(char *string, char c) Fill with up the the terminating '\0' of . char *strnset(char *string, char c, int n) Fill at most characters of with , up the the terminating '\0' of . char *substr(char *dest, char *source, int start, int end) Copy characters from to starting with character and ending with . A pointer to , which will be '\0' terminated, is returned. char *subnstr(char *dest, char *source, int start, int length) Copy characters from to starting with character . A pointer to , which will be '\0' terminated, is returned. char *strcat(char *dest, char *source) Concatenate on the end of . The terminator of will be overwritten by the first character of . The termintor from will be copied. A pointer to the modified is returned. char *strncat(char *dest, char *source, int limit) Concatenate characters from onto . If contains less than characters, the length of source is used for . The terminating '\0' is always added. A pointer to is returned. char *strupr(char *string) Convert all alphabetic characters in to upper case. char *strlwr(char *string) Convert all alphabetic characters in to lower case. char *strrev(char *string) Reverse the order of the characters in in place. int strcmp(char *str1, char *str2) Lexicographically compare the two strings. Return a value indicating the relationship between the strings. Possible return values are: negative str1 < str2 0 str1 == str2 positive str1 > str2 int strncmp(char *str1, char *str2, int limit) Compare strings as with strcmp(), but limit comparison to the characters. int stricmp(char *str1, char *str2) Compare strings as with strcmp(), but ignore the case of any alphabetic characters. int strnicmp(char *str1, char *str2, int limit) Compare strings as with strncmp(), but ignore the case of any alphabetic characters. char *strstr(char *string, char *pattern) Return a pointer to the first occurance of in . NULL is returned if is not found. char *stristr(char *string, char *pattern) Same as strstr(), but ignore the case of any alphabetic characters. char *strchr(char *string, char symbol) Return a pointer to the first occurance of in . NULL is returned if is not found. '\0' is included in the search. char *strrchr(char *string, char symbol) Return a pointer to the last occurance of in . NULL is returned if is not found. '\0' is included in the search. int strpos(char *string, char symbol) Return the index of the first occurance of in . -1 is returned if is not found. int strrpos(char *string, char symbol) Return the index of the last occurance of in . -1 is returned if is not found. int strspn(char *string, char *set) Return the length of the sub-string of that consists entirely of characters found in . The terminating '\0' in is not considered part of the match set. If the first character if is not in , 0 is returned. int strcspn(char *string, char *set) Return the length of the sub-string of that consists entirely of characters not found in . The terminating '\0' in is not considered part of the match set. If the first character if is in , 0 is returned. char *strpbrk(char *string, char *set) Return a pointer to the first occurance in of any character in . char *strrpbrk(char *string, char *set) Return a pointer to the last occurance in of any character in . char *strtok(char *string, char *delim) Return a token from . If is not NULL, it is the beginning of a string from which tokens are to be extracted. Characters found in are skipped over to find the start of a token, characters are then accumulated until a character in is found, or the terminator of is reached. A pointer to the '\0' terminated token is then returned. Note that this function modifies (by inserting '\0's) in the process. Subsequent calls to strtok() may specify NULL as the argument, in which case subsequent tokens are returned, or NULL if there are no more tokens. char *strtrim(char *string, char *junk) Remove leading and trailing characters found in from . Return a pointer to the modified . char *stradj(char *string, int dir) Adjust by adding space if is positive, or removing space if is negative. The magnitude of is the number of character positions to add or remove. Characters are added or removed at the beginning of . A pointer to the modified is returned. int strrpl(char *string, char *ptrn, char *rpl, int n) Replace at most occurances of in with . If is -1, replace all. Return the number of replacments. int strirpl(char *string, char *ptrn, char *rpl, int n) Same as strrpl() except ignore the case of alphabetic characters. char *memmove(char *dest, char *source, int len) Copies the block to the . bytes are always copied. No terminator is added to . A pointer to is returned. Overlap checking IS done. char *lmemmove(char *dest, char *source, long len) Same as memmove() except a long value is used for . char *memcpy(char *dest, char *source, int len) Copies the block to the . bytes are always copied. No terminator is added to . A pointer to is returned. Overlap checking IS NOT done. char *lmemcpy(char *dest, char *source, long len) Same as memcpy() except a long value is used for . char *memset(char *dest, char data, int len) Set bytes of to . A pointer to is returned. int memcmp(char *blk1, char *blk2, int len) Lexicographically compare the two blocks. Return a value indicating the relationship between the blocks. Possible return values are: negative blk1 < blk2 0 blk1 == blk2 positive blk1 > blk2 bytes are always compared. int memicmp(char *blk1, char *blk2, int len) Compare blocks as with memcmp(), but ignore the case of any alphabetic characters. char *memccpy(char *dst, char *src, char c, int cnt) Copy bytes from to until either bytes have been copied, or the character has been copied. If is found, a pointer to the character following in is returned, or NULL is reaches 0 before is found. char *memchr(char *buf, char c, int cnt) Search the first bytes of for . Returns a pointer to the matching character, or NULL if not found. char *bzero(char *buf, int cnt) Zero characters in . Returns . FORMATTING /TYPE CONVERSION char *ltoa(long n, char *buffer, int radix) Convert the long value to a string in using as the number base. If is negative, '-' will be the first character in . A pointer to is returned. char *ultoa(unsigned long n, char *buffer, int radix) Convert the unsigned long value to a string in using as the number base. A pointer to is returned. char *itoa(int n, char *buffer, int radix) Convert the integer value to a string in using as the number base. If is negative, '-' will be the first character in . A pointer to is returned. char *ctlcnv(char *string) Convert \ notation in to actual characters. This is useful for reading strings from a stream when you want to allow insertion of control character or other characters that may have special meaning otherwise, or may not otherwise be allowed. The following formats are supported: \n newline or linefeed \r carriage return \0 null character (value 0) \b backspace \t horizontal tab \v vertical tab \f form feed \a alarm (bell) \\ backslash \' single quote \" double quote \NNN octal constant \xNN hexadecimal constant \ "folded" line (both characters removed) A pointer to the modified is returned. 6. ASSERT AND ERRORHANDLING error handling: To use the assert macro include assert.h. MACRO assert(condition) If is not true at run-time, this macro causes an assert failure message to be written to stderr, displaying the line number and source file name, and aborts the program. If the symbol NDEBUG is #defined (usually with a -D option to cc), all assert() calls are disabled. long teststack() Non standard function, but helpful for debugging stack problems. It will return the number of bytes free stack. If the value is negative, the stack growing into the program's code. (extended.lib) ERROR HANDLING FUNCTIONS: extern int errno; This variable is set to zero when the program is loaded. It is not zeroed by any library functions, but may be set to a non-zero error number by many of them (particularly the standard i/o and system service functions). The meaning of this error number may be found in the symbolic #defines in , or by calling the seterror() functions as described below. (cf: seterror) void perror(char *msg) Write, to stderr, (if non-null and non-empty), followed by ": " and a system error messaged derived from the value of errno. (from stdio.h) char *strerror(errnum) Return the system error message for error . If is outside the range of valid error numbers, NULL is returned. (from string.h) non standard function: void perrorf(char *fmt[, arg1, ..., argN]) Write, to stderr, the name of the program, followed by ": ", followed by a message formatted as by printf() from and the optional arguments, followed by ": " and a system error message derived from the value of errno. This is a non-standard extended version of perror(). (cf: printf, perror) (from misc.h) 7. VARIABLE ARGUMENT LISTS: The macros in this section are defined in the header file. typedef ... va_list; This is the type for a variable argument list traversal variable. MACRO va_start(list, param) This macro initializes the va_list variable to begin traversing variable argument lists. is the last parameter in the function call before the variable arguments begin. This parameter MUST NOT be a register variable. MACRO va_arg(list, type) This macro retrieves a variable argument of type , updates the va_list variable , and returns the value of the retrieved argument. The should not be parenthesised. MACRO va_end(list) This macro must be called after all desired variable arguments have been retrieved, to reset the context of the va_list variable . 8. GLOBAL JUMPS: Some more process control. Include setjmp.h for using this functions. int setjmp(jmp_buf context) Save for longjmp(). You MUST include to use. Calling this function saves the current program context in the context buffer provided and returns zero. A later call to the longjmp() function will cause the context to be restored and your program will continue as if it just returned from setjmp(), but this time with the (non-zero) return value specified in the longjmp() call. THE SAVED CONTEXT WILL NOT BE VALID IF YOU EXIT THE FUNCTION THAT CALLED setjmp(). void longjmp(jmp_buf context, int rv) Return to the saved by setjmp(). (cf: setjmp) You MUST include to use. The following two are non-standard. int catch(jmp_buf context, int (*func)()) Execute with saved for throw(). You MUST include to use. Return the value returned by . The main advantage of these functions over catch/throw is the ability to return zero from the function executed, and the logically "cleaner" encapsulation of the non-local jump operation. These functions are patterned after similar functions in LISP. (cf: setjmp/longjmp) void throw(jump_buf context, int rv) Return to saved by catch(). You MUST include to use. 9. DATE/TIME FUNCTIONS: To use the functions in this section, you must include in your source file. time_t time(time_t *rawtime) Get the current system clock date/time value. Altough the value of this function is compatible with the ANSI proposed standard, on some systems (notably System V), this function returns the number of seconds elapsed since 00:00:00 GMT on Jan 1, 1970. This implementation returns an encoded date/time value instead. Therefore any programs which depend on this value being a number of seconds will not work properly. However, other functions in this section which make use of the raw time value returned by time() are implemented to be compatible with this encoding, and will work properly. In addition to returning the raw time value, if the pointer is not NULL, the value is stored in the time_t variable points to. char *ctime(time_t *rawtime) Convert to a string. A 26 character fixed field string is created from the raw time value. The following is an example of what this string might look like: "Wed Jul 08 18:43:07 1987\n\0" A 24-hour clock is used, and due to a limitation in the ST system clock value, only a resolution of 2 seconds is possible. A pointer to the formatted string, which is held in an internal buffer, is returned. struct tm *localtime(time_t *rawtime) Convert to fill time structure fields. A pointer to an internal structure is returned. Refer to for the values of the various structure fields. struct tm *gmtime(time_t *rawtime) Since there is not concept of "time zone" on the ST, this function returns NULL, as specified by the proposed ANSI standard. (will work soon) char *asctime(struct tm *time) Convert