cpu/intel: Add socket types
[coreboot2.git] / src / lib / string.c
bloba7f8074fc7ec729aa10016259c0d0b23d17076ee
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, size_t 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, size_t maxlen)
118 size_t 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 size_t strspn(const char *str, const char *spn)
130 size_t ret = 0;
132 while (*str != 0) {
133 const char *p;
134 for (p = spn; *str != *p; p++)
135 if (*p == '\0')
136 return ret;
137 ret++;
138 str++;
140 return ret;
143 size_t strcspn(const char *str, const char *spn)
145 size_t ret = 0;
147 while (*str != 0) {
148 const char *p;
149 for (p = spn; *p != '\0'; p++)
150 if (*p == *str)
151 return ret;
152 ret++;
153 str++;
155 return ret;
158 char *strstr(const char *haystack, const char *needle)
160 size_t needle_len = strlen(needle);
161 for (; *haystack; haystack++) {
162 if (!strncmp(haystack, needle, needle_len))
163 return (char *)haystack;
165 return NULL;
168 char *strtok_r(char *str, const char *delim, char **ptr)
170 char *start;
171 char *end;
173 if (str == NULL)
174 str = *ptr;
175 start = str + strspn(str, delim);
176 if (start[0] == '\0')
177 return NULL;
179 end = start + strcspn(start, delim);
180 *ptr = end;
181 if (end[0] != '\0')
182 *(*ptr)++ = '\0';
183 return start;
186 char *strtok(char *str, const char *delim)
188 static char *strtok_ptr;
190 return strtok_r(str, delim, &strtok_ptr);
193 long atol(const char *str)
195 long ret = 0;
196 long sign = 1;
198 str += strspn(str, " \t\n\r\f\v");
200 if (*str == '+') {
201 sign = 1;
202 str++;
203 } else if (*str == '-') {
204 sign = -1;
205 str++;
208 while (isdigit(*str)) {
209 ret *= 10;
210 ret += *str++ - '0';
212 return ret * sign;