| [7bb9b53] | 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Petr Koupy
|
|---|
| 3 | * Copyright (c) 2011 Jiri Zarevucky
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /** @addtogroup libposix
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| [5273eb6] | 33 | /** @file String manipulation.
|
|---|
| [7bb9b53] | 34 | */
|
|---|
| 35 |
|
|---|
| [a6d908c1] | 36 | #include "internal/common.h"
|
|---|
| [a3da2b2] | 37 | #include "posix/string.h"
|
|---|
| [7bb9b53] | 38 |
|
|---|
| [e8d3c6f5] | 39 | #include <assert.h>
|
|---|
| [0d0b319] | 40 |
|
|---|
| 41 | #include <errno.h>
|
|---|
| 42 |
|
|---|
| [a3da2b2] | 43 | #include "posix/limits.h"
|
|---|
| 44 | #include "posix/stdlib.h"
|
|---|
| 45 | #include "posix/signal.h"
|
|---|
| [ef6dd3f] | 46 |
|
|---|
| [1d6dd2a] | 47 | #include "libc/str.h"
|
|---|
| [a6d908c1] | 48 | #include "libc/str_error.h"
|
|---|
| 49 |
|
|---|
| [4f4b4e7] | 50 | /**
|
|---|
| 51 | * The same as strpbrk, except it returns pointer to the nul terminator
|
|---|
| [ef6dd3f] | 52 | * if no occurence is found.
|
|---|
| [4f4b4e7] | 53 | *
|
|---|
| [5273eb6] | 54 | * @param s1 String in which to look for the bytes.
|
|---|
| 55 | * @param s2 String of bytes to look for.
|
|---|
| 56 | * @return Pointer to the found byte on success, pointer to the
|
|---|
| 57 | * string terminator otherwise.
|
|---|
| [ef6dd3f] | 58 | */
|
|---|
| 59 | static char *strpbrk_null(const char *s1, const char *s2)
|
|---|
| 60 | {
|
|---|
| [7f9df7b9] | 61 | while (!strchr(s2, *s1)) {
|
|---|
| [4f4b4e7] | 62 | ++s1;
|
|---|
| [ef6dd3f] | 63 | }
|
|---|
| [a35b458] | 64 |
|
|---|
| [ef6dd3f] | 65 | return (char *) s1;
|
|---|
| 66 | }
|
|---|
| [7bb9b53] | 67 |
|
|---|
| 68 | /**
|
|---|
| [5273eb6] | 69 | * Copy a string.
|
|---|
| [7bb9b53] | 70 | *
|
|---|
| [5273eb6] | 71 | * @param dest Destination pre-allocated buffer.
|
|---|
| 72 | * @param src Source string to be copied.
|
|---|
| 73 | * @return Pointer to the destination buffer.
|
|---|
| [7bb9b53] | 74 | */
|
|---|
| [7f9df7b9] | 75 | char *strcpy(char *restrict dest, const char *restrict src)
|
|---|
| [7bb9b53] | 76 | {
|
|---|
| [7f9df7b9] | 77 | stpcpy(dest, src);
|
|---|
| [ef6dd3f] | 78 | return dest;
|
|---|
| [7bb9b53] | 79 | }
|
|---|
| 80 |
|
|---|
| 81 | /**
|
|---|
| [5273eb6] | 82 | * Copy fixed length string.
|
|---|
| [7bb9b53] | 83 | *
|
|---|
| [5273eb6] | 84 | * @param dest Destination pre-allocated buffer.
|
|---|
| 85 | * @param src Source string to be copied.
|
|---|
| 86 | * @param n Number of bytes to be stored into destination buffer.
|
|---|
| 87 | * @return Pointer to the destination buffer.
|
|---|
| [7bb9b53] | 88 | */
|
|---|
| [7f9df7b9] | 89 | char *strncpy(char *restrict dest, const char *restrict src, size_t n)
|
|---|
| [7bb9b53] | 90 | {
|
|---|
| [7f9df7b9] | 91 | stpncpy(dest, src, n);
|
|---|
| [ef6dd3f] | 92 | return dest;
|
|---|
| [7bb9b53] | 93 | }
|
|---|
| 94 |
|
|---|
| 95 | /**
|
|---|
| [5273eb6] | 96 | * Copy a string.
|
|---|
| [7bb9b53] | 97 | *
|
|---|
| [5273eb6] | 98 | * @param dest Destination pre-allocated buffer.
|
|---|
| 99 | * @param src Source string to be copied.
|
|---|
| 100 | * @return Pointer to the nul character in the destination string.
|
|---|
| [7bb9b53] | 101 | */
|
|---|
| [7f9df7b9] | 102 | char *stpcpy(char *restrict dest, const char *restrict src)
|
|---|
| [7bb9b53] | 103 | {
|
|---|
| [ef6dd3f] | 104 | assert(dest != NULL);
|
|---|
| 105 | assert(src != NULL);
|
|---|
| 106 |
|
|---|
| [4f4b4e7] | 107 | for (size_t i = 0; ; ++i) {
|
|---|
| [ef6dd3f] | 108 | dest[i] = src[i];
|
|---|
| [a35b458] | 109 |
|
|---|
| [ef6dd3f] | 110 | if (src[i] == '\0') {
|
|---|
| 111 | /* pointer to the terminating nul character */
|
|---|
| 112 | return &dest[i];
|
|---|
| 113 | }
|
|---|
| 114 | }
|
|---|
| [a35b458] | 115 |
|
|---|
| [ef6dd3f] | 116 | /* unreachable */
|
|---|
| 117 | return NULL;
|
|---|
| [7bb9b53] | 118 | }
|
|---|
| 119 |
|
|---|
| 120 | /**
|
|---|
| [5273eb6] | 121 | * Copy fixed length string.
|
|---|
| [7bb9b53] | 122 | *
|
|---|
| [5273eb6] | 123 | * @param dest Destination pre-allocated buffer.
|
|---|
| 124 | * @param src Source string to be copied.
|
|---|
| 125 | * @param n Number of bytes to be stored into destination buffer.
|
|---|
| 126 | * @return Pointer to the first written nul character or &dest[n].
|
|---|
| [7bb9b53] | 127 | */
|
|---|
| [7f9df7b9] | 128 | char *stpncpy(char *restrict dest, const char *restrict src, size_t n)
|
|---|
| [7bb9b53] | 129 | {
|
|---|
| [ef6dd3f] | 130 | assert(dest != NULL);
|
|---|
| 131 | assert(src != NULL);
|
|---|
| 132 |
|
|---|
| [4f4b4e7] | 133 | for (size_t i = 0; i < n; ++i) {
|
|---|
| [ef6dd3f] | 134 | dest[i] = src[i];
|
|---|
| [a35b458] | 135 |
|
|---|
| [7c3fb9b] | 136 | /*
|
|---|
| 137 | * the standard requires that nul characters
|
|---|
| [ef6dd3f] | 138 | * are appended to the length of n, in case src is shorter
|
|---|
| 139 | */
|
|---|
| 140 | if (src[i] == '\0') {
|
|---|
| 141 | char *result = &dest[i];
|
|---|
| [4f4b4e7] | 142 | for (++i; i < n; ++i) {
|
|---|
| [ef6dd3f] | 143 | dest[i] = '\0';
|
|---|
| 144 | }
|
|---|
| 145 | return result;
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| [a35b458] | 148 |
|
|---|
| [ef6dd3f] | 149 | return &dest[n];
|
|---|
| [7bb9b53] | 150 | }
|
|---|
| 151 |
|
|---|
| 152 | /**
|
|---|
| [5273eb6] | 153 | * Concatenate two strings.
|
|---|
| [ef6dd3f] | 154 | *
|
|---|
| [5273eb6] | 155 | * @param dest String to which src shall be appended.
|
|---|
| 156 | * @param src String to be appended after dest.
|
|---|
| 157 | * @return Pointer to destination buffer.
|
|---|
| [7bb9b53] | 158 | */
|
|---|
| [7f9df7b9] | 159 | char *strcat(char *restrict dest, const char *restrict src)
|
|---|
| [7bb9b53] | 160 | {
|
|---|
| [ef6dd3f] | 161 | assert(dest != NULL);
|
|---|
| 162 | assert(src != NULL);
|
|---|
| 163 |
|
|---|
| [7f9df7b9] | 164 | strcpy(strchr(dest, '\0'), src);
|
|---|
| [ef6dd3f] | 165 | return dest;
|
|---|
| [7bb9b53] | 166 | }
|
|---|
| 167 |
|
|---|
| 168 | /**
|
|---|
| [5273eb6] | 169 | * Concatenate a string with part of another.
|
|---|
| [ef6dd3f] | 170 | *
|
|---|
| [5273eb6] | 171 | * @param dest String to which part of src shall be appended.
|
|---|
| 172 | * @param src String whose part shall be appended after dest.
|
|---|
| 173 | * @param n Number of bytes to append after dest.
|
|---|
| 174 | * @return Pointer to destination buffer.
|
|---|
| [7bb9b53] | 175 | */
|
|---|
| [7f9df7b9] | 176 | char *strncat(char *restrict dest, const char *restrict src, size_t n)
|
|---|
| [7bb9b53] | 177 | {
|
|---|
| [ef6dd3f] | 178 | assert(dest != NULL);
|
|---|
| 179 | assert(src != NULL);
|
|---|
| 180 |
|
|---|
| [7f9df7b9] | 181 | char *zeroptr = strncpy(strchr(dest, '\0'), src, n);
|
|---|
| [ef6dd3f] | 182 | /* strncpy doesn't append the nul terminator, so we do it here */
|
|---|
| 183 | zeroptr[n] = '\0';
|
|---|
| 184 | return dest;
|
|---|
| [7bb9b53] | 185 | }
|
|---|
| 186 |
|
|---|
| 187 | /**
|
|---|
| [5273eb6] | 188 | * Copy limited number of bytes in memory.
|
|---|
| [7bb9b53] | 189 | *
|
|---|
| [5273eb6] | 190 | * @param dest Destination buffer.
|
|---|
| 191 | * @param src Source buffer.
|
|---|
| 192 | * @param c Character after which the copying shall stop.
|
|---|
| 193 | * @param n Number of bytes that shall be copied if not stopped earlier by c.
|
|---|
| [ef6dd3f] | 194 | * @return Pointer to the first byte after c in dest if found, NULL otherwise.
|
|---|
| [7bb9b53] | 195 | */
|
|---|
| [7f9df7b9] | 196 | void *memccpy(void *restrict dest, const void *restrict src, int c, size_t n)
|
|---|
| [7bb9b53] | 197 | {
|
|---|
| [ef6dd3f] | 198 | assert(dest != NULL);
|
|---|
| 199 | assert(src != NULL);
|
|---|
| [a35b458] | 200 |
|
|---|
| [1433ecda] | 201 | unsigned char *bdest = dest;
|
|---|
| 202 | const unsigned char *bsrc = src;
|
|---|
| [a35b458] | 203 |
|
|---|
| [4f4b4e7] | 204 | for (size_t i = 0; i < n; ++i) {
|
|---|
| [ef6dd3f] | 205 | bdest[i] = bsrc[i];
|
|---|
| [a35b458] | 206 |
|
|---|
| [ef6dd3f] | 207 | if (bsrc[i] == (unsigned char) c) {
|
|---|
| 208 | /* pointer to the next byte */
|
|---|
| 209 | return &bdest[i + 1];
|
|---|
| 210 | }
|
|---|
| 211 | }
|
|---|
| [a35b458] | 212 |
|
|---|
| [ef6dd3f] | 213 | return NULL;
|
|---|
| [7bb9b53] | 214 | }
|
|---|
| 215 |
|
|---|
| 216 | /**
|
|---|
| [5273eb6] | 217 | * Duplicate a string.
|
|---|
| [7bb9b53] | 218 | *
|
|---|
| [5273eb6] | 219 | * @param s String to be duplicated.
|
|---|
| 220 | * @return Newly allocated copy of the string.
|
|---|
| [7bb9b53] | 221 | */
|
|---|
| [7f9df7b9] | 222 | char *strdup(const char *s)
|
|---|
| [7bb9b53] | 223 | {
|
|---|
| [7f9df7b9] | 224 | return strndup(s, SIZE_MAX);
|
|---|
| [7bb9b53] | 225 | }
|
|---|
| 226 |
|
|---|
| 227 | /**
|
|---|
| [5273eb6] | 228 | * Duplicate a specific number of bytes from a string.
|
|---|
| [7bb9b53] | 229 | *
|
|---|
| [5273eb6] | 230 | * @param s String to be duplicated.
|
|---|
| 231 | * @param n Maximum length of the resulting string..
|
|---|
| 232 | * @return Newly allocated string copy of length at most n.
|
|---|
| [7bb9b53] | 233 | */
|
|---|
| [7f9df7b9] | 234 | char *strndup(const char *s, size_t n)
|
|---|
| [7bb9b53] | 235 | {
|
|---|
| [ef6dd3f] | 236 | assert(s != NULL);
|
|---|
| 237 |
|
|---|
| [7f9df7b9] | 238 | size_t len = strnlen(s, n);
|
|---|
| [ef6dd3f] | 239 | char *dup = malloc(len + 1);
|
|---|
| 240 | if (dup == NULL) {
|
|---|
| 241 | return NULL;
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | memcpy(dup, s, len);
|
|---|
| 245 | dup[len] = '\0';
|
|---|
| 246 |
|
|---|
| 247 | return dup;
|
|---|
| [7bb9b53] | 248 | }
|
|---|
| 249 |
|
|---|
| 250 | /**
|
|---|
| [5273eb6] | 251 | * Compare two strings.
|
|---|
| [ef6dd3f] | 252 | *
|
|---|
| [5273eb6] | 253 | * @param s1 First string to be compared.
|
|---|
| 254 | * @param s2 Second string to be compared.
|
|---|
| 255 | * @return Difference of the first pair of inequal characters,
|
|---|
| 256 | * or 0 if strings have the same content.
|
|---|
| [7bb9b53] | 257 | */
|
|---|
| [7f9df7b9] | 258 | int strcmp(const char *s1, const char *s2)
|
|---|
| [7bb9b53] | 259 | {
|
|---|
| [ef6dd3f] | 260 | assert(s1 != NULL);
|
|---|
| 261 | assert(s2 != NULL);
|
|---|
| 262 |
|
|---|
| [7f9df7b9] | 263 | return strncmp(s1, s2, STR_NO_LIMIT);
|
|---|
| [7bb9b53] | 264 | }
|
|---|
| 265 |
|
|---|
| 266 | /**
|
|---|
| [5273eb6] | 267 | * Compare part of two strings.
|
|---|
| [7bb9b53] | 268 | *
|
|---|
| [5273eb6] | 269 | * @param s1 First string to be compared.
|
|---|
| 270 | * @param s2 Second string to be compared.
|
|---|
| 271 | * @param n Maximum number of characters to be compared.
|
|---|
| 272 | * @return Difference of the first pair of inequal characters,
|
|---|
| 273 | * or 0 if strings have the same content.
|
|---|
| [7bb9b53] | 274 | */
|
|---|
| [7f9df7b9] | 275 | int strncmp(const char *s1, const char *s2, size_t n)
|
|---|
| [7bb9b53] | 276 | {
|
|---|
| [ef6dd3f] | 277 | assert(s1 != NULL);
|
|---|
| 278 | assert(s2 != NULL);
|
|---|
| 279 |
|
|---|
| [4f4b4e7] | 280 | for (size_t i = 0; i < n; ++i) {
|
|---|
| [ef6dd3f] | 281 | if (s1[i] != s2[i]) {
|
|---|
| [087c4c56] | 282 | return s1[i] - s2[i];
|
|---|
| [ef6dd3f] | 283 | }
|
|---|
| 284 | if (s1[i] == '\0') {
|
|---|
| 285 | break;
|
|---|
| 286 | }
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| [7bb9b53] | 289 | return 0;
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | /**
|
|---|
| [5273eb6] | 293 | * Scan string for a first occurence of a character.
|
|---|
| [7bb9b53] | 294 | *
|
|---|
| [5273eb6] | 295 | * @param s String in which to look for the character.
|
|---|
| 296 | * @param c Character to look for.
|
|---|
| 297 | * @return Pointer to the specified character on success,
|
|---|
| 298 | * NULL pointer otherwise.
|
|---|
| [7bb9b53] | 299 | */
|
|---|
| [7f9df7b9] | 300 | char *strchr(const char *s, int c)
|
|---|
| [7bb9b53] | 301 | {
|
|---|
| [ef6dd3f] | 302 | assert(s != NULL);
|
|---|
| [a35b458] | 303 |
|
|---|
| [517cedc0] | 304 | char *res = gnu_strchrnul(s, c);
|
|---|
| 305 | return (*res == c) ? res : NULL;
|
|---|
| [7bb9b53] | 306 | }
|
|---|
| 307 |
|
|---|
| 308 | /**
|
|---|
| [5273eb6] | 309 | * Scan string for a last occurence of a character.
|
|---|
| [7bb9b53] | 310 | *
|
|---|
| [5273eb6] | 311 | * @param s String in which to look for the character.
|
|---|
| 312 | * @param c Character to look for.
|
|---|
| 313 | * @return Pointer to the specified character on success,
|
|---|
| 314 | * NULL pointer otherwise.
|
|---|
| [7bb9b53] | 315 | */
|
|---|
| [7f9df7b9] | 316 | char *strrchr(const char *s, int c)
|
|---|
| [7bb9b53] | 317 | {
|
|---|
| [ef6dd3f] | 318 | assert(s != NULL);
|
|---|
| [a35b458] | 319 |
|
|---|
| [7f9df7b9] | 320 | const char *ptr = strchr(s, '\0');
|
|---|
| [a35b458] | 321 |
|
|---|
| [ef6dd3f] | 322 | /* the same as in strchr, except it loops in reverse direction */
|
|---|
| 323 | while (*ptr != (char) c) {
|
|---|
| [4f4b4e7] | 324 | if (ptr == s) {
|
|---|
| [ef6dd3f] | 325 | return NULL;
|
|---|
| [4f4b4e7] | 326 | }
|
|---|
| [ef6dd3f] | 327 |
|
|---|
| [27eddb52] | 328 | ptr--;
|
|---|
| [ef6dd3f] | 329 | }
|
|---|
| 330 |
|
|---|
| 331 | return (char *) ptr;
|
|---|
| [7bb9b53] | 332 | }
|
|---|
| 333 |
|
|---|
| [5273eb6] | 334 | /**
|
|---|
| 335 | * Scan string for a first occurence of a character.
|
|---|
| 336 | *
|
|---|
| 337 | * @param s String in which to look for the character.
|
|---|
| 338 | * @param c Character to look for.
|
|---|
| 339 | * @return Pointer to the specified character on success, pointer to the
|
|---|
| 340 | * string terminator otherwise.
|
|---|
| 341 | */
|
|---|
| [517cedc0] | 342 | char *gnu_strchrnul(const char *s, int c)
|
|---|
| 343 | {
|
|---|
| 344 | assert(s != NULL);
|
|---|
| [a35b458] | 345 |
|
|---|
| [517cedc0] | 346 | while (*s != c && *s != '\0') {
|
|---|
| 347 | s++;
|
|---|
| 348 | }
|
|---|
| [a35b458] | 349 |
|
|---|
| [517cedc0] | 350 | return (char *) s;
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| [7bb9b53] | 353 | /**
|
|---|
| [5273eb6] | 354 | * Scan a string for a first occurence of one of provided bytes.
|
|---|
| [7bb9b53] | 355 | *
|
|---|
| [5273eb6] | 356 | * @param s1 String in which to look for the bytes.
|
|---|
| 357 | * @param s2 String of bytes to look for.
|
|---|
| 358 | * @return Pointer to the found byte on success,
|
|---|
| 359 | * NULL pointer otherwise.
|
|---|
| [7bb9b53] | 360 | */
|
|---|
| [7f9df7b9] | 361 | char *strpbrk(const char *s1, const char *s2)
|
|---|
| [7bb9b53] | 362 | {
|
|---|
| [ef6dd3f] | 363 | assert(s1 != NULL);
|
|---|
| 364 | assert(s2 != NULL);
|
|---|
| 365 |
|
|---|
| 366 | char *ptr = strpbrk_null(s1, s2);
|
|---|
| 367 | return (*ptr == '\0') ? NULL : ptr;
|
|---|
| [7bb9b53] | 368 | }
|
|---|
| 369 |
|
|---|
| 370 | /**
|
|---|
| [5273eb6] | 371 | * Get the length of a complementary substring.
|
|---|
| [7bb9b53] | 372 | *
|
|---|
| [5273eb6] | 373 | * @param s1 String that shall be searched for complementary prefix.
|
|---|
| 374 | * @param s2 String of bytes that shall not occur in the prefix.
|
|---|
| 375 | * @return Length of the prefix.
|
|---|
| [7bb9b53] | 376 | */
|
|---|
| [7f9df7b9] | 377 | size_t strcspn(const char *s1, const char *s2)
|
|---|
| [7bb9b53] | 378 | {
|
|---|
| [ef6dd3f] | 379 | assert(s1 != NULL);
|
|---|
| 380 | assert(s2 != NULL);
|
|---|
| 381 |
|
|---|
| 382 | char *ptr = strpbrk_null(s1, s2);
|
|---|
| 383 | return (size_t) (ptr - s1);
|
|---|
| [7bb9b53] | 384 | }
|
|---|
| 385 |
|
|---|
| 386 | /**
|
|---|
| [5273eb6] | 387 | * Get length of a substring.
|
|---|
| [7bb9b53] | 388 | *
|
|---|
| [5273eb6] | 389 | * @param s1 String that shall be searched for prefix.
|
|---|
| 390 | * @param s2 String of bytes that the prefix must consist of.
|
|---|
| 391 | * @return Length of the prefix.
|
|---|
| [7bb9b53] | 392 | */
|
|---|
| [7f9df7b9] | 393 | size_t strspn(const char *s1, const char *s2)
|
|---|
| [7bb9b53] | 394 | {
|
|---|
| [ef6dd3f] | 395 | assert(s1 != NULL);
|
|---|
| 396 | assert(s2 != NULL);
|
|---|
| 397 |
|
|---|
| 398 | const char *ptr;
|
|---|
| [4f4b4e7] | 399 | for (ptr = s1; *ptr != '\0'; ++ptr) {
|
|---|
| [7f9df7b9] | 400 | if (!strchr(s2, *ptr)) {
|
|---|
| [ef6dd3f] | 401 | break;
|
|---|
| [4f4b4e7] | 402 | }
|
|---|
| [ef6dd3f] | 403 | }
|
|---|
| 404 | return ptr - s1;
|
|---|
| [7bb9b53] | 405 | }
|
|---|
| 406 |
|
|---|
| 407 | /**
|
|---|
| [7e10aee] | 408 | * Find a substring. Uses Knuth-Morris-Pratt algorithm.
|
|---|
| [7bb9b53] | 409 | *
|
|---|
| [5273eb6] | 410 | * @param s1 String in which to look for a substring.
|
|---|
| 411 | * @param s2 Substring to look for.
|
|---|
| 412 | * @return Pointer to the first character of the substring in s1, or NULL if
|
|---|
| 413 | * not found.
|
|---|
| [7bb9b53] | 414 | */
|
|---|
| [7f9df7b9] | 415 | char *strstr(const char *haystack, const char *needle)
|
|---|
| [7bb9b53] | 416 | {
|
|---|
| [7e10aee] | 417 | assert(haystack != NULL);
|
|---|
| 418 | assert(needle != NULL);
|
|---|
| [a35b458] | 419 |
|
|---|
| [7e10aee] | 420 | /* Special case - needle is an empty string. */
|
|---|
| 421 | if (needle[0] == '\0') {
|
|---|
| [3f33b95] | 422 | return (char *) haystack;
|
|---|
| [4f4b4e7] | 423 | }
|
|---|
| [a35b458] | 424 |
|
|---|
| [7e10aee] | 425 | /* Preprocess needle. */
|
|---|
| [7f9df7b9] | 426 | size_t nlen = strlen(needle);
|
|---|
| [7e10aee] | 427 | size_t prefix_table[nlen + 1];
|
|---|
| [a35b458] | 428 |
|
|---|
| [013e5d32] | 429 | size_t i = 0;
|
|---|
| 430 | ssize_t j = -1;
|
|---|
| [a35b458] | 431 |
|
|---|
| [013e5d32] | 432 | prefix_table[i] = j;
|
|---|
| [a35b458] | 433 |
|
|---|
| [013e5d32] | 434 | while (i < nlen) {
|
|---|
| 435 | while (j >= 0 && needle[i] != needle[j]) {
|
|---|
| 436 | j = prefix_table[j];
|
|---|
| [7e10aee] | 437 | }
|
|---|
| [1433ecda] | 438 | i++;
|
|---|
| 439 | j++;
|
|---|
| [013e5d32] | 440 | prefix_table[i] = j;
|
|---|
| [7e10aee] | 441 | }
|
|---|
| [a35b458] | 442 |
|
|---|
| [7e10aee] | 443 | /* Search needle using the precomputed table. */
|
|---|
| 444 | size_t npos = 0;
|
|---|
| [a35b458] | 445 |
|
|---|
| [3f33b95] | 446 | for (size_t hpos = 0; haystack[hpos] != '\0'; ++hpos) {
|
|---|
| [7e10aee] | 447 | while (npos != 0 && haystack[hpos] != needle[npos]) {
|
|---|
| 448 | npos = prefix_table[npos];
|
|---|
| [4f4b4e7] | 449 | }
|
|---|
| [a35b458] | 450 |
|
|---|
| [7e10aee] | 451 | if (haystack[hpos] == needle[npos]) {
|
|---|
| [3f33b95] | 452 | npos++;
|
|---|
| [a35b458] | 453 |
|
|---|
| [7e10aee] | 454 | if (npos == nlen) {
|
|---|
| [3f33b95] | 455 | return (char *) (haystack + hpos - nlen + 1);
|
|---|
| [7e10aee] | 456 | }
|
|---|
| 457 | }
|
|---|
| [ef6dd3f] | 458 | }
|
|---|
| [a35b458] | 459 |
|
|---|
| [ef6dd3f] | 460 | return NULL;
|
|---|
| [7bb9b53] | 461 | }
|
|---|
| 462 |
|
|---|
| [12b29f3] | 463 | /** Split string by delimiters.
|
|---|
| 464 | *
|
|---|
| 465 | * @param s String to be tokenized. May not be NULL.
|
|---|
| 466 | * @param delim String with the delimiters.
|
|---|
| 467 | * @return Pointer to the prefix of @a s before the first
|
|---|
| 468 | * delimiter character. NULL if no such prefix
|
|---|
| 469 | * exists.
|
|---|
| 470 | */
|
|---|
| [7f9df7b9] | 471 | char *strtok(char *s, const char *delim)
|
|---|
| [12b29f3] | 472 | {
|
|---|
| 473 | static char *next;
|
|---|
| 474 |
|
|---|
| [7f9df7b9] | 475 | return strtok_r(s, delim, &next);
|
|---|
| [12b29f3] | 476 | }
|
|---|
| 477 |
|
|---|
| 478 |
|
|---|
| 479 | /** Split string by delimiters.
|
|---|
| 480 | *
|
|---|
| 481 | * @param s String to be tokenized. May not be NULL.
|
|---|
| 482 | * @param delim String with the delimiters.
|
|---|
| 483 | * @param next Variable which will receive the pointer to the
|
|---|
| 484 | * continuation of the string following the first
|
|---|
| 485 | * occurrence of any of the delimiter characters.
|
|---|
| 486 | * May be NULL.
|
|---|
| 487 | * @return Pointer to the prefix of @a s before the first
|
|---|
| 488 | * delimiter character. NULL if no such prefix
|
|---|
| 489 | * exists.
|
|---|
| 490 | */
|
|---|
| [7f9df7b9] | 491 | char *strtok_r(char *s, const char *delim, char **next)
|
|---|
| [12b29f3] | 492 | {
|
|---|
| 493 | char *start, *end;
|
|---|
| 494 |
|
|---|
| 495 | if (s == NULL)
|
|---|
| 496 | s = *next;
|
|---|
| 497 |
|
|---|
| 498 | /* Skip over leading delimiters. */
|
|---|
| [1433ecda] | 499 | while (*s && (strchr(delim, *s) != NULL))
|
|---|
| 500 | ++s;
|
|---|
| [12b29f3] | 501 | start = s;
|
|---|
| 502 |
|
|---|
| 503 | /* Skip over token characters. */
|
|---|
| [1433ecda] | 504 | while (*s && (strchr(delim, *s) == NULL))
|
|---|
| 505 | ++s;
|
|---|
| [12b29f3] | 506 | end = s;
|
|---|
| 507 | *next = (*s ? s + 1 : s);
|
|---|
| 508 |
|
|---|
| 509 | if (start == end) {
|
|---|
| 510 | return NULL; /* No more tokens. */
|
|---|
| 511 | }
|
|---|
| 512 |
|
|---|
| 513 | /* Overwrite delimiter with NULL terminator. */
|
|---|
| 514 | *end = '\0';
|
|---|
| 515 | return start;
|
|---|
| 516 | }
|
|---|
| 517 |
|
|---|
| [7bb9b53] | 518 | /**
|
|---|
| [5273eb6] | 519 | * String comparison using collating information.
|
|---|
| 520 | *
|
|---|
| [ef6dd3f] | 521 | * Currently ignores locale and just calls strcmp.
|
|---|
| [7bb9b53] | 522 | *
|
|---|
| [5273eb6] | 523 | * @param s1 First string to be compared.
|
|---|
| 524 | * @param s2 Second string to be compared.
|
|---|
| 525 | * @return Difference of the first pair of inequal characters,
|
|---|
| 526 | * or 0 if strings have the same content.
|
|---|
| [7bb9b53] | 527 | */
|
|---|
| [7f9df7b9] | 528 | int strcoll(const char *s1, const char *s2)
|
|---|
| [7bb9b53] | 529 | {
|
|---|
| [ef6dd3f] | 530 | assert(s1 != NULL);
|
|---|
| 531 | assert(s2 != NULL);
|
|---|
| 532 |
|
|---|
| [7f9df7b9] | 533 | return strcmp(s1, s2);
|
|---|
| [7bb9b53] | 534 | }
|
|---|
| 535 |
|
|---|
| 536 | /**
|
|---|
| [5273eb6] | 537 | * Transform a string in such a way that the resulting string yields the same
|
|---|
| 538 | * results when passed to the strcmp as if the original string is passed to
|
|---|
| 539 | * the strcoll.
|
|---|
| 540 | *
|
|---|
| 541 | * Since strcoll is equal to strcmp here, this just makes a copy.
|
|---|
| [7bb9b53] | 542 | *
|
|---|
| [5273eb6] | 543 | * @param s1 Transformed string.
|
|---|
| 544 | * @param s2 Original string.
|
|---|
| 545 | * @param n Maximum length of the transformed string.
|
|---|
| 546 | * @return Length of the transformed string.
|
|---|
| [7bb9b53] | 547 | */
|
|---|
| [7f9df7b9] | 548 | size_t strxfrm(char *restrict s1, const char *restrict s2, size_t n)
|
|---|
| [7bb9b53] | 549 | {
|
|---|
| [ef6dd3f] | 550 | assert(s1 != NULL || n == 0);
|
|---|
| 551 | assert(s2 != NULL);
|
|---|
| 552 |
|
|---|
| [7f9df7b9] | 553 | size_t len = strlen(s2);
|
|---|
| [ef6dd3f] | 554 |
|
|---|
| [4f4b4e7] | 555 | if (n > len) {
|
|---|
| [7f9df7b9] | 556 | strcpy(s1, s2);
|
|---|
| [4f4b4e7] | 557 | }
|
|---|
| [ef6dd3f] | 558 |
|
|---|
| 559 | return len;
|
|---|
| [7bb9b53] | 560 | }
|
|---|
| 561 |
|
|---|
| 562 | /**
|
|---|
| [5273eb6] | 563 | * Get error message string.
|
|---|
| [7bb9b53] | 564 | *
|
|---|
| [5273eb6] | 565 | * @param errnum Error code for which to obtain human readable string.
|
|---|
| 566 | * @return Error message.
|
|---|
| [7bb9b53] | 567 | */
|
|---|
| [7f9df7b9] | 568 | char *strerror(int errnum)
|
|---|
| [7bb9b53] | 569 | {
|
|---|
| [0d0b319] | 570 | // FIXME: move strerror() and strerror_r() to libc.
|
|---|
| 571 | return (char *) str_error(errnum);
|
|---|
| [ef6dd3f] | 572 | }
|
|---|
| 573 |
|
|---|
| 574 | /**
|
|---|
| [5273eb6] | 575 | * Get error message string.
|
|---|
| [ef6dd3f] | 576 | *
|
|---|
| [5273eb6] | 577 | * @param errnum Error code for which to obtain human readable string.
|
|---|
| 578 | * @param buf Buffer to store a human readable string to.
|
|---|
| 579 | * @param bufsz Size of buffer pointed to by buf.
|
|---|
| 580 | * @return Zero on success, errno otherwise.
|
|---|
| [ef6dd3f] | 581 | */
|
|---|
| [7f9df7b9] | 582 | int strerror_r(int errnum, char *buf, size_t bufsz)
|
|---|
| [ef6dd3f] | 583 | {
|
|---|
| 584 | assert(buf != NULL);
|
|---|
| [a35b458] | 585 |
|
|---|
| [7f9df7b9] | 586 | char *errstr = strerror(errnum);
|
|---|
| [a35b458] | 587 |
|
|---|
| [7f9df7b9] | 588 | if (strlen(errstr) + 1 > bufsz) {
|
|---|
| [1b55da67] | 589 | return ERANGE;
|
|---|
| [ef6dd3f] | 590 | } else {
|
|---|
| [7f9df7b9] | 591 | strcpy(buf, errstr);
|
|---|
| [ef6dd3f] | 592 | }
|
|---|
| 593 |
|
|---|
| [0d0b319] | 594 | return EOK;
|
|---|
| [7bb9b53] | 595 | }
|
|---|
| 596 |
|
|---|
| 597 | /**
|
|---|
| [5273eb6] | 598 | * Get length of the string.
|
|---|
| [7bb9b53] | 599 | *
|
|---|
| [5273eb6] | 600 | * @param s String which length shall be determined.
|
|---|
| 601 | * @return Length of the string.
|
|---|
| [7bb9b53] | 602 | */
|
|---|
| [7f9df7b9] | 603 | size_t strlen(const char *s)
|
|---|
| [7bb9b53] | 604 | {
|
|---|
| [ef6dd3f] | 605 | assert(s != NULL);
|
|---|
| [a35b458] | 606 |
|
|---|
| [7f9df7b9] | 607 | return (size_t) (strchr(s, '\0') - s);
|
|---|
| [ef6dd3f] | 608 | }
|
|---|
| 609 |
|
|---|
| 610 | /**
|
|---|
| [5273eb6] | 611 | * Get limited length of the string.
|
|---|
| [ef6dd3f] | 612 | *
|
|---|
| [5273eb6] | 613 | * @param s String which length shall be determined.
|
|---|
| 614 | * @param n Maximum number of bytes that can be examined to determine length.
|
|---|
| 615 | * @return The lower of either string length or n limit.
|
|---|
| [ef6dd3f] | 616 | */
|
|---|
| [7f9df7b9] | 617 | size_t strnlen(const char *s, size_t n)
|
|---|
| [ef6dd3f] | 618 | {
|
|---|
| 619 | assert(s != NULL);
|
|---|
| [a35b458] | 620 |
|
|---|
| [4f4b4e7] | 621 | for (size_t sz = 0; sz < n; ++sz) {
|
|---|
| [a35b458] | 622 |
|
|---|
| [ef6dd3f] | 623 | if (s[sz] == '\0') {
|
|---|
| 624 | return sz;
|
|---|
| 625 | }
|
|---|
| 626 | }
|
|---|
| [a35b458] | 627 |
|
|---|
| [ef6dd3f] | 628 | return n;
|
|---|
| [7bb9b53] | 629 | }
|
|---|
| 630 |
|
|---|
| [d3ce33fa] | 631 | /**
|
|---|
| [5273eb6] | 632 | * Get description of a signal.
|
|---|
| [d3ce33fa] | 633 | *
|
|---|
| [5273eb6] | 634 | * @param signum Signal number.
|
|---|
| 635 | * @return Human readable signal description.
|
|---|
| [d3ce33fa] | 636 | */
|
|---|
| [7f9df7b9] | 637 | char *strsignal(int signum)
|
|---|
| [d3ce33fa] | 638 | {
|
|---|
| 639 | static const char *const sigstrings[] = {
|
|---|
| 640 | [SIGABRT] = "SIGABRT (Process abort signal)",
|
|---|
| 641 | [SIGALRM] = "SIGALRM (Alarm clock)",
|
|---|
| 642 | [SIGBUS] = "SIGBUS (Access to an undefined portion of a memory object)",
|
|---|
| 643 | [SIGCHLD] = "SIGCHLD (Child process terminated, stopped, or continued)",
|
|---|
| 644 | [SIGCONT] = "SIGCONT (Continue executing, if stopped)",
|
|---|
| 645 | [SIGFPE] = "SIGFPE (Erroneous arithmetic operation)",
|
|---|
| 646 | [SIGHUP] = "SIGHUP (Hangup)",
|
|---|
| 647 | [SIGILL] = "SIGILL (Illegal instruction)",
|
|---|
| 648 | [SIGINT] = "SIGINT (Terminal interrupt signal)",
|
|---|
| 649 | [SIGKILL] = "SIGKILL (Kill process)",
|
|---|
| 650 | [SIGPIPE] = "SIGPIPE (Write on a pipe with no one to read it)",
|
|---|
| 651 | [SIGQUIT] = "SIGQUIT (Terminal quit signal)",
|
|---|
| 652 | [SIGSEGV] = "SIGSEGV (Invalid memory reference)",
|
|---|
| 653 | [SIGSTOP] = "SIGSTOP (Stop executing)",
|
|---|
| 654 | [SIGTERM] = "SIGTERM (Termination signal)",
|
|---|
| 655 | [SIGTSTP] = "SIGTSTP (Terminal stop signal)",
|
|---|
| 656 | [SIGTTIN] = "SIGTTIN (Background process attempting read)",
|
|---|
| 657 | [SIGTTOU] = "SIGTTOU (Background process attempting write)",
|
|---|
| 658 | [SIGUSR1] = "SIGUSR1 (User-defined signal 1)",
|
|---|
| 659 | [SIGUSR2] = "SIGUSR2 (User-defined signal 2)",
|
|---|
| 660 | [SIGPOLL] = "SIGPOLL (Pollable event)",
|
|---|
| 661 | [SIGPROF] = "SIGPROF (Profiling timer expired)",
|
|---|
| 662 | [SIGSYS] = "SIGSYS (Bad system call)",
|
|---|
| 663 | [SIGTRAP] = "SIGTRAP (Trace/breakpoint trap)",
|
|---|
| 664 | [SIGURG] = "SIGURG (High bandwidth data is available at a socket)",
|
|---|
| 665 | [SIGVTALRM] = "SIGVTALRM (Virtual timer expired)",
|
|---|
| 666 | [SIGXCPU] = "SIGXCPU (CPU time limit exceeded)",
|
|---|
| 667 | [SIGXFSZ] = "SIGXFSZ (File size limit exceeded)"
|
|---|
| 668 | };
|
|---|
| 669 |
|
|---|
| 670 | if (signum <= _TOP_SIGNAL) {
|
|---|
| 671 | return (char *) sigstrings[signum];
|
|---|
| 672 | }
|
|---|
| 673 |
|
|---|
| 674 | return (char *) "ERROR, Invalid signal number";
|
|---|
| 675 | }
|
|---|
| 676 |
|
|---|
| [7bb9b53] | 677 | /** @}
|
|---|
| 678 | */
|
|---|