Embedded Xinu Operating System
An ongoing research project and educational operating system.
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Groups Pages
Functions
C library

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)
 

Detailed Description

A simple C library supporting a number of standard functions.

Function Documentation

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:

  1. Flags: 0+ of the following:
    • "-" to specify left-justification
    • "0" to specify zero padding
  2. Minimum field width: 0-1 of the following:
    • A series of decimal digits not beginning with 0 that specifies the minimum field width as a non-negative integer
    • "*", indicating that the minimum field width is given as an int variadic argument
  3. Precision: 0-1 of the following:
    • ".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
  4. Length modifier for signed and unsigned integer conversions: 0-1 of the following:
    • "hh" for signed char or unsigned char
    • "h" for signed short or unsigned short
    • "l" for signed long or unsigned long
  5. Conversion specifier character: 1 of the following:
    • "\%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 sign

If a feature is not mentioned above, assume it is not supported.

Parameters
fmtFormat string.
apVariable-length list of values that will be formatted.
putc_funcCharacter 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_argSecond argument to putc_func.
Returns
number of characters written on success, or 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
  • All other conversion specifiers, including %d and %u, are interpreted as specifying a decimal number.
  • Whitespace to match any amount of whitespace in the input.
  • Maximum length modifier
  • '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 assignment

If a feature is not mentioned above, assume it is not supported.

Parameters
fmtformat string for the scan
apvariable-length list of locations to store the scanned data
getchFunction 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.
ungetchFunction 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.
arg1First argument to getch and ungetch.
arg2Second argument to getch and ungetch.
Returns
The number of items successfully matched and assigned, which may be less than the number of format specifiers in the case of a matching failure; alternatively, if EOF or a read error occurs before any matches, EOF is returned.
int abs ( int  j)

Calculates the absolute value of an integer.

Parameters
jThe integer to get the absolute value of.
Returns
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.

Parameters
nptrPointer to the string to convert.
Returns
The resulting integer.
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.

Parameters
nptrPointer to the string to convert.
Returns
The resulting long integer.
void bzero ( void *  s,
size_t  n 
)

Zeroes a block of memory.

Parameters
sPointer to the start of the block of memory to zero.
nLength of the block of memory, in bytes.
int fgetc ( int  dev)

Reads a character from a device.

Parameters
devIndex of device from which to read the character.
Returns
On success, returns the character read as an 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.

Parameters
sThe buffer in which to place the resulting data.
nOne less than the maximum number of characters to read.
devThe index of the device from which to read.
Returns
On success, returns a pointer to the buffer 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.

Parameters
devIndex of the device to write to.
formatThe 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.
Returns
On success, returns the number of characters written. On write error, returns a negative value.
int fputc ( int  c,
int  dev 
)

Writes one character to a device.

Parameters
cThe character to write.
devIndex of the device to which to write the character.
Returns
On success, returns the character written as an 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.

Parameters
sThe null terminated string to write.
devThe device to write the string to.
Returns
A non-negative number on success, or EOF on error.
void free ( void *  ptr)

Attempt to free a block of memory based on malloc() accounting information stored in preceding two words.

Parameters
ptrA 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.

Parameters
devIndex of the device from which to scan input.
formatFormat 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 *.
Returns
The number of items successfully matched and assigned.
long labs ( long  j)

Calculates the absolute value of a long integer.

Parameters
jThe long integer to get the absolute value of.
Returns
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.

Parameters
sizenumber of bytes requested
Returns
pointer to region on success, or 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.

Parameters
sA pointer to the memory region to search.
cThe byte to locate.
nThe maximum number of bytes to search.
Returns
A pointer to the first occurrence of 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.

Parameters
s1Pointer to the first memory location.
s2Pointer to the second memory location.
nLength, in bytes, to compare.
Returns
A negative value, 0, or a positive value if the 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.

Parameters
destPointer to the destination memory.
srcPointer to the source memory.
nThe amount of data (in bytes) to copy.
Returns
dest
void* memset ( void *  s,
int  c,
size_t  n 
)

Fills a region of memory with a byte.

Parameters
spointer to the memory to place byte into
cbyte to place
nlength of region to fill, in bytes
Returns
s
int printf ( const char *  format,
  ... 
)

Print a formatted message to standard output.

Parameters
formatThe 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.
Returns
On success, returns the number of characters written. On write error, returns a negative value.
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).

Parameters
basePointer to the array of data to sort.
nmembNumber of elements in the array.
sizeSize of each element in the array, in bytes.
comparComparison 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.

Returns
the random integer
int sprintf ( char *  str,
const char *  format,
  ... 
)

Write a formatted message to a null-terminated string.

Parameters
strThe output string, which must be large enough to store the formatted result, including the null terminator.
formatThe 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.
Returns
the number of characters written, excluding the null terminator.
void srand ( unsigned int  x)

Sets the random seed that will be used in future calls to rand().

Parameters
xthe random seed to set
int sscanf ( const char *  str,
const char *  format,
  ... 
)

Scan values from a string according to the specified format.

Parameters
strString from which to read input.
formatFormat 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 *.
Returns
number of items successfully matched.
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.

Parameters
sThe string to search.
cThe character to locate.
Returns
The pointer in the string, or NULL if the character was not found.
int strcmp ( const char *  s1,
const char *  s2 
)

Compare two null-terminated strings.

Parameters
s1Pointer to the first string.
s2Pointer to the second string.
Returns
A negative value, 0, or a positive value if the 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.

Parameters
destPointer 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.
srcPointer to the string to copy.
Returns
A pointer to 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).

Parameters
destPointer to the buffer into which to copy the string.
srcPointer to the null-terminated string to copy.
destsizeSize of the dest buffer.
Returns
Length of the 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.

Parameters
sString to calculate the length of.
Returns
Length of the string in bytes, not including the null terminator.
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.

Parameters
destPointer to the null-terminated string to which to concatenate the additional string or characters.
srcPointer to the source string.
nMaximum number of bytes of the src string to concatenate, excluding the null terminator that will be appended.
Returns
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.

Parameters
s1Pointer to the first string.
s2Pointer to the second string.
nMaximum number of bytes to compare before returning 0.
Returns
A negative value, 0, or a positive value if the 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.

Parameters
destPointer to the memory to which to copy the string.
srcPointer to the string to copy.
nNumber of bytes to fill (see above description).
Returns
A pointer to 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.

Parameters
sString to calculate the length of.
maxlenMaximum length to return.
Returns
Length of the string up to but not including the null terminator, or 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.

Parameters
sThe string to search.
cThe character to locate.
Returns
The pointer in the string, or 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.

Parameters
haystackThe string to search.
needleThe string to locate.
Returns
The pointer in the string, or NULL if the string was not found.