| Skip Navigation Links | |
| Exit Print View | |
![]() | man pages section 3: Basic Library Functions Oracle Solaris 11 Information Library |
enable_extended_FILE_stdio(3C)
posix_spawnattr_getschedparam(3C)
posix_spawnattr_getschedpolicy(3C)
posix_spawnattr_getsigdefault(3C)
posix_spawnattr_getsigignore_np(3C)
posix_spawnattr_getsigmask(3C)
posix_spawnattr_setschedparam(3C)
posix_spawnattr_setschedpolicy(3C)
posix_spawnattr_setsigdefault(3C)
posix_spawnattr_setsigignore_np(3C)
posix_spawnattr_setsigmask(3C)
posix_spawn_file_actions_addclose(3C)
posix_spawn_file_actions_addclosefrom_np(3C)
posix_spawn_file_actions_adddup2(3C)
posix_spawn_file_actions_addopen(3C)
posix_spawn_file_actions_destroy(3C)
posix_spawn_file_actions_init(3C)
pthread_attr_getdetachstate(3C)
pthread_attr_getinheritsched(3C)
pthread_attr_getschedparam(3C)
pthread_attr_getschedpolicy(3C)
pthread_attr_setdetachstate(3C)
pthread_attr_setinheritsched(3C)
pthread_attr_setschedparam(3C)
pthread_attr_setschedpolicy(3C)
pthread_barrierattr_destroy(3C)
pthread_barrierattr_getpshared(3C)
pthread_barrierattr_setpshared(3C)
pthread_condattr_getpshared(3C)
pthread_condattr_setpshared(3C)
pthread_cond_reltimedwait_np(3C)
pthread_key_create_once_np(3C)
pthread_mutexattr_getprioceiling(3C)
pthread_mutexattr_getprotocol(3C)
pthread_mutexattr_getpshared(3C)
pthread_mutexattr_getrobust(3C)
pthread_mutexattr_setprioceiling(3C)
pthread_mutexattr_setprotocol(3C)
pthread_mutexattr_setpshared(3C)
pthread_mutexattr_setrobust(3C)
pthread_mutex_getprioceiling(3C)
pthread_mutex_reltimedlock_np(3C)
pthread_mutex_setprioceiling(3C)
pthread_rwlockattr_destroy(3C)
pthread_rwlockattr_getpshared(3C)
pthread_rwlockattr_setpshared(3C)
pthread_rwlock_reltimedrdlock_np(3C)
pthread_rwlock_reltimedwrlock_np(3C)
pthread_rwlock_timedrdlock(3C)
pthread_rwlock_timedwrlock(3C)
rctlblk_get_enforced_value(3C)
- string operations
#include <strings.h>intstrcasecmp(const char *s1,const char *s2);
intstrncasecmp(const char *s1,const char *s2,size_tn);
#include <string.h>char *strcat(char *restricts1,const char *restricts2);
char *strncat(char *restricts1,const char *restricts2,size_tn);
size_tstrlcat(char *dst,const char *src,size_tdstsize);
char *strchr(const char *s,intc);
char *strrchr(const char *s,intc);
char *strchrnul(const char *s, intc);
intstrcmp(const char *s1,const char *s2);
intstrncmp(const char *s1,const char *s2,size_tn);
char *strcpy(char *restricts1,const char *restricts2);
char *strncpy(char *restricts1,const char *restricts2,size_tn);
size_tstrlcpy(char *dst,const char *src,size_tdstsize);
char *stpcpy(char *restricts1, const char *restricts2);
char *stpncpy(char *restricts1, const char *restricts2, size_tn);
size_tstrcspn(const char *s1,const char *s2);
size_tstrspn(const char *s1,const char *s2);
char *strdup(const char *s);
char *strndup(const char *s, size_tsize);
char *strdupa(const char *s);
char *strndupa(const char *s, size_tsize);
size_tstrlen(const char *s);
size_tstrnlen(const char *s,size_tn);
char *strpbrk(const char *s1,const char *s2);
char *strsep(char **stringp,const char *delim);
char *strstr(const char *s1,const char *s2);
char *strnstr(const char *s1, const char *s2, size_tn);
char *strcasestr(const char *s1, const char *s2);
char *strtok(char *restricts1,const char *restricts2);
char *strtok_r(char *s1,const char *s2,char **lasts);
#include <string.h>const char *strchr(const char *s,intc);
const char *strpbrk(const char *s1,const char *s2);
const char *strrchr(const char *s,intc);
const char *strstr(const char *s1,const char *s2);
#include <cstring>char *std::strchr(char *s,intc);
char *std::strpbrk(char *s1,const char *s2);
char *std::strrchr(char *s,intc);
char *std::strstr(char *s1,const char *s2);
The argumentss,s1, ands2 point to strings (arrays of charactersterminated by a null character). Thestrcat(),strncat(),strlcat(),strcpy(),strncpy(),strlcpy(),strsep(),strtok(), andstrtok_r() functions all alter their first argument. Additionally, thestrcat() andstrcpy() functions do not check for overflow of the array.
Thestrcasecmp() andstrncasecmp() functions are case-insensitive versions ofstrcmp() andstrncmp() respectively, described below. They ignore differences in case when comparing lowerand upper case characters, using the current locale of the process to determinethe case of the characters.
Thestrcat() function appends a copy of strings2, including the terminatingnull character, to the end of strings1. Thestrncat() function appendsat mostn characters. Each returns a pointer to the null-terminated result. Theinitial character ofs2 overrides the null character at the endofs1. If copying takes place between objects that overlap, the behaviorofstrcat(),strncat(), andstrlcat() is undefined.
Thestrlcat() function appends at most (dstsize-strlen(dst)-1) characters ofsrctodst (dstsize being the size of the string bufferdst). If the string pointed to bydst contains a null-terminated stringthat fits intodstsize bytes whenstrlcat() is called, the string pointedto bydst will be a null-terminated string that fits indstsize bytes(including the terminating null character) when it completes, and the initial characterofsrc will override the null character at the end ofdst. If the string pointed to bydst is longer thandstsize byteswhenstrlcat() is called, the string pointed to bydst will notbe changed. The function returnsmin{dstsize,strlen(dst)}+strlen(src). Buffer overflow can be checked as follows:
if (strlcat(dst, src, dstsize) >= dstsize) return -1;
Thestrchr() function returns a pointer to the first occurrence ofc(converted to achar) in strings, or a null pointerifc does not occur in the string.
Thestrrchr() function returns a pointer to the last occurrence ofc.The null character terminating a string is considered to be part ofthe string.
Thestrchrnul() function is similar tostrchr() except that ifc isnot found ins, it returns a pointer to the null byteat the end ofs, rather thanNULL.
Thestrcmp() function compares two strings byte-by-byte, according to the ordering ofyour machine's character set. The function returns an integer greater than,equal to, or less than 0, if the string pointed tobys1 is greater than, equal to, or less than the string pointedto bys2 respectively. The sign of a non-zero return value isdetermined by the sign of the difference between the values ofthe first pair of bytes that differ in the strings being compared.Thestrncmp() function makes the same comparison but looks at a maximum ofn bytes. Bytes following a null byte are not compared.
Thestrcpy() andstpcpy() functions copy strings2 tos1, including theterminating null character, stopping after the null character has been copied. Thestrcpy() function returnss1. Thestpcpy() function returns a pointer to theterminating null character copied into thes1 array.
Thestrncpy()stpncpy() and functions copy not more thann bytes (bytesthat follow a null byte are not copied) from the array pointedto bys2 to the array pointed to bys1. If the arraypointed to bys2 is a string that is shorter thannbytes, null bytes are appended to the copy in the array pointedto bys1, untiln bytes in all are written. Thestpcpy()function returnss1. Ifs1 contains null bytes,stpncpy() returns a pointer tothe first such null byte. Otherwise, it returns &s1[n].
Thestrlcpy() function copies at mostdstsize-1 characters (dstsize being the size of the string bufferdst) fromsrc todst, truncatingsrc if necessary. The result is always null-terminated.The function returnsstrlen(src). Buffer overflow can be checked as follows:
if (strlcpy(dst, src, dstsize) >= dstsize) return -1;
If copying takes place between objects that overlap, the behavior of thesefunctions is undefined.
Thestrcspn() function returns the length of the initial segment of strings1 that consists entirely of characters not from strings2. Thestrspn()function returns the length of the initial segment of strings1 thatconsists entirely of characters from strings2.
Thestrdup() function returns a pointer to a new string that isa duplicate of the string pointed to bys. The returnedpointer can be passed tofree(). The space for the new stringis obtained usingmalloc(3C). If the new string cannot be created, anull pointer is returned anderrno may be set toENOMEM toindicate that the storage space available is insufficient.
Thestrndup() function is similar tostrdup(), except that it copies atmostsize bytes. If the length ofs is larger thansize,onlysize bytes are copied and a terminating null byte is added. Ifsize is larger than the length ofs, all bytes insare copied, including the terminating null character.
Thestrdupa() andstrndupa() functions are similar tostrdup() andstrndup(), respectively,but usealloca(3C) to allocate the buffer.
Thestrlen() function returns the number of bytes ins, not includingthe terminating null character.
Thestrnlen() function returns the smaller ofn or the number ofbytes ins, not including the terminating null character. Thestrnlen() functionnever examines more thann bytes of the string pointed to bys.
Thestrpbrk() function returns a pointer to the first occurrence in strings1 of any character from strings2, or a null pointer ifno character froms2 exists ins1.
Thestrsep() function locates, in the null-terminated string referenced by *stringp, thefirst occurrence of any character in the stringdelim (or the terminating`\0' character) and replaces it with a `\0'. The location of thenext character after the delimiter character (orNULL, if the end ofthe string was reached) is stored in *stringp. The original valueof *stringp is returned.
An ``empty'' field (one caused by two adjacent delimiter characters) can bedetected by comparing the location referenced by the pointer returned bystrsep()to `\0'.
If *stringp is initiallyNULL,strsep() returnsNULL.
Thestrstr() function locates the first occurrence of the strings2 (excludingthe terminating null character) in strings1 and returns a pointer tothe located string, or a null pointer if the string is notfound. Ifs2 points to a string with zero length (that is,the string""), the function returnss1.
Thestrnstr() function locates the first occurrence of the null-terminated strings2in the strings1, where not more thann characters are searched.Characters that appear after a `\0' character are not searched.
Thestrcasestr() function is similar tostrstr(), but ignores the case ofboth strings.
A sequence of calls tostrtok() breaks the string pointed to bys1 into a sequence of tokens, each of which is delimited bya byte from the string pointed to bys2. The first callin the sequence hass1 as its first argument, and is followed bycalls with a null pointer as their first argument. The separator stringpointed to bys2 can be different from call to call.
The first call in the sequence searches the string pointed to bys1 for the first byte that is not contained in the currentseparator string pointed to bys2. If no such byte is found,then there are no tokens in the string pointed to bys1andstrtok() returns a null pointer. If such a byte is found,it is the start of the first token.
Thestrtok() function then searches from there for a byte that iscontained in the current separator string. If no such byte is found,the current token extends to the end of the string pointed tobys1, and subsequent searches for a token return a null pointer. Ifsuch a byte is found, it is overwritten by a null bytethat terminates the current token. Thestrtok() function saves a pointer tothe following byte in thread-specific data, from which the next search fora token starts.
Each subsequent call, with a null pointer as the value of thefirst argument, starts searching from the saved pointer and behaves as describedabove.
See Example 1, 2, and 3 in theEXAMPLES section for examplesofstrtok() usage and the explanation inNOTES.
Thestrtok_r() function considers the null-terminated strings1 as a sequence ofzero or more text tokens separated by spans of one or morecharacters from the separator strings2. The argumentlasts points to a user-providedpointer which points to stored information necessary forstrtok_r() to continue scanningthe same string.
In the first call tostrtok_r(),s1 points to a null-terminated string,s2 to a null-terminated string of separator characters, and the value pointedto bylasts is ignored. Thestrtok_r() function returns a pointer to thefirst character of the first token, writes a null character intos1immediately following the returned token, and updates the pointer to whichlastspoints.
In subsequent calls,s1 is a null pointer andlasts is unchangedfrom the previous call so that subsequent calls move through the strings1, returning successive tokens until no tokens remain. The separator strings2can be different from call to call. When no token remains ins1, a null pointer is returned.
See Example 3 in theEXAMPLES section for an example ofstrtok_r()usage and the explanation inNOTES.
Example 1 Search for word separators.
The following example searches for tokens separated by space characters.
#include <string.h>...char *token;char line[] = "LINE TO BE SEPARATED";char *search = " "; /* Token will point to "LINE". */token = strtok(line, search); /* Token will point to "TO". */token = strtok(NULL, search);
Example 2 Break a Line.
The following example usesstrtok() to break a line into two characterstrings separated by any combination of SPACEs, TABs, or NEWLINEs.
#include <string.h>...struct element { char *key; char *data;}; ...char line[LINE_MAX];char *key, *data;...key = strtok(line, " \n");data = strtok(NULL, " \n");Example 3 Search for tokens.
The following example uses bothstrtok() andstrtok_r() to search for tokensseparated by one or more characters from the string pointed to bythe second argument, “/”.
#define __EXTENSIONS__#include <stdio.h>#include <string.h>intmain() { char buf[13]; char *token; char *lasts; (void) strlcpy(buf, "5/90/45", 8); (void) printf("tokenizing \\"%s\\" with strtok():\", buf); if ((token = strtok(buf, "/")) != NULL) { (void) printf("token = \\"%s\\"\", token); while ((token = strtok(NULL, "/")) != NULL) { (void) printf("token = \\"%s\\"\", token); } } (void) strlcpy(buf, "//5//90//45//", 13); (void) printf("\tokenizing \\"%s\\" with strtok_r():\", buf); if ((token = strtok_r(buf, "/", &lasts)) != NULL) { (void) printf("token = \\"%s\\"\", token); while ((token = strtok_r(NULL, "/", &lasts)) != NULL) { (void) printf("token = \\"%s\\"\", token); } }}When compiled and run, this example produces the following output:
tokenizing "5/90/45" withstrtok():token = "5"token = "90"token = "45"tokenizing "//5//90//45//" withstrtok_r():token = "5"token = "90"token = "45"
Seeattributes(5) for descriptions of the following attributes:
|
Thestrtok() andstrdup() functions are MT-Safe. The remaining functions are Async-Signal-Safe.
For all exceptstrlcat(),strlcpy(), andstrsep(), seestandards(5).
alloca(3C),malloc(3C),setlocale(3C),strxfrm(3C),attributes(5),standards(5)
When compiling multithreaded applications, the_REENTRANT flag must be defined on thecompile line. This flag should only be used in multithreaded applications.
A single-threaded application can gain access tostrtok_r() only by defining__EXTENSIONS__or by defining_POSIX_C_SOURCE to a value greater than or equal to199506L.
All of these functions assume the default locale ``C.'' For some locales,strxfrm(3C) should be applied to the strings before they are passed tothe functions.
Thestrtok() function is safe to use in multithreaded applications because itsaves its internal state in a thread-specific data area. However, itsuse is discouraged, even for single-threaded applications. Thestrtok_r() function should beused instead.
Do not pass the address of a character string literal as thearguments1 to eitherstrtok() orstrtok_r(). Similarly, do not pass apointer to the address of a character string literal as the argumentstringp tostrsep(). These functions can modify the storage pointed to bys1 in the case ofstrtok() andstrtok_r() or *stringp in thecase ofstrsep(). The C99 standard specifies that attempting to modify the storageoccupied by a string literal results in undefined behavior. This allows compilers(includinggcc and the Sun Studio compilers) to place string literals inread-only memory. Note that in Example 1 above, this problem is avoidedbecause the variableline is declared as a writable array of typechar that is initialized by a string literal rather than a pointertochar that points to a string literal.
Copyright © 2011, Oracle and/or its affiliates. All rights reserved.Legal Notices | ![]() ![]() |