1 | /* |
---|
2 | * string.c - User level <string> library implementation. |
---|
3 | * |
---|
4 | * Author Alain Greiner (2016,2017,2018) |
---|
5 | * |
---|
6 | * Copyright (c) UPMC Sorbonne Universites |
---|
7 | * |
---|
8 | * This file is part of ALMOS-MKH. |
---|
9 | * |
---|
10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
11 | * under the terms of the GNU General Public License as published by |
---|
12 | * the Free Software Foundation; version 2.0 of the License. |
---|
13 | * |
---|
14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | * General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
22 | */ |
---|
23 | |
---|
24 | #define NULL (void *)0 |
---|
25 | |
---|
26 | #include <string.h> |
---|
27 | |
---|
28 | /////////////////////////////////////// |
---|
29 | unsigned int strlen( const char * str ) |
---|
30 | { |
---|
31 | register const char * ptr = str; |
---|
32 | |
---|
33 | while (* ptr) ptr++; |
---|
34 | |
---|
35 | return (ptr - str); |
---|
36 | } |
---|
37 | |
---|
38 | /////////////////////////////////////// |
---|
39 | unsigned int strnlen( const char * str, |
---|
40 | unsigned int maxlen ) |
---|
41 | { |
---|
42 | register const char *ptr = str; |
---|
43 | |
---|
44 | while (*ptr && (maxlen-- > 0)) ptr++; |
---|
45 | |
---|
46 | return (ptr - str); |
---|
47 | } |
---|
48 | |
---|
49 | //////////////////////////// |
---|
50 | int strcmp( const char * s1, |
---|
51 | const char * s2 ) |
---|
52 | { |
---|
53 | |
---|
54 | while (*s1 && *s1 == *s2) |
---|
55 | { |
---|
56 | s1 ++; |
---|
57 | s2 ++; |
---|
58 | } |
---|
59 | |
---|
60 | return (*s1 - *s2); |
---|
61 | } |
---|
62 | |
---|
63 | ////////////////////////////// |
---|
64 | int strncmp( const char * s1, |
---|
65 | const char * s2, |
---|
66 | unsigned int n ) |
---|
67 | { |
---|
68 | if (n == 0) |
---|
69 | return s1 - s2; // pseudo random result... |
---|
70 | |
---|
71 | while (*s1 && (*s1 == *s2) && (n > 1)) |
---|
72 | { |
---|
73 | s1 ++; |
---|
74 | s2 ++; |
---|
75 | n--; |
---|
76 | } |
---|
77 | |
---|
78 | return (*s1 - *s2); |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | |
---|
83 | /////////////////////////// |
---|
84 | char * strcpy (char * dest, |
---|
85 | char * src ) |
---|
86 | { |
---|
87 | char *src_ptr = src; |
---|
88 | char *dst_ptr = dest; |
---|
89 | |
---|
90 | while(*src_ptr) *(dst_ptr++) = *(src_ptr++); |
---|
91 | |
---|
92 | *dst_ptr = 0; |
---|
93 | return dest; |
---|
94 | } |
---|
95 | |
---|
96 | //////////////////////////////////// |
---|
97 | char * strncpy( char * dest, |
---|
98 | char * src, |
---|
99 | unsigned int n ) |
---|
100 | { |
---|
101 | unsigned int i; |
---|
102 | |
---|
103 | for (i = 0; (i < n) && (src[i] != '\0') ; i++) dest[i] = src[i]; |
---|
104 | |
---|
105 | for (; i < n; i++) dest[i] = '\0'; |
---|
106 | |
---|
107 | return dest; |
---|
108 | } |
---|
109 | |
---|
110 | ////////////////////////////// |
---|
111 | char * strstr( char * s, |
---|
112 | const char * find ) |
---|
113 | { |
---|
114 | char sc; |
---|
115 | char c; |
---|
116 | unsigned int len; |
---|
117 | |
---|
118 | if ((c = *find++) != 0) |
---|
119 | { |
---|
120 | len = strlen( find ); |
---|
121 | do |
---|
122 | { |
---|
123 | do |
---|
124 | { |
---|
125 | if ((sc = *s++) == 0) return NULL; |
---|
126 | } |
---|
127 | while (sc != c); |
---|
128 | } |
---|
129 | while (strncmp(s, find, len) != 0); |
---|
130 | s--; |
---|
131 | } |
---|
132 | return s; |
---|
133 | } |
---|
134 | |
---|
135 | ////////////////////////////// |
---|
136 | char * strchr( const char * s, |
---|
137 | int c ) |
---|
138 | { |
---|
139 | while(*s && *s != (char) c) s++; |
---|
140 | |
---|
141 | if(*s == (char) c) return (char*)s; |
---|
142 | |
---|
143 | return NULL; |
---|
144 | } |
---|
145 | |
---|
146 | /////////////////////////////// |
---|
147 | char * strrchr( const char * t, |
---|
148 | int c ) |
---|
149 | { |
---|
150 | register char ch; |
---|
151 | register const char * l = 0; |
---|
152 | |
---|
153 | ch = c; |
---|
154 | for (;;) |
---|
155 | { |
---|
156 | if (*t == ch) l=t; |
---|
157 | if (!*t) return (char*)l; |
---|
158 | ++t; |
---|
159 | } |
---|
160 | return (char*)l; |
---|
161 | } |
---|
162 | |
---|
163 | /////////////////////////////////////////////////////////////// |
---|
164 | void * memcpy(void *_dst, const void * _src, unsigned int size) |
---|
165 | { |
---|
166 | unsigned int * dst = _dst; |
---|
167 | const unsigned int * src = _src; |
---|
168 | if (!((unsigned int) dst & 3) && !((unsigned int) src & 3) ) |
---|
169 | { |
---|
170 | while (size > 3) |
---|
171 | { |
---|
172 | *dst++ = *src++; |
---|
173 | size -= 4; |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | unsigned char *cdst = (unsigned char*)dst; |
---|
178 | unsigned char *csrc = (unsigned char*)src; |
---|
179 | |
---|
180 | while (size--) |
---|
181 | { |
---|
182 | *cdst++ = *csrc++; |
---|
183 | } |
---|
184 | return _dst; |
---|
185 | } |
---|
186 | |
---|
187 | ////////////////////////////////////////////////////////// |
---|
188 | inline void * memset(void * dst, int s, unsigned int size) |
---|
189 | { |
---|
190 | char * a = (char *) dst; |
---|
191 | while (size--) |
---|
192 | { |
---|
193 | *a++ = (char)s; |
---|
194 | } |
---|
195 | return dst; |
---|
196 | } |
---|
197 | |
---|
198 | |
---|