soc/intel/xeon_sp/cpx: Fix PCU device IDs
[coreboot.git] / src / lib / string.c
blob25d37b68f0f65a96d06bca77f800a3b177fdd54e
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 char *strchr(const char *s, int c)
38 do {
39 if (*s == c)
40 return (char *)s;
41 } while (*s++);
43 return NULL;
46 char *strrchr(const char *s, int c)
48 char *p = NULL;
50 do {
51 if (*s == c)
52 p = (char *)s;
53 } while (*s++);
55 return p;
58 char *strncpy(char *to, const char *from, size_t count)
60 char *ret = to;
61 char data;
63 while (count > 0) {
64 count--;
65 data = *from++;
66 *to++ = data;
67 if (data == '\0')
68 break;
71 while (count > 0) {
72 count--;
73 *to++ = '\0';
75 return ret;
78 char *strcpy(char *dst, const char *src)
80 char *ptr = dst;
82 while (*src)
83 *dst++ = *src++;
84 *dst = '\0';
86 return ptr;
89 int strcmp(const char *s1, const char *s2)
91 int r;
93 while ((r = (*s1 - *s2)) == 0 && *s1) {
94 s1++;
95 s2++;
97 return r;
100 int strncmp(const char *s1, const char *s2, size_t maxlen)
102 size_t i;
104 for (i = 0; i < maxlen; i++) {
105 if ((s1[i] != s2[i]) || (s1[i] == '\0'))
106 return s1[i] - s2[i];
109 return 0;
112 size_t strspn(const char *str, const char *spn)
114 size_t ret = 0;
116 while (*str != 0) {
117 const char *p;
118 for (p = spn; *str != *p; p++)
119 if (*p == '\0')
120 return ret;
121 ret++;
122 str++;
124 return ret;
127 size_t strcspn(const char *str, const char *spn)
129 size_t ret = 0;
131 while (*str != 0) {
132 const char *p;
133 for (p = spn; *p != '\0'; p++)
134 if (*p == *str)
135 return ret;
136 ret++;
137 str++;
139 return ret;
142 char *strstr(const char *haystack, const char *needle)
144 size_t needle_len = strlen(needle);
145 for (; *haystack; haystack++) {
146 if (!strncmp(haystack, needle, needle_len))
147 return (char *)haystack;
149 return NULL;
152 char *strtok_r(char *str, const char *delim, char **ptr)
154 char *start;
155 char *end;
157 if (str == NULL)
158 str = *ptr;
159 start = str + strspn(str, delim);
160 if (start[0] == '\0')
161 return NULL;
163 end = start + strcspn(start, delim);
164 *ptr = end;
165 if (end[0] != '\0')
166 *(*ptr)++ = '\0';
167 return start;
170 char *strtok(char *str, const char *delim)
172 static char *strtok_ptr;
174 return strtok_r(str, delim, &strtok_ptr);
177 long atol(const char *str)
179 long ret = 0;
180 long sign = 1;
182 str += strspn(str, " \t\n\r\f\v");
184 if (*str == '+') {
185 sign = 1;
186 str++;
187 } else if (*str == '-') {
188 sign = -1;
189 str++;
192 while (isdigit(*str)) {
193 ret *= 10;
194 ret += *str++ - '0';
196 return ret * sign;