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
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 "posix/assert.h"
43#include "posix/errno.h"
44#include "posix/limits.h"
45#include "posix/stdlib.h"
46#include "posix/signal.h"
47
48#include "libc/str_error.h"
49
50/**
51 * The same as strpbrk, except it returns pointer to the nul terminator
52 * if no occurence is found.
53 *
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.
58 */
59static char *strpbrk_null(const char *s1, const char *s2)
60{
61 while (!posix_strchr(s2, *s1)) {
62 ++s1;
63 }
64
65 return (char *) s1;
66}
67
68/**
69 * Copy a string.
70 *
71 * @param dest Destination pre-allocated buffer.
72 * @param src Source string to be copied.
73 * @return Pointer to the destination buffer.
74 */
75char *posix_strcpy(char *restrict dest, const char *restrict src)
76{
77 posix_stpcpy(dest, src);
78 return dest;
79}
80
81/**
82 * Copy fixed length string.
83 *
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.
88 */
89char *posix_strncpy(char *restrict dest, const char *restrict src, size_t n)
90{
91 posix_stpncpy(dest, src, n);
92 return dest;
93}
94
95/**
96 * Copy a string.
97 *
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.
101 */
102char *posix_stpcpy(char *restrict dest, const char *restrict src)
103{
104 assert(dest != NULL);
105 assert(src != NULL);
106
107 for (size_t i = 0; ; ++i) {
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;
118}
119
120/**
121 * Copy fixed length string.
122 *
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].
127 */
128char *posix_stpncpy(char *restrict dest, const char *restrict src, size_t n)
129{
130 assert(dest != NULL);
131 assert(src != NULL);
132
133 for (size_t i = 0; i < n; ++i) {
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];
141 for (++i; i < n; ++i) {
142 dest[i] = '\0';
143 }
144 return result;
145 }
146 }
147
148 return &dest[n];
149}
150
151/**
152 * Concatenate two strings.
153 *
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.
157 */
158char *posix_strcat(char *restrict dest, const char *restrict src)
159{
160 assert(dest != NULL);
161 assert(src != NULL);
162
163 posix_strcpy(posix_strchr(dest, '\0'), src);
164 return dest;
165}
166
167/**
168 * Concatenate a string with part of another.
169 *
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.
174 */
175char *posix_strncat(char *restrict dest, const char *restrict src, size_t n)
176{
177 assert(dest != NULL);
178 assert(src != NULL);
179
180 char *zeroptr = posix_strncpy(posix_strchr(dest, '\0'), src, n);
181 /* strncpy doesn't append the nul terminator, so we do it here */
182 zeroptr[n] = '\0';
183 return dest;
184}
185
186/**
187 * Copy limited number of bytes in memory.
188 *
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.
193 * @return Pointer to the first byte after c in dest if found, NULL otherwise.
194 */
195void *posix_memccpy(void *restrict dest, const void *restrict src, int c, size_t n)
196{
197 assert(dest != NULL);
198 assert(src != NULL);
199
200 unsigned char* bdest = dest;
201 const unsigned char* bsrc = src;
202
203 for (size_t i = 0; i < n; ++i) {
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;
213}
214
215/**
216 * Duplicate a string.
217 *
218 * @param s String to be duplicated.
219 * @return Newly allocated copy of the string.
220 */
221char *posix_strdup(const char *s)
222{
223 return posix_strndup(s, SIZE_MAX);
224}
225
226/**
227 * Duplicate a specific number of bytes from a string.
228 *
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.
232 */
233char *posix_strndup(const char *s, size_t n)
234{
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;
247}
248
249/**
250 * Compare bytes in memory.
251 *
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.
255 * @return Difference of the first pair of inequal bytes,
256 * or 0 if areas have the same content.
257 */
258int posix_memcmp(const void *mem1, const void *mem2, size_t n)
259{
260 assert(mem1 != NULL);
261 assert(mem2 != NULL);
262
263 const unsigned char *s1 = mem1;
264 const unsigned char *s2 = mem2;
265
266 for (size_t i = 0; i < n; ++i) {
267 if (s1[i] != s2[i]) {
268 return s1[i] - s2[i];
269 }
270 }
271
272 return 0;
273}
274
275/**
276 * Compare two strings.
277 *
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.
282 */
283int posix_strcmp(const char *s1, const char *s2)
284{
285 assert(s1 != NULL);
286 assert(s2 != NULL);
287
288 return posix_strncmp(s1, s2, STR_NO_LIMIT);
289}
290
291/**
292 * Compare part of two strings.
293 *
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.
299 */
300int posix_strncmp(const char *s1, const char *s2, size_t n)
301{
302 assert(s1 != NULL);
303 assert(s2 != NULL);
304
305 for (size_t i = 0; i < n; ++i) {
306 if (s1[i] != s2[i]) {
307 return s1[i] - s2[i];
308 }
309 if (s1[i] == '\0') {
310 break;
311 }
312 }
313
314 return 0;
315}
316
317/**
318 * Find byte in memory.
319 *
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.
325 */
326void *posix_memchr(const void *mem, int c, size_t n)
327{
328 assert(mem != NULL);
329
330 const unsigned char *s = mem;
331
332 for (size_t i = 0; i < n; ++i) {
333 if (s[i] == (unsigned char) c) {
334 return (void *) &s[i];
335 }
336 }
337 return NULL;
338}
339
340/**
341 * Scan string for a first occurence of a character.
342 *
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.
347 */
348char *posix_strchr(const char *s, int c)
349{
350 assert(s != NULL);
351
352 char *res = gnu_strchrnul(s, c);
353 return (*res == c) ? res : NULL;
354}
355
356/**
357 * Scan string for a last occurence of a character.
358 *
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.
363 */
364char *posix_strrchr(const char *s, int c)
365{
366 assert(s != NULL);
367
368 const char *ptr = posix_strchr(s, '\0');
369
370 /* the same as in strchr, except it loops in reverse direction */
371 while (*ptr != (char) c) {
372 if (ptr == s) {
373 return NULL;
374 }
375
376 ptr--;
377 }
378
379 return (char *) ptr;
380}
381
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 */
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
401/**
402 * Scan a string for a first occurence of one of provided bytes.
403 *
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.
408 */
409char *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 * Get the length of a complementary substring.
420 *
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.
424 */
425size_t posix_strcspn(const char *s1, const char *s2)
426{
427 assert(s1 != NULL);
428 assert(s2 != NULL);
429
430 char *ptr = strpbrk_null(s1, s2);
431 return (size_t) (ptr - s1);
432}
433
434/**
435 * Get length of a substring.
436 *
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.
440 */
441size_t posix_strspn(const char *s1, const char *s2)
442{
443 assert(s1 != NULL);
444 assert(s2 != NULL);
445
446 const char *ptr;
447 for (ptr = s1; *ptr != '\0'; ++ptr) {
448 if (!posix_strchr(s2, *ptr)) {
449 break;
450 }
451 }
452 return ptr - s1;
453}
454
455/**
456 * Find a substring. Uses Knuth-Morris-Pratt algorithm.
457 *
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.
462 */
463char *posix_strstr(const char *haystack, const char *needle)
464{
465 assert(haystack != NULL);
466 assert(needle != NULL);
467
468 /* Special case - needle is an empty string. */
469 if (needle[0] == '\0') {
470 return (char *) haystack;
471 }
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
495 for (size_t hpos = 0; haystack[hpos] != '\0'; ++hpos) {
496 while (npos != 0 && haystack[hpos] != needle[npos]) {
497 npos = prefix_table[npos];
498 }
499
500 if (haystack[hpos] == needle[npos]) {
501 npos++;
502
503 if (npos == nlen) {
504 return (char *) (haystack + hpos - nlen + 1);
505 }
506 }
507 }
508
509 return NULL;
510}
511
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
565/**
566 * String comparison using collating information.
567 *
568 * Currently ignores locale and just calls strcmp.
569 *
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.
574 */
575int posix_strcoll(const char *s1, const char *s2)
576{
577 assert(s1 != NULL);
578 assert(s2 != NULL);
579
580 return posix_strcmp(s1, s2);
581}
582
583/**
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.
589 *
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.
594 */
595size_t posix_strxfrm(char *restrict s1, const char *restrict s2, size_t n)
596{
597 assert(s1 != NULL || n == 0);
598 assert(s2 != NULL);
599
600 size_t len = posix_strlen(s2);
601
602 if (n > len) {
603 posix_strcpy(s1, s2);
604 }
605
606 return len;
607}
608
609/**
610 * Get error message string.
611 *
612 * @param errnum Error code for which to obtain human readable string.
613 * @return Error message.
614 */
615char *posix_strerror(int errnum)
616{
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)];
702}
703
704/**
705 * Get error message string.
706 *
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.
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) {
719 return ERANGE;
720 } else {
721 posix_strcpy(buf, errstr);
722 }
723
724 return 0;
725}
726
727/**
728 * Get length of the string.
729 *
730 * @param s String which length shall be determined.
731 * @return Length of the string.
732 */
733size_t posix_strlen(const char *s)
734{
735 assert(s != NULL);
736
737 return (size_t) (posix_strchr(s, '\0') - s);
738}
739
740/**
741 * Get limited length of the string.
742 *
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.
746 */
747size_t posix_strnlen(const char *s, size_t n)
748{
749 assert(s != NULL);
750
751 for (size_t sz = 0; sz < n; ++sz) {
752
753 if (s[sz] == '\0') {
754 return sz;
755 }
756 }
757
758 return n;
759}
760
761/**
762 * Get description of a signal.
763 *
764 * @param signum Signal number.
765 * @return Human readable signal description.
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
807/** @}
808 */
Note: See TracBrowser for help on using the repository browser.