Update contrib.
1 /** @file ../include/string.h
5 /** @fn memccpy(void *t, const void *f, int c, size_t n)
10 @return memccpy function returns a pointer to the next character in t after c, or NULL if c was not found in the first n characters of f.
13 copies bytes from string f to string t. If the character c (as converted to an unsigned char) occurs in the string f, the copy stops and a pointer to the byte after the copy of c in the string t is returned.
14 Otherwise, n bytes are copied, and a NULL pointer is returned.
22 char one[50] = {"\0"};
23 (void) memccpy(one, "abcdefgh",8,3);
24 printf("String after memcpy %s
26 (void) memccpy(one, "Hello",5,1);
27 printf("String after memcpy %s
35 String after memcpy abc
36 String after memcpy Hbc
51 /** @fn memchr(const void *s, int c, size_t n)
56 @return The memchr function
57 returns a pointer to the byte located,
58 or NULL if no such byte exists within n bytes.
61 locates the first occurrence of c (converted to an unsigned char)
73 ret = memchr("abcd", c,4);
74 if(!strncmp(one+2,ret,1)) printf("\ c\ found in string \"abcd\"\n");
75 ret = memchr(one, z,4);
76 if(ret == NULL) printf("\ z\ not found in string \"abcd\"\n");
83 c found in string "abcd"
84 z not found in string "abcd"
101 /** @fn memcmp(const void *s1, const void *s2, size_t n)
106 @return The memcmp function
107 returns zero if the two strings are identical,
108 otherwise returns the difference between the first two differing bytes
109 (treated as unsigned char values, so that '\\200'
110 is greater than '\\0'
112 Zero-length strings are always identical.
115 compares byte string s1 against byte string s2. Both strings are assumed to be n bytes long.
123 char str1[] = "abcdefg";
124 char str2[] = "abcdefr";
126 printf( "Compare %.6s to %.6s\n", str1, str2 );
127 result = memcmp( str1, str2, 6);
129 printf( "str1 is less than str2.\n" );
130 else if( result == 0 )
131 printf( "str1 is equal to str2.\n" );
132 else if( result > 0 )
133 printf( "str1 is greater than str2.\n" );
134 printf( "Compare %.7s to %.7s\n", str1, str2 );
135 result = memcmp( str1, str2, 7 );
137 printf( "str1 is less than str2.\n" );
138 else if( result == 0 )
139 printf( "str1 is equal to str2.\n" );
140 else if( result > 0 )
141 printf( "str1 is greater than str2.\n" );
148 Compare abcdef to abcdef
149 str1 is equal to str2.
150 Compare abcdefg to abcdefr
151 str1 is less than str2.
164 @externallyDefinedApi
167 /** @fn memcpy(void *dst, const void *src, size_t len)
171 @return The memcpy function
172 returns the original value of dst.
175 copies len bytes from string src to string dst.
183 char one[50] = {"\0"};
184 (void) memcpy(one, "abcdefgh",8);
185 printf("String after memcpy %s
187 (void) memcpy(one, "Hello",5);
188 printf("String after memcpy %s
196 String after memcpy abcdefgh
197 String after memcpy Hellofgh
209 @externallyDefinedApi
212 /** @fn memmove(void *dst, const void *src, size_t len)
217 The memmove function copies len bytes from string src to string dst. The two strings may overlap. The copy is always done in a non-destructive
227 (void) strcpy(one, "abcdefgh");
228 printf("String before memmove %s
230 (void) memmove(one+1, "xyz", 2);
231 printf("String after memmove %s
233 (void) strcpy(one, "abcdefgh");
234 printf("String before memmove %s
236 (void) memmove(one+1, "xyz", 0);
237 printf("String after memmove %s
245 String before memmove abcdefgh
246 String after memmove axydefgh
247 String before memmove abcdefgh
248 String after memmove abcdefgh
251 @return The memmove function returns pointer to dst dst.
261 @externallyDefinedApi
264 /** @fn memset(void *dst0, int c0, size_t length)
270 writes length bytes of value c0 (converted to an unsigned char) to the string dst0.
279 strcpy(one, "abcdefgh");
280 printf("String before memset %s
282 memset(one+1, 'x', 3);
283 printf("String after calling memset first time %s
285 memset(one+2, 'y', 0);
286 printf("String after calling memset second time %s
294 String before memset abcdefgh
295 String after calling memset first time axxxefgh
296 String after calling memset second time axxxefgh
299 @return The memset function returns its first argument.
308 @externallyDefinedApi
311 /** @fn stpcpy(char *to, const char *from)
315 Note: This description also covers the following functions -
318 @return The strcpy and strncpy functions
319 return to. The stpcpy function returns a pointer to the terminating \\0
322 The stpcpy and strcpy functions
323 copy the string from to to (including the terminating \\0
326 The strncpy function copies at most len characters from from into to. If from is less than len characters long,
327 the remainder of to is filled with '\\0'
329 Otherwise, to is not terminated.
337 char one[50] = {"abcdefgh"};
338 printf("String before strcpy %s
341 printf("String after strcpy %s
343 strncpy(one + 5, " ",1);
344 strncpy(one + 6, "World",5);
345 printf("String after strncpy %s
353 String before strcpy abcdefgh
354 String after strcpy Hello
355 String after strncpy Hello World
359 The following sets chararray to "abc\\0\\0\\0"
362 (void)strncpy(chararray, "abc", sizeof(chararray));
365 The following sets chararray to "abcdef:"
368 (void)strncpy(chararray, "abcdefgh", sizeof(chararray));
371 Note that it does not NULL terminate chararray because the length of the source string is greater than or equal
372 to the length argument. The following copies as many characters from input to buf as will fit and NULL terminates the result.
373 Because strncpy does not guarantee to NULL terminate the string itself, this must be done explicitly.
376 (void)strncpy(buf, input, sizeof(buf) - 1);
377 buf[sizeof(buf) - 1] = \0;
381 Security considerations:
383 The strcpy function is easily misused in a manner which enables malicious users
384 to arbitrarily change a running program's functionality through a
385 buffer overflow attack.
394 @externallyDefinedApi
397 /** @fn strcasestr(const char *s, const char *find)
401 Refer to strstr() for the documentation
415 @externallyDefinedApi
418 /** @fn strcat(char * s, const char * append)
422 Note: This description also covers the following functions -
425 @return The strcat and strncat functions
426 return the pointer s.
428 The strcat and strncat functions
429 append a copy of the null-terminated string append to the end of the null-terminated string s, then add a terminating \\0
430 The string s must have sufficient space to hold the result.
433 appends not more than count characters from append, and then adds a terminating \\0
441 char one[50] = {"\0"};
443 strcat(one," World");
444 printf("Concatenated String %s
452 Concatenated String Hello World
456 Security considerations:
458 The strcat function is easily misused in a manner
459 which enables malicious users to arbitrarily change
460 a running program's functionality through a buffer overflow attack. Avoid using strcat. Instead, use strncat or strlcat and ensure that no more characters are copied to the destination buffer
461 than it can hold. Note that strncat can also be problematic.
462 It may be a security concern for a string to be truncated at all.
463 Since the truncated string will not be as long as the original,
464 it may refer to a completely different resource
465 and usage of the truncated resource
466 could result in very incorrect behavior.
478 @externallyDefinedApi
481 /** @fn strchr(const char *s, int c)
485 Note: This description also covers the following functions -
488 @return The functions strchr and strrchr return a pointer to the located character, or NULL if the character does not appear in the string.
490 The strchr function locates the first occurrence of c (converted to a char)
491 in the string pointed to by s. The terminating null character is considered part of the string;
492 therefore if c is \\0
493 the functions locate the terminating \\0
495 The strrchr function is identical to strchr except it locates the last occurrence of c.
506 ret = strchr("abcd", 'c');
507 if(!strncmp(one+2,ret,1)) printf("\ 'c\ ' found in string \"abcd\"
509 ret = strchr(one, 'z');
510 if(ret == NULL) printf("\ 'z\ ' not found in string \"abcd\"
518 c found in string "abcd"
519 z not found in string "abcd"
534 @externallyDefinedApi
537 /** @fn strcmp(const char *s1, const char *s2)
541 Note: This description also covers the following functions -
544 @return The strcmp and strncmp return an integer greater than, equal to, or less than 0, according
545 to whether the string s1 is greater than, equal to, or less than the string s2.
547 The strcmp and strncmp functions
548 lexicographically compare the null-terminated strings s1 and s2.
551 compares not more than len characters.
552 Because strncmp is designed for comparing strings rather than binary data,
553 characters that appear after a \\0
554 character are not compared.
562 char str1[] = "abcdefg";
563 char str2[] = "abcdefr";
565 printf( "Compare '%s' to '%s
567 result = strcmp( str1, str2);
569 printf( "str1 is less than str2.
571 else if( result == 0 )
572 printf( "str1 is equal to str2.
574 else if( result > 0 )
575 printf( "str1 is greater than str2.
577 printf( "Compare '%.6s' to '%.6s
579 result = strncmp( str1, str2, 6 );
581 printf( "str1 is less than str2.
583 else if( result == 0 )
584 printf( "str1 is equal to str2.
586 else if( result > 0 )
587 printf( "str1 is greater than str2.
595 Compare abcdefg to abcdefr
596 str1 is less than str2.
597 Compare abased to abcdef
598 str1 is equal to str2.
611 @externallyDefinedApi
614 /** @fn strcoll(const char *s1, const char *s2)
618 This function lexicographically compares the null-terminated strings s1 and s2 according to the current locale collation, if locale is set.
619 Calls strcmp and returns an integer greater than, equal to, or less than 0, to whether s1 is greater than, equal to, or less than s2 if C locale is set.
620 Otherwise it will compare the strings according to the smartphone's locale collation.
630 setlocale(LC_ALL,"ar_AE.ISO-8859-6");
631 if(strcoll("abcde","abcde")==0)
632 printf("Strings are same
643 @return This function returns an integer less than, equal to, or
644 greater than zero if s1 is found, respectively, to be less than, to
645 match, or be greater than s2, when both are interpreted as appropriate
646 for the current locale.
657 @externallyDefinedApi
660 /** @fn strcpy(char * from, const char * to)
664 Refer to stpcpy() for the documentation
673 @externallyDefinedApi
676 /** @fn strcspn(const char *s, const char *charset)
679 @return The strcspn function
680 returns the number of characters spanned.
683 spans the initial part of the null-terminated string s as long as the characters from s do not occur in string charset (it
684 spans the complement of charset). In other words, it computes the string array index in s of the first character of s which is also in charset, else the index of the first null character.
692 printf("Number of characters present in s
693 which are not in charset is %d",strcspn("abcde","df"));
700 Number of characters present in s
701 which are not in charset is 3
717 @externallyDefinedApi
720 /** @fn strdup(const char *str)
724 allocates sufficient memory for a copy
726 does the copy, and returns a pointer to it.
727 The pointer may subsequently be used as an
728 argument to the function free .
730 If insufficient memory is available, NULL is returned and errno is set to ENOMEM .
739 ptr = (char *)strdup("abcde");
740 printf("Duplicated string %s
742 ptr = (char *)strdup("Hello Hi");
743 printf("Duplicated string %s
751 Duplicated string abcde
752 Duplicated string Hello Hi
755 @return The strdup() function returns a pointer to the duplicated string, or
756 NULL if insufficient memory was available.
765 @externallyDefinedApi
768 /** @fn strndup(const char *old, size_t sz)
773 allocates sufficient memory for a copy
775 does the copy of at most sz characters, and returns a pointer to it.
776 If old is longer than sz, only sz characters are copied and a terminating NULL is added.
777 The pointer may subsequently be used as an
778 argument to the function free .
780 If insufficient memory is available, NULL is returned and errno is set to ENOMEM .
790 ptr = (char *)strndup("abcde",3);
791 printf("Duplicated string %s
793 ptr = (char *)strndup("Hello Hi",5);
794 printf("Duplicated string %s
803 Duplicated string abc
804 Duplicated string Hello
807 @return The strdup() function returns a pointer to the duplicated string, or
808 NULL if insufficient memory was available.
817 @externallyDefinedApi
820 /** @fn strnlen(const char *s, size_t len)
824 @return The strnlen function returns strlen(s), if that is less than len, or len if there is no "\\0" character among the first len characters pointed to by s.
834 strcpy(one,"abcdef");
835 ret = strnlen(one,5);
836 printf("Length obtained using strnlen = %d\n",ret);
837 ret = strnlen(one,10);
838 printf("Length obtained using strnlen = %d\n",ret);
844 Length obtained using strnlen = 5
845 Length obtained using strnlen = 6
848 Feedback For additional information or queries on this page send feedback
852 @externallyDefinedApi
856 /** @fn strerror(int num)
859 Refer to perror() for the documentation
866 @externallyDefinedApi
869 /** @fn strerror_r(int errnum, char *strerrbuf, size_t buflen)
874 Refer to perror() for the documentation
881 @externallyDefinedApi
884 /** @fn strlcat(char *dst, const char *src, size_t size)
889 Refer to strlcpy() for the documentation
898 @externallyDefinedApi
901 /** @fn strlcpy(char *dst, const char *src, size_t size)
906 Note: This description also covers the following functions -
909 @return The strlcpy and strlcat functions return the total length of the string they tried to
911 For strlcpy that means the length of src .
912 For strlcat that means the initial length of dst plus
914 While this may seem somewhat confusing it was done to make
915 truncation detection simple. Note however, that if strlcat traverses size characters without finding a NULL, the length of the string is
916 considered to be size and the destination string will not be NULL-terminated (since there
917 was no space for the NULL). This prevents strlcat from running off the end of a string. In practice this should
918 not happen (as it means that either size is incorrect or that dst is not a proper "C" string). The check exists to prevent potential security problems in incorrect
921 The strlcpy and strlcat functions copy and concatenate strings respectively. They are
922 designed to be safer, more consistent, and less error prone replacements for strncpy and strncat. Unlike those functions, strlcpy and strlcat take the full size of the buffer (not just the length) and guarantee
923 to NULL-terminate the result (as long as size is larger than 0 or, in the case of strlcat , as long as there is at least one byte free in dst ). Note that you should include a byte for the NULL in size . Also note that strlcpy and strlcat only operate on true "C" strings. This means that for strlcpy src must be NUL-terminated and for strlcat both src and dst must be NULL-terminated.
925 The strlcpy function copies up to size - 1 characters from the NULL-terminated string src to dst , NULL-terminating the result.
927 The strlcat function appends the NULL-terminated string src to the end of dst . It will append at most size - strlen(dst) - 1 bytes, NULL-terminating the result.
932 The following code fragment illustrates the simple case:
934 char *s, *p, buf[BUFSIZ];
936 (void)strlcpy(buf, s, sizeof(buf));
937 (void)strlcat(buf, p, sizeof(buf));
940 To detect truncation, perhaps while building a pathname, something
941 like the following might be used:
943 char *dir, *file, pname[MAXPATHLEN];
945 if (strlcpy(pname, dir, sizeof(pname)) >= sizeof(pname))
947 if (strlcat(pname, file, sizeof(pname)) >= sizeof(pname))
951 Since we know how many characters we copied the first time, we can
952 speed things up a bit by using a copy instead of an append:
954 char *dir, *file, pname[MAXPATHLEN];
957 n = strlcpy(pname, dir, sizeof(pname));
958 if (n >= sizeof(pname))
960 if (strlcpy(pname + n, file, sizeof(pname) - n) >= sizeof(pname) - n)
964 However, one may question the validity of such optimizations, as they
965 defeat the whole purpose of strlcpy and strlcat .
966 As a matter of fact, the first version of this manual page got it wrong.
973 char one[50] = {"abcdefgh"};
974 printf("String before strcpy %s
976 strlcpy(one,"Hello");
977 printf("String after strcpy %s
979 strlcpy(one + 5, " ",1);
980 strlcpy(one + 6, "World",5);
981 printf("String after strncpy %s
995 @externallyDefinedApi
998 /** @fn strlen(const char *str)
1000 @return The strlen() function shall return the length of s; no return value shall be reserved to indicate an error.
1002 The strlen() function shall compute the number of bytes in the string to which s points,
1003 not including the terminating null byte.
1013 strcpy(one,"abcdef");
1014 ret = strnlen(one,5);
1015 printf("Length obtained using strnlen = %d
1017 ret = strnlen(one,10);
1018 printf("Length obtained using strnlen = %d
1025 Length obtained using strnlen = 5
1026 Length obtained using strnlen = 6
1032 @externallyDefinedApi
1035 /** @fn strncat(char * dst, const char * src, size_t n)
1040 Refer to strcat() for the documentation
1050 @externallyDefinedApi
1053 /** @fn strncmp(const char *s1, const char *s2, size_t n)
1058 Refer to strcmp() for the documentation
1069 @externallyDefinedApi
1072 /** @fn strncpy(char * dst, const char * src, size_t n)
1077 Refer to stpcpy() for the documentation
1086 @externallyDefinedApi
1089 /** @fn strnstr(const char *s, const char *find, size_t slen)
1094 Refer to strstr() for the documentation
1108 @externallyDefinedApi
1111 /** @fn strpbrk(const char *s1, const char *s2)
1115 The strpbrk function
1116 locates in the null-terminated string s1 the first occurrence of any character in the string s2 and returns a pointer to this character.
1117 If no characters from s2 occur anywhere in s1 strpbrk returns NULL.
1128 res = strpbrk(one, "bc");
1141 @return The strpbrk() function returns a pointer to the character in s1 that
1142 matches one of the characters in accept, or NULL if no such character
1157 @externallyDefinedApi
1160 /** @fn strrchr(const char *s, int c)
1164 Refer to strchr() for the documentation
1177 @externallyDefinedApi
1180 /** @fn strsep(char **stringp, const char *delim)
1183 @return strsep function returns a pointer to the token, i.e it returns the original value of *stringp
1185 The strsep function locates, in the string referenced by *stringp ,
1186 the first occurrence of any character in the string delim (or the terminating \\0
1187 character) and replaces it with a \\0.
1188 The location of the next character after the delimiter character
1189 (or NULL, if the end of the string was reached) is stored in *stringp .
1190 The original value of *stringp is returned.
1193 field (i.e., a character in the string delim occurs as the first character of *stringp )
1194 can be detected by comparing the location referenced by the returned pointer
1197 If *stringp is initially NULL , strsep returns NULL .
1205 char *one=(char *)malloc(12);
1208 strcpy(one,"Hello,World");
1209 res=strsep(two,",");
1210 if(strcmp(res,"hello"))
1234 @externallyDefinedApi
1237 /** @fn strspn(const char *s, const char *charset)
1240 @return strspn function returns the number of characters in the initial segment of s which consists only of characters from accept
1243 spans the initial part of the null-terminated string s as long as the characters from s occur in the null-terminated string charset .
1244 In other words, it computes the string array index in s of the first character of s which is not in charset ,
1245 else the index of the first null character.
1255 strcpy(one,"abcba");
1256 res = strspn(one, "abc");
1257 printf(" %d times characters found in the string
1265 5 times characters found in the string
1268 @return The strspn function
1269 returns the number of characters spanned.
1282 @externallyDefinedApi
1285 /** @fn strstr(const char *s, const char *find)
1289 Note: This description also covers the following functions -
1290 strcasestr() strnstr()
1292 @return The strstr function returns a pointer to the beginning of the substring, or NULL if the substring is not found.If find is an empty string, s is returned;
1293 if find occurs nowhere in s , NULL is returned;
1294 otherwise a pointer to the first character of the first occurrence of find is returned.
1298 locates the first occurrence of the null-terminated string find in the null-terminated string s .
1300 The strcasestr function is similar to strstr ,
1301 but ignores the case of both strings.
1303 The strnstr function
1304 locates the first occurrence of the null-terminated string find in the string s ,
1305 where not more than slen characters are searched.
1306 Characters that appear after a \\0
1307 character are not searched.
1308 Since the strnstr function is a specific API, it should only be used when portability is not a concern.
1317 ptr = strstr("abcd", "z");
1319 printf("strstr: \"z\" not found in \"abcd\"
1322 printf("strstr: \"z\" found in \"abcd\"
1324 ptr = strstr("abcd", "ab");
1326 printf("strstr: \"ab\" not found in \"abcd\"
1329 printf("strstr: \"ab\" found in \"abcd\"
1331 ptr = strstr("abcd", "abcde");
1333 printf("strstr: \"abcde\" found in \"abcd\"
1336 printf("strstr: \"abbcde\" not found in \"abcd\"
1344 strstr: "z" not found in "abcd"
1345 strstr: "ab" found in "abcd"
1346 strstr: "abcde" found in "abcd"
1350 The following sets the pointer ptr to the "Bar Baz"
1351 portion of largestring :
1353 const char *largestring = "Foo Bar Baz";
1354 const char *smallstring = "Bar";
1356 ptr = strstr(largestring, smallstring);
1359 The following sets the pointer ptr to NULL ,
1360 because only the first 4 characters of largestring are searched:
1362 const char *largestring = "Foo Bar Baz";
1363 const char *smallstring = "Bar";
1365 ptr = strnstr(largestring, smallstring, 4);
1381 @externallyDefinedApi
1384 /** @fn strtok(char *s, const char *delim)
1388 Note: This description also covers the following functions -
1391 @return strtok function returns a pointer to the next token, or NULL if there are no more tokens.
1393 This interface is superceded by strsep .
1396 is used to isolate sequential tokens in a null-terminated string, s .
1397 These tokens are separated in the string by at least one of the
1398 characters in delim .
1399 The first time that strtok is called, s should be specified; subsequent calls, wishing to obtain further tokens
1400 from the same string, should pass a null pointer instead.
1401 The separator string, delim ,
1402 must be supplied each time, and may change between calls.
1404 The implementation will behave as if no library function calls strtok .
1406 The strtok_r function is a reentrant version of strtok .
1407 The context pointer last must be provided on each call.
1408 The strtok_r function
1409 may also be used to nest two parsing loops within one another, as
1410 long as separate context pointers are used.
1412 The strtok and strtok_r functions
1413 return a pointer to the beginning of each subsequent token in the string,
1414 after replacing the token itself with a NUL character.
1415 When no more tokens remain, a null pointer is returned.
1425 strcpy(one,"Hello,World,Hi");
1426 res=strtok(one,",");
1427 if(!strcmp(res,"Hello"))
1430 res=strtok(NULL,",");
1431 if(!strcmp(res,"World"))
1456 The System V strtok ,
1457 if handed a string containing only delimiter characters,
1458 will not alter the next starting point, so that a call to strtok with a different (or empty) delimiter string
1459 may return a non- NULL value.
1460 Since this implementation always alters the next starting point,
1461 such a sequence of calls would always return NULL .
1465 @externallyDefinedApi
1468 /** @fn strtok_r(char *s1, const char *s2, char **lasts)
1473 Refer to strtok() for the documentation
1487 @externallyDefinedApi
1490 /** @fn strxfrm(char * dest, const char * src, size_t n)
1494 @return Upon successful completion, strxfrm returns the length of the transformed string not including
1495 the terminating null character.
1496 If this value is n or more, the contents of dst are indeterminate.
1498 The strxfrm function transforms a null-terminated string pointed to by src according to the current locale collation if any,
1499 then copies the transformed string
1501 Not more than n characters are copied into dst ,
1502 including the terminating null character added.
1504 (it helps to determine an actual size needed
1505 for transformation), dst is permitted to be a NULL pointer.
1507 Comparing two strings using strcmp after strxfrm is equal to comparing
1508 two original strings with strcoll .
1516 char src2[20] = "abc";
1517 char dst1[20] = {\0};
1518 char src1[20] = "abc";
1519 char dst2[20] = {\0};
1523 retx1 = strxfrm(dst1,src1,strlen(src1));
1524 retx2 = strxfrm(dst2,src2,strlen(src2));
1525 if((retc = strcmp(dst1,dst2))== 0)
1526 printf("Strings are same
1545 @externallyDefinedApi
1548 /** @fn swab(const void *from, void *to, ssize_t len)
1553 The function swab copies len bytes from the location referenced by from to the location referenced by to ,
1554 swapping adjacent bytes.
1556 The argument len must be an even number.
1564 int i=0x00003366,j=0x0;
1565 swab((void *)&i;,(void *)&j;,2);
1567 printf("Ouput val = %#x
1585 @externallyDefinedApi