2 Copyright (c) 2002 Koji Arai
4 Permission is hereby granted, free of charge, to any person
5 obtaining a copy of this software and associated documentation files
6 (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge,
8 publish, distribute, sublicense, and/or sell copies of the Software,
9 and to permit persons to whom the Software is furnished to do so,
10 subject to the following conditions:
12 The above copyright notice and this permission notice shall be
13 included in all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 path_addsep(char *path, size_t size)
30 int len = strlen(path);
32 if (len > 0 && path[len-1] != '/' && len + 1 < size) {
49 if (len == 1 && path[0] == '/')
51 if (path[len-1] == '/')
54 for (p1 = p2 = path; *p1 != '\0'; p1++) {
74 if (len == 1 && path[0] == '/')
76 if (path[len-1] == '/')
79 for (p1 = p2 = path; *p1 != '\0'; p1++) {
84 if (*p2 == '/' && p2 == path)
88 if (path[0] == '\0') {
96 makepath(char *dest, int dest_size,
97 char *dir, char *file, char *ext)
106 /* copy directory part */
108 len = string_copy(p, dir, size);
111 if (len > 0 && size > 0) {
112 if (p[len-1] != '/') {
120 /* copy filename part */
122 len = string_copy(p, file, size);
127 /* copy suffix part */
129 if (ext[0] != '.' && size > 0) {
134 len = string_copy(p, ext, size);
139 return p - dest; /* result string length */
142 #include <sys/stat.h>
143 #include <sys/types.h>
147 mkdir_p(char *dir, mode_t mode)
151 if (strcmp(dir, "/") == 0)
154 if (mkdir(dir, mode) == -1) {
155 if (errno == ENOENT) {
156 char *parent = dirname(strdup(dir));
158 ret = mkdir_p(parent, mode);
165 return mkdir(dir, mode);
170 mkdir_parent(char *file)
175 parent = dirname(strdup(file));
176 ret = mkdir_p(parent, 0777);
185 #define basename glibc2_basename
186 #include <string.h> /* strdup() */
191 split_path(char *path)
193 printf("\"%s\"\t= \"%s\" \"%s\"\n",
195 dirname(strdup(path)),
196 basename(strdup(path)));
204 split_path("/foo/bar");
207 split_path("bar/"); /* should print "." and "bar" */
208 split_path("/"); /* should print "/" and "/" */
209 split_path(""); /* should print "." and "" */
212 len = makepath(work, sizeof work, "foo", "bar", "baz");
213 assert(strcmp(work, "foo/bar.baz") == 0 && len == 11);
214 printf("\"%s\"\n", work);
216 len = makepath(work, sizeof work, "foo/", "bar", ".baz");
217 assert(strcmp(work, "foo/bar.baz") == 0 && len == 11);
218 printf("\"%s\"\n", work);
220 len = makepath(work, strlen("foo/bar.baz")+1, "foo", "bar", "baz");
221 assert(strcmp(work, "foo/bar.baz") == 0 && len == 11);
222 printf("\"%s\"\n", work);
224 len = makepath(work, strlen("foo/bar.baz")+1, "foo", "bar.baz", NULL);
225 assert(strcmp(work, "foo/bar.baz") == 0 && len == 11);
226 printf("\"%s\"\n", work);
228 memset(work, '*', sizeof(work)-1);
229 work[sizeof(work)-1] = 0;
230 len = makepath(work+10, sizeof(work)-10, "foo", "bar", "baz");
231 assert(strcmp(work+10, "foo/bar.baz") == 0 && len == 11);
232 printf("\"%s\"\n", work);
233 memset(work+10, '*', strlen("foo/bar.baz")+1);
236 for (i = 0; i < sizeof(work)-1; i++)
237 assert(work[i] == '*');
239 printf("\"%s\"\n", work);
241 len = makepath(work, strlen("foo/bar.baz"), "foo", "bar", "baz");
242 assert(strcmp(work, "foo/bar.ba") == 0 && len == 10);
243 printf("\"%s\"\n", work);