Julius Werner | a66c9b8 | 2019-05-20 14:53:47 -0700 | [diff] [blame] | 1 | #include <assert.h> |
Joel Kitching | 393c71c | 2019-06-16 16:09:42 +0800 | [diff] [blame] | 2 | #include <ctype.h> |
Thomas Heijligen | 9204355 | 2019-01-29 12:48:01 +0100 | [diff] [blame] | 3 | #include <string.h> |
| 4 | #include <stddef.h> |
| 5 | #include <stdlib.h> |
| 6 | |
| 7 | char *strdup(const char *s) |
| 8 | { |
Julius Werner | a66c9b8 | 2019-05-20 14:53:47 -0700 | [diff] [blame] | 9 | if (!ENV_RAMSTAGE) |
| 10 | dead_code(); /* This can't be used without malloc(). */ |
| 11 | |
Thomas Heijligen | 9204355 | 2019-01-29 12:48:01 +0100 | [diff] [blame] | 12 | size_t sz = strlen(s) + 1; |
| 13 | char *d = malloc(sz); |
Thejaswani Putta | 6f5225c | 2019-04-11 18:36:08 -0700 | [diff] [blame] | 14 | if (d) |
| 15 | memcpy(d, s, sz); |
Thomas Heijligen | 9204355 | 2019-01-29 12:48:01 +0100 | [diff] [blame] | 16 | return d; |
| 17 | } |
| 18 | |
| 19 | char *strconcat(const char *s1, const char *s2) |
| 20 | { |
Julius Werner | a66c9b8 | 2019-05-20 14:53:47 -0700 | [diff] [blame] | 21 | if (!ENV_RAMSTAGE) |
| 22 | dead_code(); /* This can't be used without malloc(). */ |
| 23 | |
Thomas Heijligen | 9204355 | 2019-01-29 12:48:01 +0100 | [diff] [blame] | 24 | size_t sz_1 = strlen(s1); |
| 25 | size_t sz_2 = strlen(s2); |
| 26 | char *d = malloc(sz_1 + sz_2 + 1); |
Thejaswani Putta | 6f5225c | 2019-04-11 18:36:08 -0700 | [diff] [blame] | 27 | if (d) { |
| 28 | memcpy(d, s1, sz_1); |
| 29 | memcpy(d + sz_1, s2, sz_2 + 1); |
| 30 | } |
Thomas Heijligen | 9204355 | 2019-01-29 12:48:01 +0100 | [diff] [blame] | 31 | return d; |
| 32 | } |
Julius Werner | a66c9b8 | 2019-05-20 14:53:47 -0700 | [diff] [blame] | 33 | |
| 34 | size_t strnlen(const char *src, size_t max) |
| 35 | { |
| 36 | size_t i = 0; |
| 37 | while ((*src++) && (i < max)) |
| 38 | i++; |
| 39 | return i; |
| 40 | } |
| 41 | |
| 42 | size_t strlen(const char *src) |
| 43 | { |
| 44 | size_t i = 0; |
| 45 | while (*src++) |
| 46 | i++; |
| 47 | return i; |
| 48 | } |
| 49 | |
| 50 | char *strchr(const char *s, int c) |
| 51 | { |
| 52 | do { |
| 53 | if (*s == c) |
| 54 | return (char *)s; |
| 55 | } while (*s++); |
| 56 | |
| 57 | return NULL; |
| 58 | } |
| 59 | |
| 60 | char *strrchr(const char *s, int c) |
| 61 | { |
| 62 | char *p = NULL; |
| 63 | |
| 64 | do { |
| 65 | if (*s == c) |
| 66 | p = (char *)s; |
| 67 | } while (*s++); |
| 68 | |
| 69 | return p; |
| 70 | } |
| 71 | |
| 72 | char *strncpy(char *to, const char *from, int count) |
| 73 | { |
| 74 | char *ret = to; |
| 75 | char data; |
| 76 | |
| 77 | while (count > 0) { |
| 78 | count--; |
| 79 | data = *from++; |
| 80 | *to++ = data; |
| 81 | if (data == '\0') |
| 82 | break; |
| 83 | } |
| 84 | |
| 85 | while (count > 0) { |
| 86 | count--; |
| 87 | *to++ = '\0'; |
| 88 | } |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | char *strcpy(char *dst, const char *src) |
| 93 | { |
| 94 | char *ptr = dst; |
| 95 | |
| 96 | while (*src) |
| 97 | *dst++ = *src++; |
| 98 | *dst = '\0'; |
| 99 | |
| 100 | return ptr; |
| 101 | } |
| 102 | |
| 103 | int strcmp(const char *s1, const char *s2) |
| 104 | { |
| 105 | int r; |
| 106 | |
| 107 | while ((r = (*s1 - *s2)) == 0 && *s1) { |
| 108 | s1++; |
| 109 | s2++; |
| 110 | } |
| 111 | return r; |
| 112 | } |
| 113 | |
| 114 | int strncmp(const char *s1, const char *s2, int maxlen) |
| 115 | { |
| 116 | int i; |
| 117 | |
| 118 | for (i = 0; i < maxlen; i++) { |
| 119 | if ((s1[i] != s2[i]) || (s1[i] == '\0')) |
| 120 | return s1[i] - s2[i]; |
| 121 | } |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | unsigned int skip_atoi(char **s) |
| 127 | { |
| 128 | unsigned int i = 0; |
| 129 | |
| 130 | while (isdigit(**s)) |
| 131 | i = i*10 + *((*s)++) - '0'; |
| 132 | return i; |
| 133 | } |
Yuji Sasaki | 6b212d8 | 2019-06-12 17:42:19 -0700 | [diff] [blame] | 134 | |
| 135 | int strspn(const char *str, const char *spn) |
| 136 | { |
| 137 | int ret = 0; |
| 138 | |
| 139 | while (*str != 0) { |
| 140 | const char *p; |
| 141 | for (p = spn; *str != *p; p++) |
| 142 | if (*p == '\0') |
| 143 | return ret; |
| 144 | ret++; |
| 145 | str++; |
| 146 | } |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | int strcspn(const char *str, const char *spn) |
| 151 | { |
| 152 | int ret = 0; |
| 153 | |
| 154 | while (*str != 0) { |
| 155 | const char *p; |
| 156 | for (p = spn; *p != '\0'; p++) |
| 157 | if (*p == *str) |
| 158 | return ret; |
| 159 | ret++; |
| 160 | str++; |
| 161 | } |
| 162 | return ret; |
| 163 | } |
| 164 | |
Jes Klinke | 683ac6f | 2020-07-31 09:58:49 -0700 | [diff] [blame] | 165 | char *strstr(const char *haystack, const char *needle) |
| 166 | { |
| 167 | size_t needle_len = strlen(needle); |
| 168 | for (; *haystack; haystack++) { |
| 169 | if (!strncmp(haystack, needle, needle_len)) |
| 170 | return (char *)haystack; |
| 171 | } |
| 172 | return NULL; |
| 173 | } |
| 174 | |
harshit | 7a6f27c | 2020-05-15 10:40:02 +0530 | [diff] [blame] | 175 | char *strtok_r(char *str, const char *delim, char **ptr) |
| 176 | { |
| 177 | char *start; |
| 178 | char *end; |
| 179 | |
| 180 | if (str == NULL) |
| 181 | str = *ptr; |
| 182 | start = str + strspn(str, delim); |
| 183 | if (start[0] == '\0') |
| 184 | return NULL; |
| 185 | |
| 186 | end = start + strcspn(start, delim); |
| 187 | *ptr = end; |
| 188 | if (end[0] != '\0') |
| 189 | *(*ptr)++ = '\0'; |
| 190 | return start; |
| 191 | } |
| 192 | |
| 193 | char *strtok(char *str, const char *delim) |
| 194 | { |
| 195 | static char *strtok_ptr; |
| 196 | |
| 197 | return strtok_r(str, delim, &strtok_ptr); |
| 198 | } |
| 199 | |
Yuji Sasaki | 6b212d8 | 2019-06-12 17:42:19 -0700 | [diff] [blame] | 200 | long atol(const char *str) |
| 201 | { |
| 202 | long ret = 0; |
| 203 | long sign = 1; |
| 204 | |
| 205 | str += strspn(str, " \t\n\r\f\v"); |
| 206 | |
| 207 | if (*str == '+') { |
| 208 | sign = 1; |
| 209 | str++; |
| 210 | } else if (*str == '-') { |
| 211 | sign = -1; |
| 212 | str++; |
| 213 | } |
| 214 | |
| 215 | while (isdigit(*str)) { |
| 216 | ret *= 10; |
| 217 | ret += *str++ - '0'; |
| 218 | } |
| 219 | return ret * sign; |
| 220 | } |