]>
Commit | Line | Data |
---|---|---|
27d9c25e AM |
1 | /* |
2 | * util-common.c | |
3 | * | |
b2ea5f68 | 4 | * Copyright (c) 2006-2013 Pacman Development Team <pacman-dev@archlinux.org> |
27d9c25e AM |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <stdlib.h> | |
21 | #include <string.h> | |
22 | ||
23 | #include "util-common.h" | |
24 | ||
25 | ||
26 | /** Parse the basename of a program from a path. | |
27 | * @param path path to parse basename from | |
28 | * | |
29 | * @return everything following the final '/' | |
30 | */ | |
31 | const char *mbasename(const char *path) | |
32 | { | |
33 | const char *last = strrchr(path, '/'); | |
34 | if(last) { | |
35 | return last + 1; | |
36 | } | |
37 | return path; | |
38 | } | |
39 | ||
40 | /** Parse the dirname of a program from a path. | |
41 | * The path returned should be freed. | |
42 | * @param path path to parse dirname from | |
43 | * | |
44 | * @return everything preceding the final '/' | |
45 | */ | |
46 | char *mdirname(const char *path) | |
47 | { | |
48 | char *ret, *last; | |
49 | ||
50 | /* null or empty path */ | |
9020bcee | 51 | if(path == NULL || *path == '\0') { |
27d9c25e AM |
52 | return strdup("."); |
53 | } | |
54 | ||
55 | if((ret = strdup(path)) == NULL) { | |
56 | return NULL; | |
57 | } | |
58 | ||
59 | last = strrchr(ret, '/'); | |
60 | ||
61 | if(last != NULL) { | |
62 | /* we found a '/', so terminate our string */ | |
63 | if(last == ret) { | |
64 | /* return "/" for root */ | |
65 | last++; | |
66 | } | |
67 | *last = '\0'; | |
68 | return ret; | |
69 | } | |
70 | ||
71 | /* no slash found */ | |
72 | free(ret); | |
73 | return strdup("."); | |
74 | } | |
75 | ||
76 | #ifndef HAVE_STRNDUP | |
77 | /* A quick and dirty implementation derived from glibc */ | |
78 | /** Determines the length of a fixed-size string. | |
79 | * @param s string to be measured | |
80 | * @param max maximum number of characters to search for the string end | |
81 | * @return length of s or max, whichever is smaller | |
82 | */ | |
83 | static size_t strnlen(const char *s, size_t max) | |
84 | { | |
85 | register const char *p; | |
86 | for(p = s; *p && max--; ++p); | |
87 | return (p - s); | |
88 | } | |
89 | ||
90 | /** Copies a string. | |
91 | * Returned string needs to be freed | |
92 | * @param s string to be copied | |
93 | * @param n maximum number of characters to copy | |
94 | * @return pointer to the new string on success, NULL on error | |
95 | */ | |
96 | char *strndup(const char *s, size_t n) | |
97 | { | |
98 | size_t len = strnlen(s, n); | |
99 | char *new = (char *) malloc(len + 1); | |
100 | ||
101 | if(new == NULL) | |
102 | return NULL; | |
103 | ||
104 | new[len] = '\0'; | |
105 | return (char *)memcpy(new, s, len); | |
106 | } | |
107 | #endif | |
108 | ||
109 | /* vim: set ts=2 sw=2 noet: */ |