[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 | * Find byte in memory.
|
---|
[ef6dd3f] | 294 | *
|
---|
[5273eb6] | 295 | * @param mem Memory area in which to look for the byte.
|
---|
| 296 | * @param c Byte to look for.
|
---|
| 297 | * @param n Maximum number of bytes to be inspected.
|
---|
| 298 | * @return Pointer to the specified byte on success,
|
---|
| 299 | * NULL pointer otherwise.
|
---|
[7bb9b53] | 300 | */
|
---|
[7f9df7b9] | 301 | void *memchr(const void *mem, int c, size_t n)
|
---|
[7bb9b53] | 302 | {
|
---|
[ef6dd3f] | 303 | assert(mem != NULL);
|
---|
[a35b458] | 304 |
|
---|
[ef6dd3f] | 305 | const unsigned char *s = mem;
|
---|
[a35b458] | 306 |
|
---|
[4f4b4e7] | 307 | for (size_t i = 0; i < n; ++i) {
|
---|
[ef6dd3f] | 308 | if (s[i] == (unsigned char) c) {
|
---|
| 309 | return (void *) &s[i];
|
---|
| 310 | }
|
---|
| 311 | }
|
---|
| 312 | return NULL;
|
---|
[7bb9b53] | 313 | }
|
---|
| 314 |
|
---|
| 315 | /**
|
---|
[5273eb6] | 316 | * Scan string for a first occurence of a character.
|
---|
[7bb9b53] | 317 | *
|
---|
[5273eb6] | 318 | * @param s String in which to look for the character.
|
---|
| 319 | * @param c Character to look for.
|
---|
| 320 | * @return Pointer to the specified character on success,
|
---|
| 321 | * NULL pointer otherwise.
|
---|
[7bb9b53] | 322 | */
|
---|
[7f9df7b9] | 323 | char *strchr(const char *s, int c)
|
---|
[7bb9b53] | 324 | {
|
---|
[ef6dd3f] | 325 | assert(s != NULL);
|
---|
[a35b458] | 326 |
|
---|
[517cedc0] | 327 | char *res = gnu_strchrnul(s, c);
|
---|
| 328 | return (*res == c) ? res : NULL;
|
---|
[7bb9b53] | 329 | }
|
---|
| 330 |
|
---|
| 331 | /**
|
---|
[5273eb6] | 332 | * Scan string for a last occurence of a character.
|
---|
[7bb9b53] | 333 | *
|
---|
[5273eb6] | 334 | * @param s String in which to look for the character.
|
---|
| 335 | * @param c Character to look for.
|
---|
| 336 | * @return Pointer to the specified character on success,
|
---|
| 337 | * NULL pointer otherwise.
|
---|
[7bb9b53] | 338 | */
|
---|
[7f9df7b9] | 339 | char *strrchr(const char *s, int c)
|
---|
[7bb9b53] | 340 | {
|
---|
[ef6dd3f] | 341 | assert(s != NULL);
|
---|
[a35b458] | 342 |
|
---|
[7f9df7b9] | 343 | const char *ptr = strchr(s, '\0');
|
---|
[a35b458] | 344 |
|
---|
[ef6dd3f] | 345 | /* the same as in strchr, except it loops in reverse direction */
|
---|
| 346 | while (*ptr != (char) c) {
|
---|
[4f4b4e7] | 347 | if (ptr == s) {
|
---|
[ef6dd3f] | 348 | return NULL;
|
---|
[4f4b4e7] | 349 | }
|
---|
[ef6dd3f] | 350 |
|
---|
[27eddb52] | 351 | ptr--;
|
---|
[ef6dd3f] | 352 | }
|
---|
| 353 |
|
---|
| 354 | return (char *) ptr;
|
---|
[7bb9b53] | 355 | }
|
---|
| 356 |
|
---|
[5273eb6] | 357 | /**
|
---|
| 358 | * Scan string for a first occurence of a character.
|
---|
| 359 | *
|
---|
| 360 | * @param s String in which to look for the character.
|
---|
| 361 | * @param c Character to look for.
|
---|
| 362 | * @return Pointer to the specified character on success, pointer to the
|
---|
| 363 | * string terminator otherwise.
|
---|
| 364 | */
|
---|
[517cedc0] | 365 | char *gnu_strchrnul(const char *s, int c)
|
---|
| 366 | {
|
---|
| 367 | assert(s != NULL);
|
---|
[a35b458] | 368 |
|
---|
[517cedc0] | 369 | while (*s != c && *s != '\0') {
|
---|
| 370 | s++;
|
---|
| 371 | }
|
---|
[a35b458] | 372 |
|
---|
[517cedc0] | 373 | return (char *) s;
|
---|
| 374 | }
|
---|
| 375 |
|
---|
[7bb9b53] | 376 | /**
|
---|
[5273eb6] | 377 | * Scan a string for a first occurence of one of provided bytes.
|
---|
[7bb9b53] | 378 | *
|
---|
[5273eb6] | 379 | * @param s1 String in which to look for the bytes.
|
---|
| 380 | * @param s2 String of bytes to look for.
|
---|
| 381 | * @return Pointer to the found byte on success,
|
---|
| 382 | * NULL pointer otherwise.
|
---|
[7bb9b53] | 383 | */
|
---|
[7f9df7b9] | 384 | char *strpbrk(const char *s1, const char *s2)
|
---|
[7bb9b53] | 385 | {
|
---|
[ef6dd3f] | 386 | assert(s1 != NULL);
|
---|
| 387 | assert(s2 != NULL);
|
---|
| 388 |
|
---|
| 389 | char *ptr = strpbrk_null(s1, s2);
|
---|
| 390 | return (*ptr == '\0') ? NULL : ptr;
|
---|
[7bb9b53] | 391 | }
|
---|
| 392 |
|
---|
| 393 | /**
|
---|
[5273eb6] | 394 | * Get the length of a complementary substring.
|
---|
[7bb9b53] | 395 | *
|
---|
[5273eb6] | 396 | * @param s1 String that shall be searched for complementary prefix.
|
---|
| 397 | * @param s2 String of bytes that shall not occur in the prefix.
|
---|
| 398 | * @return Length of the prefix.
|
---|
[7bb9b53] | 399 | */
|
---|
[7f9df7b9] | 400 | size_t strcspn(const char *s1, const char *s2)
|
---|
[7bb9b53] | 401 | {
|
---|
[ef6dd3f] | 402 | assert(s1 != NULL);
|
---|
| 403 | assert(s2 != NULL);
|
---|
| 404 |
|
---|
| 405 | char *ptr = strpbrk_null(s1, s2);
|
---|
| 406 | return (size_t) (ptr - s1);
|
---|
[7bb9b53] | 407 | }
|
---|
| 408 |
|
---|
| 409 | /**
|
---|
[5273eb6] | 410 | * Get length of a substring.
|
---|
[7bb9b53] | 411 | *
|
---|
[5273eb6] | 412 | * @param s1 String that shall be searched for prefix.
|
---|
| 413 | * @param s2 String of bytes that the prefix must consist of.
|
---|
| 414 | * @return Length of the prefix.
|
---|
[7bb9b53] | 415 | */
|
---|
[7f9df7b9] | 416 | size_t strspn(const char *s1, const char *s2)
|
---|
[7bb9b53] | 417 | {
|
---|
[ef6dd3f] | 418 | assert(s1 != NULL);
|
---|
| 419 | assert(s2 != NULL);
|
---|
| 420 |
|
---|
| 421 | const char *ptr;
|
---|
[4f4b4e7] | 422 | for (ptr = s1; *ptr != '\0'; ++ptr) {
|
---|
[7f9df7b9] | 423 | if (!strchr(s2, *ptr)) {
|
---|
[ef6dd3f] | 424 | break;
|
---|
[4f4b4e7] | 425 | }
|
---|
[ef6dd3f] | 426 | }
|
---|
| 427 | return ptr - s1;
|
---|
[7bb9b53] | 428 | }
|
---|
| 429 |
|
---|
| 430 | /**
|
---|
[7e10aee] | 431 | * Find a substring. Uses Knuth-Morris-Pratt algorithm.
|
---|
[7bb9b53] | 432 | *
|
---|
[5273eb6] | 433 | * @param s1 String in which to look for a substring.
|
---|
| 434 | * @param s2 Substring to look for.
|
---|
| 435 | * @return Pointer to the first character of the substring in s1, or NULL if
|
---|
| 436 | * not found.
|
---|
[7bb9b53] | 437 | */
|
---|
[7f9df7b9] | 438 | char *strstr(const char *haystack, const char *needle)
|
---|
[7bb9b53] | 439 | {
|
---|
[7e10aee] | 440 | assert(haystack != NULL);
|
---|
| 441 | assert(needle != NULL);
|
---|
[a35b458] | 442 |
|
---|
[7e10aee] | 443 | /* Special case - needle is an empty string. */
|
---|
| 444 | if (needle[0] == '\0') {
|
---|
[3f33b95] | 445 | return (char *) haystack;
|
---|
[4f4b4e7] | 446 | }
|
---|
[a35b458] | 447 |
|
---|
[7e10aee] | 448 | /* Preprocess needle. */
|
---|
[7f9df7b9] | 449 | size_t nlen = strlen(needle);
|
---|
[7e10aee] | 450 | size_t prefix_table[nlen + 1];
|
---|
[a35b458] | 451 |
|
---|
[013e5d32] | 452 | size_t i = 0;
|
---|
| 453 | ssize_t j = -1;
|
---|
[a35b458] | 454 |
|
---|
[013e5d32] | 455 | prefix_table[i] = j;
|
---|
[a35b458] | 456 |
|
---|
[013e5d32] | 457 | while (i < nlen) {
|
---|
| 458 | while (j >= 0 && needle[i] != needle[j]) {
|
---|
| 459 | j = prefix_table[j];
|
---|
[7e10aee] | 460 | }
|
---|
[1433ecda] | 461 | i++;
|
---|
| 462 | j++;
|
---|
[013e5d32] | 463 | prefix_table[i] = j;
|
---|
[7e10aee] | 464 | }
|
---|
[a35b458] | 465 |
|
---|
[7e10aee] | 466 | /* Search needle using the precomputed table. */
|
---|
| 467 | size_t npos = 0;
|
---|
[a35b458] | 468 |
|
---|
[3f33b95] | 469 | for (size_t hpos = 0; haystack[hpos] != '\0'; ++hpos) {
|
---|
[7e10aee] | 470 | while (npos != 0 && haystack[hpos] != needle[npos]) {
|
---|
| 471 | npos = prefix_table[npos];
|
---|
[4f4b4e7] | 472 | }
|
---|
[a35b458] | 473 |
|
---|
[7e10aee] | 474 | if (haystack[hpos] == needle[npos]) {
|
---|
[3f33b95] | 475 | npos++;
|
---|
[a35b458] | 476 |
|
---|
[7e10aee] | 477 | if (npos == nlen) {
|
---|
[3f33b95] | 478 | return (char *) (haystack + hpos - nlen + 1);
|
---|
[7e10aee] | 479 | }
|
---|
| 480 | }
|
---|
[ef6dd3f] | 481 | }
|
---|
[a35b458] | 482 |
|
---|
[ef6dd3f] | 483 | return NULL;
|
---|
[7bb9b53] | 484 | }
|
---|
| 485 |
|
---|
[12b29f3] | 486 | /** Split string by delimiters.
|
---|
| 487 | *
|
---|
| 488 | * @param s String to be tokenized. May not be NULL.
|
---|
| 489 | * @param delim String with the delimiters.
|
---|
| 490 | * @return Pointer to the prefix of @a s before the first
|
---|
| 491 | * delimiter character. NULL if no such prefix
|
---|
| 492 | * exists.
|
---|
| 493 | */
|
---|
[7f9df7b9] | 494 | char *strtok(char *s, const char *delim)
|
---|
[12b29f3] | 495 | {
|
---|
| 496 | static char *next;
|
---|
| 497 |
|
---|
[7f9df7b9] | 498 | return strtok_r(s, delim, &next);
|
---|
[12b29f3] | 499 | }
|
---|
| 500 |
|
---|
| 501 |
|
---|
| 502 | /** Split string by delimiters.
|
---|
| 503 | *
|
---|
| 504 | * @param s String to be tokenized. May not be NULL.
|
---|
| 505 | * @param delim String with the delimiters.
|
---|
| 506 | * @param next Variable which will receive the pointer to the
|
---|
| 507 | * continuation of the string following the first
|
---|
| 508 | * occurrence of any of the delimiter characters.
|
---|
| 509 | * May be NULL.
|
---|
| 510 | * @return Pointer to the prefix of @a s before the first
|
---|
| 511 | * delimiter character. NULL if no such prefix
|
---|
| 512 | * exists.
|
---|
| 513 | */
|
---|
[7f9df7b9] | 514 | char *strtok_r(char *s, const char *delim, char **next)
|
---|
[12b29f3] | 515 | {
|
---|
| 516 | char *start, *end;
|
---|
| 517 |
|
---|
| 518 | if (s == NULL)
|
---|
| 519 | s = *next;
|
---|
| 520 |
|
---|
| 521 | /* Skip over leading delimiters. */
|
---|
[1433ecda] | 522 | while (*s && (strchr(delim, *s) != NULL))
|
---|
| 523 | ++s;
|
---|
[12b29f3] | 524 | start = s;
|
---|
| 525 |
|
---|
| 526 | /* Skip over token characters. */
|
---|
[1433ecda] | 527 | while (*s && (strchr(delim, *s) == NULL))
|
---|
| 528 | ++s;
|
---|
[12b29f3] | 529 | end = s;
|
---|
| 530 | *next = (*s ? s + 1 : s);
|
---|
| 531 |
|
---|
| 532 | if (start == end) {
|
---|
| 533 | return NULL; /* No more tokens. */
|
---|
| 534 | }
|
---|
| 535 |
|
---|
| 536 | /* Overwrite delimiter with NULL terminator. */
|
---|
| 537 | *end = '\0';
|
---|
| 538 | return start;
|
---|
| 539 | }
|
---|
| 540 |
|
---|
[7bb9b53] | 541 | /**
|
---|
[5273eb6] | 542 | * String comparison using collating information.
|
---|
| 543 | *
|
---|
[ef6dd3f] | 544 | * Currently ignores locale and just calls strcmp.
|
---|
[7bb9b53] | 545 | *
|
---|
[5273eb6] | 546 | * @param s1 First string to be compared.
|
---|
| 547 | * @param s2 Second string to be compared.
|
---|
| 548 | * @return Difference of the first pair of inequal characters,
|
---|
| 549 | * or 0 if strings have the same content.
|
---|
[7bb9b53] | 550 | */
|
---|
[7f9df7b9] | 551 | int strcoll(const char *s1, const char *s2)
|
---|
[7bb9b53] | 552 | {
|
---|
[ef6dd3f] | 553 | assert(s1 != NULL);
|
---|
| 554 | assert(s2 != NULL);
|
---|
| 555 |
|
---|
[7f9df7b9] | 556 | return strcmp(s1, s2);
|
---|
[7bb9b53] | 557 | }
|
---|
| 558 |
|
---|
| 559 | /**
|
---|
[5273eb6] | 560 | * Transform a string in such a way that the resulting string yields the same
|
---|
| 561 | * results when passed to the strcmp as if the original string is passed to
|
---|
| 562 | * the strcoll.
|
---|
| 563 | *
|
---|
| 564 | * Since strcoll is equal to strcmp here, this just makes a copy.
|
---|
[7bb9b53] | 565 | *
|
---|
[5273eb6] | 566 | * @param s1 Transformed string.
|
---|
| 567 | * @param s2 Original string.
|
---|
| 568 | * @param n Maximum length of the transformed string.
|
---|
| 569 | * @return Length of the transformed string.
|
---|
[7bb9b53] | 570 | */
|
---|
[7f9df7b9] | 571 | size_t strxfrm(char *restrict s1, const char *restrict s2, size_t n)
|
---|
[7bb9b53] | 572 | {
|
---|
[ef6dd3f] | 573 | assert(s1 != NULL || n == 0);
|
---|
| 574 | assert(s2 != NULL);
|
---|
| 575 |
|
---|
[7f9df7b9] | 576 | size_t len = strlen(s2);
|
---|
[ef6dd3f] | 577 |
|
---|
[4f4b4e7] | 578 | if (n > len) {
|
---|
[7f9df7b9] | 579 | strcpy(s1, s2);
|
---|
[4f4b4e7] | 580 | }
|
---|
[ef6dd3f] | 581 |
|
---|
| 582 | return len;
|
---|
[7bb9b53] | 583 | }
|
---|
| 584 |
|
---|
| 585 | /**
|
---|
[5273eb6] | 586 | * Get error message string.
|
---|
[7bb9b53] | 587 | *
|
---|
[5273eb6] | 588 | * @param errnum Error code for which to obtain human readable string.
|
---|
| 589 | * @return Error message.
|
---|
[7bb9b53] | 590 | */
|
---|
[7f9df7b9] | 591 | char *strerror(int errnum)
|
---|
[7bb9b53] | 592 | {
|
---|
[0d0b319] | 593 | // FIXME: move strerror() and strerror_r() to libc.
|
---|
| 594 | return (char *) str_error(errnum);
|
---|
[ef6dd3f] | 595 | }
|
---|
| 596 |
|
---|
| 597 | /**
|
---|
[5273eb6] | 598 | * Get error message string.
|
---|
[ef6dd3f] | 599 | *
|
---|
[5273eb6] | 600 | * @param errnum Error code for which to obtain human readable string.
|
---|
| 601 | * @param buf Buffer to store a human readable string to.
|
---|
| 602 | * @param bufsz Size of buffer pointed to by buf.
|
---|
| 603 | * @return Zero on success, errno otherwise.
|
---|
[ef6dd3f] | 604 | */
|
---|
[7f9df7b9] | 605 | int strerror_r(int errnum, char *buf, size_t bufsz)
|
---|
[ef6dd3f] | 606 | {
|
---|
| 607 | assert(buf != NULL);
|
---|
[a35b458] | 608 |
|
---|
[7f9df7b9] | 609 | char *errstr = strerror(errnum);
|
---|
[a35b458] | 610 |
|
---|
[7f9df7b9] | 611 | if (strlen(errstr) + 1 > bufsz) {
|
---|
[1b55da67] | 612 | return ERANGE;
|
---|
[ef6dd3f] | 613 | } else {
|
---|
[7f9df7b9] | 614 | strcpy(buf, errstr);
|
---|
[ef6dd3f] | 615 | }
|
---|
| 616 |
|
---|
[0d0b319] | 617 | return EOK;
|
---|
[7bb9b53] | 618 | }
|
---|
| 619 |
|
---|
| 620 | /**
|
---|
[5273eb6] | 621 | * Get length of the string.
|
---|
[7bb9b53] | 622 | *
|
---|
[5273eb6] | 623 | * @param s String which length shall be determined.
|
---|
| 624 | * @return Length of the string.
|
---|
[7bb9b53] | 625 | */
|
---|
[7f9df7b9] | 626 | size_t strlen(const char *s)
|
---|
[7bb9b53] | 627 | {
|
---|
[ef6dd3f] | 628 | assert(s != NULL);
|
---|
[a35b458] | 629 |
|
---|
[7f9df7b9] | 630 | return (size_t) (strchr(s, '\0') - s);
|
---|
[ef6dd3f] | 631 | }
|
---|
| 632 |
|
---|
| 633 | /**
|
---|
[5273eb6] | 634 | * Get limited length of the string.
|
---|
[ef6dd3f] | 635 | *
|
---|
[5273eb6] | 636 | * @param s String which length shall be determined.
|
---|
| 637 | * @param n Maximum number of bytes that can be examined to determine length.
|
---|
| 638 | * @return The lower of either string length or n limit.
|
---|
[ef6dd3f] | 639 | */
|
---|
[7f9df7b9] | 640 | size_t strnlen(const char *s, size_t n)
|
---|
[ef6dd3f] | 641 | {
|
---|
| 642 | assert(s != NULL);
|
---|
[a35b458] | 643 |
|
---|
[4f4b4e7] | 644 | for (size_t sz = 0; sz < n; ++sz) {
|
---|
[a35b458] | 645 |
|
---|
[ef6dd3f] | 646 | if (s[sz] == '\0') {
|
---|
| 647 | return sz;
|
---|
| 648 | }
|
---|
| 649 | }
|
---|
[a35b458] | 650 |
|
---|
[ef6dd3f] | 651 | return n;
|
---|
[7bb9b53] | 652 | }
|
---|
| 653 |
|
---|
[d3ce33fa] | 654 | /**
|
---|
[5273eb6] | 655 | * Get description of a signal.
|
---|
[d3ce33fa] | 656 | *
|
---|
[5273eb6] | 657 | * @param signum Signal number.
|
---|
| 658 | * @return Human readable signal description.
|
---|
[d3ce33fa] | 659 | */
|
---|
[7f9df7b9] | 660 | char *strsignal(int signum)
|
---|
[d3ce33fa] | 661 | {
|
---|
| 662 | static const char *const sigstrings[] = {
|
---|
| 663 | [SIGABRT] = "SIGABRT (Process abort signal)",
|
---|
| 664 | [SIGALRM] = "SIGALRM (Alarm clock)",
|
---|
| 665 | [SIGBUS] = "SIGBUS (Access to an undefined portion of a memory object)",
|
---|
| 666 | [SIGCHLD] = "SIGCHLD (Child process terminated, stopped, or continued)",
|
---|
| 667 | [SIGCONT] = "SIGCONT (Continue executing, if stopped)",
|
---|
| 668 | [SIGFPE] = "SIGFPE (Erroneous arithmetic operation)",
|
---|
| 669 | [SIGHUP] = "SIGHUP (Hangup)",
|
---|
| 670 | [SIGILL] = "SIGILL (Illegal instruction)",
|
---|
| 671 | [SIGINT] = "SIGINT (Terminal interrupt signal)",
|
---|
| 672 | [SIGKILL] = "SIGKILL (Kill process)",
|
---|
| 673 | [SIGPIPE] = "SIGPIPE (Write on a pipe with no one to read it)",
|
---|
| 674 | [SIGQUIT] = "SIGQUIT (Terminal quit signal)",
|
---|
| 675 | [SIGSEGV] = "SIGSEGV (Invalid memory reference)",
|
---|
| 676 | [SIGSTOP] = "SIGSTOP (Stop executing)",
|
---|
| 677 | [SIGTERM] = "SIGTERM (Termination signal)",
|
---|
| 678 | [SIGTSTP] = "SIGTSTP (Terminal stop signal)",
|
---|
| 679 | [SIGTTIN] = "SIGTTIN (Background process attempting read)",
|
---|
| 680 | [SIGTTOU] = "SIGTTOU (Background process attempting write)",
|
---|
| 681 | [SIGUSR1] = "SIGUSR1 (User-defined signal 1)",
|
---|
| 682 | [SIGUSR2] = "SIGUSR2 (User-defined signal 2)",
|
---|
| 683 | [SIGPOLL] = "SIGPOLL (Pollable event)",
|
---|
| 684 | [SIGPROF] = "SIGPROF (Profiling timer expired)",
|
---|
| 685 | [SIGSYS] = "SIGSYS (Bad system call)",
|
---|
| 686 | [SIGTRAP] = "SIGTRAP (Trace/breakpoint trap)",
|
---|
| 687 | [SIGURG] = "SIGURG (High bandwidth data is available at a socket)",
|
---|
| 688 | [SIGVTALRM] = "SIGVTALRM (Virtual timer expired)",
|
---|
| 689 | [SIGXCPU] = "SIGXCPU (CPU time limit exceeded)",
|
---|
| 690 | [SIGXFSZ] = "SIGXFSZ (File size limit exceeded)"
|
---|
| 691 | };
|
---|
| 692 |
|
---|
| 693 | if (signum <= _TOP_SIGNAL) {
|
---|
| 694 | return (char *) sigstrings[signum];
|
---|
| 695 | }
|
---|
| 696 |
|
---|
| 697 | return (char *) "ERROR, Invalid signal number";
|
---|
| 698 | }
|
---|
| 699 |
|
---|
[7bb9b53] | 700 | /** @}
|
---|
| 701 | */
|
---|