[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 |
|
---|
[a3da2b2] | 42 | #include "posix/assert.h"
|
---|
| 43 | #include "posix/errno.h"
|
---|
| 44 | #include "posix/limits.h"
|
---|
| 45 | #include "posix/stdlib.h"
|
---|
| 46 | #include "posix/signal.h"
|
---|
[ef6dd3f] | 47 |
|
---|
[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 | {
|
---|
| 61 | while (!posix_strchr(s2, *s1)) {
|
---|
[4f4b4e7] | 62 | ++s1;
|
---|
[ef6dd3f] | 63 | }
|
---|
| 64 |
|
---|
| 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 | */
|
---|
[5273eb6] | 75 | char *posix_strcpy(char *restrict dest, const char *restrict src)
|
---|
[7bb9b53] | 76 | {
|
---|
[ef6dd3f] | 77 | posix_stpcpy(dest, src);
|
---|
| 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 | */
|
---|
[5273eb6] | 89 | char *posix_strncpy(char *restrict dest, const char *restrict src, size_t n)
|
---|
[7bb9b53] | 90 | {
|
---|
[ef6dd3f] | 91 | posix_stpncpy(dest, src, n);
|
---|
| 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 | */
|
---|
[ef6dd3f] | 102 | char *posix_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];
|
---|
| 109 |
|
---|
| 110 | if (src[i] == '\0') {
|
---|
| 111 | /* pointer to the terminating nul character */
|
---|
| 112 | return &dest[i];
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 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 | */
|
---|
[ef6dd3f] | 128 | char *posix_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];
|
---|
| 135 |
|
---|
| 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 | }
|
---|
| 147 |
|
---|
| 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 | */
|
---|
[5273eb6] | 158 | char *posix_strcat(char *restrict dest, const char *restrict src)
|
---|
[7bb9b53] | 159 | {
|
---|
[ef6dd3f] | 160 | assert(dest != NULL);
|
---|
| 161 | assert(src != NULL);
|
---|
| 162 |
|
---|
[517cedc0] | 163 | posix_strcpy(posix_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 | */
|
---|
[5273eb6] | 175 | char *posix_strncat(char *restrict dest, const char *restrict src, size_t n)
|
---|
[7bb9b53] | 176 | {
|
---|
[ef6dd3f] | 177 | assert(dest != NULL);
|
---|
| 178 | assert(src != NULL);
|
---|
| 179 |
|
---|
[517cedc0] | 180 | char *zeroptr = posix_strncpy(posix_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 | */
|
---|
[5273eb6] | 195 | void *posix_memccpy(void *restrict dest, const void *restrict src, int c, size_t n)
|
---|
[7bb9b53] | 196 | {
|
---|
[ef6dd3f] | 197 | assert(dest != NULL);
|
---|
| 198 | assert(src != NULL);
|
---|
| 199 |
|
---|
| 200 | unsigned char* bdest = dest;
|
---|
| 201 | const unsigned char* bsrc = src;
|
---|
| 202 |
|
---|
[4f4b4e7] | 203 | for (size_t i = 0; i < n; ++i) {
|
---|
[ef6dd3f] | 204 | bdest[i] = bsrc[i];
|
---|
| 205 |
|
---|
| 206 | if (bsrc[i] == (unsigned char) c) {
|
---|
| 207 | /* pointer to the next byte */
|
---|
| 208 | return &bdest[i + 1];
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 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 | */
|
---|
[ef6dd3f] | 221 | char *posix_strdup(const char *s)
|
---|
[7bb9b53] | 222 | {
|
---|
[b52ef5a] | 223 | return posix_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 | */
|
---|
[ef6dd3f] | 233 | char *posix_strndup(const char *s, size_t n)
|
---|
[7bb9b53] | 234 | {
|
---|
[ef6dd3f] | 235 | assert(s != NULL);
|
---|
| 236 |
|
---|
| 237 | size_t len = posix_strnlen(s, n);
|
---|
| 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 bytes in memory.
|
---|
[ef6dd3f] | 251 | *
|
---|
[5273eb6] | 252 | * @param mem1 First area of memory to be compared.
|
---|
| 253 | * @param mem2 Second area of memory to be compared.
|
---|
| 254 | * @param n Maximum number of bytes to be compared.
|
---|
[ef6dd3f] | 255 | * @return Difference of the first pair of inequal bytes,
|
---|
[5273eb6] | 256 | * or 0 if areas have the same content.
|
---|
[7bb9b53] | 257 | */
|
---|
[ef6dd3f] | 258 | int posix_memcmp(const void *mem1, const void *mem2, size_t n)
|
---|
[7bb9b53] | 259 | {
|
---|
[ef6dd3f] | 260 | assert(mem1 != NULL);
|
---|
| 261 | assert(mem2 != NULL);
|
---|
| 262 |
|
---|
| 263 | const unsigned char *s1 = mem1;
|
---|
| 264 | const unsigned char *s2 = mem2;
|
---|
| 265 |
|
---|
[4f4b4e7] | 266 | for (size_t i = 0; i < n; ++i) {
|
---|
[ef6dd3f] | 267 | if (s1[i] != s2[i]) {
|
---|
[087c4c56] | 268 | return s1[i] - s2[i];
|
---|
[ef6dd3f] | 269 | }
|
---|
| 270 | }
|
---|
| 271 |
|
---|
[7bb9b53] | 272 | return 0;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | /**
|
---|
[5273eb6] | 276 | * Compare two strings.
|
---|
[ef6dd3f] | 277 | *
|
---|
[5273eb6] | 278 | * @param s1 First string to be compared.
|
---|
| 279 | * @param s2 Second string to be compared.
|
---|
| 280 | * @return Difference of the first pair of inequal characters,
|
---|
| 281 | * or 0 if strings have the same content.
|
---|
[7bb9b53] | 282 | */
|
---|
[ef6dd3f] | 283 | int posix_strcmp(const char *s1, const char *s2)
|
---|
[7bb9b53] | 284 | {
|
---|
[ef6dd3f] | 285 | assert(s1 != NULL);
|
---|
| 286 | assert(s2 != NULL);
|
---|
| 287 |
|
---|
| 288 | return posix_strncmp(s1, s2, STR_NO_LIMIT);
|
---|
[7bb9b53] | 289 | }
|
---|
| 290 |
|
---|
| 291 | /**
|
---|
[5273eb6] | 292 | * Compare part of two strings.
|
---|
[7bb9b53] | 293 | *
|
---|
[5273eb6] | 294 | * @param s1 First string to be compared.
|
---|
| 295 | * @param s2 Second string to be compared.
|
---|
| 296 | * @param n Maximum number of characters to be compared.
|
---|
| 297 | * @return Difference of the first pair of inequal characters,
|
---|
| 298 | * or 0 if strings have the same content.
|
---|
[7bb9b53] | 299 | */
|
---|
[ef6dd3f] | 300 | int posix_strncmp(const char *s1, const char *s2, size_t n)
|
---|
[7bb9b53] | 301 | {
|
---|
[ef6dd3f] | 302 | assert(s1 != NULL);
|
---|
| 303 | assert(s2 != NULL);
|
---|
| 304 |
|
---|
[4f4b4e7] | 305 | for (size_t i = 0; i < n; ++i) {
|
---|
[ef6dd3f] | 306 | if (s1[i] != s2[i]) {
|
---|
[087c4c56] | 307 | return s1[i] - s2[i];
|
---|
[ef6dd3f] | 308 | }
|
---|
| 309 | if (s1[i] == '\0') {
|
---|
| 310 | break;
|
---|
| 311 | }
|
---|
| 312 | }
|
---|
| 313 |
|
---|
[7bb9b53] | 314 | return 0;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | /**
|
---|
[5273eb6] | 318 | * Find byte in memory.
|
---|
[ef6dd3f] | 319 | *
|
---|
[5273eb6] | 320 | * @param mem Memory area in which to look for the byte.
|
---|
| 321 | * @param c Byte to look for.
|
---|
| 322 | * @param n Maximum number of bytes to be inspected.
|
---|
| 323 | * @return Pointer to the specified byte on success,
|
---|
| 324 | * NULL pointer otherwise.
|
---|
[7bb9b53] | 325 | */
|
---|
[ef6dd3f] | 326 | void *posix_memchr(const void *mem, int c, size_t n)
|
---|
[7bb9b53] | 327 | {
|
---|
[ef6dd3f] | 328 | assert(mem != NULL);
|
---|
| 329 |
|
---|
| 330 | const unsigned char *s = mem;
|
---|
| 331 |
|
---|
[4f4b4e7] | 332 | for (size_t i = 0; i < n; ++i) {
|
---|
[ef6dd3f] | 333 | if (s[i] == (unsigned char) c) {
|
---|
| 334 | return (void *) &s[i];
|
---|
| 335 | }
|
---|
| 336 | }
|
---|
| 337 | return NULL;
|
---|
[7bb9b53] | 338 | }
|
---|
| 339 |
|
---|
| 340 | /**
|
---|
[5273eb6] | 341 | * Scan string for a first occurence of a character.
|
---|
[7bb9b53] | 342 | *
|
---|
[5273eb6] | 343 | * @param s String in which to look for the character.
|
---|
| 344 | * @param c Character to look for.
|
---|
| 345 | * @return Pointer to the specified character on success,
|
---|
| 346 | * NULL pointer otherwise.
|
---|
[7bb9b53] | 347 | */
|
---|
| 348 | char *posix_strchr(const char *s, int c)
|
---|
| 349 | {
|
---|
[ef6dd3f] | 350 | assert(s != NULL);
|
---|
| 351 |
|
---|
[517cedc0] | 352 | char *res = gnu_strchrnul(s, c);
|
---|
| 353 | return (*res == c) ? res : NULL;
|
---|
[7bb9b53] | 354 | }
|
---|
| 355 |
|
---|
| 356 | /**
|
---|
[5273eb6] | 357 | * Scan string for a last occurence of a character.
|
---|
[7bb9b53] | 358 | *
|
---|
[5273eb6] | 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,
|
---|
| 362 | * NULL pointer otherwise.
|
---|
[7bb9b53] | 363 | */
|
---|
| 364 | char *posix_strrchr(const char *s, int c)
|
---|
| 365 | {
|
---|
[ef6dd3f] | 366 | assert(s != NULL);
|
---|
| 367 |
|
---|
[517cedc0] | 368 | const char *ptr = posix_strchr(s, '\0');
|
---|
[ef6dd3f] | 369 |
|
---|
| 370 | /* the same as in strchr, except it loops in reverse direction */
|
---|
| 371 | while (*ptr != (char) c) {
|
---|
[4f4b4e7] | 372 | if (ptr == s) {
|
---|
[ef6dd3f] | 373 | return NULL;
|
---|
[4f4b4e7] | 374 | }
|
---|
[ef6dd3f] | 375 |
|
---|
[27eddb52] | 376 | ptr--;
|
---|
[ef6dd3f] | 377 | }
|
---|
| 378 |
|
---|
| 379 | return (char *) ptr;
|
---|
[7bb9b53] | 380 | }
|
---|
| 381 |
|
---|
[5273eb6] | 382 | /**
|
---|
| 383 | * Scan string for a first occurence of a character.
|
---|
| 384 | *
|
---|
| 385 | * @param s String in which to look for the character.
|
---|
| 386 | * @param c Character to look for.
|
---|
| 387 | * @return Pointer to the specified character on success, pointer to the
|
---|
| 388 | * string terminator otherwise.
|
---|
| 389 | */
|
---|
[517cedc0] | 390 | char *gnu_strchrnul(const char *s, int c)
|
---|
| 391 | {
|
---|
| 392 | assert(s != NULL);
|
---|
| 393 |
|
---|
| 394 | while (*s != c && *s != '\0') {
|
---|
| 395 | s++;
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | return (char *) s;
|
---|
| 399 | }
|
---|
| 400 |
|
---|
[7bb9b53] | 401 | /**
|
---|
[5273eb6] | 402 | * Scan a string for a first occurence of one of provided bytes.
|
---|
[7bb9b53] | 403 | *
|
---|
[5273eb6] | 404 | * @param s1 String in which to look for the bytes.
|
---|
| 405 | * @param s2 String of bytes to look for.
|
---|
| 406 | * @return Pointer to the found byte on success,
|
---|
| 407 | * NULL pointer otherwise.
|
---|
[7bb9b53] | 408 | */
|
---|
| 409 | char *posix_strpbrk(const char *s1, const char *s2)
|
---|
| 410 | {
|
---|
[ef6dd3f] | 411 | assert(s1 != NULL);
|
---|
| 412 | assert(s2 != NULL);
|
---|
| 413 |
|
---|
| 414 | char *ptr = strpbrk_null(s1, s2);
|
---|
| 415 | return (*ptr == '\0') ? NULL : ptr;
|
---|
[7bb9b53] | 416 | }
|
---|
| 417 |
|
---|
| 418 | /**
|
---|
[5273eb6] | 419 | * Get the length of a complementary substring.
|
---|
[7bb9b53] | 420 | *
|
---|
[5273eb6] | 421 | * @param s1 String that shall be searched for complementary prefix.
|
---|
| 422 | * @param s2 String of bytes that shall not occur in the prefix.
|
---|
| 423 | * @return Length of the prefix.
|
---|
[7bb9b53] | 424 | */
|
---|
| 425 | size_t posix_strcspn(const char *s1, const char *s2)
|
---|
| 426 | {
|
---|
[ef6dd3f] | 427 | assert(s1 != NULL);
|
---|
| 428 | assert(s2 != NULL);
|
---|
| 429 |
|
---|
| 430 | char *ptr = strpbrk_null(s1, s2);
|
---|
| 431 | return (size_t) (ptr - s1);
|
---|
[7bb9b53] | 432 | }
|
---|
| 433 |
|
---|
| 434 | /**
|
---|
[5273eb6] | 435 | * Get length of a substring.
|
---|
[7bb9b53] | 436 | *
|
---|
[5273eb6] | 437 | * @param s1 String that shall be searched for prefix.
|
---|
| 438 | * @param s2 String of bytes that the prefix must consist of.
|
---|
| 439 | * @return Length of the prefix.
|
---|
[7bb9b53] | 440 | */
|
---|
| 441 | size_t posix_strspn(const char *s1, const char *s2)
|
---|
| 442 | {
|
---|
[ef6dd3f] | 443 | assert(s1 != NULL);
|
---|
| 444 | assert(s2 != NULL);
|
---|
| 445 |
|
---|
| 446 | const char *ptr;
|
---|
[4f4b4e7] | 447 | for (ptr = s1; *ptr != '\0'; ++ptr) {
|
---|
| 448 | if (!posix_strchr(s2, *ptr)) {
|
---|
[ef6dd3f] | 449 | break;
|
---|
[4f4b4e7] | 450 | }
|
---|
[ef6dd3f] | 451 | }
|
---|
| 452 | return ptr - s1;
|
---|
[7bb9b53] | 453 | }
|
---|
| 454 |
|
---|
| 455 | /**
|
---|
[7e10aee] | 456 | * Find a substring. Uses Knuth-Morris-Pratt algorithm.
|
---|
[7bb9b53] | 457 | *
|
---|
[5273eb6] | 458 | * @param s1 String in which to look for a substring.
|
---|
| 459 | * @param s2 Substring to look for.
|
---|
| 460 | * @return Pointer to the first character of the substring in s1, or NULL if
|
---|
| 461 | * not found.
|
---|
[7bb9b53] | 462 | */
|
---|
[3f33b95] | 463 | char *posix_strstr(const char *haystack, const char *needle)
|
---|
[7bb9b53] | 464 | {
|
---|
[7e10aee] | 465 | assert(haystack != NULL);
|
---|
| 466 | assert(needle != NULL);
|
---|
| 467 |
|
---|
| 468 | /* Special case - needle is an empty string. */
|
---|
| 469 | if (needle[0] == '\0') {
|
---|
[3f33b95] | 470 | return (char *) haystack;
|
---|
[4f4b4e7] | 471 | }
|
---|
[7e10aee] | 472 |
|
---|
| 473 | /* Preprocess needle. */
|
---|
| 474 | size_t nlen = posix_strlen(needle);
|
---|
| 475 | size_t prefix_table[nlen + 1];
|
---|
| 476 |
|
---|
| 477 | {
|
---|
| 478 | size_t i = 0;
|
---|
| 479 | ssize_t j = -1;
|
---|
| 480 |
|
---|
| 481 | prefix_table[i] = j;
|
---|
| 482 |
|
---|
| 483 | while (i < nlen) {
|
---|
| 484 | while (j >= 0 && needle[i] != needle[j]) {
|
---|
| 485 | j = prefix_table[j];
|
---|
| 486 | }
|
---|
| 487 | i++; j++;
|
---|
| 488 | prefix_table[i] = j;
|
---|
| 489 | }
|
---|
| 490 | }
|
---|
| 491 |
|
---|
| 492 | /* Search needle using the precomputed table. */
|
---|
| 493 | size_t npos = 0;
|
---|
| 494 |
|
---|
[3f33b95] | 495 | for (size_t hpos = 0; haystack[hpos] != '\0'; ++hpos) {
|
---|
[7e10aee] | 496 | while (npos != 0 && haystack[hpos] != needle[npos]) {
|
---|
| 497 | npos = prefix_table[npos];
|
---|
[4f4b4e7] | 498 | }
|
---|
[ef6dd3f] | 499 |
|
---|
[7e10aee] | 500 | if (haystack[hpos] == needle[npos]) {
|
---|
[3f33b95] | 501 | npos++;
|
---|
[7e10aee] | 502 |
|
---|
| 503 | if (npos == nlen) {
|
---|
[3f33b95] | 504 | return (char *) (haystack + hpos - nlen + 1);
|
---|
[7e10aee] | 505 | }
|
---|
| 506 | }
|
---|
[ef6dd3f] | 507 | }
|
---|
| 508 |
|
---|
| 509 | return NULL;
|
---|
[7bb9b53] | 510 | }
|
---|
| 511 |
|
---|
| 512 | /**
|
---|
[5273eb6] | 513 | * String comparison using collating information.
|
---|
| 514 | *
|
---|
[ef6dd3f] | 515 | * Currently ignores locale and just calls strcmp.
|
---|
[7bb9b53] | 516 | *
|
---|
[5273eb6] | 517 | * @param s1 First string to be compared.
|
---|
| 518 | * @param s2 Second string to be compared.
|
---|
| 519 | * @return Difference of the first pair of inequal characters,
|
---|
| 520 | * or 0 if strings have the same content.
|
---|
[7bb9b53] | 521 | */
|
---|
| 522 | int posix_strcoll(const char *s1, const char *s2)
|
---|
| 523 | {
|
---|
[ef6dd3f] | 524 | assert(s1 != NULL);
|
---|
| 525 | assert(s2 != NULL);
|
---|
| 526 |
|
---|
| 527 | return posix_strcmp(s1, s2);
|
---|
[7bb9b53] | 528 | }
|
---|
| 529 |
|
---|
| 530 | /**
|
---|
[5273eb6] | 531 | * Transform a string in such a way that the resulting string yields the same
|
---|
| 532 | * results when passed to the strcmp as if the original string is passed to
|
---|
| 533 | * the strcoll.
|
---|
| 534 | *
|
---|
| 535 | * Since strcoll is equal to strcmp here, this just makes a copy.
|
---|
[7bb9b53] | 536 | *
|
---|
[5273eb6] | 537 | * @param s1 Transformed string.
|
---|
| 538 | * @param s2 Original string.
|
---|
| 539 | * @param n Maximum length of the transformed string.
|
---|
| 540 | * @return Length of the transformed string.
|
---|
[7bb9b53] | 541 | */
|
---|
[5273eb6] | 542 | size_t posix_strxfrm(char *restrict s1, const char *restrict s2, size_t n)
|
---|
[7bb9b53] | 543 | {
|
---|
[ef6dd3f] | 544 | assert(s1 != NULL || n == 0);
|
---|
| 545 | assert(s2 != NULL);
|
---|
| 546 |
|
---|
| 547 | size_t len = posix_strlen(s2);
|
---|
| 548 |
|
---|
[4f4b4e7] | 549 | if (n > len) {
|
---|
[ef6dd3f] | 550 | posix_strcpy(s1, s2);
|
---|
[4f4b4e7] | 551 | }
|
---|
[ef6dd3f] | 552 |
|
---|
| 553 | return len;
|
---|
[7bb9b53] | 554 | }
|
---|
| 555 |
|
---|
| 556 | /**
|
---|
[5273eb6] | 557 | * Get error message string.
|
---|
[7bb9b53] | 558 | *
|
---|
[5273eb6] | 559 | * @param errnum Error code for which to obtain human readable string.
|
---|
| 560 | * @return Error message.
|
---|
[7bb9b53] | 561 | */
|
---|
| 562 | char *posix_strerror(int errnum)
|
---|
| 563 | {
|
---|
[1b55da67] | 564 | static const char *error_msgs[] = {
|
---|
| 565 | [E2BIG] = "[E2BIG] Argument list too long",
|
---|
| 566 | [EACCES] = "[EACCES] Permission denied",
|
---|
| 567 | [EADDRINUSE] = "[EADDRINUSE] Address in use",
|
---|
| 568 | [EADDRNOTAVAIL] = "[EADDRNOTAVAIL] Address not available",
|
---|
| 569 | [EAFNOSUPPORT] = "[EAFNOSUPPORT] Address family not supported",
|
---|
| 570 | [EAGAIN] = "[EAGAIN] Resource unavailable, try again",
|
---|
| 571 | [EALREADY] = "[EALREADY] Connection already in progress",
|
---|
| 572 | [EBADF] = "[EBADF] Bad file descriptor",
|
---|
| 573 | [EBADMSG] = "[EBADMSG] Bad message",
|
---|
| 574 | [EBUSY] = "[EBUSY] Device or resource busy",
|
---|
| 575 | [ECANCELED] = "[ECANCELED] Operation canceled",
|
---|
| 576 | [ECHILD] = "[ECHILD] No child processes",
|
---|
| 577 | [ECONNABORTED] = "[ECONNABORTED] Connection aborted",
|
---|
| 578 | [ECONNREFUSED] = "[ECONNREFUSED] Connection refused",
|
---|
| 579 | [ECONNRESET] = "[ECONNRESET] Connection reset",
|
---|
| 580 | [EDEADLK] = "[EDEADLK] Resource deadlock would occur",
|
---|
| 581 | [EDESTADDRREQ] = "[EDESTADDRREQ] Destination address required",
|
---|
| 582 | [EDOM] = "[EDOM] Mathematics argument out of domain of function",
|
---|
| 583 | [EDQUOT] = "[EDQUOT] Reserved",
|
---|
| 584 | [EEXIST] = "[EEXIST] File exists",
|
---|
| 585 | [EFAULT] = "[EFAULT] Bad address",
|
---|
| 586 | [EFBIG] = "[EFBIG] File too large",
|
---|
| 587 | [EHOSTUNREACH] = "[EHOSTUNREACH] Host is unreachable",
|
---|
| 588 | [EIDRM] = "[EIDRM] Identifier removed",
|
---|
| 589 | [EILSEQ] = "[EILSEQ] Illegal byte sequence",
|
---|
| 590 | [EINPROGRESS] = "[EINPROGRESS] Operation in progress",
|
---|
| 591 | [EINTR] = "[EINTR] Interrupted function",
|
---|
| 592 | [EINVAL] = "[EINVAL] Invalid argument",
|
---|
| 593 | [EIO] = "[EIO] I/O error",
|
---|
| 594 | [EISCONN] = "[EISCONN] Socket is connected",
|
---|
| 595 | [EISDIR] = "[EISDIR] Is a directory",
|
---|
| 596 | [ELOOP] = "[ELOOP] Too many levels of symbolic links",
|
---|
| 597 | [EMFILE] = "[EMFILE] File descriptor value too large",
|
---|
| 598 | [EMLINK] = "[EMLINK] Too many links",
|
---|
| 599 | [EMSGSIZE] = "[EMSGSIZE] Message too large",
|
---|
| 600 | [EMULTIHOP] = "[EMULTIHOP] Reserved",
|
---|
| 601 | [ENAMETOOLONG] = "[ENAMETOOLONG] Filename too long",
|
---|
| 602 | [ENETDOWN] = "[ENETDOWN] Network is down",
|
---|
| 603 | [ENETRESET] = "[ENETRESET] Connection aborted by network",
|
---|
| 604 | [ENETUNREACH] = "[ENETUNREACH] Network unreachable",
|
---|
| 605 | [ENFILE] = "[ENFILE] Too many files open in system",
|
---|
| 606 | [ENOBUFS] = "[ENOBUFS] No buffer space available",
|
---|
| 607 | [ENODATA] = "[ENODATA] No message is available on the STREAM head read queue",
|
---|
| 608 | [ENODEV] = "[ENODEV] No such device",
|
---|
| 609 | [ENOENT] = "[ENOENT] No such file or directory",
|
---|
| 610 | [ENOEXEC] = "[ENOEXEC] Executable file format error",
|
---|
| 611 | [ENOLCK] = "[ENOLCK] No locks available",
|
---|
| 612 | [ENOLINK] = "[ENOLINK] Reserved",
|
---|
| 613 | [ENOMEM] = "[ENOMEM] Not enough space",
|
---|
| 614 | [ENOMSG] = "[ENOMSG] No message of the desired type",
|
---|
| 615 | [ENOPROTOOPT] = "[ENOPROTOOPT] Protocol not available",
|
---|
| 616 | [ENOSPC] = "[ENOSPC] No space left on device",
|
---|
| 617 | [ENOSR] = "[ENOSR] No STREAM resources.",
|
---|
| 618 | [ENOSTR] = "[ENOSTR] Not a STREAM",
|
---|
| 619 | [ENOSYS] = "[ENOSYS] Function not supported",
|
---|
| 620 | [ENOTCONN] = "[ENOTCONN] The socket is not connected",
|
---|
| 621 | [ENOTDIR] = "[ENOTDIR] Not a directory",
|
---|
| 622 | [ENOTEMPTY] = "[ENOTEMPTY] Directory not empty",
|
---|
| 623 | [ENOTRECOVERABLE] = "[ENOTRECOVERABLE] State not recoverable",
|
---|
| 624 | [ENOTSOCK] = "[ENOTSOCK] Not a socket",
|
---|
| 625 | [ENOTSUP] = "[ENOTSUP] Not supported",
|
---|
| 626 | [ENOTTY] = "[ENOTTY] Inappropriate I/O control operation",
|
---|
| 627 | [ENXIO] = "[ENXIO] No such device or address",
|
---|
| 628 | [EOPNOTSUPP] = "[EOPNOTSUPP] Operation not supported",
|
---|
| 629 | [EOVERFLOW] = "[EOVERFLOW] Value too large to be stored in data type",
|
---|
| 630 | [EOWNERDEAD] = "[EOWNERDEAD] Previous owned died",
|
---|
| 631 | [EPERM] = "[EPERM] Operation not permitted",
|
---|
| 632 | [EPIPE] = "[EPIPE] Broken pipe",
|
---|
| 633 | [EPROTO] = "[EPROTO] Protocol error",
|
---|
| 634 | [EPROTONOSUPPORT] = "[EPROTONOSUPPORT] Protocol not supported",
|
---|
| 635 | [EPROTOTYPE] = "[EPROTOTYPE] Protocol wrong type for socket",
|
---|
| 636 | [ERANGE] = "[ERANGE] Result too large",
|
---|
| 637 | [EROFS] = "[EROFS] Read-only file system",
|
---|
| 638 | [ESPIPE] = "[ESPIPE] Invalid seek",
|
---|
| 639 | [ESRCH] = "[ESRCH] No such process",
|
---|
| 640 | [ESTALE] = "[ESTALE] Reserved",
|
---|
| 641 | [ETIME] = "[ETIME] Stream ioctl() timeout",
|
---|
| 642 | [ETIMEDOUT] = "[ETIMEDOUT] Connection timed out",
|
---|
| 643 | [ETXTBSY] = "[ETXTBSY] Text file busy",
|
---|
| 644 | [EWOULDBLOCK] = "[EWOULDBLOCK] Operation would block",
|
---|
| 645 | [EXDEV] = "[EXDEV] Cross-device link",
|
---|
| 646 | };
|
---|
| 647 |
|
---|
| 648 | return (char *) error_msgs[posix_abs(errnum)];
|
---|
[ef6dd3f] | 649 | }
|
---|
| 650 |
|
---|
| 651 | /**
|
---|
[5273eb6] | 652 | * Get error message string.
|
---|
[ef6dd3f] | 653 | *
|
---|
[5273eb6] | 654 | * @param errnum Error code for which to obtain human readable string.
|
---|
| 655 | * @param buf Buffer to store a human readable string to.
|
---|
| 656 | * @param bufsz Size of buffer pointed to by buf.
|
---|
| 657 | * @return Zero on success, errno otherwise.
|
---|
[ef6dd3f] | 658 | */
|
---|
| 659 | int posix_strerror_r(int errnum, char *buf, size_t bufsz)
|
---|
| 660 | {
|
---|
| 661 | assert(buf != NULL);
|
---|
| 662 |
|
---|
| 663 | char *errstr = posix_strerror(errnum);
|
---|
| 664 |
|
---|
| 665 | if (posix_strlen(errstr) + 1 > bufsz) {
|
---|
[1b55da67] | 666 | return ERANGE;
|
---|
[ef6dd3f] | 667 | } else {
|
---|
| 668 | posix_strcpy(buf, errstr);
|
---|
| 669 | }
|
---|
| 670 |
|
---|
[7bb9b53] | 671 | return 0;
|
---|
| 672 | }
|
---|
| 673 |
|
---|
| 674 | /**
|
---|
[5273eb6] | 675 | * Get length of the string.
|
---|
[7bb9b53] | 676 | *
|
---|
[5273eb6] | 677 | * @param s String which length shall be determined.
|
---|
| 678 | * @return Length of the string.
|
---|
[7bb9b53] | 679 | */
|
---|
| 680 | size_t posix_strlen(const char *s)
|
---|
| 681 | {
|
---|
[ef6dd3f] | 682 | assert(s != NULL);
|
---|
| 683 |
|
---|
[517cedc0] | 684 | return (size_t) (posix_strchr(s, '\0') - s);
|
---|
[ef6dd3f] | 685 | }
|
---|
| 686 |
|
---|
| 687 | /**
|
---|
[5273eb6] | 688 | * Get limited length of the string.
|
---|
[ef6dd3f] | 689 | *
|
---|
[5273eb6] | 690 | * @param s String which length shall be determined.
|
---|
| 691 | * @param n Maximum number of bytes that can be examined to determine length.
|
---|
| 692 | * @return The lower of either string length or n limit.
|
---|
[ef6dd3f] | 693 | */
|
---|
| 694 | size_t posix_strnlen(const char *s, size_t n)
|
---|
| 695 | {
|
---|
| 696 | assert(s != NULL);
|
---|
| 697 |
|
---|
[4f4b4e7] | 698 | for (size_t sz = 0; sz < n; ++sz) {
|
---|
[ef6dd3f] | 699 |
|
---|
| 700 | if (s[sz] == '\0') {
|
---|
| 701 | return sz;
|
---|
| 702 | }
|
---|
| 703 | }
|
---|
| 704 |
|
---|
| 705 | return n;
|
---|
[7bb9b53] | 706 | }
|
---|
| 707 |
|
---|
[d3ce33fa] | 708 | /**
|
---|
[5273eb6] | 709 | * Get description of a signal.
|
---|
[d3ce33fa] | 710 | *
|
---|
[5273eb6] | 711 | * @param signum Signal number.
|
---|
| 712 | * @return Human readable signal description.
|
---|
[d3ce33fa] | 713 | */
|
---|
| 714 | char *posix_strsignal(int signum)
|
---|
| 715 | {
|
---|
| 716 | static const char *const sigstrings[] = {
|
---|
| 717 | [SIGABRT] = "SIGABRT (Process abort signal)",
|
---|
| 718 | [SIGALRM] = "SIGALRM (Alarm clock)",
|
---|
| 719 | [SIGBUS] = "SIGBUS (Access to an undefined portion of a memory object)",
|
---|
| 720 | [SIGCHLD] = "SIGCHLD (Child process terminated, stopped, or continued)",
|
---|
| 721 | [SIGCONT] = "SIGCONT (Continue executing, if stopped)",
|
---|
| 722 | [SIGFPE] = "SIGFPE (Erroneous arithmetic operation)",
|
---|
| 723 | [SIGHUP] = "SIGHUP (Hangup)",
|
---|
| 724 | [SIGILL] = "SIGILL (Illegal instruction)",
|
---|
| 725 | [SIGINT] = "SIGINT (Terminal interrupt signal)",
|
---|
| 726 | [SIGKILL] = "SIGKILL (Kill process)",
|
---|
| 727 | [SIGPIPE] = "SIGPIPE (Write on a pipe with no one to read it)",
|
---|
| 728 | [SIGQUIT] = "SIGQUIT (Terminal quit signal)",
|
---|
| 729 | [SIGSEGV] = "SIGSEGV (Invalid memory reference)",
|
---|
| 730 | [SIGSTOP] = "SIGSTOP (Stop executing)",
|
---|
| 731 | [SIGTERM] = "SIGTERM (Termination signal)",
|
---|
| 732 | [SIGTSTP] = "SIGTSTP (Terminal stop signal)",
|
---|
| 733 | [SIGTTIN] = "SIGTTIN (Background process attempting read)",
|
---|
| 734 | [SIGTTOU] = "SIGTTOU (Background process attempting write)",
|
---|
| 735 | [SIGUSR1] = "SIGUSR1 (User-defined signal 1)",
|
---|
| 736 | [SIGUSR2] = "SIGUSR2 (User-defined signal 2)",
|
---|
| 737 | [SIGPOLL] = "SIGPOLL (Pollable event)",
|
---|
| 738 | [SIGPROF] = "SIGPROF (Profiling timer expired)",
|
---|
| 739 | [SIGSYS] = "SIGSYS (Bad system call)",
|
---|
| 740 | [SIGTRAP] = "SIGTRAP (Trace/breakpoint trap)",
|
---|
| 741 | [SIGURG] = "SIGURG (High bandwidth data is available at a socket)",
|
---|
| 742 | [SIGVTALRM] = "SIGVTALRM (Virtual timer expired)",
|
---|
| 743 | [SIGXCPU] = "SIGXCPU (CPU time limit exceeded)",
|
---|
| 744 | [SIGXFSZ] = "SIGXFSZ (File size limit exceeded)"
|
---|
| 745 | };
|
---|
| 746 |
|
---|
| 747 | if (signum <= _TOP_SIGNAL) {
|
---|
| 748 | return (char *) sigstrings[signum];
|
---|
| 749 | }
|
---|
| 750 |
|
---|
| 751 | return (char *) "ERROR, Invalid signal number";
|
---|
| 752 | }
|
---|
| 753 |
|
---|
[7bb9b53] | 754 | /** @}
|
---|
| 755 | */
|
---|