The str_cmp() function compares the string pointed to by str1 to the string pointed to by str2. It returns an integer less than, equal to, or greater than zero if str1 is found, respectively, to be less than, to match, or be greater than str2.
The strcpy() function copies the string pointed to by src (including the terminating `\0' character) to the array pointed to by dst. The strings may not overlap, and the destination string dst must be large enough to receive the copy. The strncpy() function is similar, except that not more than n bytes of src are copied. Thus, if there is no null byte among the first n bytes of src, the result will not be null-terminated.
The str_dup() function returns a pointer to a new string which is a duplicate of the string str. The str_dupn() function is similar, but only copies at most n characters. If s is longer than n, only n characters are copied, and a terminating null byte is added.
The str_index() returns a pointer to the first occurence of the character c in the string pointed to by str.
The str_len() function calculates the length of the string str, not including the terminating `\0' character.
The str_path_concat() function concatenates the directory name pointed to by dirname and file name pointed to by basename and checks that the latter does not contain any dot entries.
The str_path_isabs() and str_path_isdot() functions check if the file path pointed to by str is absolute or contains dots, respectively.
The str_toupper() and str_tolower() functions map lower-case to upper case and vice-versa, respectively.
The str_zero() function sets the first n bytes of the byte area starting at s to zero (bytes containing '\0').
The str_toumax() function converts the string pointed to by str to an unsigned long long int val using base as conversion base.
Defines | |
#define | CC_ALNUM (1 << 1) |
class for alpha-numerical characters | |
#define | CC_ALPHA (1 << 2) |
class for upper- or lower-case characters | |
#define | CC_ASCII (1 << 3) |
class for ASCII characters | |
#define | CC_BLANK (1 << 4) |
class for blank characters | |
#define | CC_CNTRL (1 << 5) |
class for ASCII control characters | |
#define | CC_DIGIT (1 << 6) |
class for digit characters | |
#define | CC_GRAPH (1 << 7) |
class for graphable characters | |
#define | CC_LOWER (1 << 8) |
class for lower-case characters | |
#define | CC_PRINT (1 << 9) |
class for printable characters | |
#define | CC_PUNCT (1 << 10) |
class for punctuation characters | |
#define | CC_SPACE (1 << 11) |
class for white space characters | |
#define | CC_UPPER (1 << 12) |
class for upper-case characters | |
#define | CC_XDIGIT (1 << 13) |
class for hexadecimal characters | |
#define | str_isempty(str) (!str || str_check(str, CC_BLANK)) |
check if string is empty | |
#define | str_isalnum(str) str_check(str, CC_ALNUM) |
check string for alpha-numerical characters | |
#define | str_isalpha(str) str_check(str, CC_ALPHA) |
check string for upper- or lower-case characters | |
#define | str_isascii(str) str_check(str, CC_ASCII) |
check string for ASCII characters | |
#define | str_isdigit(str) str_check(str, CC_DIGIT) |
check string for digit characters | |
#define | str_isgraph(str) str_check(str, CC_GRAPH) |
check string for graphable characters | |
#define | str_islower(str) str_check(str, CC_LOWER) |
check string for lower-case characters | |
#define | str_isprint(str) str_check(str, CC_PRINT) |
check string for printable characters | |
#define | str_isupper(str) str_check(str, CC_UPPER) |
check string for upper-case characters | |
#define | str_isxdigit(str) str_check(str, CC_XDIGIT) |
check string for hexadecimal characters | |
#define | CHUNKSIZE 4096 |
Functions | |
int | str_check (const char *str, int allowed) |
check string against classes of allowed characters | |
int | str_cmp (const char *str1, const char *str2) |
compare two strings | |
int | str_cmpn (const char *str1, const char *str2, int n) |
compare two strings | |
int | str_equal (const char *str1, const char *str2) |
compare two strings | |
char * | str_cpy (char *dst, const char *src) |
copy a string | |
char * | str_cpyn (char *dst, const char *src, int n) |
copy a string | |
char * | str_dup (const char *str) |
duplicate a string | |
char * | str_chr (const char *str, int c, int n) |
scan string for character | |
char * | str_rchr (const char *str, int c, int n) |
scan string for character beginning at the end | |
char * | str_str (const char *str, const char *needle) |
locate a substring | |
int | str_len (const char *str) |
calculate the length of a string | |
char * | str_path_dirname (const char *path) |
parse directory component | |
char * | str_path_basename (const char *path) |
parse basename component | |
char * | str_path_concat (const char *dirname, const char *basename) |
concatenate dirname and basename | |
int | str_path_isabs (const char *str) |
check if path is absolute and contains no dot entries or ungraphable characters | |
int | str_path_isdot (const char *str) |
check if given path contains . or .. entries | |
char * | str_tolower (char *str) |
convert string to lower-case | |
char * | str_toupper (char *str) |
convert string to upper-case | |
int | str_toumax (const char *str, unsigned long long int *val, int base, int n) |
convert string to integer | |
int | str_readline (int fd, char **str) |
read a line of input | |
int | str_readfile (int fd, char **str) |
read until end of file | |
int | str_read (int fd, char **str, int len) |
read exact number of bytes |
#define CC_ALNUM (1 << 1) |
class for alpha-numerical characters
Definition at line 71 of file str.h.
Referenced by str_check().
#define CC_ALPHA (1 << 2) |
class for upper- or lower-case characters
Definition at line 74 of file str.h.
Referenced by str_check().
#define CC_ASCII (1 << 3) |
#define CC_BLANK (1 << 4) |
#define CC_CNTRL (1 << 5) |
#define CC_DIGIT (1 << 6) |
#define CC_GRAPH (1 << 7) |
#define CC_LOWER (1 << 8) |
#define CC_PRINT (1 << 9) |
#define CC_PUNCT (1 << 10) |
#define CC_SPACE (1 << 11) |
#define CC_UPPER (1 << 12) |
#define CC_XDIGIT (1 << 13) |
#define str_isempty | ( | str | ) | (!str || str_check(str, CC_BLANK)) |
check if string is empty
Definition at line 120 of file str.h.
Referenced by addr_from_str(), ismount(), log_init(), mkdirnamep(), mkdirp(), str_path_basename(), str_path_concat(), str_path_dirname(), str_path_isabs(), str_path_isdot(), strtok_init_argv(), and strtok_init_str().
#define str_isalnum | ( | str | ) | str_check(str, CC_ALNUM) |
#define str_isalpha | ( | str | ) | str_check(str, CC_ALPHA) |
#define str_isascii | ( | str | ) | str_check(str, CC_ASCII) |
#define str_isdigit | ( | str | ) | str_check(str, CC_DIGIT) |
check string for digit characters
Definition at line 132 of file str.h.
Referenced by addr_from_str().
#define str_isgraph | ( | str | ) | str_check(str, CC_GRAPH) |
check string for graphable characters
Definition at line 135 of file str.h.
Referenced by str_path_isabs().
#define str_islower | ( | str | ) | str_check(str, CC_LOWER) |
#define str_isprint | ( | str | ) | str_check(str, CC_PRINT) |
#define str_isupper | ( | str | ) | str_check(str, CC_UPPER) |
#define str_isxdigit | ( | str | ) | str_check(str, CC_XDIGIT) |
int str_check | ( | const char * | str, | |
int | allowed | |||
) |
check string against classes of allowed characters
[in] | str | string to check |
[in] | allowed | allowed classes of characters (multiple classes by ORing) |
Definition at line 20 of file str_check.c.
References CC_ALNUM, CC_ALPHA, CC_ASCII, CC_BLANK, CC_CNTRL, CC_DIGIT, CC_GRAPH, CC_LOWER, CC_PRINT, CC_PUNCT, CC_SPACE, CC_UPPER, CC_XDIGIT, char_isalnum, char_isalpha, char_isascii, char_isblank, char_iscntrl, char_isdigit, char_isgraph, char_islower, char_isprint, char_ispunct, char_isspace, char_isupper, char_isxdigit, and str_len().
00021 { 00022 int i, n; 00023 00024 if (!str) 00025 return 1; 00026 00027 n = str_len(str); 00028 00029 for (i = 0; i < n; i++) { 00030 if (allowed & CC_ALNUM && char_isalnum (str[i])) continue; 00031 if (allowed & CC_ALPHA && char_isalpha (str[i])) continue; 00032 if (allowed & CC_ASCII && char_isascii (str[i])) continue; 00033 if (allowed & CC_BLANK && char_isblank (str[i])) continue; 00034 if (allowed & CC_CNTRL && char_iscntrl (str[i])) continue; 00035 if (allowed & CC_DIGIT && char_isdigit (str[i])) continue; 00036 if (allowed & CC_GRAPH && char_isgraph (str[i])) continue; 00037 if (allowed & CC_LOWER && char_islower (str[i])) continue; 00038 if (allowed & CC_PRINT && char_isprint (str[i])) continue; 00039 if (allowed & CC_PUNCT && char_ispunct (str[i])) continue; 00040 if (allowed & CC_SPACE && char_isspace (str[i])) continue; 00041 if (allowed & CC_UPPER && char_isupper (str[i])) continue; 00042 if (allowed & CC_XDIGIT && char_isxdigit(str[i])) continue; 00043 00044 return 0; 00045 } 00046 00047 return 1; 00048 }
int str_cmp | ( | const char * | str1, | |
const char * | str2 | |||
) |
compare two strings
[in] | str1 | first string |
[in] | str2 | second string |
Definition at line 19 of file str_cmp.c.
Referenced by str_equal().
00020 { 00021 while (*str1 && *str2 && *str1 == *str2) 00022 str1++, str2++; 00023 00024 return *str1 - *str2; 00025 }
int str_cmpn | ( | const char * | str1, | |
const char * | str2, | |||
int | n | |||
) |
compare two strings
[in] | str1 | first string |
[in] | str2 | second string |
[in] | n | compare first n bytes |
Definition at line 19 of file str_cmpn.c.
00020 { 00021 while (--n && *str1 && *str2 && *str1 == *str2) 00022 str1++, str2++; 00023 00024 return *str1 - *str2; 00025 }
int str_equal | ( | const char * | str1, | |
const char * | str2 | |||
) |
compare two strings
[in] | str1 | first string |
[in] | str2 | second string |
Definition at line 19 of file str_equal.c.
References str_cmp().
Referenced by flist32_getval(), flist64_getval(), ismount(), str_path_dirname(), str_path_isabs(), str_path_isdot(), and strtok_delete().
00020 { 00021 return str_cmp(str1, str2) == 0; 00022 }
char* str_cpy | ( | char * | dst, | |
const char * | src | |||
) |
char* str_cpyn | ( | char * | dst, | |
const char * | src, | |||
int | n | |||
) |
copy a string
[out] | dst | destination string |
[in] | src | source string |
[in] | n | copy at most n bytes |
Definition at line 20 of file str_cpyn.c.
References mem_cpy(), and str_len().
00021 { 00022 int len = str_len(src) + 1; 00023 return mem_cpy(dst, src, n > len ? len : n); 00024 }
char* str_dup | ( | const char * | str | ) |
duplicate a string
[in] | str | source string |
Definition at line 20 of file str_dup.c.
References mem_dup(), and str_len().
Referenced by str_path_basename(), str_path_dirname(), strtok_append(), strtok_init_argv(), and strtok_init_str().
char* str_chr | ( | const char * | str, | |
int | c, | |||
int | n | |||
) |
scan string for character
[in] | str | string to scan |
[in] | c | character to look for |
[in] | n | scan first n bytes |
Definition at line 19 of file str_chr.c.
Referenced by _lucid_vsnprintf(), and addr_from_str().
00020 { 00021 for (; n; str++, n--) 00022 if (*str == c) 00023 return (char *) str; 00024 00025 return 0; 00026 }
char* str_rchr | ( | const char * | str, | |
int | c, | |||
int | n | |||
) |
scan string for character beginning at the end
[in] | str | string to scan |
[in] | c | character to look for |
[in] | n | scan first n bytes |
Definition at line 19 of file str_rchr.c.
Referenced by str_path_basename(), and str_path_dirname().
00020 { 00021 for (str += n - 1; n; str--, n--) 00022 if (*str == c) 00023 return (char *) str; 00024 00025 return 0; 00026 }
char* str_str | ( | const char * | str, | |
const char * | needle | |||
) |
locate a substring
[in] | str | string to scan |
[in] | needle | string to look for |
Definition at line 20 of file str_str.c.
References mem_cmp(), and str_len().
Referenced by strtok_init_str().
00021 { 00022 int i; 00023 int slen = str_len(str); 00024 int nlen = str_len(needle); 00025 00026 if (nlen < 1) 00027 return (char *) str; 00028 00029 if (nlen > slen) 00030 return 0; 00031 00032 for (i = slen - nlen + 1; i; i--) { 00033 if (mem_cmp(str, needle, nlen) == 0) 00034 return (char *) str; 00035 00036 str++; 00037 } 00038 00039 return 0; 00040 }
int str_len | ( | const char * | str | ) |
calculate the length of a string
[in] | str | source string |
Definition at line 19 of file str_len.c.
Referenced by _lucid_vsnprintf(), _lucid_vsscanf(), addr_from_str(), flist32_to_str(), flist64_to_str(), str_check(), str_cpy(), str_cpyn(), str_dup(), str_path_basename(), str_path_concat(), str_path_dirname(), str_str(), stralloc_cats(), stralloc_copys(), strtok_init_str(), strtok_tostr(), and whirlpool_digest().
char* str_path_dirname | ( | const char * | path | ) |
parse directory component
[in] | path | path to parse |
Definition at line 20 of file str_path_dirname.c.
References mem_free(), str_dup(), str_equal(), str_isempty, str_len(), and str_rchr().
Referenced by ismount(), and mkdirnamep().
00021 { 00022 /* empty string or '..' */ 00023 if (str_isempty(path) || str_equal(path, "..")) 00024 return str_dup("."); 00025 00026 /* skip prefixing '/' but preserve exactly one */ 00027 while (*path && *(path+1) && *path == '/' && *(path+1) == '/') 00028 path++; 00029 00030 int found = 0; 00031 char *p, *buf = str_dup(path); 00032 00033 while ((p = str_rchr(buf, '/', str_len(buf)))) { 00034 /* remove trailing slash */ 00035 if (p[1] == 0 && p != buf) 00036 *p = 0; 00037 00038 /* no basename was found until yet */ 00039 else if (!found) { 00040 *p = 0; 00041 found = 1; 00042 } 00043 00044 /* a basename was found and no trailing slash anymore */ 00045 else 00046 break; 00047 } 00048 00049 char *dn; 00050 00051 /* path consists only of basename and slashes */ 00052 if (str_isempty(buf)) 00053 dn = str_dup("/"); 00054 00055 /* path is relative or absolute, basename was stripped */ 00056 else if (p) 00057 dn = str_dup(buf); 00058 00059 /* path is relative, no basename was stripped */ 00060 else 00061 dn = str_dup("."); 00062 00063 mem_free(buf); 00064 00065 return dn; 00066 }
char* str_path_basename | ( | const char * | path | ) |
parse basename component
[in] | path | path to parse |
Definition at line 20 of file str_path_basename.c.
References mem_free(), str_dup(), str_isempty, str_len(), and str_rchr().
Referenced by log_traceme().
00021 { 00022 /* empty string */ 00023 if (str_isempty(path)) 00024 return str_dup("."); 00025 00026 /* skip prefixing '/' */ 00027 while (*path && *path == '/') 00028 path++; 00029 00030 /* string consisting entirely of '/' */ 00031 if (!*path) 00032 return str_dup("/"); 00033 00034 char *p, *buf = str_dup(path); 00035 00036 while ((p = str_rchr(buf, '/', str_len(buf)))) { 00037 /* remove trailing lash */ 00038 if (p[1] == 0 && p != buf) 00039 *p = 0; 00040 00041 /* no trailing slash anymore */ 00042 else 00043 break; 00044 } 00045 00046 char *bn; 00047 00048 /* if a non-trailing slash was found, return everything after it */ 00049 if (p) 00050 bn = str_dup(p + 1); 00051 00052 /* otherwise buf already contains basename */ 00053 else 00054 bn = str_dup(buf); 00055 00056 mem_free(buf); 00057 00058 return bn; 00059 }
char* str_path_concat | ( | const char * | dirname, | |
const char * | basename | |||
) |
concatenate dirname and basename
[in] | dirname | directory part |
[in] | basename | basename part |
Definition at line 22 of file str_path_concat.c.
References _lucid_asprintf(), mem_cmp(), str_isempty, str_len(), and str_path_isdot().
00023 { 00024 char *path = 0; 00025 00026 if (str_len(basename) > 1 && mem_cmp(basename, "./", 2) == 0) 00027 basename += 2; 00028 00029 if (str_isempty(dirname) || str_path_isdot(basename)) 00030 return 0; 00031 00032 _lucid_asprintf(&path, "%s/%s", dirname, basename); 00033 00034 return path; 00035 }
int str_path_isabs | ( | const char * | str | ) |
check if path is absolute and contains no dot entries or ungraphable characters
[in] | str | path to check |
Definition at line 20 of file str_path_isabs.c.
References str_equal(), str_isempty, str_isgraph, strtok_for_each, strtok_free(), and strtok_init_str().
00021 { 00022 int abs = 1; 00023 00024 if (str_isempty(str)) 00025 return 0; 00026 00027 if (*str != '/') 00028 return 0; 00029 00030 strtok_t _st, *st = &_st, *p; 00031 00032 if (!strtok_init_str(st, str, "/", 0)) 00033 return -1; 00034 00035 strtok_for_each(st, p) { 00036 if (str_equal(p->token, ".") || str_equal(p->token, "..") || 00037 !str_isgraph(p->token)) { 00038 abs = 0; 00039 break; 00040 } 00041 } 00042 00043 strtok_free(st); 00044 00045 return abs; 00046 }
int str_path_isdot | ( | const char * | str | ) |
check if given path contains . or .. entries
[in] | str | path to check |
Definition at line 20 of file str_path_isdot.c.
References str_equal(), str_isempty, strtok_for_each, strtok_free(), and strtok_init_str().
Referenced by mkdirp(), and str_path_concat().
00021 { 00022 int found = 0; 00023 00024 if (str_isempty(str)) 00025 return 0; 00026 00027 strtok_t _st, *st = &_st, *p; 00028 00029 if (!strtok_init_str(st, str, "/", 0)) 00030 return 0; 00031 00032 strtok_for_each(st, p) { 00033 if (str_equal(p->token, ".") || str_equal(p->token, "..")) { 00034 found = 1; 00035 break; 00036 } 00037 } 00038 00039 strtok_free(st); 00040 00041 return found; 00042 }
char* str_tolower | ( | char * | str | ) |
convert string to lower-case
[out] | str | string to convert |
Definition at line 20 of file str_tolower.c.
References char_tolower.
00021 { 00022 char *p = str; 00023 00024 while (*p) { 00025 char_tolower(*p); 00026 p++; 00027 } 00028 00029 return str; 00030 }
char* str_toupper | ( | char * | str | ) |
convert string to upper-case
[out] | str | string to convert |
Definition at line 20 of file str_toupper.c.
References char_toupper.
00021 { 00022 char *p = str; 00023 00024 while (*p) { 00025 char_toupper(*p); 00026 p++; 00027 } 00028 00029 return str; 00030 }
int str_toumax | ( | const char * | str, | |
unsigned long long int * | val, | |||
int | base, | |||
int | n | |||
) |
convert string to integer
[in] | str | source string |
[out] | val | destination integer |
[in] | base | conversion base |
[in] | n | convert first n bytes |
Definition at line 36 of file str_toumax.c.
References char_isspace.
Referenced by _lucid_vsscanf().
00037 { 00038 char c; 00039 const char *p = str; 00040 int d, minus = 0; 00041 unsigned long long int v = 0; 00042 00043 while (n && char_isspace((unsigned char) *p)) { 00044 p++; 00045 n--; 00046 } 00047 00048 /* Single optional + or - */ 00049 if (n) { 00050 c = *p; 00051 00052 if (c == '-' || c == '+') { 00053 minus = (c == '-'); 00054 p++; 00055 n--; 00056 } 00057 } 00058 00059 if (base == 0) { 00060 if (n >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { 00061 n -= 2; 00062 p += 2; 00063 base = 16; 00064 } 00065 00066 else if (n >= 1 && p[0] == '0') { 00067 n--; 00068 p++; 00069 base = 8; 00070 } 00071 00072 else { 00073 base = 10; 00074 } 00075 } 00076 00077 else if (base == 16) { 00078 if (n >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { 00079 n -= 2; 00080 p += 2; 00081 } 00082 } 00083 00084 while (n && (d = char_todigit(*p)) >= 0 && d < base) { 00085 v = v * base + d; 00086 n--; 00087 p++; 00088 } 00089 00090 if (p - str > 0) 00091 *val = minus ? -v : v; 00092 00093 return p - str; 00094 }
int str_readline | ( | int | fd, | |
char ** | str | |||
) |
read a line of input
[in] | fd | file descriptor to read from |
[out] | line | pointer to a string |
free(3)
read(2)
Definition at line 22 of file str_readline.c.
References CHUNKSIZE, mem_alloc(), mem_free(), and mem_realloc().
00023 { 00024 int chunks = 1, len = 0; 00025 char *buf = mem_alloc(chunks * CHUNKSIZE + 1); 00026 char c; 00027 00028 while (1) { 00029 switch(read(fd, &c, 1)) { 00030 case -1: 00031 mem_free(buf); 00032 return -1; 00033 00034 case 0: 00035 goto out; 00036 00037 default: 00038 if (c == '\n' || c == '\r') 00039 goto out; 00040 00041 if (len >= chunks * CHUNKSIZE) { 00042 chunks++; 00043 buf = mem_realloc(buf, chunks * CHUNKSIZE + 1); 00044 } 00045 00046 buf[len++] = c; 00047 break; 00048 } 00049 } 00050 00051 out: 00052 *line = buf; 00053 return len; 00054 }
int str_readfile | ( | int | fd, | |
char ** | str | |||
) |
read until end of file
[in] | fd | file descriptor to read from |
[out] | file | pointer to a string |
free(3)
read(2)
Definition at line 22 of file str_readfile.c.
References CHUNKSIZE, mem_alloc(), mem_free(), and mem_realloc().
Referenced by exec_fork_pipe().
00023 { 00024 int chunks = 1, len = 0; 00025 char *buf = mem_alloc(chunks * CHUNKSIZE + 1); 00026 00027 for (;;) { 00028 int bytes_read = read(fd, buf+len, CHUNKSIZE); 00029 00030 if (bytes_read == -1) { 00031 mem_free(buf); 00032 return -1; 00033 } 00034 00035 len += bytes_read; 00036 buf[len] = '\0'; 00037 00038 if (bytes_read == 0) 00039 break; 00040 00041 if (bytes_read == CHUNKSIZE) { 00042 chunks++; 00043 buf = mem_realloc(buf, chunks * CHUNKSIZE + 1); 00044 } 00045 } 00046 00047 *str = buf; 00048 return len; 00049 }
int str_read | ( | int | fd, | |
char ** | str, | |||
int | len | |||
) |
read exact number of bytes
[in] | fd | file descriptor to read from |
[out] | str | pointer to a string |
[in] | len | bytes to be read |
free(3)
read(2)
Definition at line 22 of file str_read.c.
References mem_alloc(), and mem_free().
00023 { 00024 int buflen; 00025 char *buf = mem_alloc(len + 1); 00026 00027 if ((buflen = read(fd, buf, len)) == -1) { 00028 mem_free(buf); 00029 return -1; 00030 } 00031 00032 *str = buf; 00033 return buflen; 00034 }