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