source: mainline/uspace/lib/posix/source/string.c@ bd76871

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bd76871 was e8d3c6f5, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Align <assert.h> with standards, remove it from libposix,
and do not let malloc() use printf() with corrupted heap.

  • Property mode set to 100644
File size: 17.4 KB
Line 
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 String manipulation.
34 */
35
36#define LIBPOSIX_INTERNAL
37#define __POSIX_DEF__(x) posix_##x
38
39#include "internal/common.h"
40#include "posix/string.h"
41
42#include <assert.h>
43
44#include <errno.h>
45
46#include "posix/limits.h"
47#include "posix/stdlib.h"
48#include "posix/signal.h"
49
50#include "libc/str_error.h"
51
52/**
53 * The same as strpbrk, except it returns pointer to the nul terminator
54 * if no occurence is found.
55 *
56 * @param s1 String in which to look for the bytes.
57 * @param s2 String of bytes to look for.
58 * @return Pointer to the found byte on success, pointer to the
59 * string terminator otherwise.
60 */
61static char *strpbrk_null(const char *s1, const char *s2)
62{
63 while (!posix_strchr(s2, *s1)) {
64 ++s1;
65 }
66
67 return (char *) s1;
68}
69
70/**
71 * Copy a string.
72 *
73 * @param dest Destination pre-allocated buffer.
74 * @param src Source string to be copied.
75 * @return Pointer to the destination buffer.
76 */
77char *posix_strcpy(char *restrict dest, const char *restrict src)
78{
79 posix_stpcpy(dest, src);
80 return dest;
81}
82
83/**
84 * Copy fixed length string.
85 *
86 * @param dest Destination pre-allocated buffer.
87 * @param src Source string to be copied.
88 * @param n Number of bytes to be stored into destination buffer.
89 * @return Pointer to the destination buffer.
90 */
91char *posix_strncpy(char *restrict dest, const char *restrict src, size_t n)
92{
93 posix_stpncpy(dest, src, n);
94 return dest;
95}
96
97/**
98 * Copy a string.
99 *
100 * @param dest Destination pre-allocated buffer.
101 * @param src Source string to be copied.
102 * @return Pointer to the nul character in the destination string.
103 */
104char *posix_stpcpy(char *restrict dest, const char *restrict src)
105{
106 assert(dest != NULL);
107 assert(src != NULL);
108
109 for (size_t i = 0; ; ++i) {
110 dest[i] = src[i];
111
112 if (src[i] == '\0') {
113 /* pointer to the terminating nul character */
114 return &dest[i];
115 }
116 }
117
118 /* unreachable */
119 return NULL;
120}
121
122/**
123 * Copy fixed length string.
124 *
125 * @param dest Destination pre-allocated buffer.
126 * @param src Source string to be copied.
127 * @param n Number of bytes to be stored into destination buffer.
128 * @return Pointer to the first written nul character or &dest[n].
129 */
130char *posix_stpncpy(char *restrict dest, const char *restrict src, size_t n)
131{
132 assert(dest != NULL);
133 assert(src != NULL);
134
135 for (size_t i = 0; i < n; ++i) {
136 dest[i] = src[i];
137
138 /* the standard requires that nul characters
139 * are appended to the length of n, in case src is shorter
140 */
141 if (src[i] == '\0') {
142 char *result = &dest[i];
143 for (++i; i < n; ++i) {
144 dest[i] = '\0';
145 }
146 return result;
147 }
148 }
149
150 return &dest[n];
151}
152
153/**
154 * Concatenate two strings.
155 *
156 * @param dest String to which src shall be appended.
157 * @param src String to be appended after dest.
158 * @return Pointer to destination buffer.
159 */
160char *posix_strcat(char *restrict dest, const char *restrict src)
161{
162 assert(dest != NULL);
163 assert(src != NULL);
164
165 posix_strcpy(posix_strchr(dest, '\0'), src);
166 return dest;
167}
168
169/**
170 * Concatenate a string with part of another.
171 *
172 * @param dest String to which part of src shall be appended.
173 * @param src String whose part shall be appended after dest.
174 * @param n Number of bytes to append after dest.
175 * @return Pointer to destination buffer.
176 */
177char *posix_strncat(char *restrict dest, const char *restrict src, size_t n)
178{
179 assert(dest != NULL);
180 assert(src != NULL);
181
182 char *zeroptr = posix_strncpy(posix_strchr(dest, '\0'), src, n);
183 /* strncpy doesn't append the nul terminator, so we do it here */
184 zeroptr[n] = '\0';
185 return dest;
186}
187
188/**
189 * Copy limited number of bytes in memory.
190 *
191 * @param dest Destination buffer.
192 * @param src Source buffer.
193 * @param c Character after which the copying shall stop.
194 * @param n Number of bytes that shall be copied if not stopped earlier by c.
195 * @return Pointer to the first byte after c in dest if found, NULL otherwise.
196 */
197void *posix_memccpy(void *restrict dest, const void *restrict src, int c, size_t n)
198{
199 assert(dest != NULL);
200 assert(src != NULL);
201
202 unsigned char* bdest = dest;
203 const unsigned char* bsrc = src;
204
205 for (size_t i = 0; i < n; ++i) {
206 bdest[i] = bsrc[i];
207
208 if (bsrc[i] == (unsigned char) c) {
209 /* pointer to the next byte */
210 return &bdest[i + 1];
211 }
212 }
213
214 return NULL;
215}
216
217/**
218 * Duplicate a string.
219 *
220 * @param s String to be duplicated.
221 * @return Newly allocated copy of the string.
222 */
223char *posix_strdup(const char *s)
224{
225 return posix_strndup(s, SIZE_MAX);
226}
227
228/**
229 * Duplicate a specific number of bytes from a string.
230 *
231 * @param s String to be duplicated.
232 * @param n Maximum length of the resulting string..
233 * @return Newly allocated string copy of length at most n.
234 */
235char *posix_strndup(const char *s, size_t n)
236{
237 assert(s != NULL);
238
239 size_t len = posix_strnlen(s, n);
240 char *dup = malloc(len + 1);
241 if (dup == NULL) {
242 return NULL;
243 }
244
245 memcpy(dup, s, len);
246 dup[len] = '\0';
247
248 return dup;
249}
250
251/**
252 * Compare bytes in memory.
253 *
254 * @param mem1 First area of memory to be compared.
255 * @param mem2 Second area of memory to be compared.
256 * @param n Maximum number of bytes to be compared.
257 * @return Difference of the first pair of inequal bytes,
258 * or 0 if areas have the same content.
259 */
260int posix_memcmp(const void *mem1, const void *mem2, size_t n)
261{
262 assert(mem1 != NULL);
263 assert(mem2 != NULL);
264
265 const unsigned char *s1 = mem1;
266 const unsigned char *s2 = mem2;
267
268 for (size_t i = 0; i < n; ++i) {
269 if (s1[i] != s2[i]) {
270 return s1[i] - s2[i];
271 }
272 }
273
274 return 0;
275}
276
277/**
278 * Compare two strings.
279 *
280 * @param s1 First string to be compared.
281 * @param s2 Second string to be compared.
282 * @return Difference of the first pair of inequal characters,
283 * or 0 if strings have the same content.
284 */
285int posix_strcmp(const char *s1, const char *s2)
286{
287 assert(s1 != NULL);
288 assert(s2 != NULL);
289
290 return posix_strncmp(s1, s2, STR_NO_LIMIT);
291}
292
293/**
294 * Compare part of two strings.
295 *
296 * @param s1 First string to be compared.
297 * @param s2 Second string to be compared.
298 * @param n Maximum number of characters to be compared.
299 * @return Difference of the first pair of inequal characters,
300 * or 0 if strings have the same content.
301 */
302int posix_strncmp(const char *s1, const char *s2, size_t n)
303{
304 assert(s1 != NULL);
305 assert(s2 != NULL);
306
307 for (size_t i = 0; i < n; ++i) {
308 if (s1[i] != s2[i]) {
309 return s1[i] - s2[i];
310 }
311 if (s1[i] == '\0') {
312 break;
313 }
314 }
315
316 return 0;
317}
318
319/**
320 * Find byte in memory.
321 *
322 * @param mem Memory area in which to look for the byte.
323 * @param c Byte to look for.
324 * @param n Maximum number of bytes to be inspected.
325 * @return Pointer to the specified byte on success,
326 * NULL pointer otherwise.
327 */
328void *posix_memchr(const void *mem, int c, size_t n)
329{
330 assert(mem != NULL);
331
332 const unsigned char *s = mem;
333
334 for (size_t i = 0; i < n; ++i) {
335 if (s[i] == (unsigned char) c) {
336 return (void *) &s[i];
337 }
338 }
339 return NULL;
340}
341
342/**
343 * Scan string for a first occurence of a character.
344 *
345 * @param s String in which to look for the character.
346 * @param c Character to look for.
347 * @return Pointer to the specified character on success,
348 * NULL pointer otherwise.
349 */
350char *posix_strchr(const char *s, int c)
351{
352 assert(s != NULL);
353
354 char *res = gnu_strchrnul(s, c);
355 return (*res == c) ? res : NULL;
356}
357
358/**
359 * Scan string for a last occurence of a character.
360 *
361 * @param s String in which to look for the character.
362 * @param c Character to look for.
363 * @return Pointer to the specified character on success,
364 * NULL pointer otherwise.
365 */
366char *posix_strrchr(const char *s, int c)
367{
368 assert(s != NULL);
369
370 const char *ptr = posix_strchr(s, '\0');
371
372 /* the same as in strchr, except it loops in reverse direction */
373 while (*ptr != (char) c) {
374 if (ptr == s) {
375 return NULL;
376 }
377
378 ptr--;
379 }
380
381 return (char *) ptr;
382}
383
384/**
385 * Scan string for a first occurence of a character.
386 *
387 * @param s String in which to look for the character.
388 * @param c Character to look for.
389 * @return Pointer to the specified character on success, pointer to the
390 * string terminator otherwise.
391 */
392char *gnu_strchrnul(const char *s, int c)
393{
394 assert(s != NULL);
395
396 while (*s != c && *s != '\0') {
397 s++;
398 }
399
400 return (char *) s;
401}
402
403/**
404 * Scan a string for a first occurence of one of provided bytes.
405 *
406 * @param s1 String in which to look for the bytes.
407 * @param s2 String of bytes to look for.
408 * @return Pointer to the found byte on success,
409 * NULL pointer otherwise.
410 */
411char *posix_strpbrk(const char *s1, const char *s2)
412{
413 assert(s1 != NULL);
414 assert(s2 != NULL);
415
416 char *ptr = strpbrk_null(s1, s2);
417 return (*ptr == '\0') ? NULL : ptr;
418}
419
420/**
421 * Get the length of a complementary substring.
422 *
423 * @param s1 String that shall be searched for complementary prefix.
424 * @param s2 String of bytes that shall not occur in the prefix.
425 * @return Length of the prefix.
426 */
427size_t posix_strcspn(const char *s1, const char *s2)
428{
429 assert(s1 != NULL);
430 assert(s2 != NULL);
431
432 char *ptr = strpbrk_null(s1, s2);
433 return (size_t) (ptr - s1);
434}
435
436/**
437 * Get length of a substring.
438 *
439 * @param s1 String that shall be searched for prefix.
440 * @param s2 String of bytes that the prefix must consist of.
441 * @return Length of the prefix.
442 */
443size_t posix_strspn(const char *s1, const char *s2)
444{
445 assert(s1 != NULL);
446 assert(s2 != NULL);
447
448 const char *ptr;
449 for (ptr = s1; *ptr != '\0'; ++ptr) {
450 if (!posix_strchr(s2, *ptr)) {
451 break;
452 }
453 }
454 return ptr - s1;
455}
456
457/**
458 * Find a substring. Uses Knuth-Morris-Pratt algorithm.
459 *
460 * @param s1 String in which to look for a substring.
461 * @param s2 Substring to look for.
462 * @return Pointer to the first character of the substring in s1, or NULL if
463 * not found.
464 */
465char *posix_strstr(const char *haystack, const char *needle)
466{
467 assert(haystack != NULL);
468 assert(needle != NULL);
469
470 /* Special case - needle is an empty string. */
471 if (needle[0] == '\0') {
472 return (char *) haystack;
473 }
474
475 /* Preprocess needle. */
476 size_t nlen = posix_strlen(needle);
477 size_t prefix_table[nlen + 1];
478
479 {
480 size_t i = 0;
481 ssize_t j = -1;
482
483 prefix_table[i] = j;
484
485 while (i < nlen) {
486 while (j >= 0 && needle[i] != needle[j]) {
487 j = prefix_table[j];
488 }
489 i++; j++;
490 prefix_table[i] = j;
491 }
492 }
493
494 /* Search needle using the precomputed table. */
495 size_t npos = 0;
496
497 for (size_t hpos = 0; haystack[hpos] != '\0'; ++hpos) {
498 while (npos != 0 && haystack[hpos] != needle[npos]) {
499 npos = prefix_table[npos];
500 }
501
502 if (haystack[hpos] == needle[npos]) {
503 npos++;
504
505 if (npos == nlen) {
506 return (char *) (haystack + hpos - nlen + 1);
507 }
508 }
509 }
510
511 return NULL;
512}
513
514/** Split string by delimiters.
515 *
516 * @param s String to be tokenized. May not be NULL.
517 * @param delim String with the delimiters.
518 * @return Pointer to the prefix of @a s before the first
519 * delimiter character. NULL if no such prefix
520 * exists.
521 */
522char *posix_strtok(char *s, const char *delim)
523{
524 static char *next;
525
526 return posix_strtok_r(s, delim, &next);
527}
528
529
530/** Split string by delimiters.
531 *
532 * @param s String to be tokenized. May not be NULL.
533 * @param delim String with the delimiters.
534 * @param next Variable which will receive the pointer to the
535 * continuation of the string following the first
536 * occurrence of any of the delimiter characters.
537 * May be NULL.
538 * @return Pointer to the prefix of @a s before the first
539 * delimiter character. NULL if no such prefix
540 * exists.
541 */
542char *posix_strtok_r(char *s, const char *delim, char **next)
543{
544 char *start, *end;
545
546 if (s == NULL)
547 s = *next;
548
549 /* Skip over leading delimiters. */
550 while (*s && (posix_strchr(delim, *s) != NULL)) ++s;
551 start = s;
552
553 /* Skip over token characters. */
554 while (*s && (posix_strchr(delim, *s) == NULL)) ++s;
555 end = s;
556 *next = (*s ? s + 1 : s);
557
558 if (start == end) {
559 return NULL; /* No more tokens. */
560 }
561
562 /* Overwrite delimiter with NULL terminator. */
563 *end = '\0';
564 return start;
565}
566
567/**
568 * String comparison using collating information.
569 *
570 * Currently ignores locale and just calls strcmp.
571 *
572 * @param s1 First string to be compared.
573 * @param s2 Second string to be compared.
574 * @return Difference of the first pair of inequal characters,
575 * or 0 if strings have the same content.
576 */
577int posix_strcoll(const char *s1, const char *s2)
578{
579 assert(s1 != NULL);
580 assert(s2 != NULL);
581
582 return posix_strcmp(s1, s2);
583}
584
585/**
586 * Transform a string in such a way that the resulting string yields the same
587 * results when passed to the strcmp as if the original string is passed to
588 * the strcoll.
589 *
590 * Since strcoll is equal to strcmp here, this just makes a copy.
591 *
592 * @param s1 Transformed string.
593 * @param s2 Original string.
594 * @param n Maximum length of the transformed string.
595 * @return Length of the transformed string.
596 */
597size_t posix_strxfrm(char *restrict s1, const char *restrict s2, size_t n)
598{
599 assert(s1 != NULL || n == 0);
600 assert(s2 != NULL);
601
602 size_t len = posix_strlen(s2);
603
604 if (n > len) {
605 posix_strcpy(s1, s2);
606 }
607
608 return len;
609}
610
611/**
612 * Get error message string.
613 *
614 * @param errnum Error code for which to obtain human readable string.
615 * @return Error message.
616 */
617char *posix_strerror(int errnum)
618{
619 // FIXME: move strerror() and strerror_r() to libc.
620 return (char *) str_error(errnum);
621}
622
623/**
624 * Get error message string.
625 *
626 * @param errnum Error code for which to obtain human readable string.
627 * @param buf Buffer to store a human readable string to.
628 * @param bufsz Size of buffer pointed to by buf.
629 * @return Zero on success, errno otherwise.
630 */
631int posix_strerror_r(int errnum, char *buf, size_t bufsz)
632{
633 assert(buf != NULL);
634
635 char *errstr = posix_strerror(errnum);
636
637 if (posix_strlen(errstr) + 1 > bufsz) {
638 return ERANGE;
639 } else {
640 posix_strcpy(buf, errstr);
641 }
642
643 return EOK;
644}
645
646/**
647 * Get length of the string.
648 *
649 * @param s String which length shall be determined.
650 * @return Length of the string.
651 */
652size_t posix_strlen(const char *s)
653{
654 assert(s != NULL);
655
656 return (size_t) (posix_strchr(s, '\0') - s);
657}
658
659/**
660 * Get limited length of the string.
661 *
662 * @param s String which length shall be determined.
663 * @param n Maximum number of bytes that can be examined to determine length.
664 * @return The lower of either string length or n limit.
665 */
666size_t posix_strnlen(const char *s, size_t n)
667{
668 assert(s != NULL);
669
670 for (size_t sz = 0; sz < n; ++sz) {
671
672 if (s[sz] == '\0') {
673 return sz;
674 }
675 }
676
677 return n;
678}
679
680/**
681 * Get description of a signal.
682 *
683 * @param signum Signal number.
684 * @return Human readable signal description.
685 */
686char *posix_strsignal(int signum)
687{
688 static const char *const sigstrings[] = {
689 [SIGABRT] = "SIGABRT (Process abort signal)",
690 [SIGALRM] = "SIGALRM (Alarm clock)",
691 [SIGBUS] = "SIGBUS (Access to an undefined portion of a memory object)",
692 [SIGCHLD] = "SIGCHLD (Child process terminated, stopped, or continued)",
693 [SIGCONT] = "SIGCONT (Continue executing, if stopped)",
694 [SIGFPE] = "SIGFPE (Erroneous arithmetic operation)",
695 [SIGHUP] = "SIGHUP (Hangup)",
696 [SIGILL] = "SIGILL (Illegal instruction)",
697 [SIGINT] = "SIGINT (Terminal interrupt signal)",
698 [SIGKILL] = "SIGKILL (Kill process)",
699 [SIGPIPE] = "SIGPIPE (Write on a pipe with no one to read it)",
700 [SIGQUIT] = "SIGQUIT (Terminal quit signal)",
701 [SIGSEGV] = "SIGSEGV (Invalid memory reference)",
702 [SIGSTOP] = "SIGSTOP (Stop executing)",
703 [SIGTERM] = "SIGTERM (Termination signal)",
704 [SIGTSTP] = "SIGTSTP (Terminal stop signal)",
705 [SIGTTIN] = "SIGTTIN (Background process attempting read)",
706 [SIGTTOU] = "SIGTTOU (Background process attempting write)",
707 [SIGUSR1] = "SIGUSR1 (User-defined signal 1)",
708 [SIGUSR2] = "SIGUSR2 (User-defined signal 2)",
709 [SIGPOLL] = "SIGPOLL (Pollable event)",
710 [SIGPROF] = "SIGPROF (Profiling timer expired)",
711 [SIGSYS] = "SIGSYS (Bad system call)",
712 [SIGTRAP] = "SIGTRAP (Trace/breakpoint trap)",
713 [SIGURG] = "SIGURG (High bandwidth data is available at a socket)",
714 [SIGVTALRM] = "SIGVTALRM (Virtual timer expired)",
715 [SIGXCPU] = "SIGXCPU (CPU time limit exceeded)",
716 [SIGXFSZ] = "SIGXFSZ (File size limit exceeded)"
717 };
718
719 if (signum <= _TOP_SIGNAL) {
720 return (char *) sigstrings[signum];
721 }
722
723 return (char *) "ERROR, Invalid signal number";
724}
725
726/** @}
727 */
Note: See TracBrowser for help on using the repository browser.