summaryrefslogtreecommitdiff
path: root/common/path.c
blob: 818befcca65a5e17ed3f8857848630ca5dd30aea (plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
 * Copyright (c) 2005 Stefan Walter
 * Copyright (c) 2011 Collabora Ltd.
 * Copyright (c) 2013 Red Hat Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *     * Redistributions of source code must retain the above
 *       copyright notice, this list of conditions and the
 *       following disclaimer.
 *     * Redistributions in binary form must reproduce the
 *       above copyright notice, this list of conditions and
 *       the following disclaimer in the documentation and/or
 *       other materials provided with the distribution.
 *     * The names of contributors to this software may not be
 *       used to endorse or promote products derived from this
 *       software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 *
 * CONTRIBUTORS
 *  Stef Walter <stefw@redhat.com>
 */

#include "config.h"

#include "debug.h"
#include "message.h"
#include "path.h"

#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

#ifdef OS_UNIX
#include <paths.h>
#include <pwd.h>
#include <unistd.h>
#endif

#ifdef OS_WIN32
#include <shlobj.h>
#endif


char *
p11_path_base (const char *path)
{
#ifdef OS_WIN32
	static const char *delims = "/\\";
#else
	static const char *delims = "/";
#endif

	const char *end;
	const char *beg;

	return_val_if_fail (path != NULL, NULL);

	/* Any trailing slashes */
	end = path + strlen (path);
	while (end != path) {
		if (!strchr (delims, *(end - 1)))
			break;
		end--;
	}

	/* Find the last slash after those */
	beg = end;
	while (beg != path) {
		if (strchr (delims, *(beg - 1)))
			break;
		beg--;
	}

	return strndup (beg, end - beg);
}

static char *
expand_homedir (const char *remainder)
{
	const char *env;

	if (remainder[0] == '\0')
		remainder = NULL;

	env = getenv ("HOME");
	if (env && env[0]) {
		return p11_path_build (env, remainder, NULL);

	} else {
#ifdef OS_UNIX
		struct passwd *pwd;
		int error = 0;

		pwd = getpwuid (getuid ());
		if (!pwd) {
			error = errno;
			p11_message ("couldn't lookup home directory for user %d: %s",
				     getuid (), strerror (errno));
			errno = error;
			return NULL;
		}

		return p11_path_build (pwd->pw_dir, remainder, NULL);

#else /* OS_WIN32 */
		char directory[MAX_PATH + 1];

		if (!SHGetSpecialFolderPathA (NULL, directory, CSIDL_PROFILE, TRUE)) {
			p11_message ("couldn't lookup home directory for user");
			errno = ENOTDIR;
			return NULL;
		}

		return p11_path_build (directory, remainder, NULL);

#endif /* OS_WIN32 */
	}
}

static char *
expand_tempdir (const char *remainder)
{
	const char *env;

	if (remainder[0] == '\0')
		remainder = NULL;

	env = getenv ("TEMP");
	if (env && env[0]) {
		return p11_path_build (env, remainder, NULL);

	} else {
#ifdef OS_UNIX
#ifdef _PATH_TMP
		return p11_path_build (_PATH_TMP, remainder, NULL);
#else
		return p11_path_build ("/tmp", remainder, NULL);
#endif

#else /* OS_WIN32 */
		char directory[MAX_PATH + 1];

		if (!GetTempPathA (MAX_PATH + 1, directory)) {
			p11_message ("couldn't lookup temp directory");
			errno = ENOTDIR;
			return NULL;
		}

		return p11_path_build (directory, remainder, NULL);

#endif /* OS_WIN32 */
	}
}

static inline bool
is_path_component_or_null (char ch)
{
	return (ch == '\0' || ch == '/'
#ifdef OS_WIN32
			|| ch == '\\'
#endif
		);
}

char *
p11_path_expand (const char *path)
{
	return_val_if_fail (path != NULL, NULL);

	if (strncmp (path, "~", 1) == 0 &&
	    is_path_component_or_null (path[1])) {
		return expand_homedir (path + 1);

	} else if (strncmp (path, "$HOME", 5) == 0 &&
	    is_path_component_or_null (path[5])) {
		return expand_homedir (path + 5);

	} else if (strncmp (path, "$TEMP", 5) == 0 &&
	    is_path_component_or_null (path[5])) {
		return expand_tempdir (path + 5);

	} else {
		return strdup (path);
	}
}

bool
p11_path_absolute (const char *path)
{
	return_val_if_fail (path != NULL, false);

	return (path[0] == '/')
#ifdef OS_WIN32
	|| (path[0] != '\0' && path[1] == ':' && path[2] == '\\')
#endif
	;
}

char *
p11_path_build (const char *path,
                ...)
{
#ifdef OS_WIN32
	static const char delim = '\\';
#else
	static const char delim = '/';
#endif
	const char *first = path;
	char *built;
	size_t len;
	size_t at;
	size_t num;
	size_t until;
	va_list va;

	return_val_if_fail (path != NULL, NULL);

	len = 1;
	va_start (va, path);
	while (path != NULL) {
		len += strlen (path) + 1;
		path = va_arg (va, const char *);
	}
	va_end (va);

	built = malloc (len + 1);
	return_val_if_fail (built != NULL, NULL);

	at = 0;
	path = first;
	va_start (va, path);
	while (path != NULL) {
		num = strlen (path);

		/* Trim end of the path */
		until = (at > 0) ? 0 : 1;
		while (num > until && is_path_component_or_null (path[num - 1]))
			num--;

		if (at != 0) {
			if (num == 0)
				continue;
			built[at++] = delim;
		}

		assert (at + num < len);
		memcpy (built + at, path, num);
		at += num;

		path = va_arg (va, const char *);

		/* Trim beginning of path */
		while (path && path[0] && is_path_component_or_null (path[0]))
			path++;
	}
	va_end (va);

	assert (at < len);
	built[at] = '\0';
	return built;
}

char *
p11_path_parent (const char *path)
{
	const char *e;
	char *parent;
	bool had = false;

	return_val_if_fail (path != NULL, NULL);

	/* Find the end of the last component */
	e = path + strlen (path);
	while (e != path && is_path_component_or_null (*e))
		e--;

	/* Find the beginning of the last component */
	while (e != path && !is_path_component_or_null (*e)) {
		had = true;
		e--;
	}

	/* Find the end of the last component */
	while (e != path && is_path_component_or_null (*e))
		e--;

	if (e == path) {
		if (!had)
			return NULL;
		parent = strdup ("/");
	} else {
		parent = strndup (path, (e - path) + 1);
	}

	return_val_if_fail (parent != NULL, NULL);
	return parent;
}

bool
p11_path_prefix (const char *string,
                 const char *prefix)
{
	int a, b;

	return_val_if_fail (string != NULL, false);
	return_val_if_fail (prefix != NULL, false);

	a = strlen (string);
	b = strlen (prefix);

	return a > b &&
	       strncmp (string, prefix, b) == 0 &&
	       is_path_component_or_null (string[b]);
}

void
p11_path_canon (char *name)
{
	static const char *VALID =
		"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_";
	int i;

	return_if_fail (name != NULL);

	for (i = 0; name[i] != '\0'; i++) {
		if (strchr (VALID, name[i]) == NULL)
			name[i] = '_';
	}
}