[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"
|
---|
[ef6dd3f] | 45 |
|
---|
[a6d908c1] | 46 | #include "libc/str_error.h"
|
---|
| 47 |
|
---|
[4f4b4e7] | 48 | /**
|
---|
| 49 | * Returns true if s2 is a prefix of s1.
|
---|
| 50 | *
|
---|
| 51 | * @param s1
|
---|
| 52 | * @param s2
|
---|
| 53 | * @return
|
---|
[ef6dd3f] | 54 | */
|
---|
| 55 | static bool begins_with(const char *s1, const char *s2)
|
---|
| 56 | {
|
---|
| 57 | while (*s1 == *s2 && *s2 != '\0') {
|
---|
[4f4b4e7] | 58 | s1++;
|
---|
| 59 | s2++;
|
---|
[ef6dd3f] | 60 | }
|
---|
| 61 |
|
---|
| 62 | /* true if the end was reached */
|
---|
| 63 | return *s2 == '\0';
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[4f4b4e7] | 66 | /**
|
---|
| 67 | * The same as strpbrk, except it returns pointer to the nul terminator
|
---|
[ef6dd3f] | 68 | * if no occurence is found.
|
---|
[4f4b4e7] | 69 | *
|
---|
| 70 | * @param s1
|
---|
| 71 | * @param s2
|
---|
| 72 | * @return
|
---|
[ef6dd3f] | 73 | */
|
---|
| 74 | static char *strpbrk_null(const char *s1, const char *s2)
|
---|
| 75 | {
|
---|
| 76 | while (!posix_strchr(s2, *s1)) {
|
---|
[4f4b4e7] | 77 | ++s1;
|
---|
[ef6dd3f] | 78 | }
|
---|
| 79 |
|
---|
| 80 | return (char *) s1;
|
---|
| 81 | }
|
---|
[7bb9b53] | 82 |
|
---|
| 83 | /**
|
---|
| 84 | *
|
---|
| 85 | * @param dest
|
---|
| 86 | * @param src
|
---|
[4f4b4e7] | 87 | * @return
|
---|
[7bb9b53] | 88 | */
|
---|
| 89 | char *posix_strcpy(char *dest, const char *src)
|
---|
| 90 | {
|
---|
[ef6dd3f] | 91 | posix_stpcpy(dest, src);
|
---|
| 92 | return dest;
|
---|
[7bb9b53] | 93 | }
|
---|
| 94 |
|
---|
| 95 | /**
|
---|
| 96 | *
|
---|
| 97 | * @param dest
|
---|
| 98 | * @param src
|
---|
| 99 | * @param n
|
---|
[4f4b4e7] | 100 | * @return
|
---|
[7bb9b53] | 101 | */
|
---|
| 102 | char *posix_strncpy(char *dest, const char *src, size_t n)
|
---|
| 103 | {
|
---|
[ef6dd3f] | 104 | posix_stpncpy(dest, src, n);
|
---|
| 105 | return dest;
|
---|
[7bb9b53] | 106 | }
|
---|
| 107 |
|
---|
| 108 | /**
|
---|
| 109 | *
|
---|
| 110 | * @param dest
|
---|
| 111 | * @param src
|
---|
[ef6dd3f] | 112 | * @return Pointer to the nul character in the dest string
|
---|
[7bb9b53] | 113 | */
|
---|
[ef6dd3f] | 114 | char *posix_stpcpy(char *restrict dest, const char *restrict src)
|
---|
[7bb9b53] | 115 | {
|
---|
[ef6dd3f] | 116 | assert(dest != NULL);
|
---|
| 117 | assert(src != NULL);
|
---|
| 118 |
|
---|
[4f4b4e7] | 119 | for (size_t i = 0; ; ++i) {
|
---|
[ef6dd3f] | 120 | dest[i] = src[i];
|
---|
| 121 |
|
---|
| 122 | if (src[i] == '\0') {
|
---|
| 123 | /* pointer to the terminating nul character */
|
---|
| 124 | return &dest[i];
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | /* unreachable */
|
---|
| 129 | return NULL;
|
---|
[7bb9b53] | 130 | }
|
---|
| 131 |
|
---|
| 132 | /**
|
---|
| 133 | *
|
---|
| 134 | * @param dest
|
---|
| 135 | * @param src
|
---|
| 136 | * @param n
|
---|
[ef6dd3f] | 137 | * @return Pointer to the first written nul character or &dest[n]
|
---|
[7bb9b53] | 138 | */
|
---|
[ef6dd3f] | 139 | char *posix_stpncpy(char *restrict dest, const char *restrict src, size_t n)
|
---|
[7bb9b53] | 140 | {
|
---|
[ef6dd3f] | 141 | assert(dest != NULL);
|
---|
| 142 | assert(src != NULL);
|
---|
| 143 |
|
---|
[4f4b4e7] | 144 | for (size_t i = 0; i < n; ++i) {
|
---|
[ef6dd3f] | 145 | dest[i] = src[i];
|
---|
| 146 |
|
---|
| 147 | /* the standard requires that nul characters
|
---|
| 148 | * are appended to the length of n, in case src is shorter
|
---|
| 149 | */
|
---|
| 150 | if (src[i] == '\0') {
|
---|
| 151 | char *result = &dest[i];
|
---|
[4f4b4e7] | 152 | for (++i; i < n; ++i) {
|
---|
[ef6dd3f] | 153 | dest[i] = '\0';
|
---|
| 154 | }
|
---|
| 155 | return result;
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | return &dest[n];
|
---|
[7bb9b53] | 160 | }
|
---|
| 161 |
|
---|
| 162 | /**
|
---|
[ef6dd3f] | 163 | *
|
---|
[7bb9b53] | 164 | * @param dest
|
---|
| 165 | * @param src
|
---|
[4f4b4e7] | 166 | * @return
|
---|
[7bb9b53] | 167 | */
|
---|
[ef6dd3f] | 168 | char *posix_strcat(char *dest, const char *src)
|
---|
[7bb9b53] | 169 | {
|
---|
[ef6dd3f] | 170 | assert(dest != NULL);
|
---|
| 171 | assert(src != NULL);
|
---|
| 172 |
|
---|
[517cedc0] | 173 | posix_strcpy(posix_strchr(dest, '\0'), src);
|
---|
[ef6dd3f] | 174 | return dest;
|
---|
[7bb9b53] | 175 | }
|
---|
| 176 |
|
---|
| 177 | /**
|
---|
[ef6dd3f] | 178 | *
|
---|
| 179 | * @param dest
|
---|
| 180 | * @param src
|
---|
| 181 | * @param n
|
---|
[4f4b4e7] | 182 | * @return
|
---|
[7bb9b53] | 183 | */
|
---|
[ef6dd3f] | 184 | char *posix_strncat(char *dest, const char *src, size_t n)
|
---|
[7bb9b53] | 185 | {
|
---|
[ef6dd3f] | 186 | assert(dest != NULL);
|
---|
| 187 | assert(src != NULL);
|
---|
| 188 |
|
---|
[517cedc0] | 189 | char *zeroptr = posix_strncpy(posix_strchr(dest, '\0'), src, n);
|
---|
[ef6dd3f] | 190 | /* strncpy doesn't append the nul terminator, so we do it here */
|
---|
| 191 | zeroptr[n] = '\0';
|
---|
| 192 | return dest;
|
---|
[7bb9b53] | 193 | }
|
---|
| 194 |
|
---|
| 195 | /**
|
---|
| 196 | *
|
---|
[ef6dd3f] | 197 | * @param dest
|
---|
| 198 | * @param src
|
---|
| 199 | * @param c
|
---|
[7bb9b53] | 200 | * @param n
|
---|
[ef6dd3f] | 201 | * @return Pointer to the first byte after c in dest if found, NULL otherwise.
|
---|
[7bb9b53] | 202 | */
|
---|
[4f4b4e7] | 203 | void *posix_memccpy(void *dest, const void *src, int c, size_t n)
|
---|
[7bb9b53] | 204 | {
|
---|
[ef6dd3f] | 205 | assert(dest != NULL);
|
---|
| 206 | assert(src != NULL);
|
---|
| 207 |
|
---|
| 208 | unsigned char* bdest = dest;
|
---|
| 209 | const unsigned char* bsrc = src;
|
---|
| 210 |
|
---|
[4f4b4e7] | 211 | for (size_t i = 0; i < n; ++i) {
|
---|
[ef6dd3f] | 212 | bdest[i] = bsrc[i];
|
---|
| 213 |
|
---|
| 214 | if (bsrc[i] == (unsigned char) c) {
|
---|
| 215 | /* pointer to the next byte */
|
---|
| 216 | return &bdest[i + 1];
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | return NULL;
|
---|
[7bb9b53] | 221 | }
|
---|
| 222 |
|
---|
| 223 | /**
|
---|
| 224 | *
|
---|
[ef6dd3f] | 225 | * @param s
|
---|
| 226 | * @return Newly allocated string
|
---|
[7bb9b53] | 227 | */
|
---|
[ef6dd3f] | 228 | char *posix_strdup(const char *s)
|
---|
[7bb9b53] | 229 | {
|
---|
[b52ef5a] | 230 | return posix_strndup(s, SIZE_MAX);
|
---|
[7bb9b53] | 231 | }
|
---|
| 232 |
|
---|
| 233 | /**
|
---|
| 234 | *
|
---|
[ef6dd3f] | 235 | * @param s
|
---|
[7bb9b53] | 236 | * @param n
|
---|
[ef6dd3f] | 237 | * @return Newly allocated string of length at most n
|
---|
[7bb9b53] | 238 | */
|
---|
[ef6dd3f] | 239 | char *posix_strndup(const char *s, size_t n)
|
---|
[7bb9b53] | 240 | {
|
---|
[ef6dd3f] | 241 | assert(s != NULL);
|
---|
| 242 |
|
---|
| 243 | size_t len = posix_strnlen(s, n);
|
---|
| 244 | char *dup = malloc(len + 1);
|
---|
| 245 | if (dup == NULL) {
|
---|
| 246 | return NULL;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | memcpy(dup, s, len);
|
---|
| 250 | dup[len] = '\0';
|
---|
| 251 |
|
---|
| 252 | return dup;
|
---|
[7bb9b53] | 253 | }
|
---|
| 254 |
|
---|
| 255 | /**
|
---|
[ef6dd3f] | 256 | *
|
---|
| 257 | * @param mem1
|
---|
| 258 | * @param mem2
|
---|
| 259 | * @param n
|
---|
| 260 | * @return Difference of the first pair of inequal bytes,
|
---|
[4f4b4e7] | 261 | * or 0 if areas have the same content
|
---|
[7bb9b53] | 262 | */
|
---|
[ef6dd3f] | 263 | int posix_memcmp(const void *mem1, const void *mem2, size_t n)
|
---|
[7bb9b53] | 264 | {
|
---|
[ef6dd3f] | 265 | assert(mem1 != NULL);
|
---|
| 266 | assert(mem2 != NULL);
|
---|
| 267 |
|
---|
| 268 | const unsigned char *s1 = mem1;
|
---|
| 269 | const unsigned char *s2 = mem2;
|
---|
| 270 |
|
---|
[4f4b4e7] | 271 | for (size_t i = 0; i < n; ++i) {
|
---|
[ef6dd3f] | 272 | if (s1[i] != s2[i]) {
|
---|
| 273 | return s2[i] - s1[i];
|
---|
| 274 | }
|
---|
| 275 | }
|
---|
| 276 |
|
---|
[7bb9b53] | 277 | return 0;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | /**
|
---|
[ef6dd3f] | 281 | *
|
---|
[7bb9b53] | 282 | * @param s1
|
---|
| 283 | * @param s2
|
---|
| 284 | * @return
|
---|
| 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 | /**
|
---|
| 295 | *
|
---|
[ef6dd3f] | 296 | * @param s1
|
---|
| 297 | * @param s2
|
---|
[7bb9b53] | 298 | * @param n
|
---|
| 299 | * @return
|
---|
| 300 | */
|
---|
[ef6dd3f] | 301 | int posix_strncmp(const char *s1, const char *s2, size_t n)
|
---|
[7bb9b53] | 302 | {
|
---|
[ef6dd3f] | 303 | assert(s1 != NULL);
|
---|
| 304 | assert(s2 != NULL);
|
---|
| 305 |
|
---|
[4f4b4e7] | 306 | for (size_t i = 0; i < n; ++i) {
|
---|
[ef6dd3f] | 307 | if (s1[i] != s2[i]) {
|
---|
| 308 | return s2[i] - s1[i];
|
---|
| 309 | }
|
---|
| 310 | if (s1[i] == '\0') {
|
---|
| 311 | break;
|
---|
| 312 | }
|
---|
| 313 | }
|
---|
| 314 |
|
---|
[7bb9b53] | 315 | return 0;
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | /**
|
---|
[ef6dd3f] | 319 | *
|
---|
[7bb9b53] | 320 | * @param mem
|
---|
| 321 | * @param c
|
---|
[ef6dd3f] | 322 | * @param n
|
---|
[7bb9b53] | 323 | * @return
|
---|
| 324 | */
|
---|
[ef6dd3f] | 325 | void *posix_memchr(const void *mem, int c, size_t n)
|
---|
[7bb9b53] | 326 | {
|
---|
[ef6dd3f] | 327 | assert(mem != NULL);
|
---|
| 328 |
|
---|
| 329 | const unsigned char *s = mem;
|
---|
| 330 |
|
---|
[4f4b4e7] | 331 | for (size_t i = 0; i < n; ++i) {
|
---|
[ef6dd3f] | 332 | if (s[i] == (unsigned char) c) {
|
---|
| 333 | return (void *) &s[i];
|
---|
| 334 | }
|
---|
| 335 | }
|
---|
| 336 | return NULL;
|
---|
[7bb9b53] | 337 | }
|
---|
| 338 |
|
---|
| 339 | /**
|
---|
| 340 | *
|
---|
| 341 | * @param s
|
---|
| 342 | * @param c
|
---|
| 343 | * @return
|
---|
| 344 | */
|
---|
| 345 | char *posix_strchr(const char *s, int c)
|
---|
| 346 | {
|
---|
[ef6dd3f] | 347 | assert(s != NULL);
|
---|
| 348 |
|
---|
[517cedc0] | 349 | char *res = gnu_strchrnul(s, c);
|
---|
| 350 | return (*res == c) ? res : NULL;
|
---|
[7bb9b53] | 351 | }
|
---|
| 352 |
|
---|
| 353 | /**
|
---|
| 354 | *
|
---|
| 355 | * @param s
|
---|
| 356 | * @param c
|
---|
| 357 | * @return
|
---|
| 358 | */
|
---|
| 359 | char *posix_strrchr(const char *s, int c)
|
---|
| 360 | {
|
---|
[ef6dd3f] | 361 | assert(s != NULL);
|
---|
| 362 |
|
---|
[517cedc0] | 363 | const char *ptr = posix_strchr(s, '\0');
|
---|
[ef6dd3f] | 364 |
|
---|
| 365 | /* the same as in strchr, except it loops in reverse direction */
|
---|
| 366 | while (*ptr != (char) c) {
|
---|
[4f4b4e7] | 367 | if (ptr == s) {
|
---|
[ef6dd3f] | 368 | return NULL;
|
---|
[4f4b4e7] | 369 | }
|
---|
[ef6dd3f] | 370 |
|
---|
[4f4b4e7] | 371 | ptr++;
|
---|
[ef6dd3f] | 372 | }
|
---|
| 373 |
|
---|
| 374 | return (char *) ptr;
|
---|
[7bb9b53] | 375 | }
|
---|
| 376 |
|
---|
[517cedc0] | 377 | char *gnu_strchrnul(const char *s, int c)
|
---|
| 378 | {
|
---|
| 379 | assert(s != NULL);
|
---|
| 380 |
|
---|
| 381 | while (*s != c && *s != '\0') {
|
---|
| 382 | s++;
|
---|
| 383 | }
|
---|
| 384 |
|
---|
| 385 | return (char *) s;
|
---|
| 386 | }
|
---|
| 387 |
|
---|
[7bb9b53] | 388 | /**
|
---|
| 389 | *
|
---|
| 390 | * @param s1
|
---|
| 391 | * @param s2
|
---|
| 392 | * @return
|
---|
| 393 | */
|
---|
| 394 | char *posix_strpbrk(const char *s1, const char *s2)
|
---|
| 395 | {
|
---|
[ef6dd3f] | 396 | assert(s1 != NULL);
|
---|
| 397 | assert(s2 != NULL);
|
---|
| 398 |
|
---|
| 399 | char *ptr = strpbrk_null(s1, s2);
|
---|
| 400 | return (*ptr == '\0') ? NULL : ptr;
|
---|
[7bb9b53] | 401 | }
|
---|
| 402 |
|
---|
| 403 | /**
|
---|
| 404 | *
|
---|
| 405 | * @param s1
|
---|
| 406 | * @param s2
|
---|
| 407 | * @return
|
---|
| 408 | */
|
---|
| 409 | size_t posix_strcspn(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 (size_t) (ptr - s1);
|
---|
[7bb9b53] | 416 | }
|
---|
| 417 |
|
---|
| 418 | /**
|
---|
| 419 | *
|
---|
| 420 | * @param s1
|
---|
| 421 | * @param s2
|
---|
| 422 | * @return
|
---|
| 423 | */
|
---|
| 424 | size_t posix_strspn(const char *s1, const char *s2)
|
---|
| 425 | {
|
---|
[ef6dd3f] | 426 | assert(s1 != NULL);
|
---|
| 427 | assert(s2 != NULL);
|
---|
| 428 |
|
---|
| 429 | const char *ptr;
|
---|
[4f4b4e7] | 430 | for (ptr = s1; *ptr != '\0'; ++ptr) {
|
---|
| 431 | if (!posix_strchr(s2, *ptr)) {
|
---|
[ef6dd3f] | 432 | break;
|
---|
[4f4b4e7] | 433 | }
|
---|
[ef6dd3f] | 434 | }
|
---|
| 435 | return ptr - s1;
|
---|
[7bb9b53] | 436 | }
|
---|
| 437 |
|
---|
| 438 | /**
|
---|
| 439 | *
|
---|
| 440 | * @param s1
|
---|
| 441 | * @param s2
|
---|
| 442 | * @return
|
---|
| 443 | */
|
---|
| 444 | char *posix_strstr(const char *s1, const char *s2)
|
---|
| 445 | {
|
---|
[ef6dd3f] | 446 | assert(s1 != NULL);
|
---|
| 447 | assert(s2 != NULL);
|
---|
| 448 |
|
---|
| 449 | /* special case - needle is an empty string */
|
---|
[4f4b4e7] | 450 | if (*s2 == '\0') {
|
---|
[ef6dd3f] | 451 | return (char *) s1;
|
---|
[4f4b4e7] | 452 | }
|
---|
[ef6dd3f] | 453 |
|
---|
| 454 | // TODO: use faster algorithm
|
---|
| 455 | /* check for prefix from every position - quadratic complexity */
|
---|
| 456 | while (*s1 != '\0') {
|
---|
[4f4b4e7] | 457 | if (begins_with(s1, s2)) {
|
---|
[ef6dd3f] | 458 | return (char *) s1;
|
---|
[4f4b4e7] | 459 | }
|
---|
[ef6dd3f] | 460 |
|
---|
[4f4b4e7] | 461 | s1++;
|
---|
[ef6dd3f] | 462 | }
|
---|
| 463 |
|
---|
| 464 | return NULL;
|
---|
[7bb9b53] | 465 | }
|
---|
| 466 |
|
---|
| 467 | /**
|
---|
[ef6dd3f] | 468 | * Currently ignores locale and just calls strcmp.
|
---|
[7bb9b53] | 469 | *
|
---|
| 470 | * @param s1
|
---|
| 471 | * @param s2
|
---|
| 472 | * @return
|
---|
| 473 | */
|
---|
| 474 | int posix_strcoll(const char *s1, const char *s2)
|
---|
| 475 | {
|
---|
[ef6dd3f] | 476 | assert(s1 != NULL);
|
---|
| 477 | assert(s2 != NULL);
|
---|
| 478 |
|
---|
| 479 | return posix_strcmp(s1, s2);
|
---|
[7bb9b53] | 480 | }
|
---|
| 481 |
|
---|
| 482 | /**
|
---|
[ef6dd3f] | 483 | * strcoll is equal to strcmp here, so this just makes a copy.
|
---|
[7bb9b53] | 484 | *
|
---|
| 485 | * @param s1
|
---|
| 486 | * @param s2
|
---|
| 487 | * @param n
|
---|
| 488 | * @return
|
---|
| 489 | */
|
---|
| 490 | size_t posix_strxfrm(char *s1, const char *s2, size_t n)
|
---|
| 491 | {
|
---|
[ef6dd3f] | 492 | assert(s1 != NULL || n == 0);
|
---|
| 493 | assert(s2 != NULL);
|
---|
| 494 |
|
---|
| 495 | size_t len = posix_strlen(s2);
|
---|
| 496 |
|
---|
[4f4b4e7] | 497 | if (n > len) {
|
---|
[ef6dd3f] | 498 | posix_strcpy(s1, s2);
|
---|
[4f4b4e7] | 499 | }
|
---|
[ef6dd3f] | 500 |
|
---|
| 501 | return len;
|
---|
[7bb9b53] | 502 | }
|
---|
| 503 |
|
---|
| 504 | /**
|
---|
| 505 | *
|
---|
| 506 | * @param errnum
|
---|
| 507 | * @return
|
---|
| 508 | */
|
---|
| 509 | char *posix_strerror(int errnum)
|
---|
| 510 | {
|
---|
[ef6dd3f] | 511 | /* uses function from libc, we just have to negate errno
|
---|
[4f4b4e7] | 512 | * (POSIX uses positive errorcodes, HelenOS has negative)
|
---|
| 513 | */
|
---|
| 514 | return (char *) str_error(-errnum);
|
---|
[ef6dd3f] | 515 | }
|
---|
| 516 |
|
---|
| 517 | /**
|
---|
| 518 | *
|
---|
| 519 | * @param errnum Error code
|
---|
[4f4b4e7] | 520 | * @param buf Buffer to store a human readable string to
|
---|
| 521 | * @param bufsz Size of buffer pointed to by buf
|
---|
[ef6dd3f] | 522 | * @return
|
---|
| 523 | */
|
---|
| 524 | int posix_strerror_r(int errnum, char *buf, size_t bufsz)
|
---|
| 525 | {
|
---|
| 526 | assert(buf != NULL);
|
---|
| 527 |
|
---|
| 528 | char *errstr = posix_strerror(errnum);
|
---|
| 529 | /* HelenOS str_error can't fail */
|
---|
| 530 |
|
---|
| 531 | if (posix_strlen(errstr) + 1 > bufsz) {
|
---|
| 532 | return -ERANGE;
|
---|
| 533 | } else {
|
---|
| 534 | posix_strcpy(buf, errstr);
|
---|
| 535 | }
|
---|
| 536 |
|
---|
[7bb9b53] | 537 | return 0;
|
---|
| 538 | }
|
---|
| 539 |
|
---|
| 540 | /**
|
---|
| 541 | *
|
---|
| 542 | * @param s
|
---|
| 543 | * @return
|
---|
| 544 | */
|
---|
| 545 | size_t posix_strlen(const char *s)
|
---|
| 546 | {
|
---|
[ef6dd3f] | 547 | assert(s != NULL);
|
---|
| 548 |
|
---|
[517cedc0] | 549 | return (size_t) (posix_strchr(s, '\0') - s);
|
---|
[ef6dd3f] | 550 | }
|
---|
| 551 |
|
---|
| 552 | /**
|
---|
| 553 | *
|
---|
| 554 | * @param s
|
---|
| 555 | * @param n
|
---|
| 556 | * @return
|
---|
| 557 | */
|
---|
| 558 | size_t posix_strnlen(const char *s, size_t n)
|
---|
| 559 | {
|
---|
| 560 | assert(s != NULL);
|
---|
| 561 |
|
---|
[4f4b4e7] | 562 | for (size_t sz = 0; sz < n; ++sz) {
|
---|
[ef6dd3f] | 563 |
|
---|
| 564 | if (s[sz] == '\0') {
|
---|
| 565 | return sz;
|
---|
| 566 | }
|
---|
| 567 | }
|
---|
| 568 |
|
---|
| 569 | return n;
|
---|
[7bb9b53] | 570 | }
|
---|
| 571 |
|
---|
| 572 | /** @}
|
---|
| 573 | */
|
---|