blob: a19f017852082afe84f27f1a12d0b4d84842902a [file] [log] [blame]
Julius Wernera66c9b82019-05-20 14:53:47 -07001#include <assert.h>
2#include <rules.h>
Thomas Heijligen92043552019-01-29 12:48:01 +01003#include <string.h>
4#include <stddef.h>
5#include <stdlib.h>
6
7char *strdup(const char *s)
8{
Julius Wernera66c9b82019-05-20 14:53:47 -07009 if (!ENV_RAMSTAGE)
10 dead_code(); /* This can't be used without malloc(). */
11
Thomas Heijligen92043552019-01-29 12:48:01 +010012 size_t sz = strlen(s) + 1;
13 char *d = malloc(sz);
Thejaswani Putta6f5225c2019-04-11 18:36:08 -070014 if (d)
15 memcpy(d, s, sz);
Thomas Heijligen92043552019-01-29 12:48:01 +010016 return d;
17}
18
19char *strconcat(const char *s1, const char *s2)
20{
Julius Wernera66c9b82019-05-20 14:53:47 -070021 if (!ENV_RAMSTAGE)
22 dead_code(); /* This can't be used without malloc(). */
23
Thomas Heijligen92043552019-01-29 12:48:01 +010024 size_t sz_1 = strlen(s1);
25 size_t sz_2 = strlen(s2);
26 char *d = malloc(sz_1 + sz_2 + 1);
Thejaswani Putta6f5225c2019-04-11 18:36:08 -070027 if (d) {
28 memcpy(d, s1, sz_1);
29 memcpy(d + sz_1, s2, sz_2 + 1);
30 }
Thomas Heijligen92043552019-01-29 12:48:01 +010031 return d;
32}
Julius Wernera66c9b82019-05-20 14:53:47 -070033
34size_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
42size_t strlen(const char *src)
43{
44 size_t i = 0;
45 while (*src++)
46 i++;
47 return i;
48}
49
50char *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
60char *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
72char *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
92char *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
103int 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
114int 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
126unsigned 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}