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