coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
string.c
Go to the documentation of this file.
1 #include <assert.h>
2 #include <ctype.h>
3 #include <string.h>
4 #include <stddef.h>
5 #include <stdlib.h>
6 
7 char *strdup(const char *s)
8 {
9  if (!ENV_RAMSTAGE)
10  dead_code(); /* This can't be used without malloc(). */
11 
12  size_t sz = strlen(s) + 1;
13  char *d = malloc(sz);
14  if (d)
15  memcpy(d, s, sz);
16  return d;
17 }
18 
19 char *strconcat(const char *s1, const char *s2)
20 {
21  if (!ENV_RAMSTAGE)
22  dead_code(); /* This can't be used without malloc(). */
23 
24  size_t sz_1 = strlen(s1);
25  size_t sz_2 = strlen(s2);
26  char *d = malloc(sz_1 + sz_2 + 1);
27  if (d) {
28  memcpy(d, s1, sz_1);
29  memcpy(d + sz_1, s2, sz_2 + 1);
30  }
31  return d;
32 }
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 }
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 
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 
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 
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 }
void * memcpy(void *dest, const void *src, size_t n)
Definition: memcpy.c:7
#define dead_code()
Definition: assert.h:89
static int isdigit(int c)
Definition: ctype.h:20
void * malloc(size_t size)
Definition: malloc.c:53
#define ENV_RAMSTAGE
Definition: rules.h:150
#define NULL
Definition: stddef.h:19
char * strrchr(const char *s, int c)
Find a character in a string.
Definition: string.c:60
int strcmp(const char *s1, const char *s2)
Definition: string.c:103
char * strchr(const char *s, int c)
Definition: string.c:50
char * strncpy(char *to, const char *from, int count)
Definition: string.c:72
char * strcpy(char *dst, const char *src)
Definition: string.c:92
char * strconcat(const char *s1, const char *s2)
Definition: string.c:19
unsigned int skip_atoi(char **s)
Definition: string.c:126
int strncmp(const char *s1, const char *s2, int maxlen)
Definition: string.c:114
char * strstr(const char *haystack, const char *needle)
Definition: string.c:165
int strcspn(const char *str, const char *spn)
Definition: string.c:150
char * strtok(char *str, const char *delim)
Definition: string.c:193
size_t strnlen(const char *src, size_t max)
Definition: string.c:34
int strspn(const char *str, const char *spn)
Definition: string.c:135
char * strdup(const char *s)
Definition: string.c:7
size_t strlen(const char *src)
Definition: string.c:42
long atol(const char *str)
Definition: string.c:200
char * strtok_r(char *str, const char *delim, char **ptr)
Definition: string.c:175
#define s(param, src_bits, pmcreg, dst_bits)
#define c(value, pmcreg, dst_bits)
#define count