Add the implementation of tfs
[thunix.git] / include / string.h
blob09a8ddd5efa58263f8316e33441ff9a20b87393d
1 #ifndef _STRING_H
2 #define _STRING_H 1
4 #include <types.h>
5 #include <malloc.h>
7 #ifndef NULL
8 #define NULL ((void *) 0)
9 #endif
11 static int strcmp(const char *s1, const char *s2)
13 while ((*s1 == *s2) && *s1 != '\0') {
14 s1++;
15 s2++;
18 return (*s1 - *s2);
21 static int strncmp(const char *s1, const char *s2, int n)
23 while ((--n) && (*s1 == *s2) && *s1 != '\0') {
24 s1++;
25 s2++;
28 return (*s1 - *s2);
32 static char *strcpy(char *dest, const char *src)
34 char *tmp = dest;
36 while ((*dest++ = *src++) != '\0')
37 /* nothing */;
38 return tmp;
42 static char *strcat(char *dest, const char *src)
44 char *tmp = dest;
46 while (*dest)
47 dest++;
48 while ((*dest++ = *src++) != '\0')
50 return tmp;
54 static size_t strlen(const char *s)
56 const char *sc;
58 for (sc = s; *sc; ++sc)
59 /* nothing */;
60 return sc - s;
64 static char *strchr (const char *s, int c)
66 while (*s) {
67 if (*s == c)
68 return (char *) s;
69 s++;
72 return (void *)0;
75 static char *strrchr (char *s, int c)
77 char *end = s + strlen(s) - 1;
79 while (*end != c && end >= s)
80 end--;
81 if (end < s)
82 return NULL;
83 return end;
86 static char *strdup(const char *str)
88 char *s = malloc(strlen(str) + 1);
89 if (!s)
90 return s;
91 strcpy(s, str);
92 return s;
95 static void *memset(void *s, int c, size_t count)
97 char *xs = s;
99 while (count--)
100 *xs++ = c;
101 return s;
104 static void *memset_word(void *s, unsigned short word, size_t count)
106 unsigned short *xs = (unsigned short *)s;
108 while (count--)
109 *xs++ = word;
110 return s;
113 static void *memcpy(void *dest, const void *src, size_t count)
115 char *tmp = dest;
116 const char *s = src;
118 while (count--)
119 *tmp++ = *s++;
120 return dest;
124 static void *memmove(void *dest, const void *src, size_t count)
126 char *tmp;
127 const char *s;
129 if (dest <= src) {
130 tmp = dest;
131 s = src;
132 while (count--)
133 *tmp++ = *s++;
134 } else {
135 tmp = dest;
136 tmp += count;
137 s = src;
138 s += count;
139 while (count--)
140 *--tmp = *--s;
142 return dest;
146 static int memcmp(const void *cs, const void *ct, size_t count)
148 const unsigned char *su1, *su2;
149 int res = 0;
151 for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
152 if ((res = *su1 - *su2) != 0)
153 break;
154 return res;
158 #endif /* _STRING_H */