AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
string.h
Go to the documentation of this file.
1#ifndef _STRING_H
2#define _STRING_H
3
4#include <core/system.h>
5
6/* The kernel's own string and memory functions, as the C library's. */
7
8/* Formats a string into a buffer (core/snprintf.c). */
9int snprintf(char *s, size_t n, const char *fmt, ...);
10
12static inline void *memmove(void *dest, const void *src, size_t n)
13{
14 void *_dest = dest;
15
16 if ((uintptr_t) dest < (uintptr_t) src) {
17 while (n-- != 0)
18 *(char *) dest++ = *(char *) src++;
19 } else if ((uintptr_t) dest > (uintptr_t) src) {
20 while (n != 0) {
21 --n;
22 *((char *) dest + n) = *((char *) src + n);
23 }
24 }
25
26 return _dest;
27}
28
30static inline void *memcpy(void *dest, const void *src, size_t n)
31{
32 void *_dest = dest;
33 while (n-- != 0)
34 *(char *) dest++ = *(char *) src++;
35
36 return _dest;
37}
38
40static inline void *memset(void *s, int c, size_t n)
41{
42 void *_s = s;
43
44 while (n-- != 0) {
45 *(char *) s++ = (char) c;
46 }
47
48 return _s;
49}
50
53static inline int memcmp(const void *s1, const void *s2, size_t n)
54{
55 const unsigned char *a = s1, *b = s2;
56
57 for (; n; --n, ++a, ++b)
58 if (*a != *b)
59 return *a - *b;
60
61 return 0;
62}
63
66static inline int strcmp(const char *s1, const char *s2)
67{
68 while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2) {s1++; s2++;}
69
70 return (int)(*s1 - *s2);
71}
72
74static inline int strncmp(const char *s1, const char *s2, size_t n)
75{
76 for (; n != 0; --n, ++s1, ++s2) {
77 if (*s1 != *s2)
78 return (int) *(const unsigned char *) s1 - (int) *(const unsigned char *) s2;
79
80 if (*s1 == '\0')
81 break;
82 }
83
84 return 0;
85}
86
88static inline int strlen(const char *s)
89{
90 const char *_s = s;
91 while (*_s) _s++;
92 return _s - s;
93}
94
97static inline size_t strnlen(const char *s, size_t max)
98{
99 size_t n = 0;
100 while (n < max && s[n]) n++;
101 return n;
102}
103
106static inline char *strdup(const char *s)
107{
108 int len = strlen(s);
109 char *ret = kmalloc(len + 1, &M_BUFFER, 0);
110
111 if (!ret)
112 return NULL;
113
114 (void) memcpy(ret, s, len + 1);
115 return ret;
116}
117
119static inline char *strcpy(char *dst, const char *src)
120{
121 char *retval = dst;
122
123 while (*src != '\0')
124 *dst++ = *src++;
125
126 *dst = *src; /* the NUL */
127
128 return retval;
129}
130
133static inline char *stpcpy(char *dst, const char *src)
134{
135 while ((*dst = *src++))
136 ++dst;
137
138 return dst;
139}
140
147static inline char **tokenize(const char *s, char c)
148{
149 if (s == NULL || *s == '\0')
150 return NULL;
151
152 while (*s == c)
153 ++s;
154
155 char *tokens = strdup(s);
156
157 if (tokens == NULL)
158 return NULL;
159
160 int len = strlen(s);
161
162 if (!len) {
163 /* nothing but separators: no pieces */
164 kfree(tokens);
165
166 char **ret = kmalloc(sizeof(char *), &M_BUFFER, 0);
167
168 if (ret)
169 *ret = NULL;
170
171 return ret;
172 }
173
174 int i, count = 0;
175 for (i = 0; i < len; ++i) {
176 if (tokens[i] == c) {
177 tokens[i] = 0;
178 ++count;
179 }
180 }
181
182 if (s[len-1] != c)
183 ++count;
184
185 char **ret = kmalloc(sizeof(char *) * (count + 1), &M_BUFFER, 0);
186
187 if (!ret) {
188 kfree(tokens);
189 return NULL;
190 }
191
192 int j = 0;
193 ret[j++] = tokens;
194
195 for (i = 0; i < strlen(s) - 1; ++i)
196 if (tokens[i] == 0)
197 ret[j++] = &tokens[i+1];
198
199 ret[j] = NULL;
200
201 return ret;
202}
203
205static inline void free_tokens(char **ptr)
206{
207 if (!ptr) return;
208 if (*ptr)
209 kfree(*ptr);
210 kfree(ptr);
211}
212
213#endif /* !_STRING_H */
void * kmalloc(size_t size, struct malloc_type *type, int flags)
Allocates memory for one of the kernel's structures.
Definition kvmem.c:123
void kfree(void *)
Frees an object that kmalloc() gave.
Definition kvmem.c:146
int memcmp(const void *a, const void *b, size_t n)
Compares n bytes.
Definition libc.c:51
void * memmove(void *dst, const void *src, size_t n)
Copies n bytes; dst and src may overlap.
Definition libc.c:34
void * memset(void *dst, int c, size_t n)
Sets n bytes to c.
Definition libc.c:23
void * memcpy(void *dst, const void *src, size_t n)
Copies n bytes; dst and src must not overlap.
Definition libc.c:11
int snprintf(char *s, size_t n, const char *fmt,...)
Formats a string into a buffer.
Definition snprintf.c:226
#define NULL
Definition stddef.h:27
__SIZE_TYPE__ uintptr_t
Definition stddef.h:12
static int strcmp(const char *s1, const char *s2)
Compares two strings, as the C library's strcmp().
Definition string.h:66
static size_t strnlen(const char *s, size_t max)
Returns the length of a string, or max if there is no NUL in its first max bytes.
Definition string.h:97
static int strncmp(const char *s1, const char *s2, size_t n)
Compares at most n characters of two strings.
Definition string.h:74
static char ** tokenize(const char *s, char c)
Splits a string at each c, leading ones skipped.
Definition string.h:147
static int strlen(const char *s)
Returns the length of a string.
Definition string.h:88
static char * strcpy(char *dst, const char *src)
Copies a string, with its NUL, and returns dst.
Definition string.h:119
static char * strdup(const char *s)
Returns a copy of a string, from kmalloc(), or NULL if there's no memory for it.
Definition string.h:106
static char * stpcpy(char *dst, const char *src)
Copies a string, as strcpy(), but returns the end of dst (its NUL).
Definition string.h:133
static void free_tokens(char **ptr)
Frees what tokenize() returned.
Definition string.h:205