[7bb9b53] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Petr Koupy
|
---|
| 3 | * Copyright (c) 2011 Jiri Zarevucky
|
---|
[99d3123] | 4 | * Copyright (c) 2018 Jiri Svoboda
|
---|
[7bb9b53] | 5 | * All rights reserved.
|
---|
| 6 | *
|
---|
| 7 | * Redistribution and use in source and binary forms, with or without
|
---|
| 8 | * modification, are permitted provided that the following conditions
|
---|
| 9 | * are met:
|
---|
| 10 | *
|
---|
| 11 | * - Redistributions of source code must retain the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer.
|
---|
| 13 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 14 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 15 | * documentation and/or other materials provided with the distribution.
|
---|
| 16 | * - The name of the author may not be used to endorse or promote products
|
---|
| 17 | * derived from this software without specific prior written permission.
|
---|
| 18 | *
|
---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 29 | */
|
---|
| 30 |
|
---|
| 31 | /** @addtogroup libposix
|
---|
| 32 | * @{
|
---|
| 33 | */
|
---|
[5273eb6] | 34 | /** @file String manipulation.
|
---|
[7bb9b53] | 35 | */
|
---|
| 36 |
|
---|
[a6d908c1] | 37 | #include "internal/common.h"
|
---|
[9b8be79] | 38 | #include <string.h>
|
---|
[7bb9b53] | 39 |
|
---|
[e8d3c6f5] | 40 | #include <assert.h>
|
---|
[0d0b319] | 41 |
|
---|
| 42 | #include <errno.h>
|
---|
| 43 |
|
---|
[9b8be79] | 44 | #include <limits.h>
|
---|
| 45 | #include <stdlib.h>
|
---|
| 46 | #include <signal.h>
|
---|
| 47 | #include <str.h>
|
---|
| 48 | #include <str_error.h>
|
---|
[a6d908c1] | 49 |
|
---|
[7bb9b53] | 50 | /**
|
---|
[5273eb6] | 51 | * Copy a string.
|
---|
[7bb9b53] | 52 | *
|
---|
[5273eb6] | 53 | * @param dest Destination pre-allocated buffer.
|
---|
| 54 | * @param src Source string to be copied.
|
---|
| 55 | * @return Pointer to the nul character in the destination string.
|
---|
[7bb9b53] | 56 | */
|
---|
[7f9df7b9] | 57 | char *stpcpy(char *restrict dest, const char *restrict src)
|
---|
[7bb9b53] | 58 | {
|
---|
[ef6dd3f] | 59 | assert(dest != NULL);
|
---|
| 60 | assert(src != NULL);
|
---|
| 61 |
|
---|
[4f4b4e7] | 62 | for (size_t i = 0; ; ++i) {
|
---|
[ef6dd3f] | 63 | dest[i] = src[i];
|
---|
[a35b458] | 64 |
|
---|
[ef6dd3f] | 65 | if (src[i] == '\0') {
|
---|
| 66 | /* pointer to the terminating nul character */
|
---|
| 67 | return &dest[i];
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
[a35b458] | 70 |
|
---|
[ef6dd3f] | 71 | /* unreachable */
|
---|
| 72 | return NULL;
|
---|
[7bb9b53] | 73 | }
|
---|
| 74 |
|
---|
| 75 | /**
|
---|
[5273eb6] | 76 | * Copy fixed length string.
|
---|
[7bb9b53] | 77 | *
|
---|
[5273eb6] | 78 | * @param dest Destination pre-allocated buffer.
|
---|
| 79 | * @param src Source string to be copied.
|
---|
| 80 | * @param n Number of bytes to be stored into destination buffer.
|
---|
| 81 | * @return Pointer to the first written nul character or &dest[n].
|
---|
[7bb9b53] | 82 | */
|
---|
[7f9df7b9] | 83 | char *stpncpy(char *restrict dest, const char *restrict src, size_t n)
|
---|
[7bb9b53] | 84 | {
|
---|
[ef6dd3f] | 85 | assert(dest != NULL);
|
---|
| 86 | assert(src != NULL);
|
---|
| 87 |
|
---|
[4f4b4e7] | 88 | for (size_t i = 0; i < n; ++i) {
|
---|
[ef6dd3f] | 89 | dest[i] = src[i];
|
---|
[a35b458] | 90 |
|
---|
[7c3fb9b] | 91 | /*
|
---|
| 92 | * the standard requires that nul characters
|
---|
[ef6dd3f] | 93 | * are appended to the length of n, in case src is shorter
|
---|
| 94 | */
|
---|
| 95 | if (src[i] == '\0') {
|
---|
| 96 | char *result = &dest[i];
|
---|
[4f4b4e7] | 97 | for (++i; i < n; ++i) {
|
---|
[ef6dd3f] | 98 | dest[i] = '\0';
|
---|
| 99 | }
|
---|
| 100 | return result;
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
[a35b458] | 103 |
|
---|
[ef6dd3f] | 104 | return &dest[n];
|
---|
[7bb9b53] | 105 | }
|
---|
| 106 |
|
---|
| 107 | /**
|
---|
[5273eb6] | 108 | * Copy limited number of bytes in memory.
|
---|
[7bb9b53] | 109 | *
|
---|
[5273eb6] | 110 | * @param dest Destination buffer.
|
---|
| 111 | * @param src Source buffer.
|
---|
| 112 | * @param c Character after which the copying shall stop.
|
---|
| 113 | * @param n Number of bytes that shall be copied if not stopped earlier by c.
|
---|
[ef6dd3f] | 114 | * @return Pointer to the first byte after c in dest if found, NULL otherwise.
|
---|
[7bb9b53] | 115 | */
|
---|
[7f9df7b9] | 116 | void *memccpy(void *restrict dest, const void *restrict src, int c, size_t n)
|
---|
[7bb9b53] | 117 | {
|
---|
[ef6dd3f] | 118 | assert(dest != NULL);
|
---|
| 119 | assert(src != NULL);
|
---|
[a35b458] | 120 |
|
---|
[1433ecda] | 121 | unsigned char *bdest = dest;
|
---|
| 122 | const unsigned char *bsrc = src;
|
---|
[a35b458] | 123 |
|
---|
[4f4b4e7] | 124 | for (size_t i = 0; i < n; ++i) {
|
---|
[ef6dd3f] | 125 | bdest[i] = bsrc[i];
|
---|
[a35b458] | 126 |
|
---|
[ef6dd3f] | 127 | if (bsrc[i] == (unsigned char) c) {
|
---|
| 128 | /* pointer to the next byte */
|
---|
| 129 | return &bdest[i + 1];
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
[a35b458] | 132 |
|
---|
[ef6dd3f] | 133 | return NULL;
|
---|
[7bb9b53] | 134 | }
|
---|
| 135 |
|
---|
| 136 | /**
|
---|
[5273eb6] | 137 | * Duplicate a string.
|
---|
[7bb9b53] | 138 | *
|
---|
[5273eb6] | 139 | * @param s String to be duplicated.
|
---|
| 140 | * @return Newly allocated copy of the string.
|
---|
[7bb9b53] | 141 | */
|
---|
[7f9df7b9] | 142 | char *strdup(const char *s)
|
---|
[7bb9b53] | 143 | {
|
---|
[7f9df7b9] | 144 | return strndup(s, SIZE_MAX);
|
---|
[7bb9b53] | 145 | }
|
---|
| 146 |
|
---|
| 147 | /**
|
---|
[5273eb6] | 148 | * Duplicate a specific number of bytes from a string.
|
---|
[7bb9b53] | 149 | *
|
---|
[5273eb6] | 150 | * @param s String to be duplicated.
|
---|
| 151 | * @param n Maximum length of the resulting string..
|
---|
| 152 | * @return Newly allocated string copy of length at most n.
|
---|
[7bb9b53] | 153 | */
|
---|
[7f9df7b9] | 154 | char *strndup(const char *s, size_t n)
|
---|
[7bb9b53] | 155 | {
|
---|
[ef6dd3f] | 156 | assert(s != NULL);
|
---|
| 157 |
|
---|
[7f9df7b9] | 158 | size_t len = strnlen(s, n);
|
---|
[ef6dd3f] | 159 | char *dup = malloc(len + 1);
|
---|
| 160 | if (dup == NULL) {
|
---|
| 161 | return NULL;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | memcpy(dup, s, len);
|
---|
| 165 | dup[len] = '\0';
|
---|
| 166 |
|
---|
| 167 | return dup;
|
---|
[7bb9b53] | 168 | }
|
---|
| 169 |
|
---|
[5273eb6] | 170 | /**
|
---|
| 171 | * Scan string for a first occurence of a character.
|
---|
| 172 | *
|
---|
| 173 | * @param s String in which to look for the character.
|
---|
| 174 | * @param c Character to look for.
|
---|
| 175 | * @return Pointer to the specified character on success, pointer to the
|
---|
| 176 | * string terminator otherwise.
|
---|
| 177 | */
|
---|
[517cedc0] | 178 | char *gnu_strchrnul(const char *s, int c)
|
---|
| 179 | {
|
---|
| 180 | assert(s != NULL);
|
---|
[a35b458] | 181 |
|
---|
[517cedc0] | 182 | while (*s != c && *s != '\0') {
|
---|
| 183 | s++;
|
---|
| 184 | }
|
---|
[a35b458] | 185 |
|
---|
[517cedc0] | 186 | return (char *) s;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
[12b29f3] | 189 | /** Split string by delimiters.
|
---|
| 190 | *
|
---|
| 191 | * @param s String to be tokenized. May not be NULL.
|
---|
| 192 | * @param delim String with the delimiters.
|
---|
| 193 | * @param next Variable which will receive the pointer to the
|
---|
| 194 | * continuation of the string following the first
|
---|
| 195 | * occurrence of any of the delimiter characters.
|
---|
| 196 | * May be NULL.
|
---|
| 197 | * @return Pointer to the prefix of @a s before the first
|
---|
| 198 | * delimiter character. NULL if no such prefix
|
---|
| 199 | * exists.
|
---|
| 200 | */
|
---|
[7f9df7b9] | 201 | char *strtok_r(char *s, const char *delim, char **next)
|
---|
[12b29f3] | 202 | {
|
---|
[99d3123] | 203 | return __strtok_r(s, delim, next);
|
---|
[ef6dd3f] | 204 | }
|
---|
| 205 |
|
---|
| 206 | /**
|
---|
[5273eb6] | 207 | * Get error message string.
|
---|
[ef6dd3f] | 208 | *
|
---|
[5273eb6] | 209 | * @param errnum Error code for which to obtain human readable string.
|
---|
| 210 | * @param buf Buffer to store a human readable string to.
|
---|
| 211 | * @param bufsz Size of buffer pointed to by buf.
|
---|
| 212 | * @return Zero on success, errno otherwise.
|
---|
[ef6dd3f] | 213 | */
|
---|
[7f9df7b9] | 214 | int strerror_r(int errnum, char *buf, size_t bufsz)
|
---|
[ef6dd3f] | 215 | {
|
---|
| 216 | assert(buf != NULL);
|
---|
[a35b458] | 217 |
|
---|
[7f9df7b9] | 218 | char *errstr = strerror(errnum);
|
---|
[a35b458] | 219 |
|
---|
[7f9df7b9] | 220 | if (strlen(errstr) + 1 > bufsz) {
|
---|
[1b55da67] | 221 | return ERANGE;
|
---|
[ef6dd3f] | 222 | } else {
|
---|
[7f9df7b9] | 223 | strcpy(buf, errstr);
|
---|
[ef6dd3f] | 224 | }
|
---|
| 225 |
|
---|
[0d0b319] | 226 | return EOK;
|
---|
[7bb9b53] | 227 | }
|
---|
| 228 |
|
---|
[ef6dd3f] | 229 | /**
|
---|
[5273eb6] | 230 | * Get limited length of the string.
|
---|
[ef6dd3f] | 231 | *
|
---|
[5273eb6] | 232 | * @param s String which length shall be determined.
|
---|
| 233 | * @param n Maximum number of bytes that can be examined to determine length.
|
---|
| 234 | * @return The lower of either string length or n limit.
|
---|
[ef6dd3f] | 235 | */
|
---|
[7f9df7b9] | 236 | size_t strnlen(const char *s, size_t n)
|
---|
[ef6dd3f] | 237 | {
|
---|
| 238 | assert(s != NULL);
|
---|
[a35b458] | 239 |
|
---|
[4f4b4e7] | 240 | for (size_t sz = 0; sz < n; ++sz) {
|
---|
[a35b458] | 241 |
|
---|
[ef6dd3f] | 242 | if (s[sz] == '\0') {
|
---|
| 243 | return sz;
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
[a35b458] | 246 |
|
---|
[ef6dd3f] | 247 | return n;
|
---|
[7bb9b53] | 248 | }
|
---|
| 249 |
|
---|
[d3ce33fa] | 250 | /**
|
---|
[5273eb6] | 251 | * Get description of a signal.
|
---|
[d3ce33fa] | 252 | *
|
---|
[5273eb6] | 253 | * @param signum Signal number.
|
---|
| 254 | * @return Human readable signal description.
|
---|
[d3ce33fa] | 255 | */
|
---|
[7f9df7b9] | 256 | char *strsignal(int signum)
|
---|
[d3ce33fa] | 257 | {
|
---|
| 258 | static const char *const sigstrings[] = {
|
---|
| 259 | [SIGABRT] = "SIGABRT (Process abort signal)",
|
---|
| 260 | [SIGALRM] = "SIGALRM (Alarm clock)",
|
---|
| 261 | [SIGBUS] = "SIGBUS (Access to an undefined portion of a memory object)",
|
---|
| 262 | [SIGCHLD] = "SIGCHLD (Child process terminated, stopped, or continued)",
|
---|
| 263 | [SIGCONT] = "SIGCONT (Continue executing, if stopped)",
|
---|
| 264 | [SIGFPE] = "SIGFPE (Erroneous arithmetic operation)",
|
---|
| 265 | [SIGHUP] = "SIGHUP (Hangup)",
|
---|
| 266 | [SIGILL] = "SIGILL (Illegal instruction)",
|
---|
| 267 | [SIGINT] = "SIGINT (Terminal interrupt signal)",
|
---|
| 268 | [SIGKILL] = "SIGKILL (Kill process)",
|
---|
| 269 | [SIGPIPE] = "SIGPIPE (Write on a pipe with no one to read it)",
|
---|
| 270 | [SIGQUIT] = "SIGQUIT (Terminal quit signal)",
|
---|
| 271 | [SIGSEGV] = "SIGSEGV (Invalid memory reference)",
|
---|
| 272 | [SIGSTOP] = "SIGSTOP (Stop executing)",
|
---|
| 273 | [SIGTERM] = "SIGTERM (Termination signal)",
|
---|
| 274 | [SIGTSTP] = "SIGTSTP (Terminal stop signal)",
|
---|
| 275 | [SIGTTIN] = "SIGTTIN (Background process attempting read)",
|
---|
| 276 | [SIGTTOU] = "SIGTTOU (Background process attempting write)",
|
---|
| 277 | [SIGUSR1] = "SIGUSR1 (User-defined signal 1)",
|
---|
| 278 | [SIGUSR2] = "SIGUSR2 (User-defined signal 2)",
|
---|
| 279 | [SIGPOLL] = "SIGPOLL (Pollable event)",
|
---|
| 280 | [SIGPROF] = "SIGPROF (Profiling timer expired)",
|
---|
| 281 | [SIGSYS] = "SIGSYS (Bad system call)",
|
---|
| 282 | [SIGTRAP] = "SIGTRAP (Trace/breakpoint trap)",
|
---|
| 283 | [SIGURG] = "SIGURG (High bandwidth data is available at a socket)",
|
---|
| 284 | [SIGVTALRM] = "SIGVTALRM (Virtual timer expired)",
|
---|
| 285 | [SIGXCPU] = "SIGXCPU (CPU time limit exceeded)",
|
---|
| 286 | [SIGXFSZ] = "SIGXFSZ (File size limit exceeded)"
|
---|
| 287 | };
|
---|
| 288 |
|
---|
| 289 | if (signum <= _TOP_SIGNAL) {
|
---|
| 290 | return (char *) sigstrings[signum];
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | return (char *) "ERROR, Invalid signal number";
|
---|
| 294 | }
|
---|
| 295 |
|
---|
[7bb9b53] | 296 | /** @}
|
---|
| 297 | */
|
---|