source: mainline/uspace/lib/posix/string.c@ ec18957a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ec18957a was ec18957a, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 14 years ago

Added errno.h (see commentary inside).

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