source: mainline/uspace/lib/posix/src/string.c@ e0f47f5

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

Rename uspace/lib/posix/source to src, for consistency with other libraries.

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