Ignore:
File:
1 edited

Legend:

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

    rec18957a r4f4b4e7  
    3636#define LIBPOSIX_INTERNAL
    3737
    38 #include "internal/common.h"
    3938#include "string.h"
    4039
    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 */
     51static char *strzero(const char *s)
     52{
     53        while (*s != '\0') {
     54                s++;
     55        }
     56
     57        return (char *) s;
     58}
    4859
    4960/**
     
    172183        assert(src != NULL);
    173184
    174         posix_strcpy(posix_strchr(dest, '\0'), src);
     185        posix_strcpy(strzero(dest), src);
    175186        return dest;
    176187}
     
    188199        assert(src != NULL);
    189200
    190         char *zeroptr = posix_strncpy(posix_strchr(dest, '\0'), src, n);
     201        char *zeroptr = posix_strncpy(strzero(dest), src, n);
    191202        /* strncpy doesn't append the nul terminator, so we do it here */
    192203        zeroptr[n] = '\0';
     
    229240char *posix_strdup(const char *s)
    230241{
    231         return posix_strndup(s, SIZE_MAX);
     242        // FIXME: SIZE_MAX doesn't work
     243        return posix_strndup(s, STR_NO_LIMIT);
    232244}
    233245
     
    348360        assert(s != NULL);
    349361       
    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;
    352377}
    353378
     
    362387        assert(s != NULL);
    363388       
    364         const char *ptr = posix_strchr(s, '\0');
     389        const char *ptr = strzero(s);
    365390       
    366391        /* the same as in strchr, except it loops in reverse direction */
     
    374399
    375400        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;
    387401}
    388402
     
    510524char *posix_strerror(int errnum)
    511525{
    512         /* Uses function from libc, we just have to negate errno
    513          * (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)
    514528         */
    515         // FIXME: not all POSIX error codes are in libc
    516         return (char *) str_error(-posix_abs(errnum));
     529        return (char *) str_error(-errnum);
    517530}
    518531
     
    549562        assert(s != NULL);
    550563       
    551         return (size_t) (posix_strchr(s, '\0') - s);
     564        return (size_t) (strzero(s) - s);
    552565}
    553566
     
    572585}
    573586
    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 
    619587/** @}
    620588 */
Note: See TracChangeset for help on using the changeset viewer.