Xeon-SP boards: Factor out OCP VPD `get_cxl_mode()` impl
[coreboot2.git] / src / lib / string.c
blobdd7d1512ea1119ef129a1062fbf480cdf6fcb2c2
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <assert.h>
4 #include <ctype.h>
5 #include <string.h>
6 #include <stddef.h>
7 #include <stdlib.h>
9 char *strdup(const char *s)
11 if (!ENV_RAMSTAGE)
12 dead_code(); /* This can't be used without malloc(). */
14 size_t sz = strlen(s) + 1;
15 char *d = malloc(sz);
16 if (d)
17 memcpy(d, s, sz);
18 return d;
21 char *strconcat(const char *s1, const char *s2)
23 if (!ENV_RAMSTAGE)
24 dead_code(); /* This can't be used without malloc(). */
26 size_t sz_1 = strlen(s1);
27 size_t sz_2 = strlen(s2);
28 char *d = malloc(sz_1 + sz_2 + 1);
29 if (d) {
30 memcpy(d, s1, sz_1);
31 memcpy(d + sz_1, s2, sz_2 + 1);
33 return d;
36 size_t strnlen(const char *src, size_t max)
38 size_t i = 0;
39 while ((*src++) && (i < max))
40 i++;
41 return i;
44 size_t strlen(const char *src)
46 size_t i = 0;
47 while (*src++)
48 i++;
49 return i;
52 char *strchr(const char *s, int c)
54 do {
55 if (*s == c)
56 return (char *)s;
57 } while (*s++);
59 return NULL;
62 char *strrchr(const char *s, int c)
64 char *p = NULL;
66 do {
67 if (*s == c)
68 p = (char *)s;
69 } while (*s++);
71 return p;
74 char *strncpy(char *to, const char *from, int count)
76 char *ret = to;
77 char data;
79 while (count > 0) {
80 count--;
81 data = *from++;
82 *to++ = data;
83 if (data == '\0')
84 break;
87 while (count > 0) {
88 count--;
89 *to++ = '\0';
91 return ret;
94 char *strcpy(char *dst, const char *src)
96 char *ptr = dst;
98 while (*src)
99 *dst++ = *src++;
100 *dst = '\0';
102 return ptr;
105 int strcmp(const char *s1, const char *s2)
107 int r;
109 while ((r = (*s1 - *s2)) == 0 && *s1) {
110 s1++;
111 s2++;
113 return r;
116 int strncmp(const char *s1, const char *s2, int maxlen)
118 int i;
120 for (i = 0; i < maxlen; i++) {
121 if ((s1[i] != s2[i]) || (s1[i] == '\0'))
122 return s1[i] - s2[i];
125 return 0;
128 unsigned int skip_atoi(char **s)
130 unsigned int i = 0;
132 while (isdigit(**s))
133 i = i*10 + *((*s)++) - '0';
134 return i;
137 int strspn(const char *str, const char *spn)
139 int ret = 0;
141 while (*str != 0) {
142 const char *p;
143 for (p = spn; *str != *p; p++)
144 if (*p == '\0')
145 return ret;
146 ret++;
147 str++;
149 return ret;
152 int strcspn(const char *str, const char *spn)
154 int ret = 0;
156 while (*str != 0) {
157 const char *p;
158 for (p = spn; *p != '\0'; p++)
159 if (*p == *str)
160 return ret;
161 ret++;
162 str++;
164 return ret;
167 char *strstr(const char *haystack, const char *needle)
169 size_t needle_len = strlen(needle);
170 for (; *haystack; haystack++) {
171 if (!strncmp(haystack, needle, needle_len))
172 return (char *)haystack;
174 return NULL;
177 char *strtok_r(char *str, const char *delim, char **ptr)
179 char *start;
180 char *end;
182 if (str == NULL)
183 str = *ptr;
184 start = str + strspn(str, delim);
185 if (start[0] == '\0')
186 return NULL;
188 end = start + strcspn(start, delim);
189 *ptr = end;
190 if (end[0] != '\0')
191 *(*ptr)++ = '\0';
192 return start;
195 char *strtok(char *str, const char *delim)
197 static char *strtok_ptr;
199 return strtok_r(str, delim, &strtok_ptr);
202 long atol(const char *str)
204 long ret = 0;
205 long sign = 1;
207 str += strspn(str, " \t\n\r\f\v");
209 if (*str == '+') {
210 sign = 1;
211 str++;
212 } else if (*str == '-') {
213 sign = -1;
214 str++;
217 while (isdigit(*str)) {
218 ret *= 10;
219 ret += *str++ - '0';
221 return ret * sign;