source: mainline/uspace/lib/posix/source/string.c@ 79ea5af

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 79ea5af was 12b29f3, checked in by Vojtech Horky <vojtechhorky@…>, 11 years ago

Reintroduce strtok to libposix (needed by GCC & Python)

Implementation taken from the original libc implementation before
revision 2122 (where it was changed to str_tok()).

  • Property mode set to 100644
File size: 21.3 KB
RevLine 
[7bb9b53]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 */
[5273eb6]33/** @file String manipulation.
[7bb9b53]34 */
35
[4d10fc8]36#define LIBPOSIX_INTERNAL
[fdf97f6]37#define __POSIX_DEF__(x) posix_##x
[4d10fc8]38
[a6d908c1]39#include "internal/common.h"
[a3da2b2]40#include "posix/string.h"
[7bb9b53]41
[a3da2b2]42#include "posix/assert.h"
43#include "posix/errno.h"
44#include "posix/limits.h"
45#include "posix/stdlib.h"
46#include "posix/signal.h"
[ef6dd3f]47
[a6d908c1]48#include "libc/str_error.h"
49
[4f4b4e7]50/**
51 * The same as strpbrk, except it returns pointer to the nul terminator
[ef6dd3f]52 * if no occurence is found.
[4f4b4e7]53 *
[5273eb6]54 * @param s1 String in which to look for the bytes.
55 * @param s2 String of bytes to look for.
56 * @return Pointer to the found byte on success, pointer to the
57 * string terminator otherwise.
[ef6dd3f]58 */
59static char *strpbrk_null(const char *s1, const char *s2)
60{
61 while (!posix_strchr(s2, *s1)) {
[4f4b4e7]62 ++s1;
[ef6dd3f]63 }
64
65 return (char *) s1;
66}
[7bb9b53]67
68/**
[5273eb6]69 * Copy a string.
[7bb9b53]70 *
[5273eb6]71 * @param dest Destination pre-allocated buffer.
72 * @param src Source string to be copied.
73 * @return Pointer to the destination buffer.
[7bb9b53]74 */
[5273eb6]75char *posix_strcpy(char *restrict dest, const char *restrict src)
[7bb9b53]76{
[ef6dd3f]77 posix_stpcpy(dest, src);
78 return dest;
[7bb9b53]79}
80
81/**
[5273eb6]82 * Copy fixed length string.
[7bb9b53]83 *
[5273eb6]84 * @param dest Destination pre-allocated buffer.
85 * @param src Source string to be copied.
86 * @param n Number of bytes to be stored into destination buffer.
87 * @return Pointer to the destination buffer.
[7bb9b53]88 */
[5273eb6]89char *posix_strncpy(char *restrict dest, const char *restrict src, size_t n)
[7bb9b53]90{
[ef6dd3f]91 posix_stpncpy(dest, src, n);
92 return dest;
[7bb9b53]93}
94
95/**
[5273eb6]96 * Copy a string.
[7bb9b53]97 *
[5273eb6]98 * @param dest Destination pre-allocated buffer.
99 * @param src Source string to be copied.
100 * @return Pointer to the nul character in the destination string.
[7bb9b53]101 */
[ef6dd3f]102char *posix_stpcpy(char *restrict dest, const char *restrict src)
[7bb9b53]103{
[ef6dd3f]104 assert(dest != NULL);
105 assert(src != NULL);
106
[4f4b4e7]107 for (size_t i = 0; ; ++i) {
[ef6dd3f]108 dest[i] = src[i];
109
110 if (src[i] == '\0') {
111 /* pointer to the terminating nul character */
112 return &dest[i];
113 }
114 }
115
116 /* unreachable */
117 return NULL;
[7bb9b53]118}
119
120/**
[5273eb6]121 * Copy fixed length string.
[7bb9b53]122 *
[5273eb6]123 * @param dest Destination pre-allocated buffer.
124 * @param src Source string to be copied.
125 * @param n Number of bytes to be stored into destination buffer.
126 * @return Pointer to the first written nul character or &dest[n].
[7bb9b53]127 */
[ef6dd3f]128char *posix_stpncpy(char *restrict dest, const char *restrict src, size_t n)
[7bb9b53]129{
[ef6dd3f]130 assert(dest != NULL);
131 assert(src != NULL);
132
[4f4b4e7]133 for (size_t i = 0; i < n; ++i) {
[ef6dd3f]134 dest[i] = src[i];
135
136 /* the standard requires that nul characters
137 * are appended to the length of n, in case src is shorter
138 */
139 if (src[i] == '\0') {
140 char *result = &dest[i];
[4f4b4e7]141 for (++i; i < n; ++i) {
[ef6dd3f]142 dest[i] = '\0';
143 }
144 return result;
145 }
146 }
147
148 return &dest[n];
[7bb9b53]149}
150
151/**
[5273eb6]152 * Concatenate two strings.
[ef6dd3f]153 *
[5273eb6]154 * @param dest String to which src shall be appended.
155 * @param src String to be appended after dest.
156 * @return Pointer to destination buffer.
[7bb9b53]157 */
[5273eb6]158char *posix_strcat(char *restrict dest, const char *restrict src)
[7bb9b53]159{
[ef6dd3f]160 assert(dest != NULL);
161 assert(src != NULL);
162
[517cedc0]163 posix_strcpy(posix_strchr(dest, '\0'), src);
[ef6dd3f]164 return dest;
[7bb9b53]165}
166
167/**
[5273eb6]168 * Concatenate a string with part of another.
[ef6dd3f]169 *
[5273eb6]170 * @param dest String to which part of src shall be appended.
171 * @param src String whose part shall be appended after dest.
172 * @param n Number of bytes to append after dest.
173 * @return Pointer to destination buffer.
[7bb9b53]174 */
[5273eb6]175char *posix_strncat(char *restrict dest, const char *restrict src, size_t n)
[7bb9b53]176{
[ef6dd3f]177 assert(dest != NULL);
178 assert(src != NULL);
179
[517cedc0]180 char *zeroptr = posix_strncpy(posix_strchr(dest, '\0'), src, n);
[ef6dd3f]181 /* strncpy doesn't append the nul terminator, so we do it here */
182 zeroptr[n] = '\0';
183 return dest;
[7bb9b53]184}
185
186/**
[5273eb6]187 * Copy limited number of bytes in memory.
[7bb9b53]188 *
[5273eb6]189 * @param dest Destination buffer.
190 * @param src Source buffer.
191 * @param c Character after which the copying shall stop.
192 * @param n Number of bytes that shall be copied if not stopped earlier by c.
[ef6dd3f]193 * @return Pointer to the first byte after c in dest if found, NULL otherwise.
[7bb9b53]194 */
[5273eb6]195void *posix_memccpy(void *restrict dest, const void *restrict src, int c, size_t n)
[7bb9b53]196{
[ef6dd3f]197 assert(dest != NULL);
198 assert(src != NULL);
199
200 unsigned char* bdest = dest;
201 const unsigned char* bsrc = src;
202
[4f4b4e7]203 for (size_t i = 0; i < n; ++i) {
[ef6dd3f]204 bdest[i] = bsrc[i];
205
206 if (bsrc[i] == (unsigned char) c) {
207 /* pointer to the next byte */
208 return &bdest[i + 1];
209 }
210 }
211
212 return NULL;
[7bb9b53]213}
214
215/**
[5273eb6]216 * Duplicate a string.
[7bb9b53]217 *
[5273eb6]218 * @param s String to be duplicated.
219 * @return Newly allocated copy of the string.
[7bb9b53]220 */
[ef6dd3f]221char *posix_strdup(const char *s)
[7bb9b53]222{
[b52ef5a]223 return posix_strndup(s, SIZE_MAX);
[7bb9b53]224}
225
226/**
[5273eb6]227 * Duplicate a specific number of bytes from a string.
[7bb9b53]228 *
[5273eb6]229 * @param s String to be duplicated.
230 * @param n Maximum length of the resulting string..
231 * @return Newly allocated string copy of length at most n.
[7bb9b53]232 */
[ef6dd3f]233char *posix_strndup(const char *s, size_t n)
[7bb9b53]234{
[ef6dd3f]235 assert(s != NULL);
236
237 size_t len = posix_strnlen(s, n);
238 char *dup = malloc(len + 1);
239 if (dup == NULL) {
240 return NULL;
241 }
242
243 memcpy(dup, s, len);
244 dup[len] = '\0';
245
246 return dup;
[7bb9b53]247}
248
249/**
[5273eb6]250 * Compare bytes in memory.
[ef6dd3f]251 *
[5273eb6]252 * @param mem1 First area of memory to be compared.
253 * @param mem2 Second area of memory to be compared.
254 * @param n Maximum number of bytes to be compared.
[ef6dd3f]255 * @return Difference of the first pair of inequal bytes,
[5273eb6]256 * or 0 if areas have the same content.
[7bb9b53]257 */
[ef6dd3f]258int posix_memcmp(const void *mem1, const void *mem2, size_t n)
[7bb9b53]259{
[ef6dd3f]260 assert(mem1 != NULL);
261 assert(mem2 != NULL);
262
263 const unsigned char *s1 = mem1;
264 const unsigned char *s2 = mem2;
265
[4f4b4e7]266 for (size_t i = 0; i < n; ++i) {
[ef6dd3f]267 if (s1[i] != s2[i]) {
[087c4c56]268 return s1[i] - s2[i];
[ef6dd3f]269 }
270 }
271
[7bb9b53]272 return 0;
273}
274
275/**
[5273eb6]276 * Compare two strings.
[ef6dd3f]277 *
[5273eb6]278 * @param s1 First string to be compared.
279 * @param s2 Second string to be compared.
280 * @return Difference of the first pair of inequal characters,
281 * or 0 if strings have the same content.
[7bb9b53]282 */
[ef6dd3f]283int posix_strcmp(const char *s1, const char *s2)
[7bb9b53]284{
[ef6dd3f]285 assert(s1 != NULL);
286 assert(s2 != NULL);
287
288 return posix_strncmp(s1, s2, STR_NO_LIMIT);
[7bb9b53]289}
290
291/**
[5273eb6]292 * Compare part of two strings.
[7bb9b53]293 *
[5273eb6]294 * @param s1 First string to be compared.
295 * @param s2 Second string to be compared.
296 * @param n Maximum number of characters to be compared.
297 * @return Difference of the first pair of inequal characters,
298 * or 0 if strings have the same content.
[7bb9b53]299 */
[ef6dd3f]300int posix_strncmp(const char *s1, const char *s2, size_t n)
[7bb9b53]301{
[ef6dd3f]302 assert(s1 != NULL);
303 assert(s2 != NULL);
304
[4f4b4e7]305 for (size_t i = 0; i < n; ++i) {
[ef6dd3f]306 if (s1[i] != s2[i]) {
[087c4c56]307 return s1[i] - s2[i];
[ef6dd3f]308 }
309 if (s1[i] == '\0') {
310 break;
311 }
312 }
313
[7bb9b53]314 return 0;
315}
316
317/**
[5273eb6]318 * Find byte in memory.
[ef6dd3f]319 *
[5273eb6]320 * @param mem Memory area in which to look for the byte.
321 * @param c Byte to look for.
322 * @param n Maximum number of bytes to be inspected.
323 * @return Pointer to the specified byte on success,
324 * NULL pointer otherwise.
[7bb9b53]325 */
[ef6dd3f]326void *posix_memchr(const void *mem, int c, size_t n)
[7bb9b53]327{
[ef6dd3f]328 assert(mem != NULL);
329
330 const unsigned char *s = mem;
331
[4f4b4e7]332 for (size_t i = 0; i < n; ++i) {
[ef6dd3f]333 if (s[i] == (unsigned char) c) {
334 return (void *) &s[i];
335 }
336 }
337 return NULL;
[7bb9b53]338}
339
340/**
[5273eb6]341 * Scan string for a first occurence of a character.
[7bb9b53]342 *
[5273eb6]343 * @param s String in which to look for the character.
344 * @param c Character to look for.
345 * @return Pointer to the specified character on success,
346 * NULL pointer otherwise.
[7bb9b53]347 */
348char *posix_strchr(const char *s, int c)
349{
[ef6dd3f]350 assert(s != NULL);
351
[517cedc0]352 char *res = gnu_strchrnul(s, c);
353 return (*res == c) ? res : NULL;
[7bb9b53]354}
355
356/**
[5273eb6]357 * Scan string for a last occurence of a character.
[7bb9b53]358 *
[5273eb6]359 * @param s String in which to look for the character.
360 * @param c Character to look for.
361 * @return Pointer to the specified character on success,
362 * NULL pointer otherwise.
[7bb9b53]363 */
364char *posix_strrchr(const char *s, int c)
365{
[ef6dd3f]366 assert(s != NULL);
367
[517cedc0]368 const char *ptr = posix_strchr(s, '\0');
[ef6dd3f]369
370 /* the same as in strchr, except it loops in reverse direction */
371 while (*ptr != (char) c) {
[4f4b4e7]372 if (ptr == s) {
[ef6dd3f]373 return NULL;
[4f4b4e7]374 }
[ef6dd3f]375
[27eddb52]376 ptr--;
[ef6dd3f]377 }
378
379 return (char *) ptr;
[7bb9b53]380}
381
[5273eb6]382/**
383 * Scan string for a first occurence of a character.
384 *
385 * @param s String in which to look for the character.
386 * @param c Character to look for.
387 * @return Pointer to the specified character on success, pointer to the
388 * string terminator otherwise.
389 */
[517cedc0]390char *gnu_strchrnul(const char *s, int c)
391{
392 assert(s != NULL);
393
394 while (*s != c && *s != '\0') {
395 s++;
396 }
397
398 return (char *) s;
399}
400
[7bb9b53]401/**
[5273eb6]402 * Scan a string for a first occurence of one of provided bytes.
[7bb9b53]403 *
[5273eb6]404 * @param s1 String in which to look for the bytes.
405 * @param s2 String of bytes to look for.
406 * @return Pointer to the found byte on success,
407 * NULL pointer otherwise.
[7bb9b53]408 */
409char *posix_strpbrk(const char *s1, const char *s2)
410{
[ef6dd3f]411 assert(s1 != NULL);
412 assert(s2 != NULL);
413
414 char *ptr = strpbrk_null(s1, s2);
415 return (*ptr == '\0') ? NULL : ptr;
[7bb9b53]416}
417
418/**
[5273eb6]419 * Get the length of a complementary substring.
[7bb9b53]420 *
[5273eb6]421 * @param s1 String that shall be searched for complementary prefix.
422 * @param s2 String of bytes that shall not occur in the prefix.
423 * @return Length of the prefix.
[7bb9b53]424 */
425size_t posix_strcspn(const char *s1, const char *s2)
426{
[ef6dd3f]427 assert(s1 != NULL);
428 assert(s2 != NULL);
429
430 char *ptr = strpbrk_null(s1, s2);
431 return (size_t) (ptr - s1);
[7bb9b53]432}
433
434/**
[5273eb6]435 * Get length of a substring.
[7bb9b53]436 *
[5273eb6]437 * @param s1 String that shall be searched for prefix.
438 * @param s2 String of bytes that the prefix must consist of.
439 * @return Length of the prefix.
[7bb9b53]440 */
441size_t posix_strspn(const char *s1, const char *s2)
442{
[ef6dd3f]443 assert(s1 != NULL);
444 assert(s2 != NULL);
445
446 const char *ptr;
[4f4b4e7]447 for (ptr = s1; *ptr != '\0'; ++ptr) {
448 if (!posix_strchr(s2, *ptr)) {
[ef6dd3f]449 break;
[4f4b4e7]450 }
[ef6dd3f]451 }
452 return ptr - s1;
[7bb9b53]453}
454
455/**
[7e10aee]456 * Find a substring. Uses Knuth-Morris-Pratt algorithm.
[7bb9b53]457 *
[5273eb6]458 * @param s1 String in which to look for a substring.
459 * @param s2 Substring to look for.
460 * @return Pointer to the first character of the substring in s1, or NULL if
461 * not found.
[7bb9b53]462 */
[3f33b95]463char *posix_strstr(const char *haystack, const char *needle)
[7bb9b53]464{
[7e10aee]465 assert(haystack != NULL);
466 assert(needle != NULL);
467
468 /* Special case - needle is an empty string. */
469 if (needle[0] == '\0') {
[3f33b95]470 return (char *) haystack;
[4f4b4e7]471 }
[7e10aee]472
473 /* Preprocess needle. */
474 size_t nlen = posix_strlen(needle);
475 size_t prefix_table[nlen + 1];
476
477 {
478 size_t i = 0;
479 ssize_t j = -1;
480
481 prefix_table[i] = j;
482
483 while (i < nlen) {
484 while (j >= 0 && needle[i] != needle[j]) {
485 j = prefix_table[j];
486 }
487 i++; j++;
488 prefix_table[i] = j;
489 }
490 }
491
492 /* Search needle using the precomputed table. */
493 size_t npos = 0;
494
[3f33b95]495 for (size_t hpos = 0; haystack[hpos] != '\0'; ++hpos) {
[7e10aee]496 while (npos != 0 && haystack[hpos] != needle[npos]) {
497 npos = prefix_table[npos];
[4f4b4e7]498 }
[ef6dd3f]499
[7e10aee]500 if (haystack[hpos] == needle[npos]) {
[3f33b95]501 npos++;
[7e10aee]502
503 if (npos == nlen) {
[3f33b95]504 return (char *) (haystack + hpos - nlen + 1);
[7e10aee]505 }
506 }
[ef6dd3f]507 }
508
509 return NULL;
[7bb9b53]510}
511
[12b29f3]512/** Split string by delimiters.
513 *
514 * @param s String to be tokenized. May not be NULL.
515 * @param delim String with the delimiters.
516 * @return Pointer to the prefix of @a s before the first
517 * delimiter character. NULL if no such prefix
518 * exists.
519 */
520char *posix_strtok(char *s, const char *delim)
521{
522 static char *next;
523
524 return posix_strtok_r(s, delim, &next);
525}
526
527
528/** Split string by delimiters.
529 *
530 * @param s String to be tokenized. May not be NULL.
531 * @param delim String with the delimiters.
532 * @param next Variable which will receive the pointer to the
533 * continuation of the string following the first
534 * occurrence of any of the delimiter characters.
535 * May be NULL.
536 * @return Pointer to the prefix of @a s before the first
537 * delimiter character. NULL if no such prefix
538 * exists.
539 */
540char *posix_strtok_r(char *s, const char *delim, char **next)
541{
542 char *start, *end;
543
544 if (s == NULL)
545 s = *next;
546
547 /* Skip over leading delimiters. */
548 while (*s && (posix_strchr(delim, *s) != NULL)) ++s;
549 start = s;
550
551 /* Skip over token characters. */
552 while (*s && (posix_strchr(delim, *s) == NULL)) ++s;
553 end = s;
554 *next = (*s ? s + 1 : s);
555
556 if (start == end) {
557 return NULL; /* No more tokens. */
558 }
559
560 /* Overwrite delimiter with NULL terminator. */
561 *end = '\0';
562 return start;
563}
564
[7bb9b53]565/**
[5273eb6]566 * String comparison using collating information.
567 *
[ef6dd3f]568 * Currently ignores locale and just calls strcmp.
[7bb9b53]569 *
[5273eb6]570 * @param s1 First string to be compared.
571 * @param s2 Second string to be compared.
572 * @return Difference of the first pair of inequal characters,
573 * or 0 if strings have the same content.
[7bb9b53]574 */
575int posix_strcoll(const char *s1, const char *s2)
576{
[ef6dd3f]577 assert(s1 != NULL);
578 assert(s2 != NULL);
579
580 return posix_strcmp(s1, s2);
[7bb9b53]581}
582
583/**
[5273eb6]584 * Transform a string in such a way that the resulting string yields the same
585 * results when passed to the strcmp as if the original string is passed to
586 * the strcoll.
587 *
588 * Since strcoll is equal to strcmp here, this just makes a copy.
[7bb9b53]589 *
[5273eb6]590 * @param s1 Transformed string.
591 * @param s2 Original string.
592 * @param n Maximum length of the transformed string.
593 * @return Length of the transformed string.
[7bb9b53]594 */
[5273eb6]595size_t posix_strxfrm(char *restrict s1, const char *restrict s2, size_t n)
[7bb9b53]596{
[ef6dd3f]597 assert(s1 != NULL || n == 0);
598 assert(s2 != NULL);
599
600 size_t len = posix_strlen(s2);
601
[4f4b4e7]602 if (n > len) {
[ef6dd3f]603 posix_strcpy(s1, s2);
[4f4b4e7]604 }
[ef6dd3f]605
606 return len;
[7bb9b53]607}
608
609/**
[5273eb6]610 * Get error message string.
[7bb9b53]611 *
[5273eb6]612 * @param errnum Error code for which to obtain human readable string.
613 * @return Error message.
[7bb9b53]614 */
615char *posix_strerror(int errnum)
616{
[1b55da67]617 static const char *error_msgs[] = {
618 [E2BIG] = "[E2BIG] Argument list too long",
619 [EACCES] = "[EACCES] Permission denied",
620 [EADDRINUSE] = "[EADDRINUSE] Address in use",
621 [EADDRNOTAVAIL] = "[EADDRNOTAVAIL] Address not available",
622 [EAFNOSUPPORT] = "[EAFNOSUPPORT] Address family not supported",
623 [EAGAIN] = "[EAGAIN] Resource unavailable, try again",
624 [EALREADY] = "[EALREADY] Connection already in progress",
625 [EBADF] = "[EBADF] Bad file descriptor",
626 [EBADMSG] = "[EBADMSG] Bad message",
627 [EBUSY] = "[EBUSY] Device or resource busy",
628 [ECANCELED] = "[ECANCELED] Operation canceled",
629 [ECHILD] = "[ECHILD] No child processes",
630 [ECONNABORTED] = "[ECONNABORTED] Connection aborted",
631 [ECONNREFUSED] = "[ECONNREFUSED] Connection refused",
632 [ECONNRESET] = "[ECONNRESET] Connection reset",
633 [EDEADLK] = "[EDEADLK] Resource deadlock would occur",
634 [EDESTADDRREQ] = "[EDESTADDRREQ] Destination address required",
635 [EDOM] = "[EDOM] Mathematics argument out of domain of function",
636 [EDQUOT] = "[EDQUOT] Reserved",
637 [EEXIST] = "[EEXIST] File exists",
638 [EFAULT] = "[EFAULT] Bad address",
639 [EFBIG] = "[EFBIG] File too large",
640 [EHOSTUNREACH] = "[EHOSTUNREACH] Host is unreachable",
641 [EIDRM] = "[EIDRM] Identifier removed",
642 [EILSEQ] = "[EILSEQ] Illegal byte sequence",
643 [EINPROGRESS] = "[EINPROGRESS] Operation in progress",
644 [EINTR] = "[EINTR] Interrupted function",
645 [EINVAL] = "[EINVAL] Invalid argument",
646 [EIO] = "[EIO] I/O error",
647 [EISCONN] = "[EISCONN] Socket is connected",
648 [EISDIR] = "[EISDIR] Is a directory",
649 [ELOOP] = "[ELOOP] Too many levels of symbolic links",
650 [EMFILE] = "[EMFILE] File descriptor value too large",
651 [EMLINK] = "[EMLINK] Too many links",
652 [EMSGSIZE] = "[EMSGSIZE] Message too large",
653 [EMULTIHOP] = "[EMULTIHOP] Reserved",
654 [ENAMETOOLONG] = "[ENAMETOOLONG] Filename too long",
655 [ENETDOWN] = "[ENETDOWN] Network is down",
656 [ENETRESET] = "[ENETRESET] Connection aborted by network",
657 [ENETUNREACH] = "[ENETUNREACH] Network unreachable",
658 [ENFILE] = "[ENFILE] Too many files open in system",
659 [ENOBUFS] = "[ENOBUFS] No buffer space available",
660 [ENODATA] = "[ENODATA] No message is available on the STREAM head read queue",
661 [ENODEV] = "[ENODEV] No such device",
662 [ENOENT] = "[ENOENT] No such file or directory",
663 [ENOEXEC] = "[ENOEXEC] Executable file format error",
664 [ENOLCK] = "[ENOLCK] No locks available",
665 [ENOLINK] = "[ENOLINK] Reserved",
666 [ENOMEM] = "[ENOMEM] Not enough space",
667 [ENOMSG] = "[ENOMSG] No message of the desired type",
668 [ENOPROTOOPT] = "[ENOPROTOOPT] Protocol not available",
669 [ENOSPC] = "[ENOSPC] No space left on device",
670 [ENOSR] = "[ENOSR] No STREAM resources.",
671 [ENOSTR] = "[ENOSTR] Not a STREAM",
672 [ENOSYS] = "[ENOSYS] Function not supported",
673 [ENOTCONN] = "[ENOTCONN] The socket is not connected",
674 [ENOTDIR] = "[ENOTDIR] Not a directory",
675 [ENOTEMPTY] = "[ENOTEMPTY] Directory not empty",
676 [ENOTRECOVERABLE] = "[ENOTRECOVERABLE] State not recoverable",
677 [ENOTSOCK] = "[ENOTSOCK] Not a socket",
678 [ENOTSUP] = "[ENOTSUP] Not supported",
679 [ENOTTY] = "[ENOTTY] Inappropriate I/O control operation",
680 [ENXIO] = "[ENXIO] No such device or address",
681 [EOPNOTSUPP] = "[EOPNOTSUPP] Operation not supported",
682 [EOVERFLOW] = "[EOVERFLOW] Value too large to be stored in data type",
683 [EOWNERDEAD] = "[EOWNERDEAD] Previous owned died",
684 [EPERM] = "[EPERM] Operation not permitted",
685 [EPIPE] = "[EPIPE] Broken pipe",
686 [EPROTO] = "[EPROTO] Protocol error",
687 [EPROTONOSUPPORT] = "[EPROTONOSUPPORT] Protocol not supported",
688 [EPROTOTYPE] = "[EPROTOTYPE] Protocol wrong type for socket",
689 [ERANGE] = "[ERANGE] Result too large",
690 [EROFS] = "[EROFS] Read-only file system",
691 [ESPIPE] = "[ESPIPE] Invalid seek",
692 [ESRCH] = "[ESRCH] No such process",
693 [ESTALE] = "[ESTALE] Reserved",
694 [ETIME] = "[ETIME] Stream ioctl() timeout",
695 [ETIMEDOUT] = "[ETIMEDOUT] Connection timed out",
696 [ETXTBSY] = "[ETXTBSY] Text file busy",
697 [EWOULDBLOCK] = "[EWOULDBLOCK] Operation would block",
698 [EXDEV] = "[EXDEV] Cross-device link",
699 };
700
701 return (char *) error_msgs[posix_abs(errnum)];
[ef6dd3f]702}
703
704/**
[5273eb6]705 * Get error message string.
[ef6dd3f]706 *
[5273eb6]707 * @param errnum Error code for which to obtain human readable string.
708 * @param buf Buffer to store a human readable string to.
709 * @param bufsz Size of buffer pointed to by buf.
710 * @return Zero on success, errno otherwise.
[ef6dd3f]711 */
712int posix_strerror_r(int errnum, char *buf, size_t bufsz)
713{
714 assert(buf != NULL);
715
716 char *errstr = posix_strerror(errnum);
717
718 if (posix_strlen(errstr) + 1 > bufsz) {
[1b55da67]719 return ERANGE;
[ef6dd3f]720 } else {
721 posix_strcpy(buf, errstr);
722 }
723
[7bb9b53]724 return 0;
725}
726
727/**
[5273eb6]728 * Get length of the string.
[7bb9b53]729 *
[5273eb6]730 * @param s String which length shall be determined.
731 * @return Length of the string.
[7bb9b53]732 */
733size_t posix_strlen(const char *s)
734{
[ef6dd3f]735 assert(s != NULL);
736
[517cedc0]737 return (size_t) (posix_strchr(s, '\0') - s);
[ef6dd3f]738}
739
740/**
[5273eb6]741 * Get limited length of the string.
[ef6dd3f]742 *
[5273eb6]743 * @param s String which length shall be determined.
744 * @param n Maximum number of bytes that can be examined to determine length.
745 * @return The lower of either string length or n limit.
[ef6dd3f]746 */
747size_t posix_strnlen(const char *s, size_t n)
748{
749 assert(s != NULL);
750
[4f4b4e7]751 for (size_t sz = 0; sz < n; ++sz) {
[ef6dd3f]752
753 if (s[sz] == '\0') {
754 return sz;
755 }
756 }
757
758 return n;
[7bb9b53]759}
760
[d3ce33fa]761/**
[5273eb6]762 * Get description of a signal.
[d3ce33fa]763 *
[5273eb6]764 * @param signum Signal number.
765 * @return Human readable signal description.
[d3ce33fa]766 */
767char *posix_strsignal(int signum)
768{
769 static const char *const sigstrings[] = {
770 [SIGABRT] = "SIGABRT (Process abort signal)",
771 [SIGALRM] = "SIGALRM (Alarm clock)",
772 [SIGBUS] = "SIGBUS (Access to an undefined portion of a memory object)",
773 [SIGCHLD] = "SIGCHLD (Child process terminated, stopped, or continued)",
774 [SIGCONT] = "SIGCONT (Continue executing, if stopped)",
775 [SIGFPE] = "SIGFPE (Erroneous arithmetic operation)",
776 [SIGHUP] = "SIGHUP (Hangup)",
777 [SIGILL] = "SIGILL (Illegal instruction)",
778 [SIGINT] = "SIGINT (Terminal interrupt signal)",
779 [SIGKILL] = "SIGKILL (Kill process)",
780 [SIGPIPE] = "SIGPIPE (Write on a pipe with no one to read it)",
781 [SIGQUIT] = "SIGQUIT (Terminal quit signal)",
782 [SIGSEGV] = "SIGSEGV (Invalid memory reference)",
783 [SIGSTOP] = "SIGSTOP (Stop executing)",
784 [SIGTERM] = "SIGTERM (Termination signal)",
785 [SIGTSTP] = "SIGTSTP (Terminal stop signal)",
786 [SIGTTIN] = "SIGTTIN (Background process attempting read)",
787 [SIGTTOU] = "SIGTTOU (Background process attempting write)",
788 [SIGUSR1] = "SIGUSR1 (User-defined signal 1)",
789 [SIGUSR2] = "SIGUSR2 (User-defined signal 2)",
790 [SIGPOLL] = "SIGPOLL (Pollable event)",
791 [SIGPROF] = "SIGPROF (Profiling timer expired)",
792 [SIGSYS] = "SIGSYS (Bad system call)",
793 [SIGTRAP] = "SIGTRAP (Trace/breakpoint trap)",
794 [SIGURG] = "SIGURG (High bandwidth data is available at a socket)",
795 [SIGVTALRM] = "SIGVTALRM (Virtual timer expired)",
796 [SIGXCPU] = "SIGXCPU (CPU time limit exceeded)",
797 [SIGXFSZ] = "SIGXFSZ (File size limit exceeded)"
798 };
799
800 if (signum <= _TOP_SIGNAL) {
801 return (char *) sigstrings[signum];
802 }
803
804 return (char *) "ERROR, Invalid signal number";
805}
806
[7bb9b53]807/** @}
808 */
Note: See TracBrowser for help on using the repository browser.