Changeset 8cd8bf6 in mainline for uspace/lib/posix/string.c
- Timestamp:
- 2011-07-08T17:25:53Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 11809eab
- Parents:
- f5b2522 (diff), ddc63fd (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/string.c
rf5b2522 r8cd8bf6 36 36 #define LIBPOSIX_INTERNAL 37 37 38 #include "internal/common.h" 38 39 #include "string.h" 39 40 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 } 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" 59 48 60 49 /** … … 183 172 assert(src != NULL); 184 173 185 posix_strcpy( strzero(dest), src);174 posix_strcpy(posix_strchr(dest, '\0'), src); 186 175 return dest; 187 176 } … … 199 188 assert(src != NULL); 200 189 201 char *zeroptr = posix_strncpy( strzero(dest), src, n);190 char *zeroptr = posix_strncpy(posix_strchr(dest, '\0'), src, n); 202 191 /* strncpy doesn't append the nul terminator, so we do it here */ 203 192 zeroptr[n] = '\0'; … … 240 229 char *posix_strdup(const char *s) 241 230 { 242 // FIXME: SIZE_MAX doesn't work 243 return posix_strndup(s, STR_NO_LIMIT); 231 return posix_strndup(s, SIZE_MAX); 244 232 } 245 233 … … 360 348 assert(s != NULL); 361 349 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; 350 char *res = gnu_strchrnul(s, c); 351 return (*res == c) ? res : NULL; 377 352 } 378 353 … … 387 362 assert(s != NULL); 388 363 389 const char *ptr = strzero(s);364 const char *ptr = posix_strchr(s, '\0'); 390 365 391 366 /* the same as in strchr, except it loops in reverse direction */ … … 399 374 400 375 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; 401 387 } 402 388 … … 524 510 char *posix_strerror(int errnum) 525 511 { 526 /* uses function from libc, we just have to negate errno527 * (POSIX uses positive errorcodes, HelenOS has negative) 512 /* Uses function from libc, we just have to negate errno 513 * (POSIX uses positive errorcodes, HelenOS has negative). 528 514 */ 529 return (char *) str_error(-errnum); 515 // FIXME: not all POSIX error codes are in libc 516 return (char *) str_error(-posix_abs(errnum)); 530 517 } 531 518 … … 562 549 assert(s != NULL); 563 550 564 return (size_t) ( strzero(s) - s);551 return (size_t) (posix_strchr(s, '\0') - s); 565 552 } 566 553 … … 585 572 } 586 573 574 /** 575 * 576 * @param signum 577 * @return 578 */ 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 587 619 /** @} 588 620 */
Note:
See TracChangeset
for help on using the changeset viewer.