1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
#ifdef _WIN32
/*
*
* Cross-platform basename/dirname
*
* Copyright 2005 Syd Logan, All Rights Reserved
*
* This code is distributed without warranty. You are free to use this
* code for any purpose, however, if this code is republished or
* redistributed in its original form, as hardcopy or electronically,
* then you must include this copyright notice along with the code.
*
*/
// minor changes 2008 by Stefan Ludewig stefan.ludewig@exitgames.com for WIN32 version RRDtool
#include <memory.h>
#include <stdlib.h>
#include "plbasename.h"
#include <string.h>
#if defined(TEST)
#include <stdio.h>
#endif
#if defined(__cplusplus)
extern "C" {
#endif
const char *
PL_basename(const char *name)
{
const char *base;
char *p;
static char *tmp = NULL;
int len;
if (tmp) {
free(tmp);
tmp = NULL;
}
if (!name || !strcmp(name, ""))
return "";
if (!strcmp(name, "/"))
return "/";
len = strlen(name);
if (name[len - 1] == '/') {
// ditch the trailing '/'
p = tmp = (char*)malloc(len);
strncpy(p, name, len - 1);
} else {
p = (char *) name;
}
for (base = p; *p; p++)
if (*p == '/')
base = p + 1;
return base;
}
const char *
PL_dirname(const char *name)
{
static char *ret = NULL;
int len;
int size = 0;
const char *p;
if (ret) {
free(ret);
ret = NULL;
}
if (!name || !strcmp(name, "") || !strstr(name, "/"))
return(".");
if (!strcmp(name, "/"))
return(name);
// find the last slash in the string
len = strlen(name);
p = &name[len - 1];
if (*p == '/') p--; // skip the trailing /
while (p != name && *p != '/') p--;
size = p - name;
if (size) {
ret = (char*)malloc(size + 1);
memcpy(ret, name, size);
ret[size] = '\0';
} else if (*p == '/')
return "/";
else
return "";
return (const char *) ret;
}
#if defined(__cplusplus)
}
#endif
#if defined(TEST)
int
main(int argc, char *argv[])
{
/* run the following tests:
path dirname basename
"/usr/lib" "/usr" "lib"
"/usr/" "/" "usr"
"usr" "." "usr"
"/" "/" "/"
"." "." "."
".." "." ".."
NULL "." ""
"" "." ""
"./.." "." ".."
These results can be verified by running the unix commands
basename(1) and dirname(1). One tweak to the test strategy
used here would be, on darwin and linux, to shell out to
get the expected results vs hardcoding.
*/
if (!strcmp(PL_basename("/usr/lib"), "lib"))
printf("PL_basename /usr/lib passed\n");
else
printf("PL_basename /usr/lib failed expected lib\n");
if (!strcmp(PL_dirname("/usr/lib"), "/usr"))
printf("PL_dirname /usr/lib passed\n");
else
printf("PL_dirname /usr/lib failed expected /usr\n");
if (!strcmp(PL_basename("/usr/"), "usr"))
printf("PL_basename /usr/ passed\n");
else
printf("PL_basename /usr/ failed expected usr\n");
if (!strcmp(PL_dirname("/usr/"), "/"))
printf("PL_dirname /usr/ passed\n");
else
printf("PL_dirname /usr/ failed expected /\n");
if (!strcmp(PL_basename("usr"), "usr"))
printf("PL_basename usr passed\n");
else
printf("PL_basename usr failed expected usr\n");
if (!strcmp(PL_dirname("usr"), "."))
printf("PL_dirname usr passed\n");
else
printf("PL_dirname usr failed expected .\n");
if (!strcmp(PL_basename("/"), "/"))
printf("PL_basename / passed\n");
else
printf("PL_basename / failed expected /\n");
if (!strcmp(PL_dirname("/"), "/"))
printf("PL_dirname / passed\n");
else
printf("PL_dirname / failed expected /\n");
if (!strcmp(PL_basename("."), "."))
printf("PL_basename . passed\n");
else
printf("PL_basename . failed\n");
if (!strcmp(PL_dirname("."), "."))
printf("PL_dirname . passed\n");
else
printf("PL_dirname . failed expected .\n");
if (!strcmp(PL_basename(".."), ".."))
printf("PL_basename .. passed\n");
else
printf("PL_basename .. failed expected ..\n");
if (!strcmp(PL_dirname(".."), "."))
printf("PL_dirname .. passed\n");
else
printf("PL_dirname .. failed expected .\n");
if (!strcmp(PL_basename(NULL), ""))
printf("PL_basename NULL passed\n");
else
printf("PL_basename NULL failed expected \"\"\n");
if (!strcmp(PL_dirname(NULL), "."))
printf("PL_dirname NULL passed\n");
else
printf("PL_dirname NULL failed expected .\n");
if (!strcmp(PL_basename(""), ""))
printf("PL_basename \"\" passed\n");
else
printf("PL_basename \"\" failed expected \"\"\n");
if (!strcmp(PL_dirname(""), "."))
printf("PL_dirname \"\" passed\n");
else
printf("PL_dirname \"\" failed expected .\n");
if (!strcmp(PL_basename("./.."), ".."))
printf("PL_basename ./.. passed\n");
else
printf("PL_basename ./.. failed expected ..\n");
if (!strcmp(PL_dirname("./.."), "."))
printf("PL_dirname ./.. passed\n");
else
printf("PL_dirname ./.. failed expected .\n");
}
#endif
#endif // WIN32
|