-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsafe_dirname.c
More file actions
138 lines (122 loc) · 2.97 KB
/
safe_dirname.c
File metadata and controls
138 lines (122 loc) · 2.97 KB
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
/*
* A thread safe implementation of dirname() and basename() using thread specific data
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <limits.h>
#include <string.h>
static pthread_once_t once = PTHREAD_ONCE_INIT;
static pthread_key_t basename_key;
static pthread_key_t dirname_key;
static void err(char *reason) {
puts(reason);
exit(EXIT_FAILURE);
}
static void initkeys() {
if (pthread_key_create(&basename_key, free) != 0)
err("pthread_key_create");
if (pthread_key_create(&dirname_key, free) != 0)
err("pthread_key_create");
}
char *basename(const char *path) {
char const *sp, *ep;
if (pthread_once(&once, initkeys) != 0)
err("pthread_once");
char *buf = (char*)pthread_getspecific(basename_key);
if (buf == NULL) {
buf = malloc(PATH_MAX + 1);
if (buf == NULL)
err("malloc");
if (pthread_setspecific(basename_key, (void*)buf) != 0)
err("pthread_setspecific");
}
if (path == NULL || *path == '\0') {
buf[0] = '.';
buf[1] = '\0';
} else {
// skip trailing /
for (ep = (path + strnlen(path, PATH_MAX + 1)) - 1; ep >= path && *ep =='/';
ep--)
;
// It was all /.
if (ep < path) {
buf[0] = '/';
buf[1] = '\0';
} else {
for (sp = ep; sp > path && *(sp - 1) != '/'; sp--)
;
memcpy(buf, sp, (ep - sp) + 1);
buf[(ep - sp) + 2] = '\0';
}
}
return buf;
}
char *dirname(const char *path) {
char const *ep;
int slash;
if (pthread_once(&once, initkeys) != 0) {
puts("pthread_once");
exit(EXIT_FAILURE);
}
char *buf = (char*)pthread_getspecific(dirname_key);
if (buf == NULL) {
buf = malloc(PATH_MAX + 1);
if (buf == NULL)
err("malloc");
if (pthread_setspecific(basename_key, (void*)buf) != 0)
err("pthread_setspecific");
}
/*
Being a bit lazy with this impl! That spagheti logic down there works but I've not
bothered thinking too hard about it! Could be edge cases which aren't correct.
*/
if (path == NULL || *path == '\0') {
buf[0] = '.';
buf[1] = '\0';
} else {
// Skip any slashes
slash = 0;
for (ep = (path + strnlen(path, PATH_MAX + 1)) - 1; *ep == '/' && ep >= path;
ep--)
slash = 1;
// There were no trailing slashes, so continue back to prev.
if (!slash) {
for (; *(ep + 1) != '/' && ep >= path; ep--)
;
}
if ((ep - path) + 1 == 0) {
if (*path == '/')
buf[0] = '/';
else
buf[0] = '.';
buf[1] = '\0';
} else {
memcpy(buf, path, (ep - path) + 1);
buf[(ep - path) + 2] = '\0';
}
}
return buf;
}
/** Testing **/
static void *prinfo(void *arg) {
char *bnam = basename(arg);
char *dirnam = dirname(arg);
sleep(1);
printf("(%u)basename: %s\n", (unsigned)pthread_self(), bnam);
printf("(%u)dirname: %s\n", (unsigned)pthread_self(), dirnam);
return (void*)0;
}
int main(int argc, char **argv) {
int i;
pthread_t p;
if (argc < 2) {
printf("Usage: %s <path> [<path>]...\n", argv[0]);
exit(EXIT_FAILURE);
}
for (i = 1; i < argc; i++) {
pthread_create(&p, NULL, prinfo, argv[i]);
}
pthread_exit((void*)0);
}