4 * Cross-platform basename/dirname
6 * Copyright 2005 Syd Logan, All Rights Reserved
8 * This code is distributed without warranty. You are free to use this
9 * code for any purpose, however, if this code is republished or
10 * redistributed in its original form, as hardcopy or electronically,
11 * then you must include this copyright notice along with the code.
15 // minor changes 2008 by Stefan Ludewig stefan.ludewig@exitgames.com for WIN32 version RRDtool
19 #include "plbasename.h"
25 #if defined(__cplusplus)
32 PL_basename(const char *name)
36 static char *tmp = NULL;
44 if (!name || !strcmp(name, ""))
47 if (!strcmp(name, "/"))
51 if (name[len - 1] == '/') {
52 // ditch the trailing '/'
53 p = tmp = (char*)malloc(len);
54 strncpy(p, name, len - 1);
59 for (base = p; *p; p++)
67 PL_dirname(const char *name)
69 static char *ret = NULL;
79 if (!name || !strcmp(name, "") || !strstr(name, "/"))
82 if (!strcmp(name, "/"))
85 // find the last slash in the string
90 if (*p == '/') p--; // skip the trailing /
92 while (p != name && *p != '/') p--;
96 ret = (char*)malloc(size + 1);
97 memcpy(ret, name, size);
104 return (const char *) ret;
107 #if defined(__cplusplus)
116 main(int argc, char *argv[])
118 /* run the following tests:
120 path dirname basename
121 "/usr/lib" "/usr" "lib"
131 These results can be verified by running the unix commands
132 basename(1) and dirname(1). One tweek to the test strategy
133 used here would be, on darwin and linux, to shell out to
134 get the expected results vs hardcoding.
136 if (!strcmp(PL_basename("/usr/lib"), "lib"))
137 printf("PL_basename /usr/lib passed\n");
139 printf("PL_basename /usr/lib failed expected lib\n");
140 if (!strcmp(PL_dirname("/usr/lib"), "/usr"))
141 printf("PL_dirname /usr/lib passed\n");
143 printf("PL_dirname /usr/lib failed expected /usr\n");
144 if (!strcmp(PL_basename("/usr/"), "usr"))
145 printf("PL_basename /usr/ passed\n");
147 printf("PL_basename /usr/ failed expected usr\n");
148 if (!strcmp(PL_dirname("/usr/"), "/"))
149 printf("PL_dirname /usr/ passed\n");
151 printf("PL_dirname /usr/ failed expected /\n");
152 if (!strcmp(PL_basename("usr"), "usr"))
153 printf("PL_basename usr passed\n");
155 printf("PL_basename usr failed expected usr\n");
156 if (!strcmp(PL_dirname("usr"), "."))
157 printf("PL_dirname usr passed\n");
159 printf("PL_dirname usr failed expected .\n");
160 if (!strcmp(PL_basename("/"), "/"))
161 printf("PL_basename / passed\n");
163 printf("PL_basename / failed expected /\n");
164 if (!strcmp(PL_dirname("/"), "/"))
165 printf("PL_dirname / passed\n");
167 printf("PL_dirname / failed expected /\n");
168 if (!strcmp(PL_basename("."), "."))
169 printf("PL_basename . passed\n");
171 printf("PL_basename . failed\n");
172 if (!strcmp(PL_dirname("."), "."))
173 printf("PL_dirname . passed\n");
175 printf("PL_dirname . failed expected .\n");
176 if (!strcmp(PL_basename(".."), ".."))
177 printf("PL_basename .. passed\n");
179 printf("PL_basename .. failed expected ..\n");
180 if (!strcmp(PL_dirname(".."), "."))
181 printf("PL_dirname .. passed\n");
183 printf("PL_dirname .. failed expected .\n");
184 if (!strcmp(PL_basename(NULL), ""))
185 printf("PL_basename NULL passed\n");
187 printf("PL_basename NULL failed expected \"\"\n");
188 if (!strcmp(PL_dirname(NULL), "."))
189 printf("PL_dirname NULL passed\n");
191 printf("PL_dirname NULL failed expected .\n");
192 if (!strcmp(PL_basename(""), ""))
193 printf("PL_basename \"\" passed\n");
195 printf("PL_basename \"\" failed expected \"\"\n");
196 if (!strcmp(PL_dirname(""), "."))
197 printf("PL_dirname \"\" passed\n");
199 printf("PL_dirname \"\" failed expected .\n");
201 if (!strcmp(PL_basename("./.."), ".."))
202 printf("PL_basename ./.. passed\n");
204 printf("PL_basename ./.. failed expected ..\n");
205 if (!strcmp(PL_dirname("./.."), "."))
206 printf("PL_dirname ./.. passed\n");
208 printf("PL_dirname ./.. failed expected .\n");