[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 |
|
---|
[ef6dd3f] | 136 | /* the standard requires that nul characters
|
---|
| 137 | * are appended to the length of n, in case src is shorter
|
---|
| 138 | */
|
---|
| 139 | if (src[i] == '\0') {
|
---|
| 140 | char *result = &dest[i];
|
---|
[4f4b4e7] | 141 | for (++i; i < n; ++i) {
|
---|
[ef6dd3f] | 142 | dest[i] = '\0';
|
---|
| 143 | }
|
---|
| 144 | return result;
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
[a35b458] | 147 |
|
---|
[ef6dd3f] | 148 | return &dest[n];
|
---|
[7bb9b53] | 149 | }
|
---|
| 150 |
|
---|
| 151 | /**
|
---|
[5273eb6] | 152 | * Concatenate two strings.
|
---|
[ef6dd3f] | 153 | *
|
---|
[5273eb6] | 154 | * @param dest String to which src shall be appended.
|
---|
| 155 | * @param src String to be appended after dest.
|
---|
| 156 | * @return Pointer to destination buffer.
|
---|
[7bb9b53] | 157 | */
|
---|
[7f9df7b9] | 158 | char *strcat(char *restrict dest, const char *restrict src)
|
---|
[7bb9b53] | 159 | {
|
---|
[ef6dd3f] | 160 | assert(dest != NULL);
|
---|
| 161 | assert(src != NULL);
|
---|
| 162 |
|
---|
[7f9df7b9] | 163 | strcpy(strchr(dest, '\0'), src);
|
---|
[ef6dd3f] | 164 | return dest;
|
---|
[7bb9b53] | 165 | }
|
---|
| 166 |
|
---|
| 167 | /**
|
---|
[5273eb6] | 168 | * Concatenate a string with part of another.
|
---|
[ef6dd3f] | 169 | *
|
---|
[5273eb6] | 170 | * @param dest String to which part of src shall be appended.
|
---|
| 171 | * @param src String whose part shall be appended after dest.
|
---|
| 172 | * @param n Number of bytes to append after dest.
|
---|
| 173 | * @return Pointer to destination buffer.
|
---|
[7bb9b53] | 174 | */
|
---|
[7f9df7b9] | 175 | char *strncat(char *restrict dest, const char *restrict src, size_t n)
|
---|
[7bb9b53] | 176 | {
|
---|
[ef6dd3f] | 177 | assert(dest != NULL);
|
---|
| 178 | assert(src != NULL);
|
---|
| 179 |
|
---|
[7f9df7b9] | 180 | char *zeroptr = strncpy(strchr(dest, '\0'), src, n);
|
---|
[ef6dd3f] | 181 | /* strncpy doesn't append the nul terminator, so we do it here */
|
---|
| 182 | zeroptr[n] = '\0';
|
---|
| 183 | return dest;
|
---|
[7bb9b53] | 184 | }
|
---|
| 185 |
|
---|
| 186 | /**
|
---|
[5273eb6] | 187 | * Copy limited number of bytes in memory.
|
---|
[7bb9b53] | 188 | *
|
---|
[5273eb6] | 189 | * @param dest Destination buffer.
|
---|
| 190 | * @param src Source buffer.
|
---|
| 191 | * @param c Character after which the copying shall stop.
|
---|
| 192 | * @param n Number of bytes that shall be copied if not stopped earlier by c.
|
---|
[ef6dd3f] | 193 | * @return Pointer to the first byte after c in dest if found, NULL otherwise.
|
---|
[7bb9b53] | 194 | */
|
---|
[7f9df7b9] | 195 | void *memccpy(void *restrict dest, const void *restrict src, int c, size_t n)
|
---|
[7bb9b53] | 196 | {
|
---|
[ef6dd3f] | 197 | assert(dest != NULL);
|
---|
| 198 | assert(src != NULL);
|
---|
[a35b458] | 199 |
|
---|
[1433ecda] | 200 | unsigned char *bdest = dest;
|
---|
| 201 | const unsigned char *bsrc = src;
|
---|
[a35b458] | 202 |
|
---|
[4f4b4e7] | 203 | for (size_t i = 0; i < n; ++i) {
|
---|
[ef6dd3f] | 204 | bdest[i] = bsrc[i];
|
---|
[a35b458] | 205 |
|
---|
[ef6dd3f] | 206 | if (bsrc[i] == (unsigned char) c) {
|
---|
| 207 | /* pointer to the next byte */
|
---|
| 208 | return &bdest[i + 1];
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
[a35b458] | 211 |
|
---|
[ef6dd3f] | 212 | return NULL;
|
---|
[7bb9b53] | 213 | }
|
---|
| 214 |
|
---|
| 215 | /**
|
---|
[5273eb6] | 216 | * Duplicate a string.
|
---|
[7bb9b53] | 217 | *
|
---|
[5273eb6] | 218 | * @param s String to be duplicated.
|
---|
| 219 | * @return Newly allocated copy of the string.
|
---|
[7bb9b53] | 220 | */
|
---|
[7f9df7b9] | 221 | char *strdup(const char *s)
|
---|
[7bb9b53] | 222 | {
|
---|
[7f9df7b9] | 223 | return strndup(s, SIZE_MAX);
|
---|
[7bb9b53] | 224 | }
|
---|
| 225 |
|
---|
| 226 | /**
|
---|
[5273eb6] | 227 | * Duplicate a specific number of bytes from a string.
|
---|
[7bb9b53] | 228 | *
|
---|
[5273eb6] | 229 | * @param s String to be duplicated.
|
---|
| 230 | * @param n Maximum length of the resulting string..
|
---|
| 231 | * @return Newly allocated string copy of length at most n.
|
---|
[7bb9b53] | 232 | */
|
---|
[7f9df7b9] | 233 | char *strndup(const char *s, size_t n)
|
---|
[7bb9b53] | 234 | {
|
---|
[ef6dd3f] | 235 | assert(s != NULL);
|
---|
| 236 |
|
---|
[7f9df7b9] | 237 | size_t len = strnlen(s, n);
|
---|
[ef6dd3f] | 238 | char *dup = malloc(len + 1);
|
---|
| 239 | if (dup == NULL) {
|
---|
| 240 | return NULL;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | memcpy(dup, s, len);
|
---|
| 244 | dup[len] = '\0';
|
---|
| 245 |
|
---|
| 246 | return dup;
|
---|
[7bb9b53] | 247 | }
|
---|
| 248 |
|
---|
| 249 | /**
|
---|
[5273eb6] | 250 | * Compare two strings.
|
---|
[ef6dd3f] | 251 | *
|
---|
[5273eb6] | 252 | * @param s1 First string to be compared.
|
---|
| 253 | * @param s2 Second string to be compared.
|
---|
| 254 | * @return Difference of the first pair of inequal characters,
|
---|
| 255 | * or 0 if strings have the same content.
|
---|
[7bb9b53] | 256 | */
|
---|
[7f9df7b9] | 257 | int strcmp(const char *s1, const char *s2)
|
---|
[7bb9b53] | 258 | {
|
---|
[ef6dd3f] | 259 | assert(s1 != NULL);
|
---|
| 260 | assert(s2 != NULL);
|
---|
| 261 |
|
---|
[7f9df7b9] | 262 | return strncmp(s1, s2, STR_NO_LIMIT);
|
---|
[7bb9b53] | 263 | }
|
---|
| 264 |
|
---|
| 265 | /**
|
---|
[5273eb6] | 266 | * Compare part of two strings.
|
---|
[7bb9b53] | 267 | *
|
---|
[5273eb6] | 268 | * @param s1 First string to be compared.
|
---|
| 269 | * @param s2 Second string to be compared.
|
---|
| 270 | * @param n Maximum number of characters to be compared.
|
---|
| 271 | * @return Difference of the first pair of inequal characters,
|
---|
| 272 | * or 0 if strings have the same content.
|
---|
[7bb9b53] | 273 | */
|
---|
[7f9df7b9] | 274 | int strncmp(const char *s1, const char *s2, size_t n)
|
---|
[7bb9b53] | 275 | {
|
---|
[ef6dd3f] | 276 | assert(s1 != NULL);
|
---|
| 277 | assert(s2 != NULL);
|
---|
| 278 |
|
---|
[4f4b4e7] | 279 | for (size_t i = 0; i < n; ++i) {
|
---|
[ef6dd3f] | 280 | if (s1[i] != s2[i]) {
|
---|
[087c4c56] | 281 | return s1[i] - s2[i];
|
---|
[ef6dd3f] | 282 | }
|
---|
| 283 | if (s1[i] == '\0') {
|
---|
| 284 | break;
|
---|
| 285 | }
|
---|
| 286 | }
|
---|
| 287 |
|
---|
[7bb9b53] | 288 | return 0;
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | /**
|
---|
[5273eb6] | 292 | * Find byte in memory.
|
---|
[ef6dd3f] | 293 | *
|
---|
[5273eb6] | 294 | * @param mem Memory area in which to look for the byte.
|
---|
| 295 | * @param c Byte to look for.
|
---|
| 296 | * @param n Maximum number of bytes to be inspected.
|
---|
| 297 | * @return Pointer to the specified byte on success,
|
---|
| 298 | * NULL pointer otherwise.
|
---|
[7bb9b53] | 299 | */
|
---|
[7f9df7b9] | 300 | void *memchr(const void *mem, int c, size_t n)
|
---|
[7bb9b53] | 301 | {
|
---|
[ef6dd3f] | 302 | assert(mem != NULL);
|
---|
[a35b458] | 303 |
|
---|
[ef6dd3f] | 304 | const unsigned char *s = mem;
|
---|
[a35b458] | 305 |
|
---|
[4f4b4e7] | 306 | for (size_t i = 0; i < n; ++i) {
|
---|
[ef6dd3f] | 307 | if (s[i] == (unsigned char) c) {
|
---|
| 308 | return (void *) &s[i];
|
---|
| 309 | }
|
---|
| 310 | }
|
---|
| 311 | return NULL;
|
---|
[7bb9b53] | 312 | }
|
---|
| 313 |
|
---|
| 314 | /**
|
---|
[5273eb6] | 315 | * Scan string for a first occurence of a character.
|
---|
[7bb9b53] | 316 | *
|
---|
[5273eb6] | 317 | * @param s String in which to look for the character.
|
---|
| 318 | * @param c Character to look for.
|
---|
| 319 | * @return Pointer to the specified character on success,
|
---|
| 320 | * NULL pointer otherwise.
|
---|
[7bb9b53] | 321 | */
|
---|
[7f9df7b9] | 322 | char *strchr(const char *s, int c)
|
---|
[7bb9b53] | 323 | {
|
---|
[ef6dd3f] | 324 | assert(s != NULL);
|
---|
[a35b458] | 325 |
|
---|
[517cedc0] | 326 | char *res = gnu_strchrnul(s, c);
|
---|
| 327 | return (*res == c) ? res : NULL;
|
---|
[7bb9b53] | 328 | }
|
---|
| 329 |
|
---|
| 330 | /**
|
---|
[5273eb6] | 331 | * Scan string for a last occurence of a character.
|
---|
[7bb9b53] | 332 | *
|
---|
[5273eb6] | 333 | * @param s String in which to look for the character.
|
---|
| 334 | * @param c Character to look for.
|
---|
| 335 | * @return Pointer to the specified character on success,
|
---|
| 336 | * NULL pointer otherwise.
|
---|
[7bb9b53] | 337 | */
|
---|
[7f9df7b9] | 338 | char *strrchr(const char *s, int c)
|
---|
[7bb9b53] | 339 | {
|
---|
[ef6dd3f] | 340 | assert(s != NULL);
|
---|
[a35b458] | 341 |
|
---|
[7f9df7b9] | 342 | const char *ptr = strchr(s, '\0');
|
---|
[a35b458] | 343 |
|
---|
[ef6dd3f] | 344 | /* the same as in strchr, except it loops in reverse direction */
|
---|
| 345 | while (*ptr != (char) c) {
|
---|
[4f4b4e7] | 346 | if (ptr == s) {
|
---|
[ef6dd3f] | 347 | return NULL;
|
---|
[4f4b4e7] | 348 | }
|
---|
[ef6dd3f] | 349 |
|
---|
[27eddb52] | 350 | ptr--;
|
---|
[ef6dd3f] | 351 | }
|
---|
| 352 |
|
---|
| 353 | return (char *) ptr;
|
---|
[7bb9b53] | 354 | }
|
---|
| 355 |
|
---|
[5273eb6] | 356 | /**
|
---|
| 357 | * Scan string for a first occurence of a character.
|
---|
| 358 | *
|
---|
| 359 | * @param s String in which to look for the character.
|
---|
| 360 | * @param c Character to look for.
|
---|
| 361 | * @return Pointer to the specified character on success, pointer to the
|
---|
| 362 | * string terminator otherwise.
|
---|
| 363 | */
|
---|
[517cedc0] | 364 | char *gnu_strchrnul(const char *s, int c)
|
---|
| 365 | {
|
---|
| 366 | assert(s != NULL);
|
---|
[a35b458] | 367 |
|
---|
[517cedc0] | 368 | while (*s != c && *s != '\0') {
|
---|
| 369 | s++;
|
---|
| 370 | }
|
---|
[a35b458] | 371 |
|
---|
[517cedc0] | 372 | return (char *) s;
|
---|
| 373 | }
|
---|
| 374 |
|
---|
[7bb9b53] | 375 | /**
|
---|
[5273eb6] | 376 | * Scan a string for a first occurence of one of provided bytes.
|
---|
[7bb9b53] | 377 | *
|
---|
[5273eb6] | 378 | * @param s1 String in which to look for the bytes.
|
---|
| 379 | * @param s2 String of bytes to look for.
|
---|
| 380 | * @return Pointer to the found byte on success,
|
---|
| 381 | * NULL pointer otherwise.
|
---|
[7bb9b53] | 382 | */
|
---|
[7f9df7b9] | 383 | char *strpbrk(const char *s1, const char *s2)
|
---|
[7bb9b53] | 384 | {
|
---|
[ef6dd3f] | 385 | assert(s1 != NULL);
|
---|
| 386 | assert(s2 != NULL);
|
---|
| 387 |
|
---|
| 388 | char *ptr = strpbrk_null(s1, s2);
|
---|
| 389 | return (*ptr == '\0') ? NULL : ptr;
|
---|
[7bb9b53] | 390 | }
|
---|
| 391 |
|
---|
| 392 | /**
|
---|
[5273eb6] | 393 | * Get the length of a complementary substring.
|
---|
[7bb9b53] | 394 | *
|
---|
[5273eb6] | 395 | * @param s1 String that shall be searched for complementary prefix.
|
---|
| 396 | * @param s2 String of bytes that shall not occur in the prefix.
|
---|
| 397 | * @return Length of the prefix.
|
---|
[7bb9b53] | 398 | */
|
---|
[7f9df7b9] | 399 | size_t strcspn(const char *s1, const char *s2)
|
---|
[7bb9b53] | 400 | {
|
---|
[ef6dd3f] | 401 | assert(s1 != NULL);
|
---|
| 402 | assert(s2 != NULL);
|
---|
| 403 |
|
---|
| 404 | char *ptr = strpbrk_null(s1, s2);
|
---|
| 405 | return (size_t) (ptr - s1);
|
---|
[7bb9b53] | 406 | }
|
---|
| 407 |
|
---|
| 408 | /**
|
---|
[5273eb6] | 409 | * Get length of a substring.
|
---|
[7bb9b53] | 410 | *
|
---|
[5273eb6] | 411 | * @param s1 String that shall be searched for prefix.
|
---|
| 412 | * @param s2 String of bytes that the prefix must consist of.
|
---|
| 413 | * @return Length of the prefix.
|
---|
[7bb9b53] | 414 | */
|
---|
[7f9df7b9] | 415 | size_t strspn(const char *s1, const char *s2)
|
---|
[7bb9b53] | 416 | {
|
---|
[ef6dd3f] | 417 | assert(s1 != NULL);
|
---|
| 418 | assert(s2 != NULL);
|
---|
| 419 |
|
---|
| 420 | const char *ptr;
|
---|
[4f4b4e7] | 421 | for (ptr = s1; *ptr != '\0'; ++ptr) {
|
---|
[7f9df7b9] | 422 | if (!strchr(s2, *ptr)) {
|
---|
[ef6dd3f] | 423 | break;
|
---|
[4f4b4e7] | 424 | }
|
---|
[ef6dd3f] | 425 | }
|
---|
| 426 | return ptr - s1;
|
---|
[7bb9b53] | 427 | }
|
---|
| 428 |
|
---|
| 429 | /**
|
---|
[7e10aee] | 430 | * Find a substring. Uses Knuth-Morris-Pratt algorithm.
|
---|
[7bb9b53] | 431 | *
|
---|
[5273eb6] | 432 | * @param s1 String in which to look for a substring.
|
---|
| 433 | * @param s2 Substring to look for.
|
---|
| 434 | * @return Pointer to the first character of the substring in s1, or NULL if
|
---|
| 435 | * not found.
|
---|
[7bb9b53] | 436 | */
|
---|
[7f9df7b9] | 437 | char *strstr(const char *haystack, const char *needle)
|
---|
[7bb9b53] | 438 | {
|
---|
[7e10aee] | 439 | assert(haystack != NULL);
|
---|
| 440 | assert(needle != NULL);
|
---|
[a35b458] | 441 |
|
---|
[7e10aee] | 442 | /* Special case - needle is an empty string. */
|
---|
| 443 | if (needle[0] == '\0') {
|
---|
[3f33b95] | 444 | return (char *) haystack;
|
---|
[4f4b4e7] | 445 | }
|
---|
[a35b458] | 446 |
|
---|
[7e10aee] | 447 | /* Preprocess needle. */
|
---|
[7f9df7b9] | 448 | size_t nlen = strlen(needle);
|
---|
[7e10aee] | 449 | size_t prefix_table[nlen + 1];
|
---|
[a35b458] | 450 |
|
---|
[013e5d32] | 451 | size_t i = 0;
|
---|
| 452 | ssize_t j = -1;
|
---|
[a35b458] | 453 |
|
---|
[013e5d32] | 454 | prefix_table[i] = j;
|
---|
[a35b458] | 455 |
|
---|
[013e5d32] | 456 | while (i < nlen) {
|
---|
| 457 | while (j >= 0 && needle[i] != needle[j]) {
|
---|
| 458 | j = prefix_table[j];
|
---|
[7e10aee] | 459 | }
|
---|
[1433ecda] | 460 | i++;
|
---|
| 461 | j++;
|
---|
[013e5d32] | 462 | prefix_table[i] = j;
|
---|
[7e10aee] | 463 | }
|
---|
[a35b458] | 464 |
|
---|
[7e10aee] | 465 | /* Search needle using the precomputed table. */
|
---|
| 466 | size_t npos = 0;
|
---|
[a35b458] | 467 |
|
---|
[3f33b95] | 468 | for (size_t hpos = 0; haystack[hpos] != '\0'; ++hpos) {
|
---|
[7e10aee] | 469 | while (npos != 0 && haystack[hpos] != needle[npos]) {
|
---|
| 470 | npos = prefix_table[npos];
|
---|
[4f4b4e7] | 471 | }
|
---|
[a35b458] | 472 |
|
---|
[7e10aee] | 473 | if (haystack[hpos] == needle[npos]) {
|
---|
[3f33b95] | 474 | npos++;
|
---|
[a35b458] | 475 |
|
---|
[7e10aee] | 476 | if (npos == nlen) {
|
---|
[3f33b95] | 477 | return (char *) (haystack + hpos - nlen + 1);
|
---|
[7e10aee] | 478 | }
|
---|
| 479 | }
|
---|
[ef6dd3f] | 480 | }
|
---|
[a35b458] | 481 |
|
---|
[ef6dd3f] | 482 | return NULL;
|
---|
[7bb9b53] | 483 | }
|
---|
| 484 |
|
---|
[12b29f3] | 485 | /** Split string by delimiters.
|
---|
| 486 | *
|
---|
| 487 | * @param s String to be tokenized. May not be NULL.
|
---|
| 488 | * @param delim String with the delimiters.
|
---|
| 489 | * @return Pointer to the prefix of @a s before the first
|
---|
| 490 | * delimiter character. NULL if no such prefix
|
---|
| 491 | * exists.
|
---|
| 492 | */
|
---|
[7f9df7b9] | 493 | char *strtok(char *s, const char *delim)
|
---|
[12b29f3] | 494 | {
|
---|
| 495 | static char *next;
|
---|
| 496 |
|
---|
[7f9df7b9] | 497 | return strtok_r(s, delim, &next);
|
---|
[12b29f3] | 498 | }
|
---|
| 499 |
|
---|
| 500 |
|
---|
| 501 | /** Split string by delimiters.
|
---|
| 502 | *
|
---|
| 503 | * @param s String to be tokenized. May not be NULL.
|
---|
| 504 | * @param delim String with the delimiters.
|
---|
| 505 | * @param next Variable which will receive the pointer to the
|
---|
| 506 | * continuation of the string following the first
|
---|
| 507 | * occurrence of any of the delimiter characters.
|
---|
| 508 | * May be NULL.
|
---|
| 509 | * @return Pointer to the prefix of @a s before the first
|
---|
| 510 | * delimiter character. NULL if no such prefix
|
---|
| 511 | * exists.
|
---|
| 512 | */
|
---|
[7f9df7b9] | 513 | char *strtok_r(char *s, const char *delim, char **next)
|
---|
[12b29f3] | 514 | {
|
---|
| 515 | char *start, *end;
|
---|
| 516 |
|
---|
| 517 | if (s == NULL)
|
---|
| 518 | s = *next;
|
---|
| 519 |
|
---|
| 520 | /* Skip over leading delimiters. */
|
---|
[1433ecda] | 521 | while (*s && (strchr(delim, *s) != NULL))
|
---|
| 522 | ++s;
|
---|
[12b29f3] | 523 | start = s;
|
---|
| 524 |
|
---|
| 525 | /* Skip over token characters. */
|
---|
[1433ecda] | 526 | while (*s && (strchr(delim, *s) == NULL))
|
---|
| 527 | ++s;
|
---|
[12b29f3] | 528 | end = s;
|
---|
| 529 | *next = (*s ? s + 1 : s);
|
---|
| 530 |
|
---|
| 531 | if (start == end) {
|
---|
| 532 | return NULL; /* No more tokens. */
|
---|
| 533 | }
|
---|
| 534 |
|
---|
| 535 | /* Overwrite delimiter with NULL terminator. */
|
---|
| 536 | *end = '\0';
|
---|
| 537 | return start;
|
---|
| 538 | }
|
---|
| 539 |
|
---|
[7bb9b53] | 540 | /**
|
---|
[5273eb6] | 541 | * String comparison using collating information.
|
---|
| 542 | *
|
---|
[ef6dd3f] | 543 | * Currently ignores locale and just calls strcmp.
|
---|
[7bb9b53] | 544 | *
|
---|
[5273eb6] | 545 | * @param s1 First string to be compared.
|
---|
| 546 | * @param s2 Second string to be compared.
|
---|
| 547 | * @return Difference of the first pair of inequal characters,
|
---|
| 548 | * or 0 if strings have the same content.
|
---|
[7bb9b53] | 549 | */
|
---|
[7f9df7b9] | 550 | int strcoll(const char *s1, const char *s2)
|
---|
[7bb9b53] | 551 | {
|
---|
[ef6dd3f] | 552 | assert(s1 != NULL);
|
---|
| 553 | assert(s2 != NULL);
|
---|
| 554 |
|
---|
[7f9df7b9] | 555 | return strcmp(s1, s2);
|
---|
[7bb9b53] | 556 | }
|
---|
| 557 |
|
---|
| 558 | /**
|
---|
[5273eb6] | 559 | * Transform a string in such a way that the resulting string yields the same
|
---|
| 560 | * results when passed to the strcmp as if the original string is passed to
|
---|
| 561 | * the strcoll.
|
---|
| 562 | *
|
---|
| 563 | * Since strcoll is equal to strcmp here, this just makes a copy.
|
---|
[7bb9b53] | 564 | *
|
---|
[5273eb6] | 565 | * @param s1 Transformed string.
|
---|
| 566 | * @param s2 Original string.
|
---|
| 567 | * @param n Maximum length of the transformed string.
|
---|
| 568 | * @return Length of the transformed string.
|
---|
[7bb9b53] | 569 | */
|
---|
[7f9df7b9] | 570 | size_t strxfrm(char *restrict s1, const char *restrict s2, size_t n)
|
---|
[7bb9b53] | 571 | {
|
---|
[ef6dd3f] | 572 | assert(s1 != NULL || n == 0);
|
---|
| 573 | assert(s2 != NULL);
|
---|
| 574 |
|
---|
[7f9df7b9] | 575 | size_t len = strlen(s2);
|
---|
[ef6dd3f] | 576 |
|
---|
[4f4b4e7] | 577 | if (n > len) {
|
---|
[7f9df7b9] | 578 | strcpy(s1, s2);
|
---|
[4f4b4e7] | 579 | }
|
---|
[ef6dd3f] | 580 |
|
---|
| 581 | return len;
|
---|
[7bb9b53] | 582 | }
|
---|
| 583 |
|
---|
| 584 | /**
|
---|
[5273eb6] | 585 | * Get error message string.
|
---|
[7bb9b53] | 586 | *
|
---|
[5273eb6] | 587 | * @param errnum Error code for which to obtain human readable string.
|
---|
| 588 | * @return Error message.
|
---|
[7bb9b53] | 589 | */
|
---|
[7f9df7b9] | 590 | char *strerror(int errnum)
|
---|
[7bb9b53] | 591 | {
|
---|
[0d0b319] | 592 | // FIXME: move strerror() and strerror_r() to libc.
|
---|
| 593 | return (char *) str_error(errnum);
|
---|
[ef6dd3f] | 594 | }
|
---|
| 595 |
|
---|
| 596 | /**
|
---|
[5273eb6] | 597 | * Get error message string.
|
---|
[ef6dd3f] | 598 | *
|
---|
[5273eb6] | 599 | * @param errnum Error code for which to obtain human readable string.
|
---|
| 600 | * @param buf Buffer to store a human readable string to.
|
---|
| 601 | * @param bufsz Size of buffer pointed to by buf.
|
---|
| 602 | * @return Zero on success, errno otherwise.
|
---|
[ef6dd3f] | 603 | */
|
---|
[7f9df7b9] | 604 | int strerror_r(int errnum, char *buf, size_t bufsz)
|
---|
[ef6dd3f] | 605 | {
|
---|
| 606 | assert(buf != NULL);
|
---|
[a35b458] | 607 |
|
---|
[7f9df7b9] | 608 | char *errstr = strerror(errnum);
|
---|
[a35b458] | 609 |
|
---|
[7f9df7b9] | 610 | if (strlen(errstr) + 1 > bufsz) {
|
---|
[1b55da67] | 611 | return ERANGE;
|
---|
[ef6dd3f] | 612 | } else {
|
---|
[7f9df7b9] | 613 | strcpy(buf, errstr);
|
---|
[ef6dd3f] | 614 | }
|
---|
| 615 |
|
---|
[0d0b319] | 616 | return EOK;
|
---|
[7bb9b53] | 617 | }
|
---|
| 618 |
|
---|
| 619 | /**
|
---|
[5273eb6] | 620 | * Get length of the string.
|
---|
[7bb9b53] | 621 | *
|
---|
[5273eb6] | 622 | * @param s String which length shall be determined.
|
---|
| 623 | * @return Length of the string.
|
---|
[7bb9b53] | 624 | */
|
---|
[7f9df7b9] | 625 | size_t strlen(const char *s)
|
---|
[7bb9b53] | 626 | {
|
---|
[ef6dd3f] | 627 | assert(s != NULL);
|
---|
[a35b458] | 628 |
|
---|
[7f9df7b9] | 629 | return (size_t) (strchr(s, '\0') - s);
|
---|
[ef6dd3f] | 630 | }
|
---|
| 631 |
|
---|
| 632 | /**
|
---|
[5273eb6] | 633 | * Get limited length of the string.
|
---|
[ef6dd3f] | 634 | *
|
---|
[5273eb6] | 635 | * @param s String which length shall be determined.
|
---|
| 636 | * @param n Maximum number of bytes that can be examined to determine length.
|
---|
| 637 | * @return The lower of either string length or n limit.
|
---|
[ef6dd3f] | 638 | */
|
---|
[7f9df7b9] | 639 | size_t strnlen(const char *s, size_t n)
|
---|
[ef6dd3f] | 640 | {
|
---|
| 641 | assert(s != NULL);
|
---|
[a35b458] | 642 |
|
---|
[4f4b4e7] | 643 | for (size_t sz = 0; sz < n; ++sz) {
|
---|
[a35b458] | 644 |
|
---|
[ef6dd3f] | 645 | if (s[sz] == '\0') {
|
---|
| 646 | return sz;
|
---|
| 647 | }
|
---|
| 648 | }
|
---|
[a35b458] | 649 |
|
---|
[ef6dd3f] | 650 | return n;
|
---|
[7bb9b53] | 651 | }
|
---|
| 652 |
|
---|
[d3ce33fa] | 653 | /**
|
---|
[5273eb6] | 654 | * Get description of a signal.
|
---|
[d3ce33fa] | 655 | *
|
---|
[5273eb6] | 656 | * @param signum Signal number.
|
---|
| 657 | * @return Human readable signal description.
|
---|
[d3ce33fa] | 658 | */
|
---|
[7f9df7b9] | 659 | char *strsignal(int signum)
|
---|
[d3ce33fa] | 660 | {
|
---|
| 661 | static const char *const sigstrings[] = {
|
---|
| 662 | [SIGABRT] = "SIGABRT (Process abort signal)",
|
---|
| 663 | [SIGALRM] = "SIGALRM (Alarm clock)",
|
---|
| 664 | [SIGBUS] = "SIGBUS (Access to an undefined portion of a memory object)",
|
---|
| 665 | [SIGCHLD] = "SIGCHLD (Child process terminated, stopped, or continued)",
|
---|
| 666 | [SIGCONT] = "SIGCONT (Continue executing, if stopped)",
|
---|
| 667 | [SIGFPE] = "SIGFPE (Erroneous arithmetic operation)",
|
---|
| 668 | [SIGHUP] = "SIGHUP (Hangup)",
|
---|
| 669 | [SIGILL] = "SIGILL (Illegal instruction)",
|
---|
| 670 | [SIGINT] = "SIGINT (Terminal interrupt signal)",
|
---|
| 671 | [SIGKILL] = "SIGKILL (Kill process)",
|
---|
| 672 | [SIGPIPE] = "SIGPIPE (Write on a pipe with no one to read it)",
|
---|
| 673 | [SIGQUIT] = "SIGQUIT (Terminal quit signal)",
|
---|
| 674 | [SIGSEGV] = "SIGSEGV (Invalid memory reference)",
|
---|
| 675 | [SIGSTOP] = "SIGSTOP (Stop executing)",
|
---|
| 676 | [SIGTERM] = "SIGTERM (Termination signal)",
|
---|
| 677 | [SIGTSTP] = "SIGTSTP (Terminal stop signal)",
|
---|
| 678 | [SIGTTIN] = "SIGTTIN (Background process attempting read)",
|
---|
| 679 | [SIGTTOU] = "SIGTTOU (Background process attempting write)",
|
---|
| 680 | [SIGUSR1] = "SIGUSR1 (User-defined signal 1)",
|
---|
| 681 | [SIGUSR2] = "SIGUSR2 (User-defined signal 2)",
|
---|
| 682 | [SIGPOLL] = "SIGPOLL (Pollable event)",
|
---|
| 683 | [SIGPROF] = "SIGPROF (Profiling timer expired)",
|
---|
| 684 | [SIGSYS] = "SIGSYS (Bad system call)",
|
---|
| 685 | [SIGTRAP] = "SIGTRAP (Trace/breakpoint trap)",
|
---|
| 686 | [SIGURG] = "SIGURG (High bandwidth data is available at a socket)",
|
---|
| 687 | [SIGVTALRM] = "SIGVTALRM (Virtual timer expired)",
|
---|
| 688 | [SIGXCPU] = "SIGXCPU (CPU time limit exceeded)",
|
---|
| 689 | [SIGXFSZ] = "SIGXFSZ (File size limit exceeded)"
|
---|
| 690 | };
|
---|
| 691 |
|
---|
| 692 | if (signum <= _TOP_SIGNAL) {
|
---|
| 693 | return (char *) sigstrings[signum];
|
---|
| 694 | }
|
---|
| 695 |
|
---|
| 696 | return (char *) "ERROR, Invalid signal number";
|
---|
| 697 | }
|
---|
| 698 |
|
---|
[7bb9b53] | 699 | /** @}
|
---|
| 700 | */
|
---|