Changes in uspace/lib/posix/string.c [ec18957a:4f4b4e7] in mainline
- File:
-
- 1 edited
-
uspace/lib/posix/string.c (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/string.c
rec18957a r4f4b4e7 36 36 #define LIBPOSIX_INTERNAL 37 37 38 #include "internal/common.h"39 38 #include "string.h" 40 39 41 #include "assert.h" 42 #include "errno.h" 43 #include "limits.h" 44 #include "stdlib.h" 45 #include "signal.h" 46 47 #include "libc/str_error.h" 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 } 48 59 49 60 /** … … 172 183 assert(src != NULL); 173 184 174 posix_strcpy( posix_strchr(dest, '\0'), src);185 posix_strcpy(strzero(dest), src); 175 186 return dest; 176 187 } … … 188 199 assert(src != NULL); 189 200 190 char *zeroptr = posix_strncpy( posix_strchr(dest, '\0'), src, n);201 char *zeroptr = posix_strncpy(strzero(dest), src, n); 191 202 /* strncpy doesn't append the nul terminator, so we do it here */ 192 203 zeroptr[n] = '\0'; … … 229 240 char *posix_strdup(const char *s) 230 241 { 231 return posix_strndup(s, SIZE_MAX); 242 // FIXME: SIZE_MAX doesn't work 243 return posix_strndup(s, STR_NO_LIMIT); 232 244 } 233 245 … … 348 360 assert(s != NULL); 349 361 350 char *res = gnu_strchrnul(s, c); 351 return (*res == c) ? res : NULL; 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; 352 377 } 353 378 … … 362 387 assert(s != NULL); 363 388 364 const char *ptr = posix_strchr(s, '\0');389 const char *ptr = strzero(s); 365 390 366 391 /* the same as in strchr, except it loops in reverse direction */ … … 374 399 375 400 return (char *) ptr; 376 }377 378 char *gnu_strchrnul(const char *s, int c)379 {380 assert(s != NULL);381 382 while (*s != c && *s != '\0') {383 s++;384 }385 386 return (char *) s;387 401 } 388 402 … … 510 524 char *posix_strerror(int errnum) 511 525 { 512 /* Uses function from libc, we just have to negate errno513 * (POSIX uses positive errorcodes, HelenOS has negative) .526 /* uses function from libc, we just have to negate errno 527 * (POSIX uses positive errorcodes, HelenOS has negative) 514 528 */ 515 // FIXME: not all POSIX error codes are in libc 516 return (char *) str_error(-posix_abs(errnum)); 529 return (char *) str_error(-errnum); 517 530 } 518 531 … … 549 562 assert(s != NULL); 550 563 551 return (size_t) ( posix_strchr(s, '\0') - s);564 return (size_t) (strzero(s) - s); 552 565 } 553 566 … … 572 585 } 573 586 574 /**575 *576 * @param signum577 * @return578 */579 char *posix_strsignal(int signum)580 {581 static const char *const sigstrings[] = {582 [SIGABRT] = "SIGABRT (Process abort signal)",583 [SIGALRM] = "SIGALRM (Alarm clock)",584 [SIGBUS] = "SIGBUS (Access to an undefined portion of a memory object)",585 [SIGCHLD] = "SIGCHLD (Child process terminated, stopped, or continued)",586 [SIGCONT] = "SIGCONT (Continue executing, if stopped)",587 [SIGFPE] = "SIGFPE (Erroneous arithmetic operation)",588 [SIGHUP] = "SIGHUP (Hangup)",589 [SIGILL] = "SIGILL (Illegal instruction)",590 [SIGINT] = "SIGINT (Terminal interrupt signal)",591 [SIGKILL] = "SIGKILL (Kill process)",592 [SIGPIPE] = "SIGPIPE (Write on a pipe with no one to read it)",593 [SIGQUIT] = "SIGQUIT (Terminal quit signal)",594 [SIGSEGV] = "SIGSEGV (Invalid memory reference)",595 [SIGSTOP] = "SIGSTOP (Stop executing)",596 [SIGTERM] = "SIGTERM (Termination signal)",597 [SIGTSTP] = "SIGTSTP (Terminal stop signal)",598 [SIGTTIN] = "SIGTTIN (Background process attempting read)",599 [SIGTTOU] = "SIGTTOU (Background process attempting write)",600 [SIGUSR1] = "SIGUSR1 (User-defined signal 1)",601 [SIGUSR2] = "SIGUSR2 (User-defined signal 2)",602 [SIGPOLL] = "SIGPOLL (Pollable event)",603 [SIGPROF] = "SIGPROF (Profiling timer expired)",604 [SIGSYS] = "SIGSYS (Bad system call)",605 [SIGTRAP] = "SIGTRAP (Trace/breakpoint trap)",606 [SIGURG] = "SIGURG (High bandwidth data is available at a socket)",607 [SIGVTALRM] = "SIGVTALRM (Virtual timer expired)",608 [SIGXCPU] = "SIGXCPU (CPU time limit exceeded)",609 [SIGXFSZ] = "SIGXFSZ (File size limit exceeded)"610 };611 612 if (signum <= _TOP_SIGNAL) {613 return (char *) sigstrings[signum];614 }615 616 return (char *) "ERROR, Invalid signal number";617 }618 619 587 /** @} 620 588 */
Note:
See TracChangeset
for help on using the changeset viewer.
