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