1 | /*
|
---|
2 | * Copyright (c) 2011 Petr Koupy
|
---|
3 | * Copyright (c) 2011 Jiri Zarevucky
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup libposix
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /** @file
|
---|
34 | */
|
---|
35 |
|
---|
36 | #define LIBPOSIX_INTERNAL
|
---|
37 |
|
---|
38 | #include "string.h"
|
---|
39 |
|
---|
40 | #include <assert.h>
|
---|
41 | #include <str_error.h>
|
---|
42 | #include <stdlib.h>
|
---|
43 | #include <errno.h>
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Defined for convenience. Returns pointer to the terminating nul character.
|
---|
47 | *
|
---|
48 | * @param s
|
---|
49 | * @return
|
---|
50 | */
|
---|
51 | static char *strzero(const char *s)
|
---|
52 | {
|
---|
53 | while (*s != '\0') {
|
---|
54 | s++;
|
---|
55 | }
|
---|
56 |
|
---|
57 | return (char *) s;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Returns true if s2 is a prefix of s1.
|
---|
62 | *
|
---|
63 | * @param s1
|
---|
64 | * @param s2
|
---|
65 | * @return
|
---|
66 | */
|
---|
67 | static bool begins_with(const char *s1, const char *s2)
|
---|
68 | {
|
---|
69 | while (*s1 == *s2 && *s2 != '\0') {
|
---|
70 | s1++;
|
---|
71 | s2++;
|
---|
72 | }
|
---|
73 |
|
---|
74 | /* true if the end was reached */
|
---|
75 | return *s2 == '\0';
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * The same as strpbrk, except it returns pointer to the nul terminator
|
---|
80 | * if no occurence is found.
|
---|
81 | *
|
---|
82 | * @param s1
|
---|
83 | * @param s2
|
---|
84 | * @return
|
---|
85 | */
|
---|
86 | static char *strpbrk_null(const char *s1, const char *s2)
|
---|
87 | {
|
---|
88 | while (!posix_strchr(s2, *s1)) {
|
---|
89 | ++s1;
|
---|
90 | }
|
---|
91 |
|
---|
92 | return (char *) s1;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | *
|
---|
97 | * @param dest
|
---|
98 | * @param src
|
---|
99 | * @return
|
---|
100 | */
|
---|
101 | char *posix_strcpy(char *dest, const char *src)
|
---|
102 | {
|
---|
103 | posix_stpcpy(dest, src);
|
---|
104 | return dest;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | *
|
---|
109 | * @param dest
|
---|
110 | * @param src
|
---|
111 | * @param n
|
---|
112 | * @return
|
---|
113 | */
|
---|
114 | char *posix_strncpy(char *dest, const char *src, size_t n)
|
---|
115 | {
|
---|
116 | posix_stpncpy(dest, src, n);
|
---|
117 | return dest;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | *
|
---|
122 | * @param dest
|
---|
123 | * @param src
|
---|
124 | * @return Pointer to the nul character in the dest string
|
---|
125 | */
|
---|
126 | char *posix_stpcpy(char *restrict dest, const char *restrict src)
|
---|
127 | {
|
---|
128 | assert(dest != NULL);
|
---|
129 | assert(src != NULL);
|
---|
130 |
|
---|
131 | for (size_t i = 0; ; ++i) {
|
---|
132 | dest[i] = src[i];
|
---|
133 |
|
---|
134 | if (src[i] == '\0') {
|
---|
135 | /* pointer to the terminating nul character */
|
---|
136 | return &dest[i];
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | /* unreachable */
|
---|
141 | return NULL;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | *
|
---|
146 | * @param dest
|
---|
147 | * @param src
|
---|
148 | * @param n
|
---|
149 | * @return Pointer to the first written nul character or &dest[n]
|
---|
150 | */
|
---|
151 | char *posix_stpncpy(char *restrict dest, const char *restrict src, size_t n)
|
---|
152 | {
|
---|
153 | assert(dest != NULL);
|
---|
154 | assert(src != NULL);
|
---|
155 |
|
---|
156 | for (size_t i = 0; i < n; ++i) {
|
---|
157 | dest[i] = src[i];
|
---|
158 |
|
---|
159 | /* the standard requires that nul characters
|
---|
160 | * are appended to the length of n, in case src is shorter
|
---|
161 | */
|
---|
162 | if (src[i] == '\0') {
|
---|
163 | char *result = &dest[i];
|
---|
164 | for (++i; i < n; ++i) {
|
---|
165 | dest[i] = '\0';
|
---|
166 | }
|
---|
167 | return result;
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | return &dest[n];
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | *
|
---|
176 | * @param dest
|
---|
177 | * @param src
|
---|
178 | * @return
|
---|
179 | */
|
---|
180 | char *posix_strcat(char *dest, const char *src)
|
---|
181 | {
|
---|
182 | assert(dest != NULL);
|
---|
183 | assert(src != NULL);
|
---|
184 |
|
---|
185 | posix_strcpy(strzero(dest), src);
|
---|
186 | return dest;
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | *
|
---|
191 | * @param dest
|
---|
192 | * @param src
|
---|
193 | * @param n
|
---|
194 | * @return
|
---|
195 | */
|
---|
196 | char *posix_strncat(char *dest, const char *src, size_t n)
|
---|
197 | {
|
---|
198 | assert(dest != NULL);
|
---|
199 | assert(src != NULL);
|
---|
200 |
|
---|
201 | char *zeroptr = posix_strncpy(strzero(dest), src, n);
|
---|
202 | /* strncpy doesn't append the nul terminator, so we do it here */
|
---|
203 | zeroptr[n] = '\0';
|
---|
204 | return dest;
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | *
|
---|
209 | * @param dest
|
---|
210 | * @param src
|
---|
211 | * @param c
|
---|
212 | * @param n
|
---|
213 | * @return Pointer to the first byte after c in dest if found, NULL otherwise.
|
---|
214 | */
|
---|
215 | void *posix_memccpy(void *dest, const void *src, int c, size_t n)
|
---|
216 | {
|
---|
217 | assert(dest != NULL);
|
---|
218 | assert(src != NULL);
|
---|
219 |
|
---|
220 | unsigned char* bdest = dest;
|
---|
221 | const unsigned char* bsrc = src;
|
---|
222 |
|
---|
223 | for (size_t i = 0; i < n; ++i) {
|
---|
224 | bdest[i] = bsrc[i];
|
---|
225 |
|
---|
226 | if (bsrc[i] == (unsigned char) c) {
|
---|
227 | /* pointer to the next byte */
|
---|
228 | return &bdest[i + 1];
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | return NULL;
|
---|
233 | }
|
---|
234 |
|
---|
235 | /**
|
---|
236 | *
|
---|
237 | * @param s
|
---|
238 | * @return Newly allocated string
|
---|
239 | */
|
---|
240 | char *posix_strdup(const char *s)
|
---|
241 | {
|
---|
242 | // FIXME: SIZE_MAX doesn't work
|
---|
243 | return posix_strndup(s, STR_NO_LIMIT);
|
---|
244 | }
|
---|
245 |
|
---|
246 | /**
|
---|
247 | *
|
---|
248 | * @param s
|
---|
249 | * @param n
|
---|
250 | * @return Newly allocated string of length at most n
|
---|
251 | */
|
---|
252 | char *posix_strndup(const char *s, size_t n)
|
---|
253 | {
|
---|
254 | assert(s != NULL);
|
---|
255 |
|
---|
256 | size_t len = posix_strnlen(s, n);
|
---|
257 | char *dup = malloc(len + 1);
|
---|
258 | if (dup == NULL) {
|
---|
259 | return NULL;
|
---|
260 | }
|
---|
261 |
|
---|
262 | memcpy(dup, s, len);
|
---|
263 | dup[len] = '\0';
|
---|
264 |
|
---|
265 | return dup;
|
---|
266 | }
|
---|
267 |
|
---|
268 | /**
|
---|
269 | *
|
---|
270 | * @param mem1
|
---|
271 | * @param mem2
|
---|
272 | * @param n
|
---|
273 | * @return Difference of the first pair of inequal bytes,
|
---|
274 | * or 0 if areas have the same content
|
---|
275 | */
|
---|
276 | int posix_memcmp(const void *mem1, const void *mem2, size_t n)
|
---|
277 | {
|
---|
278 | assert(mem1 != NULL);
|
---|
279 | assert(mem2 != NULL);
|
---|
280 |
|
---|
281 | const unsigned char *s1 = mem1;
|
---|
282 | const unsigned char *s2 = mem2;
|
---|
283 |
|
---|
284 | for (size_t i = 0; i < n; ++i) {
|
---|
285 | if (s1[i] != s2[i]) {
|
---|
286 | return s2[i] - s1[i];
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | return 0;
|
---|
291 | }
|
---|
292 |
|
---|
293 | /**
|
---|
294 | *
|
---|
295 | * @param s1
|
---|
296 | * @param s2
|
---|
297 | * @return
|
---|
298 | */
|
---|
299 | int posix_strcmp(const char *s1, const char *s2)
|
---|
300 | {
|
---|
301 | assert(s1 != NULL);
|
---|
302 | assert(s2 != NULL);
|
---|
303 |
|
---|
304 | return posix_strncmp(s1, s2, STR_NO_LIMIT);
|
---|
305 | }
|
---|
306 |
|
---|
307 | /**
|
---|
308 | *
|
---|
309 | * @param s1
|
---|
310 | * @param s2
|
---|
311 | * @param n
|
---|
312 | * @return
|
---|
313 | */
|
---|
314 | int posix_strncmp(const char *s1, const char *s2, size_t n)
|
---|
315 | {
|
---|
316 | assert(s1 != NULL);
|
---|
317 | assert(s2 != NULL);
|
---|
318 |
|
---|
319 | for (size_t i = 0; i < n; ++i) {
|
---|
320 | if (s1[i] != s2[i]) {
|
---|
321 | return s2[i] - s1[i];
|
---|
322 | }
|
---|
323 | if (s1[i] == '\0') {
|
---|
324 | break;
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | return 0;
|
---|
329 | }
|
---|
330 |
|
---|
331 | /**
|
---|
332 | *
|
---|
333 | * @param mem
|
---|
334 | * @param c
|
---|
335 | * @param n
|
---|
336 | * @return
|
---|
337 | */
|
---|
338 | void *posix_memchr(const void *mem, int c, size_t n)
|
---|
339 | {
|
---|
340 | assert(mem != NULL);
|
---|
341 |
|
---|
342 | const unsigned char *s = mem;
|
---|
343 |
|
---|
344 | for (size_t i = 0; i < n; ++i) {
|
---|
345 | if (s[i] == (unsigned char) c) {
|
---|
346 | return (void *) &s[i];
|
---|
347 | }
|
---|
348 | }
|
---|
349 | return NULL;
|
---|
350 | }
|
---|
351 |
|
---|
352 | /**
|
---|
353 | *
|
---|
354 | * @param s
|
---|
355 | * @param c
|
---|
356 | * @return
|
---|
357 | */
|
---|
358 | char *posix_strchr(const char *s, int c)
|
---|
359 | {
|
---|
360 | assert(s != NULL);
|
---|
361 |
|
---|
362 | /* special handling for the case that zero is searched for */
|
---|
363 | if (c == '\0') {
|
---|
364 | return strzero(s);
|
---|
365 | }
|
---|
366 |
|
---|
367 | /* otherwise just loop through the string until found */
|
---|
368 | while (*s != (char) c) {
|
---|
369 | if (*s == '\0') {
|
---|
370 | return NULL;
|
---|
371 | }
|
---|
372 |
|
---|
373 | s++;
|
---|
374 | }
|
---|
375 |
|
---|
376 | return (char *) s;
|
---|
377 | }
|
---|
378 |
|
---|
379 | /**
|
---|
380 | *
|
---|
381 | * @param s
|
---|
382 | * @param c
|
---|
383 | * @return
|
---|
384 | */
|
---|
385 | char *posix_strrchr(const char *s, int c)
|
---|
386 | {
|
---|
387 | assert(s != NULL);
|
---|
388 |
|
---|
389 | const char *ptr = strzero(s);
|
---|
390 |
|
---|
391 | /* the same as in strchr, except it loops in reverse direction */
|
---|
392 | while (*ptr != (char) c) {
|
---|
393 | if (ptr == s) {
|
---|
394 | return NULL;
|
---|
395 | }
|
---|
396 |
|
---|
397 | ptr++;
|
---|
398 | }
|
---|
399 |
|
---|
400 | return (char *) ptr;
|
---|
401 | }
|
---|
402 |
|
---|
403 | /**
|
---|
404 | *
|
---|
405 | * @param s1
|
---|
406 | * @param s2
|
---|
407 | * @return
|
---|
408 | */
|
---|
409 | char *posix_strpbrk(const char *s1, const char *s2)
|
---|
410 | {
|
---|
411 | assert(s1 != NULL);
|
---|
412 | assert(s2 != NULL);
|
---|
413 |
|
---|
414 | char *ptr = strpbrk_null(s1, s2);
|
---|
415 | return (*ptr == '\0') ? NULL : ptr;
|
---|
416 | }
|
---|
417 |
|
---|
418 | /**
|
---|
419 | *
|
---|
420 | * @param s1
|
---|
421 | * @param s2
|
---|
422 | * @return
|
---|
423 | */
|
---|
424 | size_t posix_strcspn(const char *s1, const char *s2)
|
---|
425 | {
|
---|
426 | assert(s1 != NULL);
|
---|
427 | assert(s2 != NULL);
|
---|
428 |
|
---|
429 | char *ptr = strpbrk_null(s1, s2);
|
---|
430 | return (size_t) (ptr - s1);
|
---|
431 | }
|
---|
432 |
|
---|
433 | /**
|
---|
434 | *
|
---|
435 | * @param s1
|
---|
436 | * @param s2
|
---|
437 | * @return
|
---|
438 | */
|
---|
439 | size_t posix_strspn(const char *s1, const char *s2)
|
---|
440 | {
|
---|
441 | assert(s1 != NULL);
|
---|
442 | assert(s2 != NULL);
|
---|
443 |
|
---|
444 | const char *ptr;
|
---|
445 | for (ptr = s1; *ptr != '\0'; ++ptr) {
|
---|
446 | if (!posix_strchr(s2, *ptr)) {
|
---|
447 | break;
|
---|
448 | }
|
---|
449 | }
|
---|
450 | return ptr - s1;
|
---|
451 | }
|
---|
452 |
|
---|
453 | /**
|
---|
454 | *
|
---|
455 | * @param s1
|
---|
456 | * @param s2
|
---|
457 | * @return
|
---|
458 | */
|
---|
459 | char *posix_strstr(const char *s1, const char *s2)
|
---|
460 | {
|
---|
461 | assert(s1 != NULL);
|
---|
462 | assert(s2 != NULL);
|
---|
463 |
|
---|
464 | /* special case - needle is an empty string */
|
---|
465 | if (*s2 == '\0') {
|
---|
466 | return (char *) s1;
|
---|
467 | }
|
---|
468 |
|
---|
469 | // TODO: use faster algorithm
|
---|
470 | /* check for prefix from every position - quadratic complexity */
|
---|
471 | while (*s1 != '\0') {
|
---|
472 | if (begins_with(s1, s2)) {
|
---|
473 | return (char *) s1;
|
---|
474 | }
|
---|
475 |
|
---|
476 | s1++;
|
---|
477 | }
|
---|
478 |
|
---|
479 | return NULL;
|
---|
480 | }
|
---|
481 |
|
---|
482 | /**
|
---|
483 | * Currently ignores locale and just calls strcmp.
|
---|
484 | *
|
---|
485 | * @param s1
|
---|
486 | * @param s2
|
---|
487 | * @return
|
---|
488 | */
|
---|
489 | int posix_strcoll(const char *s1, const char *s2)
|
---|
490 | {
|
---|
491 | assert(s1 != NULL);
|
---|
492 | assert(s2 != NULL);
|
---|
493 |
|
---|
494 | return posix_strcmp(s1, s2);
|
---|
495 | }
|
---|
496 |
|
---|
497 | /**
|
---|
498 | * strcoll is equal to strcmp here, so this just makes a copy.
|
---|
499 | *
|
---|
500 | * @param s1
|
---|
501 | * @param s2
|
---|
502 | * @param n
|
---|
503 | * @return
|
---|
504 | */
|
---|
505 | size_t posix_strxfrm(char *s1, const char *s2, size_t n)
|
---|
506 | {
|
---|
507 | assert(s1 != NULL || n == 0);
|
---|
508 | assert(s2 != NULL);
|
---|
509 |
|
---|
510 | size_t len = posix_strlen(s2);
|
---|
511 |
|
---|
512 | if (n > len) {
|
---|
513 | posix_strcpy(s1, s2);
|
---|
514 | }
|
---|
515 |
|
---|
516 | return len;
|
---|
517 | }
|
---|
518 |
|
---|
519 | /**
|
---|
520 | *
|
---|
521 | * @param errnum
|
---|
522 | * @return
|
---|
523 | */
|
---|
524 | char *posix_strerror(int errnum)
|
---|
525 | {
|
---|
526 | /* uses function from libc, we just have to negate errno
|
---|
527 | * (POSIX uses positive errorcodes, HelenOS has negative)
|
---|
528 | */
|
---|
529 | return (char *) str_error(-errnum);
|
---|
530 | }
|
---|
531 |
|
---|
532 | /**
|
---|
533 | *
|
---|
534 | * @param errnum Error code
|
---|
535 | * @param buf Buffer to store a human readable string to
|
---|
536 | * @param bufsz Size of buffer pointed to by buf
|
---|
537 | * @return
|
---|
538 | */
|
---|
539 | int posix_strerror_r(int errnum, char *buf, size_t bufsz)
|
---|
540 | {
|
---|
541 | assert(buf != NULL);
|
---|
542 |
|
---|
543 | char *errstr = posix_strerror(errnum);
|
---|
544 | /* HelenOS str_error can't fail */
|
---|
545 |
|
---|
546 | if (posix_strlen(errstr) + 1 > bufsz) {
|
---|
547 | return -ERANGE;
|
---|
548 | } else {
|
---|
549 | posix_strcpy(buf, errstr);
|
---|
550 | }
|
---|
551 |
|
---|
552 | return 0;
|
---|
553 | }
|
---|
554 |
|
---|
555 | /**
|
---|
556 | *
|
---|
557 | * @param s
|
---|
558 | * @return
|
---|
559 | */
|
---|
560 | size_t posix_strlen(const char *s)
|
---|
561 | {
|
---|
562 | assert(s != NULL);
|
---|
563 |
|
---|
564 | return (size_t) (strzero(s) - s);
|
---|
565 | }
|
---|
566 |
|
---|
567 | /**
|
---|
568 | *
|
---|
569 | * @param s
|
---|
570 | * @param n
|
---|
571 | * @return
|
---|
572 | */
|
---|
573 | size_t posix_strnlen(const char *s, size_t n)
|
---|
574 | {
|
---|
575 | assert(s != NULL);
|
---|
576 |
|
---|
577 | for (size_t sz = 0; sz < n; ++sz) {
|
---|
578 |
|
---|
579 | if (s[sz] == '\0') {
|
---|
580 | return sz;
|
---|
581 | }
|
---|
582 | }
|
---|
583 |
|
---|
584 | return n;
|
---|
585 | }
|
---|
586 |
|
---|
587 | /** @}
|
---|
588 | */
|
---|