Embedded Xinu Operating System
An ongoing research project and educational operating system.
|
A simple C library supporting a number of standard functions. More...
Functions | |
int | abs (int j) |
int | atoi (const char *nptr) |
long | atol (const char *nptr) |
void | bzero (void *s, size_t n) |
int | _doprnt (const char *fmt, va_list ap, int(*putc_func)(int, int), int putc_arg) |
int | _doscan (const char *fmt, va_list ap, int(*getch)(int, int), int(*ungetch)(int, int), int arg1, int arg2) |
int | fgetc (int dev) |
char * | fgets (char *s, int n, int dev) |
int | fprintf (int dev, const char *format,...) |
int | fputc (int c, int dev) |
int | fputs (const char *s, int dev) |
void | free (void *ptr) |
int | fscanf (int dev, const char *format,...) |
long | labs (long j) |
void * | malloc (size_t size) |
void * | memchr (const void *s, int c, size_t n) |
int | memcmp (const void *s1, const void *s2, size_t n) |
void * | memcpy (void *dest, const void *src, size_t n) |
void * | memset (void *s, int c, size_t n) |
int | printf (const char *format,...) |
void | qsort (void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)) |
void | srand (unsigned int x) |
int | rand (void) |
int | sprintf (char *str, const char *format,...) |
int | sscanf (const char *str, const char *format,...) |
char * | strchr (const char *s, int c) |
int | strcmp (const char *s1, const char *s2) |
char * | strcpy (char *dest, const char *src) |
size_t | strlcpy (char *dest, const char *src, size_t destsize) |
size_t | strlen (const char *s) |
char * | strncat (char *dest, const char *src, size_t n) |
int | strncmp (const char *s1, const char *s2, size_t n) |
char * | strncpy (char *dest, const char *src, size_t n) |
size_t | strnlen (const char *s, size_t maxlen) |
char * | strrchr (const char *s, int c) |
char * | strstr (const char *haystack, const char *needle) |
A simple C library supporting a number of standard functions.
int _doprnt | ( | const char * | fmt, |
va_list | ap, | ||
int(*)(int, int) | putc_func, | ||
int | putc_arg | ||
) |
Write formatted output.
This is a simplified implementation, and not all standard conversion specifications are supported. A conversion specification (a sequence beginning with the ''
character) is divided into 5 parts, the first four of which are optional. The following list explains the features supported by this implementation, broken down by part of the conversion specification:
"-"
to specify left-justification"0"
to specify zero padding"*"
, indicating that the minimum field width is given as an int
variadic argument".PREC"
, where PREC
is a series of decimal digits that specifies the precision as a non-negative integer"*"
, indicating that the precision is given as an int
variadic argument"hh"
for signed char
or unsigned char
"h"
for signed short
or unsigned short
"l"
for signed long
or unsigned long
"\%d"
to format a signed integer in decimal (base 10)"\%b"
to format an unsigned integer in binary (base 2)"\%o"
to format an unsigned integer in octal (base 8)"\%u"
to format an unsigned integer in decimal (base 10)"\%x"
to format an unsigned integer in lower case hex (base 16)"\%X"
to format an unsigned integer in upper case hex (base 16)"\%c"
to format a single character"\%s"
to format a null-terminated string, or "(null)" for a NULL
pointer"\%\%"
to format a literal percent signIf a feature is not mentioned above, assume it is not supported.
fmt | Format string. |
ap | Variable-length list of values that will be formatted. |
putc_func | Character output function. It is passed two arguments; the first will be the character to output, and the second will be putc_arg . It is expected to return EOF on failure. |
putc_arg | Second argument to putc_func . |
EOF
on failure int _doscan | ( | const char * | fmt, |
va_list | ap, | ||
int(*)(int, int) | getch, | ||
int(*)(int, int) | ungetch, | ||
int | arg1, | ||
int | arg2 | ||
) |
Scan and recognize input according to a format.
This is a minimal implementation and not all standard features are supported. The supported conversion specifications include:
%s
to scan a whitespace-delimited string%o
to scan an octal number (%O
for long)%x
to scan a hexideminal number (%X
for long)%c
to scan a sequence of characters%
[ to scan a sequence of characters from the specified set%%
to match a literal percent sign%d
and %u
, are interpreted as specifying a decimal number.'l'
type modifier to parse a long int rather than a normal int'h'
type modifier to parse a short int rather than a normal int'*'
character to suppress assignmentIf a feature is not mentioned above, assume it is not supported.
fmt | format string for the scan |
ap | variable-length list of locations to store the scanned data |
getch | Function to get a character. It is expected to return the character as an unsigned char cast to an int on success, or EOF on end-of-file or read error. |
ungetch | Function to unget a character. Unlike the standard ungetc(), this is not passed in an explicit character but instead uses the last read character, which will then be expected to be returned on the next getch() call. Only one character of putback needs to be supported. |
arg1 | First argument to getch and ungetch . |
arg2 | Second argument to getch and ungetch . |
int abs | ( | int | j | ) |
Calculates the absolute value of an integer.
j | The integer to get the absolute value of. |
j
. As a special case, if j
is INT_MIN
, the return value will be undefined because -INT_MIN
cannot be represented in an int
. int atoi | ( | const char * | nptr | ) |
Converts the initial portion of an ASCII null-terminated string into an integer. Leading whitespace is skipped and an optional +
or -
character is allowed to indicate sign. Parsing otherwise stops once the first nondigit character is encountered. If no digits have been encountered at that point, 0 shall be returned.
nptr | Pointer to the string to convert. |
long atol | ( | const char * | nptr | ) |
Converts the initial portion of an ASCII null-terminated string into a long integer. Leading whitespace is skipped and an optional +
or -
character is allowed to indicate sign. Parsing otherwise stops once the first nondigit character is encountered. If no digits have been encountered at that point, 0 shall be returned.
nptr | Pointer to the string to convert. |
void bzero | ( | void * | s, |
size_t | n | ||
) |
Zeroes a block of memory.
s | Pointer to the start of the block of memory to zero. |
n | Length of the block of memory, in bytes. |
int fgetc | ( | int | dev | ) |
Reads a character from a device.
dev | Index of device from which to read the character. |
unsigned char
cast to an int
. On read error, invalid device, or end-of file, returns EOF
. char* fgets | ( | char * | s, |
int | n, | ||
int | dev | ||
) |
Reads a newline-terminated string from a device.
s | The buffer in which to place the resulting data. |
n | One less than the maximum number of characters to read. |
dev | The index of the device from which to read. |
s
. On EOF
before any characters were read, read error, or invalid device, returns NULL
. On success, the resulting buffer will contain a null-terminated string that is the read data up until the first of the next newline, the end of the buffer, or the point at which EOF
was reached on the device. int fprintf | ( | int | dev, |
const char * | format, | ||
... | |||
) |
Writes a formatted message to the specified device.
dev | Index of the device to write to. |
format | The format string. Not all standard conversion specifications are supported by this implementation. See _doprnt() for a description of supported conversion specifications. |
... | Arguments matching those in the format string. |
int fputc | ( | int | c, |
int | dev | ||
) |
Writes one character to a device.
c | The character to write. |
dev | Index of the device to which to write the character. |
unsigned char
cast to an int
. On write error or invalid device, returns EOF
. int fputs | ( | const char * | s, |
int | dev | ||
) |
Writes a null-terminated string to a device.
s | The null terminated string to write. |
dev | The device to write the string to. |
EOF
on error. void free | ( | void * | ptr | ) |
Attempt to free a block of memory based on malloc() accounting information stored in preceding two words.
ptr | A pointer to the memory block to free. |
int fscanf | ( | int | dev, |
const char * | format, | ||
... | |||
) |
Scan input from a device according to the specified format string.
dev | Index of the device from which to scan input. |
format | Format string. Not all standard format specifiers are supported by this implementation. See _doscan() for a description of supported conversion specifications. |
... | Additional arguments that match those specified in the format string. Generally these need to be a pointer to the corresponding argument so that the value can be set; for example, a %d conversion specifier needs to be matched with an int * . |
long labs | ( | long | j | ) |
Calculates the absolute value of a long integer.
j | The long integer to get the absolute value of. |
j
. As a special case, if j
is LONG_MIN
, the return value will be undefined because -LONG_MIN
cannot be represented in a long
. void* malloc | ( | size_t | size | ) |
Request heap storage, record accounting information, returning pointer to assigned memory region.
size | number of bytes requested |
NULL
on failure void* memchr | ( | const void * | s, |
int | c, | ||
size_t | n | ||
) |
Returns a pointer to the first location in a region of memory at which a particular byte appears.
s | A pointer to the memory region to search. |
c | The byte to locate. |
n | The maximum number of bytes to search. |
c
in the memory region, or NULL
if c
was not found in the memory region. int memcmp | ( | const void * | s1, |
const void * | s2, | ||
size_t | n | ||
) |
Compares two memory regions of a specified length.
s1 | Pointer to the first memory location. |
s2 | Pointer to the second memory location. |
n | Length, in bytes, to compare. |
s1
region of memory is less than, equal to, or greater than the s2
region of memory, respectively. void* memcpy | ( | void * | dest, |
const void * | src, | ||
size_t | n | ||
) |
Copy the specified number of bytes of memory to another location. The memory locations must not overlap.
dest | Pointer to the destination memory. |
src | Pointer to the source memory. |
n | The amount of data (in bytes) to copy. |
dest
void* memset | ( | void * | s, |
int | c, | ||
size_t | n | ||
) |
Fills a region of memory with a byte.
s | pointer to the memory to place byte into |
c | byte to place |
n | length of region to fill, in bytes |
s
int printf | ( | const char * | format, |
... | |||
) |
Print a formatted message to standard output.
format | The format string. Not all standard format specifiers are supported by this implementation. See _doprnt() for a description of supported conversion specifications. |
... | Arguments matching those in the format string. |
void qsort | ( | void * | base, |
size_t | nmemb, | ||
size_t | size, | ||
int(*)(const void *, const void *) | compar | ||
) |
Sorts an array of data using quicksort. The average-case running time is O(n log n) but the worst case running time is O(n^2).
base | Pointer to the array of data to sort. |
nmemb | Number of elements in the array. |
size | Size of each element in the array, in bytes. |
compar | Comparison callback function that is passed pointers to two elements in the array. It must return a negative value, 0, or a positive value if the first element is less than, equal to, or greater than the second element, respectively. |
int rand | ( | void | ) |
Generates a pseudorandom integer in the range [0, RAND_MAX]. Beware: this is not re-entrant.
int sprintf | ( | char * | str, |
const char * | format, | ||
... | |||
) |
Write a formatted message to a null-terminated string.
str | The output string, which must be large enough to store the formatted result, including the null terminator. |
format | The format string. Not all standard conversion specifications are supported by this implementation. See _doprnt() for a description of supported conversion specifications. |
... | Arguments matching those in the format string. |
void srand | ( | unsigned int | x | ) |
Sets the random seed that will be used in future calls to rand().
x | the random seed to set |
int sscanf | ( | const char * | str, |
const char * | format, | ||
... | |||
) |
Scan values from a string according to the specified format.
str | String from which to read input. |
format | Format string. Not all standard format specifiers are supported by this implementation. See _doscan() for a description of supported conversion specifications. |
... | Additional arguments that match those specified in the format string. Generally these need to be a pointer to the corresponding argument so that the value can be set; for example, a %d conversion specifier needs to be matched with an int * . |
char* strchr | ( | const char * | s, |
int | c | ||
) |
Returns a pointer to the first location in a null-terminated string at which a particular character appears.
s | The string to search. |
c | The character to locate. |
NULL
if the character was not found. int strcmp | ( | const char * | s1, |
const char * | s2 | ||
) |
Compare two null-terminated strings.
s1 | Pointer to the first string. |
s2 | Pointer to the second string. |
s1
string is less than, equal to, or greater than the s2
string, respectively. char* strcpy | ( | char * | dest, |
const char * | src | ||
) |
Copies a a null-terminated string to the specified location. The source and destination strings may not overlap.
dest | Pointer to the memory to which to copy the string. It must have room for at least the number of characters in the src string, including the null terminator. |
src | Pointer to the string to copy. |
dest
. size_t strlcpy | ( | char * | dest, |
const char * | src, | ||
size_t | destsize | ||
) |
Copy a null-terminated string into a fixed-size buffer. Unlike strncpy(), this will perform truncation as expected, and the resulting destination string is guaranteed to be null-terminated (unless destsize
was 0).
dest | Pointer to the buffer into which to copy the string. |
src | Pointer to the null-terminated string to copy. |
destsize | Size of the dest buffer. |
src
string. This can be tested to detect whether truncation was performed. size_t strlen | ( | const char * | s | ) |
Calculates the length of a null-terminated string.
s | String to calculate the length of. |
char* strncat | ( | char * | dest, |
const char * | src, | ||
size_t | n | ||
) |
Concatenate at most the specified number of characters from a possibly null-terminated string to another null-terminated string, always null-terminating the result. The strings may not overlap.
dest | Pointer to the null-terminated string to which to concatenate the additional string or characters. |
src | Pointer to the source string. |
n | Maximum number of bytes of the src string to concatenate, excluding the null terminator that will be appended. |
dest
int strncmp | ( | const char * | s1, |
const char * | s2, | ||
size_t | n | ||
) |
Compare two null-terminated strings, examining at most the specified number of bytes.
s1 | Pointer to the first string. |
s2 | Pointer to the second string. |
n | Maximum number of bytes to compare before returning 0. |
s1
string is less than, equal to, or greater than the s2
string, respectively. char* strncpy | ( | char * | dest, |
const char * | src, | ||
size_t | n | ||
) |
Copies at most n
bytes of a string to a new location. The source string may or may not be null-terminated, but if it is and the null terminator is present in the first n
bytes, then copying will stop early when it is reached. If less than n
bytes were copied, then the remaining bytes in the destination will be set to null characters until n
bytes total have been written.
Beware: if the source string does not contain a null terminator in its first n
bytes, the destination will not be null-terminated. To correctly write a null-terminated string into a fixed-size buffer with truncation as expected, consider using strlcpy() instead.
dest | Pointer to the memory to which to copy the string. |
src | Pointer to the string to copy. |
n | Number of bytes to fill (see above description). |
dest
. size_t strnlen | ( | const char * | s, |
size_t | maxlen | ||
) |
Returns the length of a null-terminated string, but not more than the specified length.
s | String to calculate the length of. |
maxlen | Maximum length to return. |
maxlen
if the string is longer than maxlen
bytes. char* strrchr | ( | const char * | s, |
int | c | ||
) |
Returns a pointer to the last location in a null-terminated string at which a particular character appears.
s | The string to search. |
c | The character to locate. |
NULL
if the character was not found. char* strstr | ( | const char * | haystack, |
const char * | needle | ||
) |
Returns a pointer to the first location in a null-terminated string at which a particular substring appears.
haystack | The string to search. |
needle | The string to locate. |
NULL
if the string was not found.