Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
string.h File Reference
#include <stddef.h>
#include <stdint.h>
Include dependency graph for string.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int memcmp (const void *s1, const void *s2, size_t n)
 Compares the first n characters of the two objects pointed to by s1 and s2. More...
 
void * memset (void *dest, int c, size_t n)
 Copies the value c into each of the first n characters of the object pointed to by dest. More...
 
void * memcpy (void *__restrict dest, const void *__restrict src, size_t n)
 Copies n characters from the object pointed to by src to the object pointed to by dest. More...
 
void * memmove (void *dest, const void *src, size_t n)
 Copies n characters from the object pointed to by src to the object pointed to by dest. More...
 
void * memchr (const void *s, int c, size_t n)
 Finds the first occurrence of c in the initial n characters of the object pointed to by s. More...
 
void * memmem (const void *l, size_t l_len, const void *s, size_t s_len)
 Find substring s in memory area l. More...
 
size_t strlen (const char *str)
 Returns the length of the given null-terminated byte string. More...
 
size_t strnlen (const char *str, size_t maxlen)
 Returns the length of the given null-terminated byte string. More...
 
char * strcpy (char *__restrict dst, const char *__restrict src)
 Copies the null-terminated byte string pointed to by src to the character array whose first element is pointed to by dest. More...
 
char * strncpy (char *__restrict dst, const char *__restrict src, size_t maxlen)
 Copies at most maxlen characters of the character array pointed to by src to character array pointed to by dest. More...
 
char * strstr (const char *string, const char *substring)
 Finds the first occurrence of the substring in the string. More...
 
char * strnstr (const char *s, const char *find, size_t slen)
 Finds the first occurrence of find in the initial slen characters of the object pointed to by s. More...
 
int strcmp (const char *s1, const char *s2)
 Compares two null-terminated byte strings lexicographically. More...
 
int strncmp (const char *s1, const char *s2, size_t n)
 Compares at most n characters of two possibly null-terminated arrays. More...
 
char * strdup (const char *str)
 Duplicate the passed in string str. More...
 
char * strndup (const char *str, size_t n)
 Duplicate n bytes of the passed in string str. More...
 
char * strchr (const char *s, int c)
 Finds the first occurrence of c in the null-terminated byte string pointed to by s. More...
 
char * strrchr (const char *s, int c)
 Finds the last occurrence of c in the null-terminated byte string pointed to by s. More...
 
char * strcat (char *__restrict dst, const char *__restrict src)
 Appends a copy of the null-terminated byte string pointed to by src to the end of the null-terminated byte string pointed to by dest. More...
 
char * strncat (char *__restrict dst, const char *__restrict src, size_t maxlen)
 Appends at most maxlen characters from the character array pointed to by src, to the end of the null-terminated byte string pointed to by dest. More...
 
char * strtok (char *s, const char *delim)
 Finds the next token in a null-terminated byte string pointed to by s. More...
 
size_t strxfrm (char *__restrict, const char *__restrict, size_t)
 
size_t strcspn (const char *, const char *)
 
size_t strspn (const char *, const char *)
 
char * strpbrk (const char *, const char *)
 
int strcoll (const char *, const char *)
 
char * strerror (int)
 
int strerror_r (int, char *, size_t)
 

Function Documentation

◆ memchr()

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

Finds the first occurrence of c in the initial n characters of the object pointed to by s.

Finds the first occurrence of c (after conversion to unsigned char as if by (unsigned char)c ) in the initial n characters (each interpreted as unsigned char) of the object pointed to by s.

The behavior is undefined if access occurs beyond the end of the array (s) searched. The behavior is undefined if s is a null pointer.

Parameters
spointer to the object to be examined
ccharacter to search for
nmax number of characters to examine
Returns
a copy of dest

Definition at line 13 of file memchr.c.

14 {
15  const unsigned char* s = src;
16  c = (unsigned char)c;
17 
18 #ifdef __GNUC__
19  for(; ((uintptr_t)s & ALIGN) && n && *s != c; s++, n--)
20  {
21  }
22 
23  if(n && *s != c)
24  {
25  typedef size_t __attribute__((__may_alias__)) word;
26  const word* w;
27  size_t k = ONES * (size_t)c;
28 
29  for(w = (const void*)s; n >= SS && !HASZERO(*w ^ k); w++, n -= SS)
30  {
31  }
32 
33  s = (const void*)w;
34  }
35 #endif
36 
37  for(; n && *s != c; s++, n--)
38  {
39  }
40 
41  return n ? (void*)(uintptr_t)s : 0;
42 }
#define HASZERO(x)
Definition: memchr.c:11
#define SS
Definition: memchr.c:7
__attribute__((noreturn, weak)) void __assert_fail(const char *expr
#define ALIGN
Definition: memchr.c:8
#define ONES
Definition: memchr.c:9
int word
Definition: memcpy.c:39

References __attribute__(), ALIGN, HASZERO, ONES, and SS.

Referenced by memmem().

Here is the caller graph for this function:

◆ memcmp()

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

Compares the first n characters of the two objects pointed to by s1 and s2.

Compares the first n characters of the objects pointed to by s1 and s2. The comparison is done lexicographically.

The behavior is undefined if access occurs beyond the end of either object pointed to by s1 and s2. The behavior is undefined if either s1 or s2 is a null pointer.

Parameters
s1pointers to the objects to compare
s2pointers to the objects to compare
nthe number of bytes to examine
Returns
Negative value if s1 appears before s2 in lexicographical order. Zero if s1 and s2 compare equal, or if n is zero. Positive value if s1 appears after s2 in lexicographical order.

Referenced by memmem().

◆ memcpy()

void* memcpy ( void *__restrict  dest,
const void *__restrict  src,
size_t  n 
)

Copies n characters from the object pointed to by src to the object pointed to by dest.

Copies n characters from the object pointed to by src to the object pointed to by dest. Both objects are interpreted as arrays of unsigned char.

The behavior is undefined if access occurs beyond the end of the dest array. If the objects overlap (which is a violation of the restrict contract) (since C99), the behavior is undefined. The behavior is undefined if either dest or src is a null pointer.

Parameters
destpointer to the object to copy to
srcpointer to the object to copy from
nnumber of bytes to copy
Returns
a copy of dest

Referenced by __attribute__(), realloc(), strcat(), strcpy(), strdup(), strerror_r(), strncat(), strncpy(), and strndup().

◆ memmem()

void* memmem ( const void *  l,
size_t  l_len,
const void *  s,
size_t  s_len 
)

Find substring s in memory area l.

Finds the start of the first occurrence of the substring s of length s_len in the memory area l of length l_len.

The behavior is undefined if access occurs beyond the end of the array (s) searched. The behavior is undefined if s is a null pointer.

Parameters
lpointer to the object to be examined
l_lennumber of characters to examine
sthe substring to search for
s_lennumber of characters to examine
Returns
a pointer to the beginning of the substring, or NULL if the substring is not found.

Definition at line 34 of file memmem.c.

35 {
36  const register char *cur, *last;
37  const char* cl = (const char*)l;
38  const char* cs = (const char*)s;
39 
40  /* we need something to compare */
41  if(l_len == 0 || s_len == 0)
42  {
43  return NULL;
44  }
45 
46  /* "s" must be smaller or equal to "l" */
47  if(l_len < s_len)
48  {
49  return NULL;
50  }
51 
52  /* special case where s_len == 1 */
53  if(s_len == 1)
54  {
55  return memchr(l, (int)*cs, l_len);
56  }
57 
58  /* the last position where its possible to find "s" in "l" */
59  last = cl + l_len - s_len;
60 
61  for(cur = cl; cur <= last; cur++)
62  {
63  if(cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
64  {
65  return (void*)(uintptr_t)cur;
66  }
67  }
68 
69  return NULL;
70 }
int memcmp(const void *s1, const void *s2, size_t n)
Compares the first n characters of the two objects pointed to by s1 and s2.
#define NULL
Definition: stddef.h:15
void * memchr(const void *s, int c, size_t n)
Finds the first occurrence of c in the initial n characters of the object pointed to by s.
Definition: memchr.c:13

References memchr(), memcmp(), and NULL.

◆ memmove()

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

Copies n characters from the object pointed to by src to the object pointed to by dest.

Copies n characters from the object pointed to by src to the object pointed to by dest. Both objects are interpreted as arrays of unsigned char.

The behavior is undefined if access occurs beyond the end of the dest array. The behavior is undefined if either dest or src is a null pointer.

Parameters
destpointer to the object to copy to
srcpointer to the object to copy from
nnumber of bytes to copy
Returns
a copy of dest

◆ memset()

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

Copies the value c into each of the first n characters of the object pointed to by dest.

Copies the value c (after conversion to unsigned char as if by (unsigned char)c ) into each of the first n characters of the object pointed to by dest.

The behavior is undefined if access occurs beyond the end of the dest array. The behavior is undefined if dest is a null pointer.

Parameters
destpointer to the object to fill
cfill byte
nnumber of bytes to fill
Returns
A copy of dest

Referenced by calloc(), CRTStartup(), strcspn(), and strncpy().

◆ strcat()

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

Appends a copy of the null-terminated byte string pointed to by src to the end of the null-terminated byte string pointed to by dest.

Appends a copy of the null-terminated byte string pointed to by src to the end of the null-terminated byte string pointed to by dest. The character src[0] replaces the null terminator at the end of dest. The resulting byte string is null-terminated.

The behavior is undefined if the destination array is not large enough for the contents of both src and dest and the terminating null character. The behavior is undefined if the strings overlap. The behavior is undefined if either dest or src is not a pointer to a null-terminated byte string.

Parameters
dstpointer to the null-terminated byte string to append to
srcpointer to the null-terminated byte string to copy from
Returns
a copy of dest

Definition at line 26 of file strcat.c.

27 {
28  const size_t dstlen = strlen(dst);
29  const size_t srclen = strlen(src);
30  // The strcat() and strncat() functions append a copy of the null-
31  // terminated string src to the end of the null-terminated string dst,
32  // then add a terminating '\0'. The string dst must have sufficient
33  // space to hold the result.
34  memcpy(dst + dstlen, src, srclen + 1);
35  // The strcat() and strncat() functions return dst.
36  return dst;
37 }
size_t strlen(const char *str)
Returns the length of the given null-terminated byte string.
Definition: strlen.c:77
void * memcpy(void *__restrict dest, const void *__restrict src, size_t n)
Copies n characters from the object pointed to by src to the object pointed to by dest.

References memcpy(), and strlen().

◆ strchr()

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

Finds the first occurrence of c in the null-terminated byte string pointed to by s.

Finds the first occurrence of c (after conversion to char as if by (char)c ) in the null-terminated byte string pointed to by s (each character interpreted as unsigned char). The terminating null character is considered to be a part of the string and can be found when searching for '\0'.

The behavior is undefined if s is not a pointer to a null-terminated byte string.

Parameters
spointer to the null-terminated byte string to be analyzed
ccharacter to search for
Returns
a pointer to the found character in s, or null pointer if no such character is found.

Definition at line 7 of file strchr.c.

8 {
9  char* r = __strchrnul(s, c);
10  return *(unsigned char*)r == (unsigned char)c ? r : 0;
11 }
char * __strchrnul(const char *, int)
Definition: strchrnul.c:13

References __strchrnul().

◆ strcmp()

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

Compares two null-terminated byte strings lexicographically.

Compares two null-terminated byte strings lexicographically.

The behavior is undefined if s1 or s2 are not pointers to null-terminated byte strings.

Parameters
s1pointers to the null-terminated byte strings to compare
s2pointers to the null-terminated byte strings to compare
Returns
Negative value if s1 appears before s2 in lexicographical order. Zero if s1 and s2 compare equal, or if n is zero. Positive value if s1 appears after s2 in lexicographical order.

Definition at line 12 of file strcmp.c.

13 {
14  int r = -1;
15 
16  if(s1 == s2)
17  {
18  // short circuit - same string
19  return 0;
20  }
21 
22  // I don't want to panic with a NULL ptr - we'll fall through and fail w/ -1
23  if(s1 != NULL && s2 != NULL)
24  {
25  // iterate through strings until they don't match or s1 ends (null-term)
26  for(; *s1 == *s2; ++s1, ++s2)
27  {
28  if(*s1 == 0)
29  {
30  r = 0;
31  break;
32  }
33  }
34 
35  // handle case where we didn't break early - set return code.
36  if(r != 0)
37  {
38  r = *(const char*)s1 - *(const char*)s2;
39  }
40  }
41 
42  return r;
43 }
#define NULL
Definition: stddef.h:15

References NULL.

Referenced by strcoll(), wctrans(), and wctype().

Here is the caller graph for this function:

◆ strcoll()

int strcoll ( const char *  ,
const char *   
)

Definition at line 3 of file strcoll.c.

4 {
5  return strcmp(l, r);
6 }
int strcmp(const char *s1, const char *s2)
Compares two null-terminated byte strings lexicographically.
Definition: strcmp.c:12

References strcmp().

◆ strcpy()

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

Copies the null-terminated byte string pointed to by src to the character array whose first element is pointed to by dest.

Copies the null-terminated byte string pointed to by src, including the null terminator, to the character array whose first element is pointed to by dest. It may clobber the rest of the destination array with unspecified values and that the following errors are detected at runtime and call the currently installed constraint handler function: The behavior is undefined if the dest array is not large enough. The behavior is undefined if the strings overlap. The behavior is undefined if either dest is not a pointer to a character array or src is not a pointer to a null-terminated byte string. The behavior is undefined if the size of the character array pointed to by dest <= strlen(src, destsz)

Parameters
dstpointer to the character array to write to
srcpointer to the null-terminated byte string to copy from
Returns
a copy of dest

Definition at line 26 of file strcpy.c.

27 {
28  const size_t length = strlen(src);
29  // The stpcpy() and strcpy() functions copy the string src to dst
30  // (including the terminating '\0' character).
31  memcpy(dst, src, length + 1);
32  // The strcpy() and strncpy() functions return dst.
33  return dst;
34 }
size_t strlen(const char *str)
Returns the length of the given null-terminated byte string.
Definition: strlen.c:77
void * memcpy(void *__restrict dest, const void *__restrict src, size_t n)
Copies n characters from the object pointed to by src to the object pointed to by dest.

References memcpy(), and strlen().

Referenced by strxfrm().

Here is the caller graph for this function:

◆ strcspn()

size_t strcspn ( const char *  ,
const char *   
)

Definition at line 8 of file strcspn.c.

9 {
10  const char* a = s;
11  size_t byteset[32 / sizeof(size_t)];
12 
13  if(!c[0] || !c[1])
14  {
15  return (uintptr_t)(__strchrnul(s, *c) - (uintptr_t)a);
16  }
17 
18  memset(byteset, 0, sizeof byteset);
19  for(; *c && BITOP(byteset, *(const unsigned char*)c, |=); c++)
20  {
21  {
22  ;
23  }
24  }
25  for(; *s && !BITOP(byteset, *(const unsigned char*)s, &); s++)
26  {
27  {
28  ;
29  }
30  }
31  return (uintptr_t)s - (uintptr_t)a;
32 }
char * __strchrnul(const char *, int)
Definition: strchrnul.c:13
#define BITOP(a, b, op)
Definition: strcspn.c:5
void * memset(void *dest, int c, size_t n)
Copies the value c into each of the first n characters of the object pointed to by dest.

References __strchrnul(), BITOP, and memset().

Referenced by strpbrk().

Here is the caller graph for this function:

◆ strdup()

char* strdup ( const char *  str)

Duplicate the passed in string str.

Returns a pointer to a new string which is a duplicate of the string str. Memory for the new string is obtained with malloc(3), and can be freed with free(3).

Parameters
stra pointer to a string to duplicate
Returns
a pointer to the duplicated string on success. NULL if insufficient memory was available, with errno set to indicate cause of error.

Definition at line 42 of file strdup.c.

43 {
44  char* copy = NULL;
45 
46  if(str)
47  {
48  size_t len = strlen(str) + 1;
49 
50  if((copy = malloc(len)) == NULL)
51  {
52  return (NULL);
53  }
54 
55  memcpy(copy, str, len);
56  }
57 
58  return (copy);
59 }
size_t strlen(const char *str)
Returns the length of the given null-terminated byte string.
Definition: strlen.c:77
#define NULL
Definition: stddef.h:15
void * malloc(size_t size)
Allocates size bytes of uninitialized storage.
void * memcpy(void *__restrict dest, const void *__restrict src, size_t n)
Copies n characters from the object pointed to by src to the object pointed to by dest.

References malloc(), memcpy(), NULL, and strlen().

◆ strerror()

char* strerror ( int  )

Definition at line 17 of file strerror.c.

18 {
19  const char* s = NULL;
20  int i;
21 
22  for(i = 0; errid[i] && errid[i] != err_no; i++)
23  {
24  }
25 
26  for(s = errmsg; i; s++, i--)
27  {
28  for(; *s; s++)
29  {
30  }
31  }
32  return (char*)(uintptr_t)s;
33 }
#define NULL
Definition: stddef.h:15
static const unsigned char errid[]
Definition: strerror.c:7
static const char errmsg[]
Definition: strerror.c:13

References errid, errmsg, and NULL.

Referenced by strerror_r().

Here is the caller graph for this function:

◆ strerror_r()

int strerror_r ( int  ,
char *  ,
size_t   
)

Definition at line 5 of file strerror_r.c.

6 {
7  int r = 0;
8  char* err_msg = strerror(err_no);
9  size_t length = strlen(err_msg);
10 
11  assert(buffer);
12 
13  if(length >= buffer_size)
14  {
15  if(buffer_size)
16  {
17  // -1 so we don't copy an extra byte...
18  memcpy(buffer, err_msg, buffer_size - 1);
19  // since we will null terminate the string
20  buffer[buffer_size - 1] = 0;
21  }
22 
23  r = ERANGE;
24  }
25  else
26  {
27  // +1 for null termination
28  memcpy(buffer, err_msg, length + 1);
29  }
30 
31  return r;
32 }
#define assert(x)
Definition: assert.h:11
#define ERANGE
Definition: errno.h:50
size_t strlen(const char *str)
Returns the length of the given null-terminated byte string.
Definition: strlen.c:77
char * strerror(int)
Definition: strerror.c:17
void * memcpy(void *__restrict dest, const void *__restrict src, size_t n)
Copies n characters from the object pointed to by src to the object pointed to by dest.

References assert, ERANGE, memcpy(), strerror(), and strlen().

◆ strlen()

size_t strlen ( const char *  str)

Returns the length of the given null-terminated byte string.

Returns the length of the given null-terminated byte string, that is, the number of characters in a character array whose first element is pointed to by str up to and not including the first null character.

The behavior is undefined if str is not a pointer to a null-terminated byte string.

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

Definition at line 77 of file strlen.c.

78 {
79  const char* p;
80  const unsigned long* lp;
81 
82  /* Skip the first few bytes until we have an aligned p */
83  for(p = str; (uintptr_t)p & LONGPTR_MASK; p++)
84  {
85  if(*p == '\0')
86  {
87  return ((uintptr_t)p - (uintptr_t)str);
88  }
89  }
90 
91  /* Scan the rest of the string using word sized operation */
92  // Cast to void to prevent alignment warning
93  for(lp = (const unsigned long*)(const void*)p;; lp++)
94  {
95  if((*lp - mask01) & mask80)
96  {
97  p = (const char*)(lp);
98  testbyte(0);
99  testbyte(1);
100  testbyte(2);
101  testbyte(3);
102 #if(LONG_BIT >= 64)
103  testbyte(4);
104  testbyte(5);
105  testbyte(6);
106  testbyte(7);
107 #endif
108  }
109  }
110 
111  /* NOTREACHED */
112  // return (0);
113 }
#define LONGPTR_MASK
Definition: strlen.c:64
#define testbyte(x)
Definition: strlen.c:70

References LONGPTR_MASK, and testbyte.

Referenced by __strchrnul(), strcat(), strcpy(), strdup(), strerror_r(), strncat(), strnstr(), strrchr(), and strxfrm().

Here is the caller graph for this function:

◆ strncat()

char* strncat ( char *__restrict  dst,
const char *__restrict  src,
size_t  maxlen 
)

Appends at most maxlen characters from the character array pointed to by src, to the end of the null-terminated byte string pointed to by dest.

Appends at most count characters from the character array pointed to by src, stopping if the null character is found, to the end of the null-terminated byte string pointed to by dest. The character src[0] replaces the null terminator at the end of dest. The terminating null character is always appended in the end (so the maximum number of bytes the function may write is maxlen+1).

The behavior is undefined if the destination array is not large enough for the contents of both src and dest and the terminating null character. The behavior is undefined if the strings overlap. The behavior is undefined if either dest or src is not a pointer to a null-terminated byte string.

Parameters
dstpointer to the null-terminated byte string to append to
srcpointer to the null-terminated byte string to copy from
maxlenmaximum number of characters to copy
Returns
a copy of dest

Definition at line 26 of file strncat.c.

27 {
28  const size_t dstlen = strlen(dst);
29  const size_t srclen = strnlen(src, maxlen);
30  // The strcat() and strncat() functions append a copy of the null-
31  // terminated string src to the end of the null-terminated string dst,
32  // then add a terminating '\0'. The string dst must have sufficient
33  // space to hold the result.
34  //
35  // The strncat() function appends not more than maxlen characters
36  // from src, and then adds a terminating '\0'.
37  const size_t cpylen = srclen < maxlen ? srclen : maxlen;
38  memcpy(dst + dstlen, src, cpylen);
39  dst[dstlen + cpylen] = '\0';
40  // The strcat() and strncat() functions return dst.
41  return dst;
42 }
size_t strnlen(const char *str, size_t maxlen)
Returns the length of the given null-terminated byte string.
Definition: strnlen.c:21
size_t strlen(const char *str)
Returns the length of the given null-terminated byte string.
Definition: strlen.c:77
void * memcpy(void *__restrict dest, const void *__restrict src, size_t n)
Copies n characters from the object pointed to by src to the object pointed to by dest.

References memcpy(), strlen(), and strnlen().

◆ strncmp()

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

Compares at most n characters of two possibly null-terminated arrays.

Compares at most count characters of two possibly null-terminated arrays. The comparison is done lexicographically.

The behavior is undefined when access occurs past the end of either array s1 or s2. The behavior is undefined when either s1 or s2 is the null pointer.

Parameters
s1pointers to the null-terminated byte strings to compare
s2pointers to the null-terminated byte strings to compare
nmaximum number of characters to compare
Returns
Negative value if s1 appears before s2 in lexicographical order. Zero if s1 and s2 compare equal, or if n is zero. Positive value if s1 appears after s2 in lexicographical order.

Definition at line 12 of file strncmp.c.

13 {
14  int r = -1;
15 
16  if(s1 == s2)
17  {
18  // short circuit - same string
19  return 0;
20  }
21 
22  // I don't want to panic with a NULL ptr - we'll fall through and fail
23  if(s1 != NULL && s2 != NULL)
24  {
25  // iterate through strings until they don't match, s1 ends, or n == 0
26  for(; n && *s1 == *s2; ++s1, ++s2, n--)
27  {
28  if(*s1 == 0)
29  {
30  r = 0;
31  break;
32  }
33  }
34 
35  // handle case where we didn't break early - set return code.
36  if(n == 0)
37  {
38  r = 0;
39  }
40  else if(r != 0)
41  {
42  r = *s1 - *s2;
43  }
44  }
45 
46  return r;
47 }
#define NULL
Definition: stddef.h:15

References NULL.

Referenced by strnstr().

Here is the caller graph for this function:

◆ strncpy()

char* strncpy ( char *__restrict  dst,
const char *__restrict  src,
size_t  maxlen 
)

Copies at most maxlen characters of the character array pointed to by src to character array pointed to by dest.

Copies at most maxlen characters of the character array pointed to by src (including the terminating null character, but not any of the characters that follow the null character) to character array pointed to by dest. If maxlen is reached before the entire array src was copied, the resulting character array is not null-terminated. If, after copying the terminating null character from src, maxlen is not reached, additional null characters are written to dest until the total of maxlen characters have been written.

The behavior is undefined if the character arrays overlap, if either dest or src is not a pointer to a character array (including if dest or src is a null pointer), if the size of the array pointed to by dest is less than maxlen, or if the size of the array pointed to by src is less than maxlen and it does not contain a null character.

Parameters
dstpointer to the character array to copy to
srcpointer to the character array to copy from
maxlenmaximum number of characters to copy
Returns
a copy of dest

Definition at line 26 of file strncpy.c.

27 {
28  const size_t srclen = strnlen(src, maxlen);
29  if(srclen < maxlen)
30  {
31  // The stpncpy() and strncpy() functions copy at most maxlen
32  // characters from src into dst.
33  memcpy(dst, src, srclen);
34  // If src is less than maxlen characters long, the remainder
35  // of dst is filled with '\0' characters.
36  memset(dst + srclen, 0, maxlen - srclen);
37  }
38  else
39  {
40  // Otherwise, dst is not terminated.
41  memcpy(dst, src, maxlen);
42  }
43  // The strcpy() and strncpy() functions return dst.
44  return dst;
45 }
size_t strnlen(const char *str, size_t maxlen)
Returns the length of the given null-terminated byte string.
Definition: strnlen.c:21
void * memset(void *dest, int c, size_t n)
Copies the value c into each of the first n characters of the object pointed to by dest.
void * memcpy(void *__restrict dest, const void *__restrict src, size_t n)
Copies n characters from the object pointed to by src to the object pointed to by dest.

References memcpy(), memset(), and strnlen().

◆ strndup()

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

Duplicate n bytes of the passed in string str.

strndup is similar to

See also
strdup, but copies at most n bytes. If str is longer than n, only n bytes are copied, and a terminating null byte ('\0') is added.
Parameters
stra pointer to a string to duplicate
nmaximum number of bytes to copy
Returns
a pointer to the duplicated string on success. NULL if insufficient memory was available, with errno set to indicate cause of error.

Definition at line 41 of file strndup.c.

42 {
43  char* copy = NULL;
44 
45  if(str && n)
46  {
47  size_t len;
48 
49  for(len = 0; len < n && str[len]; len++)
50  {
51  }
52 
53  if((copy = malloc(len + 1)) == NULL)
54  {
55  return (NULL);
56  }
57 
58  memcpy(copy, str, len);
59  copy[len] = '\0';
60  }
61 
62  return copy;
63 }
#define NULL
Definition: stddef.h:15
void * malloc(size_t size)
Allocates size bytes of uninitialized storage.
void * memcpy(void *__restrict dest, const void *__restrict src, size_t n)
Copies n characters from the object pointed to by src to the object pointed to by dest.

References malloc(), memcpy(), and NULL.

◆ strnlen()

size_t strnlen ( const char *  str,
size_t  maxlen 
)

Returns the length of the given null-terminated byte string.

Same as

See also
strlen, except that the function returns zero if str is a null pointer and returns maxlen if the null character was not found in the first maxlen bytes of str.

The behavior is undefined if str points to a character array which lacks the null character and the size of that character array < maxlen; in other words, an erroneous value of maxlen does not expose impending buffer overflow.

Parameters
strpointer to the null-terminated byte string to be examined
maxlenmaximum number of characters to examine
Returns
The length of the null-terminated byte string str on success, zero if str is a null pointer, maxlen if the null character was not found.

Definition at line 21 of file strnlen.c.

22 {
23  const char* cp;
24 
25  for(cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--)
26  {
27  {
28  ;
29  }
30  }
31 
32  return (size_t)(cp - str);
33 }

Referenced by strncat(), and strncpy().

Here is the caller graph for this function:

◆ strnstr()

char* strnstr ( const char *  s,
const char *  find,
size_t  slen 
)

Finds the first occurrence of find in the initial slen characters of the object pointed to by s.

Locates the first occurrence of the null-terminated string find in the string s, where not more than slen characters are searched. Characters that appear after a ‘\0’ character are not searched.

The behavior is undefined if either find or s is not a pointer to a null-terminated byte string.

Parameters
spointer to the null-terminated byte string to examine
findpointer to the null-terminated byte string to search for
slenmaxinum number of characters to search for
Returns
If find is an empty string, s is returned; if find occurs nowhere in s, NULL is returned; otherwise a pointer to the first character of the first occurrence of find is returned.

Definition at line 48 of file strnstr.c.

49 {
50  char c;
51 
52  if((c = *find++) != '\0')
53  {
54  size_t len = strlen(find);
55  do
56  {
57  char sc;
58  do
59  {
60  if(slen-- < 1 || (sc = *s++) == '\0')
61  {
62  {
63  return (NULL);
64  }
65  }
66  } while(sc != c);
67  if(len > slen)
68  {
69  {
70  return (NULL);
71  }
72  }
73  } while(strncmp(s, find, len) != 0);
74  s--;
75  }
76  return ((char*)(uintptr_t)s);
77 }
int strncmp(const char *s1, const char *s2, size_t n)
Compares at most n characters of two possibly null-terminated arrays.
Definition: strncmp.c:12
size_t strlen(const char *str)
Returns the length of the given null-terminated byte string.
Definition: strlen.c:77
#define NULL
Definition: stddef.h:15

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

◆ strpbrk()

char* strpbrk ( const char *  ,
const char *   
)

Definition at line 3 of file strpbrk.c.

4 {
5  s += strcspn(s, b);
6  return *s ? (char*)(uintptr_t)s : 0;
7 }
size_t strcspn(const char *, const char *)
Definition: strcspn.c:8

References strcspn().

◆ strrchr()

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

Finds the last occurrence of c in the null-terminated byte string pointed to by s.

Finds the last occurrence of c (after conversion to char as if by (char)c ) in the null-terminated byte string pointed to by s (each character interpreted as unsigned char). The terminating null character is considered to be a part of the string and can be found when searching for '\0'.

The behavior is undefined if s is not a pointer to a null-terminated byte string.

Parameters
spointer to the null-terminated byte string to be analyzed
ccharacter to search for
Returns
a pointer to the found character in s, or null pointer if no such character is found.

Definition at line 7 of file strrchr.c.

8 {
9  return __memrchr(s, c, strlen(s) + 1);
10 }
void * __memrchr(const void *, int, size_t)
Definition: memrchr.c:7
size_t strlen(const char *str)
Returns the length of the given null-terminated byte string.
Definition: strlen.c:77

References __memrchr(), and strlen().

◆ strspn()

size_t strspn ( const char *  ,
const char *   
)

Definition at line 7 of file strspn.c.

8 {
9  const char* a = s;
10  size_t byteset[32 / sizeof(size_t)] = {0};
11 
12  if(!c[0])
13  {
14  return 0;
15  }
16 
17  if(!c[1])
18  {
19  for(; *s == *c; s++)
20  {
21  {
22  ;
23  }
24  }
25  return (uintptr_t)s - (uintptr_t)a;
26  }
27 
28  for(; *c && BITOP(byteset, *(const unsigned char*)c, |=); c++)
29  {
30  {
31  ;
32  }
33  }
34  for(; *s && BITOP(byteset, *(const unsigned char*)s, &); s++)
35  {
36  {
37  ;
38  }
39  }
40 
41  return (uintptr_t)s - (uintptr_t)a;
42 }
#define BITOP(a, b, op)
Definition: strspn.c:4

References BITOP.

◆ strstr()

char* strstr ( const char *  string,
const char *  substring 
)

Finds the first occurrence of the substring in the string.

Finds the first occurrence of the null-terminated byte string pointed to by substring in the null-terminated byte string pointed to by string. The terminating null characters are not compared.

The behavior is undefined if either stirng or substring is not a pointer to a null-terminated byte string.

Parameters
stringpointer to the null-terminated byte string to examine
substringpointer to the null-terminated byte string to search for
Returns
Pointer to the first character of the found substring in string, or NULL if no such substring is found. If substring points to an empty string, string is returned.

Definition at line 38 of file strstr.c.

39 {
40  const char *a, *b;
41 
42  /* First scan quickly through the two strings looking for a
43  * single-character match. When it's found, then compare the
44  * rest of the substring.
45  */
46 
47  b = substring;
48 
49  if(*b == 0)
50  {
51  return (char*)(uintptr_t)string;
52  }
53 
54  for(; *string != 0; string += 1)
55  {
56  if(*string != *b)
57  {
58  continue;
59  }
60 
61  a = string;
62 
63  while(1)
64  {
65  if(*b == 0)
66  {
67  return (char*)(uintptr_t)string;
68  }
69  if(*a++ != *b++)
70  {
71  break;
72  }
73  }
74 
75  b = substring;
76  }
77 
78  return NULL;
79 }
#define NULL
Definition: stddef.h:15

References NULL.

◆ strtok()

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

Finds the next token in a null-terminated byte string pointed to by s.

Finds the next token in a null-terminated byte string pointed to by s. The separator characters are identified by null-terminated byte string pointed to by delim. This function is designed to be called multiples times to obtain successive tokens from the same string.

  • If s != NULL, the call is treated as the first call to strtok for this particular string. The function searches for the first character which is not contained in delim.

The behavior is undefined if either s or delim is not a pointer to a null-terminated byte string.

Parameters
spointer to the null-terminated byte string to tokenize
delimpointer to the null-terminated byte string identifying delimiters
Returns
Returns pointer to the beginning of the next token or NULL if there are no more tokens.

Definition at line 109 of file strtok.c.

110 {
111  static char* last;
112 
113  return (__strtok_r(s, delim, &last));
114 }
char * __strtok_r(char *, const char *, char **)
Definition: strtok.c:43

References __strtok_r().

◆ strxfrm()

size_t strxfrm ( char *  __restrict,
const char *  __restrict,
size_t   
)