blob: eb6adb67c7186f03eb0da9562fd76912af6eafa7 [file] [log] [blame]
Julius Wernera66c9b82019-05-20 14:53:47 -07001#include <assert.h>
Joel Kitching393c71c2019-06-16 16:09:42 +08002#include <ctype.h>
Julius Wernera66c9b82019-05-20 14:53:47 -07003#include <rules.h>
Thomas Heijligen92043552019-01-29 12:48:01 +01004#include <string.h>
5#include <stddef.h>
6#include <stdlib.h>
7
8char *strdup(const char *s)
9{
Julius Wernera66c9b82019-05-20 14:53:47 -070010 if (!ENV_RAMSTAGE)
11 dead_code(); /* This can't be used without malloc(). */
12
Thomas Heijligen92043552019-01-29 12:48:01 +010013 size_t sz = strlen(s) + 1;
14 char *d = malloc(sz);
Thejaswani Putta6f5225c2019-04-11 18:36:08 -070015 if (d)
16 memcpy(d, s, sz);
Thomas Heijligen92043552019-01-29 12:48:01 +010017 return d;
18}
19
20char *strconcat(const char *s1, const char *s2)
21{
Julius Wernera66c9b82019-05-20 14:53:47 -070022 if (!ENV_RAMSTAGE)
23 dead_code(); /* This can't be used without malloc(). */
24
Thomas Heijligen92043552019-01-29 12:48:01 +010025 size_t sz_1 = strlen(s1);
26 size_t sz_2 = strlen(s2);
27 char *d = malloc(sz_1 + sz_2 + 1);
Thejaswani Putta6f5225c2019-04-11 18:36:08 -070028 if (d) {
29 memcpy(d, s1, sz_1);
30 memcpy(d + sz_1, s2, sz_2 + 1);
31 }
Thomas Heijligen92043552019-01-29 12:48:01 +010032 return d;
33}
Julius Wernera66c9b82019-05-20 14:53:47 -070034
35size_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
43size_t strlen(const char *src)
44{
45 size_t i = 0;
46 while (*src++)
47 i++;
48 return i;
49}
50
51char *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
61char *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
73char *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
93char *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
104int 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
115int 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
127unsigned 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}