[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 | */
|
---|
| 33 | /** @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
[4d10fc8] | 36 | #define LIBPOSIX_INTERNAL
|
---|
| 37 |
|
---|
[a6d908c1] | 38 | #include "internal/common.h"
|
---|
[7bb9b53] | 39 | #include "string.h"
|
---|
| 40 |
|
---|
[a6d908c1] | 41 | #include "assert.h"
|
---|
[b52ef5a] | 42 | #include "errno.h"
|
---|
| 43 | #include "limits.h"
|
---|
| 44 | #include "stdlib.h"
|
---|
[d3ce33fa] | 45 | #include "signal.h"
|
---|
[ef6dd3f] | 46 |
|
---|
[a6d908c1] | 47 | #include "libc/str_error.h"
|
---|
| 48 |
|
---|
[4f4b4e7] | 49 | /**
|
---|
| 50 | * Returns true if s2 is a prefix of s1.
|
---|
| 51 | *
|
---|
| 52 | * @param s1
|
---|
| 53 | * @param s2
|
---|
| 54 | * @return
|
---|
[ef6dd3f] | 55 | */
|
---|
| 56 | static bool begins_with(const char *s1, const char *s2)
|
---|
| 57 | {
|
---|
| 58 | while (*s1 == *s2 && *s2 != '\0') {
|
---|
[4f4b4e7] | 59 | s1++;
|
---|
| 60 | s2++;
|
---|
[ef6dd3f] | 61 | }
|
---|
| 62 |
|
---|
| 63 | /* true if the end was reached */
|
---|
| 64 | return *s2 == '\0';
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[4f4b4e7] | 67 | /**
|
---|
| 68 | * The same as strpbrk, except it returns pointer to the nul terminator
|
---|
[ef6dd3f] | 69 | * if no occurence is found.
|
---|
[4f4b4e7] | 70 | *
|
---|
| 71 | * @param s1
|
---|
| 72 | * @param s2
|
---|
| 73 | * @return
|
---|
[ef6dd3f] | 74 | */
|
---|
| 75 | static char *strpbrk_null(const char *s1, const char *s2)
|
---|
| 76 | {
|
---|
| 77 | while (!posix_strchr(s2, *s1)) {
|
---|
[4f4b4e7] | 78 | ++s1;
|
---|
[ef6dd3f] | 79 | }
|
---|
| 80 |
|
---|
| 81 | return (char *) s1;
|
---|
| 82 | }
|
---|
[7bb9b53] | 83 |
|
---|
| 84 | /**
|
---|
| 85 | *
|
---|
| 86 | * @param dest
|
---|
| 87 | * @param src
|
---|
[4f4b4e7] | 88 | * @return
|
---|
[7bb9b53] | 89 | */
|
---|
| 90 | char *posix_strcpy(char *dest, const char *src)
|
---|
| 91 | {
|
---|
[ef6dd3f] | 92 | posix_stpcpy(dest, src);
|
---|
| 93 | return dest;
|
---|
[7bb9b53] | 94 | }
|
---|
| 95 |
|
---|
| 96 | /**
|
---|
| 97 | *
|
---|
| 98 | * @param dest
|
---|
| 99 | * @param src
|
---|
| 100 | * @param n
|
---|
[4f4b4e7] | 101 | * @return
|
---|
[7bb9b53] | 102 | */
|
---|
| 103 | char *posix_strncpy(char *dest, const char *src, size_t n)
|
---|
| 104 | {
|
---|
[ef6dd3f] | 105 | posix_stpncpy(dest, src, n);
|
---|
| 106 | return dest;
|
---|
[7bb9b53] | 107 | }
|
---|
| 108 |
|
---|
| 109 | /**
|
---|
| 110 | *
|
---|
| 111 | * @param dest
|
---|
| 112 | * @param src
|
---|
[ef6dd3f] | 113 | * @return Pointer to the nul character in the dest string
|
---|
[7bb9b53] | 114 | */
|
---|
[ef6dd3f] | 115 | char *posix_stpcpy(char *restrict dest, const char *restrict src)
|
---|
[7bb9b53] | 116 | {
|
---|
[ef6dd3f] | 117 | assert(dest != NULL);
|
---|
| 118 | assert(src != NULL);
|
---|
| 119 |
|
---|
[4f4b4e7] | 120 | for (size_t i = 0; ; ++i) {
|
---|
[ef6dd3f] | 121 | dest[i] = src[i];
|
---|
| 122 |
|
---|
| 123 | if (src[i] == '\0') {
|
---|
| 124 | /* pointer to the terminating nul character */
|
---|
| 125 | return &dest[i];
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | /* unreachable */
|
---|
| 130 | return NULL;
|
---|
[7bb9b53] | 131 | }
|
---|
| 132 |
|
---|
| 133 | /**
|
---|
| 134 | *
|
---|
| 135 | * @param dest
|
---|
| 136 | * @param src
|
---|
| 137 | * @param n
|
---|
[ef6dd3f] | 138 | * @return Pointer to the first written nul character or &dest[n]
|
---|
[7bb9b53] | 139 | */
|
---|
[ef6dd3f] | 140 | char *posix_stpncpy(char *restrict dest, const char *restrict src, size_t n)
|
---|
[7bb9b53] | 141 | {
|
---|
[ef6dd3f] | 142 | assert(dest != NULL);
|
---|
| 143 | assert(src != NULL);
|
---|
| 144 |
|
---|
[4f4b4e7] | 145 | for (size_t i = 0; i < n; ++i) {
|
---|
[ef6dd3f] | 146 | dest[i] = src[i];
|
---|
| 147 |
|
---|
| 148 | /* the standard requires that nul characters
|
---|
| 149 | * are appended to the length of n, in case src is shorter
|
---|
| 150 | */
|
---|
| 151 | if (src[i] == '\0') {
|
---|
| 152 | char *result = &dest[i];
|
---|
[4f4b4e7] | 153 | for (++i; i < n; ++i) {
|
---|
[ef6dd3f] | 154 | dest[i] = '\0';
|
---|
| 155 | }
|
---|
| 156 | return result;
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | return &dest[n];
|
---|
[7bb9b53] | 161 | }
|
---|
| 162 |
|
---|
| 163 | /**
|
---|
[ef6dd3f] | 164 | *
|
---|
[7bb9b53] | 165 | * @param dest
|
---|
| 166 | * @param src
|
---|
[4f4b4e7] | 167 | * @return
|
---|
[7bb9b53] | 168 | */
|
---|
[ef6dd3f] | 169 | char *posix_strcat(char *dest, const char *src)
|
---|
[7bb9b53] | 170 | {
|
---|
[ef6dd3f] | 171 | assert(dest != NULL);
|
---|
| 172 | assert(src != NULL);
|
---|
| 173 |
|
---|
[517cedc0] | 174 | posix_strcpy(posix_strchr(dest, '\0'), src);
|
---|
[ef6dd3f] | 175 | return dest;
|
---|
[7bb9b53] | 176 | }
|
---|
| 177 |
|
---|
| 178 | /**
|
---|
[ef6dd3f] | 179 | *
|
---|
| 180 | * @param dest
|
---|
| 181 | * @param src
|
---|
| 182 | * @param n
|
---|
[4f4b4e7] | 183 | * @return
|
---|
[7bb9b53] | 184 | */
|
---|
[ef6dd3f] | 185 | char *posix_strncat(char *dest, const char *src, size_t n)
|
---|
[7bb9b53] | 186 | {
|
---|
[ef6dd3f] | 187 | assert(dest != NULL);
|
---|
| 188 | assert(src != NULL);
|
---|
| 189 |
|
---|
[517cedc0] | 190 | char *zeroptr = posix_strncpy(posix_strchr(dest, '\0'), src, n);
|
---|
[ef6dd3f] | 191 | /* strncpy doesn't append the nul terminator, so we do it here */
|
---|
| 192 | zeroptr[n] = '\0';
|
---|
| 193 | return dest;
|
---|
[7bb9b53] | 194 | }
|
---|
| 195 |
|
---|
| 196 | /**
|
---|
| 197 | *
|
---|
[ef6dd3f] | 198 | * @param dest
|
---|
| 199 | * @param src
|
---|
| 200 | * @param c
|
---|
[7bb9b53] | 201 | * @param n
|
---|
[ef6dd3f] | 202 | * @return Pointer to the first byte after c in dest if found, NULL otherwise.
|
---|
[7bb9b53] | 203 | */
|
---|
[4f4b4e7] | 204 | void *posix_memccpy(void *dest, const void *src, int c, size_t n)
|
---|
[7bb9b53] | 205 | {
|
---|
[ef6dd3f] | 206 | assert(dest != NULL);
|
---|
| 207 | assert(src != NULL);
|
---|
| 208 |
|
---|
| 209 | unsigned char* bdest = dest;
|
---|
| 210 | const unsigned char* bsrc = src;
|
---|
| 211 |
|
---|
[4f4b4e7] | 212 | for (size_t i = 0; i < n; ++i) {
|
---|
[ef6dd3f] | 213 | bdest[i] = bsrc[i];
|
---|
| 214 |
|
---|
| 215 | if (bsrc[i] == (unsigned char) c) {
|
---|
| 216 | /* pointer to the next byte */
|
---|
| 217 | return &bdest[i + 1];
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | return NULL;
|
---|
[7bb9b53] | 222 | }
|
---|
| 223 |
|
---|
| 224 | /**
|
---|
| 225 | *
|
---|
[ef6dd3f] | 226 | * @param s
|
---|
| 227 | * @return Newly allocated string
|
---|
[7bb9b53] | 228 | */
|
---|
[ef6dd3f] | 229 | char *posix_strdup(const char *s)
|
---|
[7bb9b53] | 230 | {
|
---|
[b52ef5a] | 231 | return posix_strndup(s, SIZE_MAX);
|
---|
[7bb9b53] | 232 | }
|
---|
| 233 |
|
---|
| 234 | /**
|
---|
| 235 | *
|
---|
[ef6dd3f] | 236 | * @param s
|
---|
[7bb9b53] | 237 | * @param n
|
---|
[ef6dd3f] | 238 | * @return Newly allocated string of length at most n
|
---|
[7bb9b53] | 239 | */
|
---|
[ef6dd3f] | 240 | char *posix_strndup(const char *s, size_t n)
|
---|
[7bb9b53] | 241 | {
|
---|
[ef6dd3f] | 242 | assert(s != NULL);
|
---|
| 243 |
|
---|
| 244 | size_t len = posix_strnlen(s, n);
|
---|
| 245 | char *dup = malloc(len + 1);
|
---|
| 246 | if (dup == NULL) {
|
---|
| 247 | return NULL;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | memcpy(dup, s, len);
|
---|
| 251 | dup[len] = '\0';
|
---|
| 252 |
|
---|
| 253 | return dup;
|
---|
[7bb9b53] | 254 | }
|
---|
| 255 |
|
---|
| 256 | /**
|
---|
[ef6dd3f] | 257 | *
|
---|
| 258 | * @param mem1
|
---|
| 259 | * @param mem2
|
---|
| 260 | * @param n
|
---|
| 261 | * @return Difference of the first pair of inequal bytes,
|
---|
[4f4b4e7] | 262 | * or 0 if areas have the same content
|
---|
[7bb9b53] | 263 | */
|
---|
[ef6dd3f] | 264 | int posix_memcmp(const void *mem1, const void *mem2, size_t n)
|
---|
[7bb9b53] | 265 | {
|
---|
[ef6dd3f] | 266 | assert(mem1 != NULL);
|
---|
| 267 | assert(mem2 != NULL);
|
---|
| 268 |
|
---|
| 269 | const unsigned char *s1 = mem1;
|
---|
| 270 | const unsigned char *s2 = mem2;
|
---|
| 271 |
|
---|
[4f4b4e7] | 272 | for (size_t i = 0; i < n; ++i) {
|
---|
[ef6dd3f] | 273 | if (s1[i] != s2[i]) {
|
---|
| 274 | return s2[i] - s1[i];
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
| 277 |
|
---|
[7bb9b53] | 278 | return 0;
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | /**
|
---|
[ef6dd3f] | 282 | *
|
---|
[7bb9b53] | 283 | * @param s1
|
---|
| 284 | * @param s2
|
---|
| 285 | * @return
|
---|
| 286 | */
|
---|
[ef6dd3f] | 287 | int posix_strcmp(const char *s1, const char *s2)
|
---|
[7bb9b53] | 288 | {
|
---|
[ef6dd3f] | 289 | assert(s1 != NULL);
|
---|
| 290 | assert(s2 != NULL);
|
---|
| 291 |
|
---|
| 292 | return posix_strncmp(s1, s2, STR_NO_LIMIT);
|
---|
[7bb9b53] | 293 | }
|
---|
| 294 |
|
---|
| 295 | /**
|
---|
| 296 | *
|
---|
[ef6dd3f] | 297 | * @param s1
|
---|
| 298 | * @param s2
|
---|
[7bb9b53] | 299 | * @param n
|
---|
| 300 | * @return
|
---|
| 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]) {
|
---|
| 309 | return s2[i] - s1[i];
|
---|
| 310 | }
|
---|
| 311 | if (s1[i] == '\0') {
|
---|
| 312 | break;
|
---|
| 313 | }
|
---|
| 314 | }
|
---|
| 315 |
|
---|
[7bb9b53] | 316 | return 0;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | /**
|
---|
[ef6dd3f] | 320 | *
|
---|
[7bb9b53] | 321 | * @param mem
|
---|
| 322 | * @param c
|
---|
[ef6dd3f] | 323 | * @param n
|
---|
[7bb9b53] | 324 | * @return
|
---|
| 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 | /**
|
---|
| 341 | *
|
---|
| 342 | * @param s
|
---|
| 343 | * @param c
|
---|
| 344 | * @return
|
---|
| 345 | */
|
---|
| 346 | char *posix_strchr(const char *s, int c)
|
---|
| 347 | {
|
---|
[ef6dd3f] | 348 | assert(s != NULL);
|
---|
| 349 |
|
---|
[517cedc0] | 350 | char *res = gnu_strchrnul(s, c);
|
---|
| 351 | return (*res == c) ? res : NULL;
|
---|
[7bb9b53] | 352 | }
|
---|
| 353 |
|
---|
| 354 | /**
|
---|
| 355 | *
|
---|
| 356 | * @param s
|
---|
| 357 | * @param c
|
---|
| 358 | * @return
|
---|
| 359 | */
|
---|
| 360 | char *posix_strrchr(const char *s, int c)
|
---|
| 361 | {
|
---|
[ef6dd3f] | 362 | assert(s != NULL);
|
---|
| 363 |
|
---|
[517cedc0] | 364 | const char *ptr = posix_strchr(s, '\0');
|
---|
[ef6dd3f] | 365 |
|
---|
| 366 | /* the same as in strchr, except it loops in reverse direction */
|
---|
| 367 | while (*ptr != (char) c) {
|
---|
[4f4b4e7] | 368 | if (ptr == s) {
|
---|
[ef6dd3f] | 369 | return NULL;
|
---|
[4f4b4e7] | 370 | }
|
---|
[ef6dd3f] | 371 |
|
---|
[27eddb52] | 372 | ptr--;
|
---|
[ef6dd3f] | 373 | }
|
---|
| 374 |
|
---|
| 375 | return (char *) ptr;
|
---|
[7bb9b53] | 376 | }
|
---|
| 377 |
|
---|
[517cedc0] | 378 | char *gnu_strchrnul(const char *s, int c)
|
---|
| 379 | {
|
---|
| 380 | assert(s != NULL);
|
---|
| 381 |
|
---|
| 382 | while (*s != c && *s != '\0') {
|
---|
| 383 | s++;
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | return (char *) s;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
[7bb9b53] | 389 | /**
|
---|
| 390 | *
|
---|
| 391 | * @param s1
|
---|
| 392 | * @param s2
|
---|
| 393 | * @return
|
---|
| 394 | */
|
---|
| 395 | char *posix_strpbrk(const char *s1, const char *s2)
|
---|
| 396 | {
|
---|
[ef6dd3f] | 397 | assert(s1 != NULL);
|
---|
| 398 | assert(s2 != NULL);
|
---|
| 399 |
|
---|
| 400 | char *ptr = strpbrk_null(s1, s2);
|
---|
| 401 | return (*ptr == '\0') ? NULL : ptr;
|
---|
[7bb9b53] | 402 | }
|
---|
| 403 |
|
---|
| 404 | /**
|
---|
| 405 | *
|
---|
| 406 | * @param s1
|
---|
| 407 | * @param s2
|
---|
| 408 | * @return
|
---|
| 409 | */
|
---|
| 410 | size_t posix_strcspn(const char *s1, const char *s2)
|
---|
| 411 | {
|
---|
[ef6dd3f] | 412 | assert(s1 != NULL);
|
---|
| 413 | assert(s2 != NULL);
|
---|
| 414 |
|
---|
| 415 | char *ptr = strpbrk_null(s1, s2);
|
---|
| 416 | return (size_t) (ptr - s1);
|
---|
[7bb9b53] | 417 | }
|
---|
| 418 |
|
---|
| 419 | /**
|
---|
| 420 | *
|
---|
| 421 | * @param s1
|
---|
| 422 | * @param s2
|
---|
| 423 | * @return
|
---|
| 424 | */
|
---|
| 425 | size_t posix_strspn(const char *s1, const char *s2)
|
---|
| 426 | {
|
---|
[ef6dd3f] | 427 | assert(s1 != NULL);
|
---|
| 428 | assert(s2 != NULL);
|
---|
| 429 |
|
---|
| 430 | const char *ptr;
|
---|
[4f4b4e7] | 431 | for (ptr = s1; *ptr != '\0'; ++ptr) {
|
---|
| 432 | if (!posix_strchr(s2, *ptr)) {
|
---|
[ef6dd3f] | 433 | break;
|
---|
[4f4b4e7] | 434 | }
|
---|
[ef6dd3f] | 435 | }
|
---|
| 436 | return ptr - s1;
|
---|
[7bb9b53] | 437 | }
|
---|
| 438 |
|
---|
| 439 | /**
|
---|
| 440 | *
|
---|
| 441 | * @param s1
|
---|
| 442 | * @param s2
|
---|
| 443 | * @return
|
---|
| 444 | */
|
---|
| 445 | char *posix_strstr(const char *s1, const char *s2)
|
---|
| 446 | {
|
---|
[ef6dd3f] | 447 | assert(s1 != NULL);
|
---|
| 448 | assert(s2 != NULL);
|
---|
| 449 |
|
---|
| 450 | /* special case - needle is an empty string */
|
---|
[4f4b4e7] | 451 | if (*s2 == '\0') {
|
---|
[ef6dd3f] | 452 | return (char *) s1;
|
---|
[4f4b4e7] | 453 | }
|
---|
[ef6dd3f] | 454 |
|
---|
| 455 | // TODO: use faster algorithm
|
---|
| 456 | /* check for prefix from every position - quadratic complexity */
|
---|
| 457 | while (*s1 != '\0') {
|
---|
[4f4b4e7] | 458 | if (begins_with(s1, s2)) {
|
---|
[ef6dd3f] | 459 | return (char *) s1;
|
---|
[4f4b4e7] | 460 | }
|
---|
[ef6dd3f] | 461 |
|
---|
[4f4b4e7] | 462 | s1++;
|
---|
[ef6dd3f] | 463 | }
|
---|
| 464 |
|
---|
| 465 | return NULL;
|
---|
[7bb9b53] | 466 | }
|
---|
| 467 |
|
---|
| 468 | /**
|
---|
[ef6dd3f] | 469 | * Currently ignores locale and just calls strcmp.
|
---|
[7bb9b53] | 470 | *
|
---|
| 471 | * @param s1
|
---|
| 472 | * @param s2
|
---|
| 473 | * @return
|
---|
| 474 | */
|
---|
| 475 | int posix_strcoll(const char *s1, const char *s2)
|
---|
| 476 | {
|
---|
[ef6dd3f] | 477 | assert(s1 != NULL);
|
---|
| 478 | assert(s2 != NULL);
|
---|
| 479 |
|
---|
| 480 | return posix_strcmp(s1, s2);
|
---|
[7bb9b53] | 481 | }
|
---|
| 482 |
|
---|
| 483 | /**
|
---|
[ef6dd3f] | 484 | * strcoll is equal to strcmp here, so this just makes a copy.
|
---|
[7bb9b53] | 485 | *
|
---|
| 486 | * @param s1
|
---|
| 487 | * @param s2
|
---|
| 488 | * @param n
|
---|
| 489 | * @return
|
---|
| 490 | */
|
---|
| 491 | size_t posix_strxfrm(char *s1, const char *s2, size_t n)
|
---|
| 492 | {
|
---|
[ef6dd3f] | 493 | assert(s1 != NULL || n == 0);
|
---|
| 494 | assert(s2 != NULL);
|
---|
| 495 |
|
---|
| 496 | size_t len = posix_strlen(s2);
|
---|
| 497 |
|
---|
[4f4b4e7] | 498 | if (n > len) {
|
---|
[ef6dd3f] | 499 | posix_strcpy(s1, s2);
|
---|
[4f4b4e7] | 500 | }
|
---|
[ef6dd3f] | 501 |
|
---|
| 502 | return len;
|
---|
[7bb9b53] | 503 | }
|
---|
| 504 |
|
---|
| 505 | /**
|
---|
| 506 | *
|
---|
| 507 | * @param errnum
|
---|
| 508 | * @return
|
---|
| 509 | */
|
---|
| 510 | char *posix_strerror(int errnum)
|
---|
| 511 | {
|
---|
[ec18957a] | 512 | /* Uses function from libc, we just have to negate errno
|
---|
| 513 | * (POSIX uses positive errorcodes, HelenOS has negative).
|
---|
[4f4b4e7] | 514 | */
|
---|
[ec18957a] | 515 | // FIXME: not all POSIX error codes are in libc
|
---|
| 516 | return (char *) str_error(-posix_abs(errnum));
|
---|
[ef6dd3f] | 517 | }
|
---|
| 518 |
|
---|
| 519 | /**
|
---|
| 520 | *
|
---|
| 521 | * @param errnum Error code
|
---|
[4f4b4e7] | 522 | * @param buf Buffer to store a human readable string to
|
---|
| 523 | * @param bufsz Size of buffer pointed to by buf
|
---|
[ef6dd3f] | 524 | * @return
|
---|
| 525 | */
|
---|
| 526 | int posix_strerror_r(int errnum, char *buf, size_t bufsz)
|
---|
| 527 | {
|
---|
| 528 | assert(buf != NULL);
|
---|
| 529 |
|
---|
| 530 | char *errstr = posix_strerror(errnum);
|
---|
| 531 | /* HelenOS str_error can't fail */
|
---|
| 532 |
|
---|
| 533 | if (posix_strlen(errstr) + 1 > bufsz) {
|
---|
| 534 | return -ERANGE;
|
---|
| 535 | } else {
|
---|
| 536 | posix_strcpy(buf, errstr);
|
---|
| 537 | }
|
---|
| 538 |
|
---|
[7bb9b53] | 539 | return 0;
|
---|
| 540 | }
|
---|
| 541 |
|
---|
| 542 | /**
|
---|
| 543 | *
|
---|
| 544 | * @param s
|
---|
| 545 | * @return
|
---|
| 546 | */
|
---|
| 547 | size_t posix_strlen(const char *s)
|
---|
| 548 | {
|
---|
[ef6dd3f] | 549 | assert(s != NULL);
|
---|
| 550 |
|
---|
[517cedc0] | 551 | return (size_t) (posix_strchr(s, '\0') - s);
|
---|
[ef6dd3f] | 552 | }
|
---|
| 553 |
|
---|
| 554 | /**
|
---|
| 555 | *
|
---|
| 556 | * @param s
|
---|
| 557 | * @param n
|
---|
| 558 | * @return
|
---|
| 559 | */
|
---|
| 560 | size_t posix_strnlen(const char *s, size_t n)
|
---|
| 561 | {
|
---|
| 562 | assert(s != NULL);
|
---|
| 563 |
|
---|
[4f4b4e7] | 564 | for (size_t sz = 0; sz < n; ++sz) {
|
---|
[ef6dd3f] | 565 |
|
---|
| 566 | if (s[sz] == '\0') {
|
---|
| 567 | return sz;
|
---|
| 568 | }
|
---|
| 569 | }
|
---|
| 570 |
|
---|
| 571 | return n;
|
---|
[7bb9b53] | 572 | }
|
---|
| 573 |
|
---|
[d3ce33fa] | 574 | /**
|
---|
| 575 | *
|
---|
| 576 | * @param signum
|
---|
| 577 | * @return
|
---|
| 578 | */
|
---|
| 579 | char *posix_strsignal(int signum)
|
---|
| 580 | {
|
---|
| 581 | static const char *const sigstrings[] = {
|
---|
| 582 | [SIGABRT] = "SIGABRT (Process abort signal)",
|
---|
| 583 | [SIGALRM] = "SIGALRM (Alarm clock)",
|
---|
| 584 | [SIGBUS] = "SIGBUS (Access to an undefined portion of a memory object)",
|
---|
| 585 | [SIGCHLD] = "SIGCHLD (Child process terminated, stopped, or continued)",
|
---|
| 586 | [SIGCONT] = "SIGCONT (Continue executing, if stopped)",
|
---|
| 587 | [SIGFPE] = "SIGFPE (Erroneous arithmetic operation)",
|
---|
| 588 | [SIGHUP] = "SIGHUP (Hangup)",
|
---|
| 589 | [SIGILL] = "SIGILL (Illegal instruction)",
|
---|
| 590 | [SIGINT] = "SIGINT (Terminal interrupt signal)",
|
---|
| 591 | [SIGKILL] = "SIGKILL (Kill process)",
|
---|
| 592 | [SIGPIPE] = "SIGPIPE (Write on a pipe with no one to read it)",
|
---|
| 593 | [SIGQUIT] = "SIGQUIT (Terminal quit signal)",
|
---|
| 594 | [SIGSEGV] = "SIGSEGV (Invalid memory reference)",
|
---|
| 595 | [SIGSTOP] = "SIGSTOP (Stop executing)",
|
---|
| 596 | [SIGTERM] = "SIGTERM (Termination signal)",
|
---|
| 597 | [SIGTSTP] = "SIGTSTP (Terminal stop signal)",
|
---|
| 598 | [SIGTTIN] = "SIGTTIN (Background process attempting read)",
|
---|
| 599 | [SIGTTOU] = "SIGTTOU (Background process attempting write)",
|
---|
| 600 | [SIGUSR1] = "SIGUSR1 (User-defined signal 1)",
|
---|
| 601 | [SIGUSR2] = "SIGUSR2 (User-defined signal 2)",
|
---|
| 602 | [SIGPOLL] = "SIGPOLL (Pollable event)",
|
---|
| 603 | [SIGPROF] = "SIGPROF (Profiling timer expired)",
|
---|
| 604 | [SIGSYS] = "SIGSYS (Bad system call)",
|
---|
| 605 | [SIGTRAP] = "SIGTRAP (Trace/breakpoint trap)",
|
---|
| 606 | [SIGURG] = "SIGURG (High bandwidth data is available at a socket)",
|
---|
| 607 | [SIGVTALRM] = "SIGVTALRM (Virtual timer expired)",
|
---|
| 608 | [SIGXCPU] = "SIGXCPU (CPU time limit exceeded)",
|
---|
| 609 | [SIGXFSZ] = "SIGXFSZ (File size limit exceeded)"
|
---|
| 610 | };
|
---|
| 611 |
|
---|
| 612 | if (signum <= _TOP_SIGNAL) {
|
---|
| 613 | return (char *) sigstrings[signum];
|
---|
| 614 | }
|
---|
| 615 |
|
---|
| 616 | return (char *) "ERROR, Invalid signal number";
|
---|
| 617 | }
|
---|
| 618 |
|
---|
[7bb9b53] | 619 | /** @}
|
---|
| 620 | */
|
---|