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
|
#include "fs.h"
#include "../log.h"
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ftw.h>
#include <string.h>
#include <unistd.h>
bool is_dir(const char *restrict pathname) {
#ifdef STAT_MACROS_BROKEN
#error stat() macros are broken on this system. Cannot compile is_dir().
#endif
struct stat s;
return (0 == stat(pathname, &s) && S_ISDIR(s.st_mode));
}
const char *myhome() {
static char home[PATH_MAX+1];
home[0] = '\0';
if (!getenv("HOME")) {
(void)strncpy(home, getenv("HOME"), PATH_MAX);
} else { /* HOME unset or empty */
(void)strncpy(home, getpwuid(getuid())->pw_dir, PATH_MAX);
}
home[PATH_MAX] = '\0';
return (const char *)&home;
}
int creatfile(const char *path, mode_t mode) {
int fd = creat(path, mode);
if (0 < fd) {
(void)close(fd);
fd = 0;
}
return fd;
}
#if HAVE_MKDTEMP && !HAVE_DECL_MKDTEMP
extern char *mkdtemp(char *);
#endif
#if HAVE_MKTEMP && !HAVE_DECL_MKTEMP
extern char *mktemp(char *);
#endif
#if HAVE_TEMPNAM && !HAVE_DECL_TEMPNAM
extern char *tempnam(const char *, const char *);
#endif
#ifndef P_tmpdir
/* fallback */
#define P_tmpdir "/tmp"
#endif
char *mktempdir(char *dest) {
static const char *pfx = "tmp";
for (size_t try = 1; try < TMP_MAX; ++try) {
#if HAVE_MKDTEMP || HAVE_MKTEMP
if (PATH_MAX < snprintf(
dest, PATH_MAX+1, "%s/%sXXXXXX",
(getenv("TMPDIR") ? getenv("TMPDIR") : P_tmpdir),
pfx)) {
goto fail;
}
#if HAVE_MKDTEMP
if (NULL != mkdtemp(dest)) {
return dest;
}
#elif HAVE_MKTEMP
if (0 < strlen(mktemp(dest))) {
if (0 > mkdir(dest, S_IRWXU)) {
continue;
}
return dest;
} else if (errno = EINVAL) {
goto fail;
}
#endif /* HAVE_MKTEMP */
#elif HAVE_TEMPNAM
char *name = tempnam(NULL, pfx);
if (NULL == name) {
continue;
}
dest[PATH_MAX] = '\0';
(void)strncpy(dest, name, PATH_MAX);
free(name);
if (dest[PATH_MAX]) {
continue;
}
if (0 == mkdir(dest, S_IRWXU)) {
return dest;
}
#else
#error no implementation to create temporary directories
#endif
}
goto fail; /* hack around error: label defined but not used */
fail:
sk_error("mktempdir failed: %s", dest);
return NULL;
}
static int __rmtree_fn(
const char *fpath, const struct stat *sb, int tflag, struct FTW *ftwbuf) {
sk_trace("removing %s", fpath);
if (-1 == remove(fpath)) {
sk_error("failed to remove %s: %s", fpath, strerror(errno));
}
return 0 /* FTW_CONTINUE */;
}
int rmtree(const char *dir) {
return nftw(dir, __rmtree_fn, 16, FTW_DEPTH | FTW_PHYS);
}
void which(
const char *restrict name, char *restrict resolved_path,
const char *restrict search_path) {
char *path = NULL;
/* check if name is a path */
if (realpath(name, resolved_path)
&& access(resolved_path, X_OK)) {
return;
}
resolved_path[0] = '\0';
if (NULL == search_path) {
/* use PATH from environment */
search_path = getenv("PATH");
}
if (NULL != search_path) {
path = strdup(search_path);
}
#ifdef _CS_PATH
if (NULL == search_path) {
/* fall back to POSIX path */
size_t pathlen = confstr(_CS_PATH, NULL, 0);
path = malloc(pathlen);
if (NULL != path) {
(void)confstr(_CS_PATH, path, pathlen);
}
}
#endif
if (NULL == path) {
/* cannot determine PATH */
return;
}
char *saveptr = NULL;
char *path_tok = strtok_r(path, stringize(PATHSEP), &saveptr);
do {
if (PATH_MAX < pathjoin_r(resolved_path, PATH_MAX+1, path_tok, name)) {
/* TODO: handle error */
continue;
}
if (access(resolved_path, X_OK)) {
/* no match */
resolved_path[0] = '\0';
continue;
}
/* match */
break;
} while ((path_tok = strtok_r(NULL, stringize(PATHSEP), &saveptr)));
free(path);
if (!is_empty_string(resolved_path)) {
sk_debug("resolved command %s: %s", name, resolved_path);
} else {
sk_warn("could not resolve command: %s", name);
}
}
|