CS350 COS
COS
Loading...
Searching...
No Matches
string.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2006-2018 Ali Mashtizadeh
3 * All rights reserved.
4 */
5
6#include <stddef.h>
7#include <stdint.h>
8#include <string.h>
9
10char *
11strchr(const char *s, int c)
12{
13 int i;
14 for (i = 0; ; i++)
15 {
16 if (s[i] == c)
17 return (char *)s + i;
18 if (s[i] == '\0')
19 return NULL;
20 }
21}
22
23char *
24strcpy(char *to, const char *from)
25{
26 char *save = to;
27
28 for (; (*to = *from); ++from, ++to);
29
30 return save;
31}
32
33char *
34strncpy(char *to, const char *from, size_t length)
35{
36 char *save = to;
37
38 for (; (*to = *from) != '\0' && length > 0; ++from, ++to, length--);
39
40 *to = '\0';
41
42 return save;
43}
44
45char *
46strcat(char *s, const char *append)
47{
48 char *save = s;
49
50 for (; *s; ++s);
51 while ((*s++ = *append++));
52
53 return save;
54}
55
56
57char *
58strncat(char *dst, const char *src, size_t n)
59{
60 if (n != 0) {
61 char *d = dst;
62 const char *s = src;
63
64 while (*d != 0)
65 d++;
66
67 do {
68 if ((*d = *s++) == 0)
69 break;
70
71 d++;
72 } while (--n != 0);
73
74 *d = 0;
75 }
76
77 return dst;
78}
79
80int
81strcmp(const char *s1, const char *s2)
82{
83 while (*s1 == *s2++)
84 if (*s1++ == 0)
85 return 0;
86
87 return (*(const uint8_t *)s1 - *(const uint8_t *)(s2 - 1));
88}
89
90int
91strncmp(const char *s1, const char *s2, size_t len)
92{
93 if (len == 0)
94 return 0;
95
96 while (*s1 == *s2) {
97 if (*s1 == 0)
98 return 0;
99
100 s1++;
101 s2++;
102
103 len--;
104 if (len == 0)
105 return 0;
106 }
107
108 return (*(const uint8_t *)s1 - *(const uint8_t *)s2);
109}
110
111size_t
112strlen(const char *str)
113{
114 const char *s;
115
116 for (s = str; *s; ++s);
117
118 return (s - str);
119}
120
121char *
122strtok_r(char *str, const char *delim, char **last)
123{
124 char *rval;
125
126 if (str == NULL)
127 str = *last;
128
129 // Skip deliminaters in the front
130 while ((str[0] != '\0') && strchr(delim, str[0])) {
131 str++;
132 }
133
134 // We've reached the end of the string
135 if (str[0] == '\0')
136 return NULL;
137
138 // Return first non-delim until the next
139 rval = str;
140
141 // Skip deliminaters in the front
142 while ((str[0] != '\0') && !strchr(delim, str[0])) {
143 str++;
144 }
145
146 if (str[0] != '\0') {
147 str[0] = '\0';
148 str++;
149 }
150 *last = str;
151
152 return rval;
153}
154
155char *
156strtok(char *str, const char *delim)
157{
158 static char *last;
159
160 return strtok_r(str, delim, &last);
161}
162
163void *
164memset(void *dst, int c, size_t length)
165{
166 uint8_t *p = (uint8_t *)dst;
167
168 while (length-- != 0) {
169 *p = c;
170 p += 1;
171 };
172
173 return dst;
174}
175
176void *
177memcpy(void *dst, const void *src, size_t length)
178{
179 uint8_t *d = (uint8_t *)dst;
180 const uint8_t *s = (const uint8_t *)src;
181
182 while (length-- != 0) {
183 *d = *s;
184 d += 1;
185 s += 1;
186 };
187
188 return dst;
189}
190
191int
192memcmp(const void *b1, const void *b2, size_t length)
193{
194 int i;
195 const char *c1 = (const char *)b1;
196 const char *c2 = (const char *)b2;
197
198 for (i = 0; i < length; i++)
199 {
200 if (*c1 != *c2)
201 return *c2 - *c1;
202 c1++;
203 c2++;
204 }
205
206 return 0;
207}
208
size_t strlen(const char *str)
Definition: string.c:112
int strcmp(const char *s1, const char *s2)
Definition: string.c:81
char * strcpy(char *to, const char *from)
Definition: string.c:24
char * strncpy(char *to, const char *from, size_t length)
Definition: string.c:34
void * memcpy(void *dst, const void *src, size_t length)
Definition: string.c:177
char * strtok(char *str, const char *delim)
Definition: string.c:156
int memcmp(const void *b1, const void *b2, size_t length)
Definition: string.c:192
void * memset(void *dst, int c, size_t length)
Definition: string.c:164
char * strtok_r(char *str, const char *delim, char **last)
Definition: string.c:122
char * strchr(const char *s, int c)
Definition: string.c:11
int strncmp(const char *s1, const char *s2, size_t len)
Definition: string.c:91
char * strcat(char *s, const char *append)
Definition: string.c:46
char * strncat(char *dst, const char *src, size_t n)
Definition: string.c:58
uint64_t len
Definition: multiboot.h:2
#define NULL
Definition: stddef.h:6
unsigned char uint8_t
Definition: types.h:10