2 * This file is part of the libpayload project.
4 * Copyright (C) 2007 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2008 Advanced Micro Devices, Inc.
6 * Copyright (C) 2010 coresystems GmbH
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <libpayload.h>
38 * Calculate the length of a fixed-size string.
40 * @param str The input string.
41 * @param maxlen Return at most maxlen characters as length of the string.
42 * @return The length of the string, not including the final NUL character.
43 * The maximum length returned is maxlen.
45 size_t strnlen(const char *str, size_t maxlen)
49 /* NULL and empty strings have length 0. */
53 /* Loop until we find a NUL character, or maxlen is reached. */
54 while ((*str++ != '\0') && (len < maxlen))
61 * Calculate the length of a string.
63 * @param str The input string.
64 * @return The length of the string, not including the final NUL character.
66 size_t strlen(const char *str)
70 /* NULL and empty strings have length 0. */
74 /* Loop until we find a NUL character. */
75 while (*str++ != '\0')
82 * Compare two strings.
84 * @param s1 The first string.
85 * @param s2 The second string.
86 * @return Returns a value less than zero, if s1 is shorter than s2. Returns
87 * zero, if s1 equals s2. Returns a value greater than zero, if
88 * s1 is longer than s2.
90 int strcasecmp(const char *s1, const char *s2)
94 for (i = 0; s1[i] != '\0'; i++) {
95 if (tolower(s1[i]) != tolower(s2[i]))
103 * Compare two strings with fixed length.
105 * @param s1 The first string.
106 * @param s2 The second string.
107 * @param maxlen Return at most maxlen characters as length of the string.
108 * @return A non-zero value if s1 and s2 differ, or zero if s1 equals s2.
110 int strncasecmp(const char *s1, const char *s2, size_t maxlen)
114 for (i = 0; i < maxlen; i++) {
115 if (tolower(s1[i]) != tolower(s2[i]))
116 return s1[i] - s2[i];
119 return s1[i] - s2[i];
123 * Compare two strings.
125 * @param s1 The first string.
126 * @param s2 The second string.
127 * @return Returns a value less than zero, if s1 is shorter than s2. Returns
128 * zero, if s1 equals s2. Returns a value greater than zero, if
129 * s1 is longer than s2.
131 int strcmp(const char *s1, const char *s2)
135 for (i = 0; s1[i] != '\0'; i++) {
137 return s1[i] - s2[i];
140 return s1[i] - s2[i];
144 * Compare two strings with fixed length.
146 * @param s1 The first string.
147 * @param s2 The second string.
148 * @param maxlen Return at most maxlen characters as length of the string.
149 * @return A non-zero value if s1 and s2 differ, or zero if s1 equals s2.
151 int strncmp(const char *s1, const char *s2, size_t maxlen)
155 for (i = 0; i < maxlen; i++) {
157 return s1[i] - s2[i];
164 * Copy a string with a maximum length.
166 * @param d The destination memory.
167 * @param s The source string.
168 * @param n Copy at most n characters as length of the string.
169 * @return A pointer to the destination memory.
171 char *strncpy(char *d, const char *s, size_t n)
173 /* Use +1 to get the NUL terminator. */
174 int max = n > strlen(s) + 1 ? strlen(s) + 1 : n;
177 for (i = 0; i < max; i++)
186 * @param d The destination memory.
187 * @param s The source string.
188 * @return A pointer to the destination memory.
190 char *strcpy(char *d, const char *s)
192 return strncpy(d, s, strlen(s) + 1);
196 * Concatenates two strings
198 * @param d The destination string.
199 * @param s The source string.
200 * @return A pointer to the destination string.
202 char *strcat(char *d, const char *s)
204 char *p = d + strlen(d);
208 for (i = 0; i < sl; i++)
216 * Concatenates two strings with a maximum length.
218 * @param d The destination string.
219 * @param s The source string.
220 * @param n Not more than n characters from s will be appended to d.
221 * @return A pointer to the destination string.
223 char *strncat(char *d, const char *s, size_t n)
225 char *p = d + strlen(d);
227 int max = n > sl ? sl : n;
228 // int max = n > strlen(s) ? strlen(s) : n;
231 for (i = 0; i < max; i++)
239 * Concatenates two strings with a maximum length.
241 * @param d The destination string.
242 * @param s The source string.
243 * @param n Not more than n characters from s will be appended to d.
244 * @return A pointer to the destination string.
246 size_t strlcat(char *d, const char *s, size_t n)
252 int max = n > (sl + dl) ? sl : (n - dl - 1);
255 for (i = 0; i < max; i++)
263 * Find a character in a string.
265 * @param s The string.
266 * @param c The character.
267 * @return A pointer to the first occurence of the character in the
268 * string, or NULL if the character was not encountered within the string.
270 char *strchr(const char *s, int c)
274 for (; *p != 0; p++) {
283 * Find a character in a string.
285 * @param s The string.
286 * @param c The character.
287 * @return A pointer to the last occurence of the character in the
288 * string, or NULL if the character was not encountered within the string.
291 char *strrchr(const char *s, int c)
293 char *p = (char *)s + strlen(s);
295 for (; p >= s; p--) {
304 * Duplicate a string.
306 * @param s The string to duplicate.
307 * @return A pointer to the copy of the original string.
309 char *strdup(const char *s)
312 char *p = malloc(n + 1);
322 * Find a substring within a string.
324 * @param h The haystack string.
325 * @param n The needle string (substring).
326 * @return A pointer to the first occurence of the substring in
327 * the string, or NULL if the substring was not encountered within the string.
329 char *strstr(const char *h, const char *n)
335 for (i = 0; i <= hn - nn; i++)
336 if (!memcmp(&h[i], n, nn))
337 return (char *)&h[i];
345 * @param stringp reference of the string to separate.
346 * @param delim string containing all delimiters.
347 * @return Token string.
349 char *strsep(char **stringp, const char *delim)
353 if (!stringp || !*stringp || !**stringp)
356 token = walk = *stringp;
358 /* Walk, search for delimiters */
359 while(*walk && !strchr(delim, *walk))
373 /* Check that a character is in the valid range for the
377 static int _valid(char ch, int base)
379 char end = (base > 9) ? '9' : '0' + (base - 1);
381 /* all bases will be some subset of the 0-9 range */
383 if (ch >= '0' && ch <= end)
386 /* Bases > 11 will also have to match in the a-z range */
389 if (tolower(ch) >= 'a' &&
390 tolower(ch) <= 'a' + (base - 11))
397 /* Return the "value" of the character in the given base */
399 static int _offset(char ch, int base)
401 if (ch >= '0' && ch <= '9')
404 return tolower(ch) - 'a';
408 * Convert the initial portion of a string into a signed int
409 * @param ptr A pointer to the string to convert
410 * @param endptr A pointer to the unconverted part of the string
411 * @param base The base of the number to convert, or 0 for auto
412 * @return A signed integer representation of the string
415 long int strtol(const char *ptr, char **endptr, int base)
421 *endptr = (char *) ptr;
423 /* Purge whitespace */
425 for( ; *ptr && isspace(*ptr); ptr++);
435 /* Determine the base */
438 if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
440 else if (ptr[0] == '0') {
448 /* Base 16 allows the 0x on front - so skip over it */
451 if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
455 /* If the first character isn't valid, then don't
458 if (!*ptr || !_valid(*ptr, base))
461 for( ; *ptr && _valid(*ptr, base); ptr++)
462 ret = (ret * base) + _offset(*ptr, base);
465 *endptr = (char *) ptr;
467 return ret * negative;
471 * Convert the initial portion of a string into an unsigned int
472 * @param ptr A pointer to the string to convert
473 * @param endptr A pointer to the unconverted part of the string
474 * @param base The base of the number to convert, or 0 for auto
475 * @return An unsigned integer representation of the string
478 unsigned long int strtoul(const char *ptr, char **endptr, int base)
483 *endptr = (char *) ptr;
485 /* Purge whitespace */
487 for( ; *ptr && isspace(*ptr); ptr++);
492 /* Determine the base */
495 if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
497 else if (ptr[0] == '0') {
505 /* Base 16 allows the 0x on front - so skip over it */
508 if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
512 /* If the first character isn't valid, then don't
515 if (!*ptr || !_valid(*ptr, base))
518 for( ; *ptr && _valid(*ptr, base); ptr++)
519 ret = (ret * base) + _offset(*ptr, base);
522 *endptr = (char *) ptr;
528 * Determine the number of leading characters in s that match characters in a
529 * @param s A pointer to the string to analyse
530 * @param a A pointer to an array of characters that match the prefix
531 * @return The number of matching characters
534 size_t strspn(const char *s, const char *a)
538 for (i = 0; s[i] != 0; i++) {
540 for (j = 0; j < al; j++) {
553 * Determine the number of leading characters in s that do not match characters in a
554 * @param s A pointer to the string to analyse
555 * @param a A pointer to an array of characters that do not match the prefix
556 * @return The number of not matching characters
559 size_t strcspn(const char *s, const char *a)
563 for (i = 0; s[i] != 0; i++) {
565 for (j = 0; j < al; j++) {
578 * Extract first token in string str that is delimited by a character in tokens.
579 * Destroys str and eliminates the token delimiter.
580 * @param str A pointer to the string to tokenize.
581 * @param delim A pointer to an array of characters that delimit the token
582 * @param ptr A pointer to a string pointer to keep state of the tokenizer
583 * @return Pointer to token
586 char* strtok_r(char *str, const char *delim, char **ptr)
588 /* start new tokenizing job or continue existing one? */
592 /* skip over prefix delimiters */
593 char *start = str + strspn(str, delim);
595 /* find first delimiter character */
596 char *end = start + strcspn(start, delim);
603 static char **strtok_global;
606 * Extract first token in string str that is delimited by a character in tokens.
607 * Destroys str, eliminates the token delimiter and uses global state.
608 * @param str A pointer to the string to tokenize.
609 * @param delim A pointer to an array of characters that delimit the token
610 * @return Pointer to token
613 char* strtok(char *str, const char *delim)
615 return strtok_r(str, delim, strtok_global);
619 * Print error message and error number
620 * @param s Error message to print
622 void perror(const char *s)
624 printf("%s: %d\n", s?s:"(none)", errno);