welibc
A clear, secure, and well documented standard C library
Macros | Typedefs | Functions
string.h File Reference

Declares types, functions, and macros for manipulating arrays of character type and other objects treated as arrays of character type. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define _NULL
 
#define _SIZE_T
 
#define NULL   ((void *) 0)
 

Typedefs

typedef unsigned long size_t
 

Functions

void * memchr (const void *s, int c, size_t n)
 Searches for a given character. More...
 
int memcmp (const void *s1, const void *s2, size_t n)
 Compares the first n characters of s1 and s2. More...
 
void * memcpy (void *s1, const void *s2, size_t n)
 Copies n characters from s2 into s1. More...
 
void * memmove (void *s1, const void *s2, size_t n)
 Copies n characters from s2 into s1. More...
 
void * memset (void *s, int c, size_t n)
 Copies the value c into the first n characters of object s. More...
 
char * strcat (char *s1, const char *s2)
 Concatenates two strings. More...
 
char * strchr (const char *s, int c)
 Locate the first occurrence of a character in a string. More...
 
int strcmp (const char *s1, const char *s2)
 Compares the strings pointed to be s1 and s2. More...
 
char * strcpy (char *s1, const char *s2)
 Copies a string with null terminator from s2 into s1. More...
 
size_t strcspn (const char *s1, const char *s2)
 Find the length of the span of complementary characters to s2 in s1. More...
 
char * strerror (int errnum)
 Maps an error number to an error message string. More...
 
size_t strlen (const char *s)
 Computes the length of a string. More...
 
char * strncat (char *s1, const char *s2, size_t n)
 Concatenates two strings, appending no more than n characters. More...
 
int strncmp (const char *s1, const char *s2, size_t n)
 Compares the strings pointed to be s1 and s2. More...
 
char * strncpy (char *s1, const char *s2, size_t n)
 Copies no more than n characters with null terminator from s2 to s1. More...
 
char * strpbrk (const char *s1, const char *s2)
 Locate the first occurrence of any of s2 in s1. More...
 
char * strrchr (const char *s, int c)
 Locate the last occurrence of a character in a string. More...
 
size_t strspn (const char *s1, const char *s2)
 Find the length of the span of characters in s2 in s1. More...
 
char * strstr (const char *s1, const char *s2)
 Locate the first occurrence of a string in a another string. More...
 
char * strtok (char *s1, const char *s2)
 Breaks a string into a sequence of tokens split on a given delimiter. More...
 

Detailed Description

Declares types, functions, and macros for manipulating arrays of character type and other objects treated as arrays of character type.

Definition in file string.h.

Macro Definition Documentation

◆ _NULL

#define _NULL

Definition at line 49 of file string.h.

◆ _SIZE_T

#define _SIZE_T

Definition at line 43 of file string.h.

◆ NULL

#define NULL   ((void *) 0)

Definition at line 50 of file string.h.

Typedef Documentation

◆ size_t

typedef unsigned long size_t

Definition at line 44 of file string.h.

Function Documentation

◆ memchr()

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

Searches for a given character.

Parameters
*sObject being searched
cCharacter to find
nMaximum number of character to search

The memchr function locates the first occurrence of c (converted to an unsigned char) in the initial n characters (each interpreted as unsigned char) of the object pointed to by s.

Returns
A pointer to the located character
Return values
NULLThe character does not occur in the object

Definition at line 54 of file memchr.c.

References NULL.

◆ memcmp()

int memcmp ( const void *  s1,
const void *  s2,
size_t  n 
)

Compares the first n characters of s1 and s2.

Parameters
*s1The first object to be compared
*s2The second object to be compared
nThe number of characters to compare

The memcmp function compares the first n characters of the object pointed to by s1 to the first n characters of the object pointed to by s2.

Note
The contents of "holes" used as padding for purposes of alignment within structure objects are indeterminate. Strings shorter than their allocated space and unions may also cause problems in comparison.
If n is 0 then valid objects are considered equal
The returned value will never be greater than UCHAR_MAX and never less than CHAR_MIN
If the comparison will cause a memory read that wraps then the comparison will not take place
Returns
Integer indicating if s1 is greater than, equal to, or less than s2
Return values
>0s1 is greater than s2 or no comparison could be made
0s1 is equal to s2
<0s1 is less than s2

Definition at line 68 of file memcmp.c.

Referenced by strcmp(), and strncmp().

Here is the caller graph for this function:

◆ memcpy()

void* memcpy ( void *  s1,
const void *  s2,
size_t  n 
)

Copies n characters from s2 into s1.

Parameters
*s1The destination location to which to copy characters
*s2The source location from which to copy characters
nThe number of characters to copy

The memcpy function copies n characters from the object pointed to by s2 into the object pointed to by s1.

Note
Copying between overlapping objects is supported, but the source may be overwritten.
Warning
If copying is attempted such that a memory read/write will wrap, the copy will not take place.
See also
memmove()
Returns
The value of s1

Definition at line 60 of file memcpy.c.

References memmove().

Here is the call graph for this function:

◆ memmove()

void* memmove ( void *  s1,
const void *  s2,
size_t  n 
)

Copies n characters from s2 into s1.

Parameters
*s1The destination location to which to copy characters
*s2The source location from which to copy characters
nThe number of characters to copy

The memmove function copies n characters from the object pointed to by s2 into the object pointed to by s1. Copying takes place as if the n characters from the object pointed to by s2 are first copied into a temporary array of n characters that does not overlap the objects pointed to by s1 and s2, and then the n characters from the temporary array are copied into the object pointed to by s1.

Warning
If copying is attempted such that a memory read/write will wrap, the copy will not take place.
Returns
The value of s1

Definition at line 59 of file memmove.c.

Referenced by memcpy(), strcpy(), strncat(), and strncpy().

Here is the caller graph for this function:

◆ memset()

void* memset ( void *  s,
int  c,
size_t  n 
)

Copies the value c into the first n characters of object s.

Parameters
*sDestination object having its memory set
cValue being written
nNumber of characters to be set

The memset function copies the value c (converted to an unsigned char) into each of the first n characters of the object pointed to by s.

Returns
The value of s

Definition at line 52 of file memset.c.

Referenced by strncpy().

Here is the caller graph for this function:

◆ strcat()

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

Concatenates two strings.

Parameters
*s1Destination string
*s2String to be appended

The strcat function appends a copy of the string pointed to by s2 (including the terminating null character) to the end of the string pointed to by s1. The initial character of s2 overwrites the null character at the end of s1. If copying takes place between objects that overlap, the behavior is undefined.

Note
If there is no null character in the first n characters of the array pointed to by s2, the result will not be null-terminated.
Copying between overlapping objects is supported, but the source may be overwritten.
Returns
The value of s1

Definition at line 60 of file strcat.c.

References strcpy(), and strlen().

Here is the call graph for this function:

◆ strchr()

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

Locate the first occurrence of a character in a string.

Parameters
*sString being searched
cCharacter to find

The strchr function locates the first occurrence of c (converted to a char) in the string pointed to by s. The terminating null character is considered to be a part of the string.

Returns
A pointer to the located character
Return values
NULLThe character does not occur in the string

Definition at line 53 of file strchr.c.

References NULL.

Referenced by strcspn(), strspn(), and strstr().

Here is the caller graph for this function:

◆ strcmp()

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

Compares the strings pointed to be s1 and s2.

Parameters
*s1The first string to be compared
*s2The second string to be compared

The strcmp function compares the string pointed to by s1 to the string pointed to by s2.

Returns
Integer indicating if s1 is greater than, equal to, or less than s2
Return values
>0s1 is greater than s2 or no comparison could be made
0s1 is equal to s2
<0s1 is less than s2

Definition at line 54 of file strcmp.c.

References memcmp(), and strlen().

Here is the call graph for this function:

◆ strcpy()

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

Copies a string with null terminator from s2 into s1.

Parameters
*s1The destination array
*s2The source string

The strcpy function copies the string pointed to by s2 (including the terminating null character) into the array pointed to by s1.

Note
Copying between overlapping objects is supported, but the source may be overwritten.
Returns
The value of s1

Definition at line 54 of file strcpy.c.

References memmove(), and strlen().

Referenced by strcat(), and strncpy().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ strcspn()

size_t strcspn ( const char *  s1,
const char *  s2 
)

Find the length of the span of complementary characters to s2 in s1.

Parameters
*s1String being searched
*s2String of characters not counted in the span being found

The strcspn function computes the length of the maximum initial segment of the string pointed to by s1 which consists entirely of characters not from the string pointed to by s2.

Returns
The length of the segment

Definition at line 52 of file strcspn.c.

References strchr(), and strlen().

Referenced by strpbrk(), and strtok().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ strerror()

char* strerror ( int  errnum)

Maps an error number to an error message string.

Parameters
errnumError number being mapped

The strerror function maps the error number in errnum to an error message string.

The implementation shall behave as if no library function calls the strerror function.

Returns
A pointer to a string representation of the error number

Definition at line 54 of file strerror.c.

References EDOM, ERANGE, and NULL.

◆ strlen()

size_t strlen ( const char *  s)

Computes the length of a string.

Parameters
*sString having its length computed

The strlen function computes the length of the string pointed to by s.

Note
If an error or wrapping occurs, a value of 0 is returned
Returns
The number of characters that precede the terminating null character

Definition at line 51 of file strlen.c.

Referenced by strcat(), strcmp(), strcpy(), strcspn(), strncat(), strncmp(), strncpy(), strrchr(), and strstr().

Here is the caller graph for this function:

◆ strncat()

char* strncat ( char *  s1,
const char *  s2,
size_t  n 
)

Concatenates two strings, appending no more than n characters.

Parameters
*s1Destination string
*s2String to be appended
nMaximum number of characters to append

The strncat function appends not more than n characters (a null character and characters that follow it are not appended) from the array pointed to by s2 to the end of the string pointed to by s1. The initial character of s2 overwrites the null character at the end of s1. A terminating null character is always appended to the result. If copying takes place between objects that overlap, the behavior is undefined.

Note
The maximum number of characters that can end up in the array pointed to be s1 is strlen(s1)+n+1.
Copying between overlapping objects is supported, but the source may be overwritten.
Returns
The value of s1

Definition at line 62 of file strncat.c.

References memmove(), and strlen().

Here is the call graph for this function:

◆ strncmp()

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

Compares the strings pointed to be s1 and s2.

Parameters
*s1The first string to be compared
*s2The second string to be compared
nMaximum number of characters to be compared

The strncmp function compares note more than n characters (characters that follow a null character are not compared) from the array pointed to by s1 to the array pointed to by s2.

Returns
Integer indicating if s1 is greater than, equal to, or less than s2
Return values
>0s1 is greater than s2 or no comparison could be made
0s1 is equal to s2
<0s1 is less than s2

Definition at line 56 of file strncmp.c.

References memcmp(), and strlen().

Referenced by strstr().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ strncpy()

char* strncpy ( char *  s1,
const char *  s2,
size_t  n 
)

Copies no more than n characters with null terminator from s2 to s1.

Parameters
*s1The destination array
*s2The source string
nMaximum number of characters to copy

The strncpy function copies not more than n characters (characters that follow a null character are not copied) from the array pointed to by s2 to the array pointed to be s1. If the array pointed to by s2 is a string that is shorter than n characters, null characters are appended to the copy in the array pointed to by s1, until n characters in all have been written.

Note
Copying between overlapping objects is supported, but the source may be overwritten.
Returns
The value of s1

Definition at line 58 of file strncpy.c.

References memmove(), memset(), strcpy(), and strlen().

Here is the call graph for this function:

◆ strpbrk()

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

Locate the first occurrence of any of s2 in s1.

Parameters
*s1String being searched
*s2String of characters to search for

The strpbrk function locates the first occurrence in the string pointed to by s1 of any character from the string pointed to by s2.

Returns
A pointer to the character, or NULL if no character was found

Definition at line 51 of file strpbrk.c.

References NULL, and strcspn().

Here is the call graph for this function:

◆ strrchr()

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

Locate the last occurrence of a character in a string.

Parameters
*sString being searched
cCharacter to find

The strrchr function locates the last occurrence of c (converted to a char) in the string pointed to by s. The terminating null character is considered to be a part of the string.

Returns
A pointer to the located character
Return values
NULLThe character does not occur in the string

Definition at line 53 of file strrchr.c.

References NULL, and strlen().

Here is the call graph for this function:

◆ strspn()

size_t strspn ( const char *  s1,
const char *  s2 
)

Find the length of the span of characters in s2 in s1.

Parameters
*s1String being searched
*s2String of characters in the span being found

The strspn function computes the length of the maximum initial segment of the string pointed to by s1 which consists entirely of characters from the string pointed to by s2.

Returns
Length of the segment

Definition at line 52 of file strspn.c.

References strchr().

Referenced by strtok().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ strstr()

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

Locate the first occurrence of a string in a another string.

Parameters
*s1String being searched
*s2String being found

The strstr function locates the first occurrence in the string pointed to by s1 of the sequence of characters (excluding the terminating null character) in the string pointed to by s2.

Returns
A pointer to the located string
Return values
NULLThe sequence in s2 was not found in s1

Definition at line 53 of file strstr.c.

References NULL, strchr(), strlen(), and strncmp().

Here is the call graph for this function:

◆ strtok()

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

Breaks a string into a sequence of tokens split on a given delimiter.

Parameters
*s1String to be tokenized
*s2Separator string used to delimit tokens

A sequence of calls to the strtok functions breaks the string pointed to by s1 into a sequence of tokens, each of which is delimited by a character from the string pointed to by s2. The first call in the sequence has s1 as its first argument, and is followed by calls with a null pointer as their first argument. The separator string pointed to by s2 may be different from call to call.

The first call in the sequence searches the string pointed to by s1 for the first character that is not contained in the current separator string pointed to by s2. If no such character is found, then there are no tokens in the string pointed to by s1 and the strtok function returns a null pointer. If such a character is found, it is the start of the first token.

The strtok function then searches from there for a character that is contained in the current separator string. If no such character is found, the current token extends to the end of the string pointed to by s1, and subsequent searches for a token will return a null pointer. If such a character is found, it is overwritten by a null character, which terminates the current token. The strtok function saves a pointer to the following character, from which the next search for a token will start.

Each subsequent call, with a null pointer as the value of the first argument, starts searching from the saved pointer and behaves as described above.

The implementation shall behave as if no library function calls the strtok function.

Returns
A pointer to the first character of a token
Return values
NULLNo token was found

Definition at line 77 of file strtok.c.

References NULL, strcspn(), and strspn().

Here is the call graph for this function: