MentOS  0.8.0
The Mentoring Operating System
Functions
string.c File Reference

String routines. More...

Functions

char * strncpy (char *destination, const char *source, size_t num)
 Copies the first num characters of source to destination. More...
 
int strncmp (const char *s1, const char *s2, size_t n)
 Compares up to n characters of s1 to those of s2. More...
 
int stricmp (const char *s1, const char *s2)
 Case insensitive string compare. More...
 
int strnicmp (const char *s1, const char *s2, size_t n)
 Case-insensitively compare up to n characters of s1 to those of s2. More...
 
char * strchr (const char *s, int ch)
 Returns a pointer to the first occurrence of ch in str. More...
 
char * strrchr (const char *s, int ch)
 Returns a pointer to the last occurrence of ch in str. More...
 
char * strstr (const char *str1, const char *str2)
 Returns a pointer to the first occurrence of s2 in s1, or NULL if s2 is not part of s1. More...
 
size_t strspn (const char *string, const char *control)
 Returns the length of the initial portion of string which consists only of characters that are part of control. More...
 
size_t strcspn (const char *string, const char *control)
 Calculates the length of the initial segment of string which consists entirely of characters not in control. More...
 
char * strpbrk (const char *string, const char *control)
 Finds the first character in the string string that matches any character specified in control. More...
 
int tokenize (const char *string, const char *separators, size_t *offset, char *buffer, ssize_t buflen)
 Parses the string using the separator, and at each call it saves the parsed token in buffer. The pointer string will be modified. More...
 
void * memmove (void *dst, const void *src, size_t n)
 Copies the values of num bytes from the location pointed by source to the memory block pointed by destination. More...
 
void * memchr (const void *ptr, int ch, size_t n)
 Searches for the first occurrence of the character c (an unsigned char) in the first n bytes of the string pointed to, by the argument str. More...
 
char * strlwr (char *s)
 Converts a given string into lowercase. More...
 
char * strupr (char *s)
 Converts a given string into uppercase. More...
 
char * strcat (char *dst, const char *src)
 Appends a copy of the string src to the string dst. More...
 
char * strncat (char *s1, const char *s2, size_t n)
 Appends a copy of the string src to the string dst, up to n bytes. More...
 
char * strrev (char *s)
 Reverse the string s. More...
 
char * strtok_r (char *str, const char *delim, char **saveptr)
 This function is a reentrant version strtok(). More...
 
void * memset (void *ptr, int value, size_t num)
 Sets the first num bytes of the block of memory pointed by ptr to the specified value. More...
 
int memcmp (const void *dst, const void *src, size_t n)
 Compares the first n bytes of str1 and str2. More...
 
void * memcpy (void *dst, const void *src, size_t num)
 Copy a block of memory, handling overlap. More...
 
void * memccpy (void *dst, const void *src, int c, size_t n)
 The memccpy function copies no more than n bytes from memory area src to memory area dest, stopping when the character c is found. More...
 
char * strcpy (char *dst, const char *src)
 Copy the string src into the array dst. More...
 
size_t strlen (const char *s)
 Returns the length of the string s. More...
 
size_t strnlen (const char *s, size_t count)
 Returns the number of characters inside s, excluding the terminating null byte ('\0'), but at most count. More...
 
int strcmp (const char *s1, const char *s2)
 Checks if the two strings are equal. More...
 
char * strset (char *s, int c)
 Fill the string s with the character c. More...
 
char * strnset (char *s, int c, size_t n)
 Fill the string s with the character c, up to the given length n. More...
 
char * strtok (char *str, const char *delim)
 Splits string into tokens. More...
 
char * trim (char *str)
 Removes any whitespace characters from the beginning and end of str. More...
 
char * strdup (const char *s)
 Make a copy of the given string. More...
 
char * strndup (const char *s, size_t n)
 Make a copy of at most n bytes of the given string. More...
 
char * strsep (char **stringp, const char *delim)
 Separate the given string based on a given delimiter. More...
 
char * itoa (char *buffer, unsigned int num, unsigned int base)
 Move the number "num" into a string. More...
 
char * replace_char (char *str, char find, char replace)
 Replaces the occurrences of find with replace inside str. More...
 
void strmode (mode_t mode, char *p)
 Converts a file mode (the type and permission information associated with an inode) into a symbolic string which is stored in the location referenced by p. More...
 

Detailed Description

String routines.

Function Documentation

◆ itoa()

char* itoa ( char *  buffer,
unsigned int  num,
unsigned int  base 
)

Move the number "num" into a string.

Parameters
bufferThe string containing the number.
numThe number to convert.
baseThe base used to convert.
Returns
A pointer to buffer.

◆ memccpy()

void* memccpy ( void *  dst,
const void *  src,
int  c,
size_t  n 
)

The memccpy function copies no more than n bytes from memory area src to memory area dest, stopping when the character c is found.

Parameters
dstPoints to the destination memory area.
srcPoints to the source memory area.
cThe delimiter used to stop.
nThe maximum number of copied bytes.
Returns
A pointer to the next character in dst after c, or NULL if c was not found in the first n characters of src.

◆ memchr()

void* memchr ( const void *  ptr,
int  c,
size_t  n 
)

Searches for the first occurrence of the character c (an unsigned char) in the first n bytes of the string pointed to, by the argument str.

Parameters
ptrPointer to the block of memory where the search is performed.
cValue to be located.
nNumber of bytes to be analyzed.
Returns
A pointer to the first occurrence of value in the block of memory.

◆ memcmp()

int memcmp ( const void *  ptr1,
const void *  ptr2,
size_t  n 
)

Compares the first n bytes of str1 and str2.

Parameters
ptr1First pointer to block of memory.
ptr2Second pointer to block of memory.
nNumber of bytes to compare.
Returns
Returns an integral value indicating the relationship between the memory blocks: <0 the first byte that does not match has a lower value in ptr1 than in ptr2 0 the contents of both memory blocks are equal >0 the first byte that does not match has a greater value in ptr1 than in ptr2

◆ memcpy()

void* memcpy ( void *  dst,
const void *  src,
size_t  num 
)

Copy a block of memory, handling overlap.

Parameters
dstPointer to the destination.
srcPointer to the source.
numNumber of bytes to be copied.
Returns
Pointer to the destination.

◆ memmove()

void* memmove ( void *  dst,
const void *  src,
size_t  n 
)

Copies the values of num bytes from the location pointed by source to the memory block pointed by destination.

Parameters
dstPointer to the destination array where the content is to be copied, type-casted to a pointer of type void*.
srcPointer to the source of data to be copied, type-casted to a pointer of type const void*.
nNumber of bytes to copy.
Returns
A pointer to dst is returned.

◆ memset()

void* memset ( void *  ptr,
int  value,
size_t  num 
)

Sets the first num bytes of the block of memory pointed by ptr to the specified value.

Parameters
ptrPointer to the block of memory to set.
valueValue to be set.
numNumber of bytes to be set to the given value.
Returns
The same ptr.

◆ replace_char()

char* replace_char ( char *  str,
char  find,
char  replace 
)

Replaces the occurrences of find with replace inside str.

Parameters
strThe string to manipulate.
findThe character to replace.
replaceThe character used to replace.
Returns
A pointer to str.

◆ strcat()

char* strcat ( char *  dst,
const char *  src 
)

Appends a copy of the string src to the string dst.

Parameters
dstPointer to the destination array, which should be large enough to contain the concatenated resulting string.
srcString to be appended. This should not overlap dst.
Returns
destination is returned.

◆ strchr()

char* strchr ( const char *  s,
int  ch 
)

Returns a pointer to the first occurrence of ch in str.

Parameters
sThe string where the search is performed.
chCharacter to be located.
Returns
A pointer to the first occurrence of character in str.

◆ strcmp()

int strcmp ( const char *  s1,
const char *  s2 
)

Checks if the two strings are equal.

Parameters
s1First string to be compared.
s2Second string to be compared.
Returns
Returns an integral value indicating the relationship between the strings: <0 the first character that does not match has a lower value in s1 than in s2 0 the contents of both strings are equal >0 the first character that does not match has a greater value in s1 than in s2

◆ strcpy()

char* strcpy ( char *  dst,
const char *  src 
)

Copy the string src into the array dst.

Parameters
dstThe destination array where the content is to be copied.
srcString to be copied.
Returns
A pointer to dst is returned.

◆ strcspn()

size_t strcspn ( const char *  string,
const char *  control 
)

Calculates the length of the initial segment of string which consists entirely of characters not in control.

Parameters
stringString to be scanned.
controlString containing the characters to match.
Returns
The number of characters in the initial segment of string which consist only of characters that are not inside control.

◆ strdup()

char* strdup ( const char *  s)

Make a copy of the given string.

Parameters
sString to duplicate.
Returns
On success, returns a pointer to the duplicated string. On failure, returns NULL with errno indicating the cause.

◆ stricmp()

int stricmp ( const char *  s1,
const char *  s2 
)

Case insensitive string compare.

Parameters
s1First string to be compared.
s2Second string to be compared.
Returns
Returns an integral value indicating the relationship between the strings: <0 the first character that does not match has a lower value in s1 than in s2 0 the contents of both strings are equal >0 the first character that does not match has a greater value in s1 than in s2

◆ strlen()

size_t strlen ( const char *  s)

Returns the length of the string s.

Parameters
sPointer to the null-terminated byte string to be examined.
Returns
The length of the null-terminated string str.

◆ strlwr()

char* strlwr ( char *  s)

Converts a given string into lowercase.

Parameters
sString which we want to convert into lowercase.
Returns
A pointer to s.

◆ strmode()

void strmode ( mode_t  mode,
char *  p 
)

Converts a file mode (the type and permission information associated with an inode) into a symbolic string which is stored in the location referenced by p.

Parameters
modeFile mode that encodes access permissions and file type.
pBuffer used to hold the string representation of file mode m.

◆ strncat()

char* strncat ( char *  dst,
const char *  src,
size_t  n 
)

Appends a copy of the string src to the string dst, up to n bytes.

Parameters
dstPointer to the destination array, which should be large enough to contain the concatenated resulting string.
srcString to be appended. This should not overlap dst.
nThe number of bytes to copy.
Returns
destination is returned.

◆ strncmp()

int strncmp ( const char *  s1,
const char *  s2,
size_t  n 
)

Compares up to n characters of s1 to those of s2.

Parameters
s1First string to be compared.
s2Second string to be compared.
nMaximum number of characters to compare.
Returns
Returns an integral value indicating the relationship between the strings: <0 the first character that does not match has a lower value in s1 than in s2 0 the contents of both strings are equal >0 the first character that does not match has a greater value in s1 than in s2

◆ strncpy()

char* strncpy ( char *  destination,
const char *  source,
size_t  num 
)

Copies the first num characters of source to destination.

Parameters
destinationPointer to the destination array where the content is to be copied.
sourceString to be copied.
numMaximum number of characters to be copied from source.
Returns
destination is returned.

◆ strndup()

char* strndup ( const char *  s,
size_t  n 
)

Make a copy of at most n bytes of the given string.

Parameters
sString to duplicate.
nThe number of character to duplicate.
Returns
On success, returns a pointer to the duplicated string. On failure, returns NULL with errno indicating the cause.

◆ strnicmp()

int strnicmp ( const char *  s1,
const char *  s2,
size_t  n 
)

Case-insensitively compare up to n characters of s1 to those of s2.

Parameters
s1First string to be compared.
s2Second string to be compared.
nMaximum number of characters to compare.
Returns
Returns an integral value indicating the relationship between the strings: <0 the first character that does not match has a lower value in s1 than in s2 0 the contents of both strings are equal >0 the first character that does not match has a greater value in s1 than in s2

◆ strnlen()

size_t strnlen ( const char *  s,
size_t  maxlen 
)

Returns the number of characters inside s, excluding the terminating null byte ('\0'), but at most count.

Parameters
sPointer to the null-terminated byte string to be examined.
maxlenThe upperbound on the length.
Returns
Returns strlen(s), if that is less than maxlen, or maxlen if there is no null terminating ('\0') among the first maxlen characters pointed to by s.

◆ strnset()

char* strnset ( char *  s,
int  c,
size_t  n 
)

Fill the string s with the character c, up to the given length n.

Parameters
sThe string that you want to fill.
cThe character that you want to fill the string with.
nThe maximum number of bytes to fill.
Returns
The address of the string, s.

◆ strpbrk()

char* strpbrk ( const char *  string,
const char *  control 
)

Finds the first character in the string string that matches any character specified in control.

Parameters
stringString to be scanned.
controlString containing the characters to match.
Returns
A pointer to the first occurrence in string of any of the characters that are part of control, or a null pointer if none of the characters of control is found in string before the terminating null-character.

◆ strrchr()

char* strrchr ( const char *  s,
int  ch 
)

Returns a pointer to the last occurrence of ch in str.

Parameters
sThe string where the search is performed.
chCharacter to be located.
Returns
A pointer to the last occurrence of character in str.

◆ strrev()

char* strrev ( char *  s)

Reverse the string s.

Parameters
sThe given string which is needed to be reversed.
Returns
The address of the string, s.

◆ strsep()

char* strsep ( char **  stringp,
const char *  delim 
)

Separate the given string based on a given delimiter.

Parameters
stringpThe string to separate.
delimThe delimiter used to separate the string.
Returns
Returns a pointer to stringp.

Finds the first token in stringp, that is delimited by one of the bytes in the string delim. This token is terminated by overwriting the delimiter with a null byte ('\0'), and *stringp is updated to point past the token. In case no delimiter was found, the token is taken to be the entire string *stringp, and *stringp is made NULL.

◆ strset()

char* strset ( char *  s,
int  c 
)

Fill the string s with the character c.

Parameters
sThe string that you want to fill.
cThe character that you want to fill the string with.
Returns
The address of the string, s.

◆ strspn()

size_t strspn ( const char *  string,
const char *  control 
)

Returns the length of the initial portion of string which consists only of characters that are part of control.

Parameters
stringString to be scanned.
controlString containing the characters to match.
Returns
The number of characters in the initial segment of string which consist only of characters from control.

◆ strstr()

char* strstr ( const char *  s1,
const char *  s2 
)

Returns a pointer to the first occurrence of s2 in s1, or NULL if s2 is not part of s1.

Parameters
s1String to be scanned
s2String containing the sequence of characters to match.
Returns
A pointer to the first occurrence in s1 of the entire sequence of characters specified in s2, or a null pointer if the sequence is not present in s1.

◆ strtok()

char* strtok ( char *  str,
const char *  delim 
)

Splits string into tokens.

Parameters
strString to truncate.
delimString containing the delimiter characters.
Returns
If a token is found, a pointer to the beginning of the token. Otherwise, a null pointer.

Notice that str is modified by being broken into smaller strings (tokens). A null pointer may be specified, in which case the function continues scanning where a previous successful call to the function ended.

◆ strtok_r()

char* strtok_r ( char *  str,
const char *  delim,
char **  saveptr 
)

This function is a reentrant version strtok().

Parameters
strString to truncate.
delimString containing the delimiter characters.
saveptrPointer used internally to maintain context between calls.
Returns

The saveptr argument is a pointer to a char * variable that is used internally by strtok_r() in order to maintain context between successive calls that parse the same string. On the first call to strtok_r(), str should point to the string to be parsed, and the value of saveptr is ignored. In subsequent calls, str should be NULL, and saveptr should be unchanged since the previous call.

◆ strupr()

char* strupr ( char *  s)

Converts a given string into uppercase.

Parameters
sString which we want to convert into uppercase.
Returns
A pointer to s.

◆ tokenize()

int tokenize ( const char *  string,
const char *  separators,
size_t offset,
char *  buffer,
ssize_t  buflen 
)

Parses the string using the separator, and at each call it saves the parsed token in buffer. The pointer string will be modified.

Parameters
stringcursor used to parse the string, it will be modified.
separatorsthe list of separators we are using.
offsetthe offset character from which we start extracting the next token.
bufferthe buffer where we save the parsed token.
buflenthe length of the buffer.
Returns
1 if we still have things to parse, 0 if we finished parsing.

◆ trim()

char* trim ( char *  str)

Removes any whitespace characters from the beginning and end of str.

Parameters
strThe string to trim.
Returns
A pointer to str.