Changeset 8cd8bf6 in mainline for uspace/lib/posix/string.c


Ignore:
Timestamp:
2011-07-08T17:25:53Z (13 years ago)
Author:
Petr Koupy <petr.koupy@…>
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.
Message:

Merge libposix changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/posix/string.c

    rf5b2522 r8cd8bf6  
    3636#define LIBPOSIX_INTERNAL
    3737
     38#include "internal/common.h"
    3839#include "string.h"
    3940
    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"
    5948
    6049/**
     
    183172        assert(src != NULL);
    184173
    185         posix_strcpy(strzero(dest), src);
     174        posix_strcpy(posix_strchr(dest, '\0'), src);
    186175        return dest;
    187176}
     
    199188        assert(src != NULL);
    200189
    201         char *zeroptr = posix_strncpy(strzero(dest), src, n);
     190        char *zeroptr = posix_strncpy(posix_strchr(dest, '\0'), src, n);
    202191        /* strncpy doesn't append the nul terminator, so we do it here */
    203192        zeroptr[n] = '\0';
     
    240229char *posix_strdup(const char *s)
    241230{
    242         // FIXME: SIZE_MAX doesn't work
    243         return posix_strndup(s, STR_NO_LIMIT);
     231        return posix_strndup(s, SIZE_MAX);
    244232}
    245233
     
    360348        assert(s != NULL);
    361349       
    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;
    377352}
    378353
     
    387362        assert(s != NULL);
    388363       
    389         const char *ptr = strzero(s);
     364        const char *ptr = posix_strchr(s, '\0');
    390365       
    391366        /* the same as in strchr, except it loops in reverse direction */
     
    399374
    400375        return (char *) ptr;
     376}
     377
     378char *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;
    401387}
    402388
     
    524510char *posix_strerror(int errnum)
    525511{
    526         /* uses function from libc, we just have to negate errno
    527          * (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).
    528514         */
    529         return (char *) str_error(-errnum);
     515        // FIXME: not all POSIX error codes are in libc
     516        return (char *) str_error(-posix_abs(errnum));
    530517}
    531518
     
    562549        assert(s != NULL);
    563550       
    564         return (size_t) (strzero(s) - s);
     551        return (size_t) (posix_strchr(s, '\0') - s);
    565552}
    566553
     
    585572}
    586573
     574/**
     575 *
     576 * @param signum
     577 * @return
     578 */
     579char *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
    587619/** @}
    588620 */
Note: See TracChangeset for help on using the changeset viewer.