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
26 #define HAVE_BASENAME 0
27 #define HAVE_DIRNAME 1
30 path_addsep(char *path, size_t size)
32 int len = strlen(path);
34 if (len > 0 && path[len-1] != '/' && len + 1 < size) {
52 if (len == 1 && path[0] == '/')
54 if (path[len-1] == '/')
57 for (p1 = p2 = path; *p1 != '\0'; p1++) {
65 #endif /* !HAVE_BASENAME */
73 static char current[] = ".";
78 if (len == 1 && path[0] == '/')
80 if (path[len-1] == '/')
83 for (p1 = p2 = path; *p1 != '\0'; p1++) {
88 if (*p2 == '/' && p2 == path)
97 #endif /* !HAVE_DIRNAME */
100 makepath(char *dest, int dest_size,
101 char *dir, char *file, char *ext)
110 /* copy directory part */
112 len = string_copy(p, dir, size);
115 if (len > 0 && size > 0) {
116 if (p[len-1] != '/') {
124 /* copy filename part */
126 len = string_copy(p, file, size);
131 /* copy suffix part */
133 if (ext[0] != '.' && size > 0) {
138 len = string_copy(p, ext, size);
143 return p - dest; /* result string length */
149 #define basename glibc2_basename
150 #include <string.h> /* strdup() */
155 split_path(char *path)
157 printf("\"%s\"\t= \"%s\" \"%s\"\n",
159 dirname(strdup(path)),
160 basename(strdup(path)));
168 split_path("/foo/bar");
171 split_path("bar/"); /* should print "." and "bar" */
172 split_path("/"); /* should print "/" and "/" */
173 split_path(""); /* should print "." and "" */
176 len = makepath(work, sizeof work, "foo", "bar", "baz");
177 assert(strcmp(work, "foo/bar.baz") == 0 && len == 11);
178 printf("\"%s\"\n", work);
180 len = makepath(work, sizeof work, "foo/", "bar", ".baz");
181 assert(strcmp(work, "foo/bar.baz") == 0 && len == 11);
182 printf("\"%s\"\n", work);
184 len = makepath(work, strlen("foo/bar.baz")+1, "foo", "bar", "baz");
185 assert(strcmp(work, "foo/bar.baz") == 0 && len == 11);
186 printf("\"%s\"\n", work);
188 len = makepath(work, strlen("foo/bar.baz")+1, "foo", "bar.baz", NULL);
189 assert(strcmp(work, "foo/bar.baz") == 0 && len == 11);
190 printf("\"%s\"\n", work);
192 memset(work, '*', sizeof(work)-1);
193 work[sizeof(work)-1] = 0;
194 len = makepath(work+10, sizeof(work)-10, "foo", "bar", "baz");
195 assert(strcmp(work+10, "foo/bar.baz") == 0 && len == 11);
196 printf("\"%s\"\n", work);
197 memset(work+10, '*', strlen("foo/bar.baz")+1);
200 for (i = 0; i < sizeof(work)-1; i++)
201 assert(work[i] == '*');
203 printf("\"%s\"\n", work);
205 len = makepath(work, strlen("foo/bar.baz"), "foo", "bar", "baz");
206 assert(strcmp(work, "foo/bar.ba") == 0 && len == 10);
207 printf("\"%s\"\n", work);