/[path-sandbox]/trunk/libsbutil/src/string.c
Gentoo

Contents of /trunk/libsbutil/src/string.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 370 - (show annotations) (download) (as text)
Sun Nov 9 10:29:58 2008 UTC (5 years, 10 months ago) by vapier
File MIME type: text/x-csrc
File size: 1898 byte(s)
consolidate all random system includes into headers.h
1 /*
2 * string.c
3 *
4 * Miscellaneous macro's and functions.
5 *
6 * Copyright 1999-2008 Gentoo Foundation
7 * Copyright 2004-2007 Martin Schlemmer <azarah@nosferatu.za.org>
8 * Licensed under the GPL-2
9 */
10
11 #include "headers.h"
12 #include "rcscripts/rcutil.h"
13
14 char *
15 rc_memrepchr (char **str, char old, char new, size_t size)
16 {
17 char *str_p;
18
19 if (!check_arg_strv (str))
20 return NULL;
21
22 str_p = memchr (*str, old, size);
23
24 while (NULL != str_p)
25 {
26 str_p[0] = new;
27 str_p = memchr (&str_p[1], old, size - (str_p - *str) - 1);
28 }
29
30 return *str;
31 }
32
33 char *
34 rc_strcatpaths (const char *pathname1, const char *pathname2)
35 {
36 char *new_path = NULL;
37 int lenght;
38
39 if ((!check_arg_str (pathname1)) || (!check_arg_str (pathname2)))
40 return 0;
41
42 /* Lenght of pathname1 + lenght of pathname2 + '/' if needed */
43 lenght = strlen (pathname1) + strlen (pathname2) + 2;
44 /* lenght + '\0' */
45 new_path = xmalloc (lenght);
46 if (NULL == new_path)
47 return NULL;
48
49 snprintf (new_path, lenght, "%s%s%s", pathname1,
50 (pathname1[strlen (pathname1) - 1] != '/') ? "/" : "",
51 pathname2);
52
53 return new_path;
54 }
55
56 char *
57 rc_strndup (const char *str, size_t size)
58 {
59 char *new_str = NULL;
60 size_t len;
61
62 /* We cannot check if its a valid string here, as it might
63 * not be '\0' terminated ... */
64 if (!check_arg_ptr (str))
65 return NULL;
66
67 /* Check lenght of str without breaching the size limit */
68 for (len = 0; (len < size) && ('\0' != str[len]); len++);
69
70 new_str = xmalloc (len + 1);
71 if (NULL == new_str)
72 return NULL;
73
74 /* Make sure our string is NULL terminated */
75 new_str[len] = '\0';
76
77 return (char *) memcpy (new_str, str, len);
78 }
79
80 char *
81 rc_basename (const char *path)
82 {
83 char *new_path = NULL;
84
85 if (!check_arg_str (path))
86 return NULL;
87
88 /* Copied from glibc */
89 new_path = strrchr (path, '/');
90 return new_path ? new_path + 1 : (char *) path;
91 }

  ViewVC Help
Powered by ViewVC 1.1.20