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