aboutsummaryrefslogtreecommitdiffstats
path: root/simple-xdg-bdirs.h
blob: 288b7579ada61f44245a9cdad54b5aeeab764b0e (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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#ifndef SIMPLE_XDG_BDIRS_H
#define SIMPLE_XDG_BDIRS_H

#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>

/* one of these types is passed to each of the functions below, denoting the
 * category of file to which the functions actions ought to correspond.
 * - `SIMPLE_XDG_BDIRS_DATA` is used for persistent data created by a program
 * - `SIMPLE_XDG_BDIRS_CONFIG` is used for a program's configuration files
 * - `SIMPLE_XDG_BDIRS_CACHE` is used for transient data which may be deleted
 *    without warning by the system
 * - `SIMPLE_XDG_BDIRS_RUNTIME` is used for "runtime files", such as lock files
 *    or sockets 
 */
enum simple_xdg_bdirs_ftype {
	SIMPLE_XDG_BDIRS_DATA,
	SIMPLE_XDG_BDIRS_CONFIG,
	SIMPLE_XDG_BDIRS_CACHE,
	SIMPLE_XDG_BDIRS_RUNTIME,
};

/* this function finds and returns a single string that is the path of a
 * directory into which all files of type `type` ought to be written.
 *
 * because the location of this directory is determined at runtime, and in
 * particular because it relies on the state of the filesystem and environment
 * variables, it is possible that no good candidate directory will be found, in
 * which case `NULL` will be returned.
 *
 * if a string is returned, it is guaranteed to always have a trailing '/',
 * meaning that a relative path can be appended directly without fear of
 * unintended results, like '/etx/xdgherbstluftwm' 
 *
 * errors:
 *   ENOENT: no candidate dir found
 *   EINVAL: bad argument
 *   EOVERFLOW: extremely-large string encountered (unlikely)
 *   ENOMEM: malloc err
 */
static char*
simple_xdg_bdirs_write_dir(enum simple_xdg_bdirs_ftype type)
{
	char *home = getenv("HOME");
	char *xdg_data_home;
	char *xdg_config_home;
	char *xdg_cache_home;
	char *xdg_runtime_dir;
	char *empty = "";

	char *s1, *s2, *r;
	size_t l1, l2;

	/* get components */
	switch (type) {
	case SIMPLE_XDG_BDIRS_DATA:
		xdg_data_home = getenv("XDG_DATA_HOME");
		if (xdg_data_home && xdg_data_home[0]) {
			s1 = empty;
			s2 = xdg_data_home;
		} else if (home && home[0]) {
			s1 = home;
			s2 = "/.local/share";
		} else {
			errno = ENOENT;
			return NULL;
		}
		break;

	case SIMPLE_XDG_BDIRS_CONFIG:
		xdg_config_home = getenv("XDG_CONFIG_HOME");
		if (xdg_config_home && xdg_config_home[0]) {
			s1 = empty;
			s2 = xdg_config_home;
		} else if (home && home[0]) {
			s1 = home;
			s2 = "/.config";
		} else {
			errno = ENOENT;
			return NULL;
		}
		break;

	case SIMPLE_XDG_BDIRS_CACHE:
		xdg_cache_home = getenv("XDG_CACHE_HOME");
		if (xdg_cache_home && xdg_cache_home[0]) {
			s1 = empty;
			s2 = xdg_cache_home;
		} else if (home && home[0]) {
			s1 = home;
			s2 = "/.cache";
		} else {
			errno = ENOENT;
			return NULL;
		}
		break;

	case SIMPLE_XDG_BDIRS_RUNTIME:
		xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
		if (xdg_runtime_dir && xdg_runtime_dir[0]) {
			s1 = empty;
			s2 = xdg_runtime_dir;
		} else {
			errno = ENOENT;
			return NULL;
		}
		break;
	default:
		errno = EINVAL;
		return NULL;
	}

	/* get s1 part length */
	for (l1 = 0; s1[l1]; l1++) {
		if (l1 == SIZE_MAX) {
			errno = EOVERFLOW;
			return NULL;
		}
	}

	/* get s2 part length */
	for (l2 = 0; s2[l2]; l1++, l2++) {
		if (l1 == SIZE_MAX) {
			errno = EOVERFLOW;
			return NULL;
		}
	}

	/* extra 2 for '/' and '\0' */
	for (l2 = 0; l2 < 2; l1++, l2++) {
		if (l1 == SIZE_MAX) {
			errno = EOVERFLOW;
			return NULL;
		}
	}

	/* build str */
	r = malloc(l1 * sizeof(char));
	if (r == NULL) {
		errno = ENOMEM;
		return NULL;
	}

	for (l1 = 0; s1[l1]; l1++)
		r[l1] = s1[l1];

	for (l2 = 0; s2[l2]; l1++, l2++)
		r[l1] = s2[l2];

	r[l1] = '/';

	/* slash dedupe */
	for (l2 = 0; l2 < l1; l2++) {
		if (r[l2] == '/' && r[l2 + 1] == '/') {
			memmove(r + l2, r + l2 + 1, l1 - l2 - 1);
			l1--;
			l2--;
		}
	}

	r[l1 + 1] = '\0';

	return r;
}

/* this function finds and returns a null-terminated array of strings that are
 * the paths, in order, where one ought to search for files of type `type` when
 * reading.
 *
 * because some filetypes have only a single, environment-determined read dir,
 * it's possible that none will be found, in which case `NULL` will be
 * returned.
 *
 * returned strings are guaranteed to always have a trailing '/', meaning that
 * a relative path can be appended directly without fear of unintended results,
 * like '/etx/xdgherbstluftwm' 
 *
 * errors:
 *   ENOENT: no candidate dirs found
 *   EINVAL: bad argument
 *   EOVERFLOW: extremely-large string encountered (unlikely)
 *   ENOMEM: malloc err
 */
static char**
simple_xdg_bdirs_read_dirs(enum simple_xdg_bdirs_ftype type)
{
	char *s1 = NULL, *s2 = NULL;
	bool flag;

	size_t l1, l2, l3, l4;

	char **r = NULL, **cur;

	/* cache and runtime are the same as write_dir */
	switch (type) {
	case SIMPLE_XDG_BDIRS_CACHE:
		r = malloc(2 * sizeof(char*));

		if (r == NULL) {
			errno = ENOMEM;
			return NULL;
		}

		r[1] = NULL;
		r[0] = simple_xdg_bdirs_write_dir(SIMPLE_XDG_BDIRS_CACHE);

		if (r[0] == NULL)
			goto abort;

		return r;

	case SIMPLE_XDG_BDIRS_RUNTIME:
		r = malloc(2 * sizeof(char*));

		if (r == NULL) {
			errno = ENOMEM;
			return NULL;
		}

		r[1] = NULL;
		r[0] = simple_xdg_bdirs_write_dir(SIMPLE_XDG_BDIRS_RUNTIME);

		if (r[0] == NULL)
			goto abort;

		return r;

	/* data and config are write_dir + fallbacks */
	case SIMPLE_XDG_BDIRS_DATA:
		s1 = simple_xdg_bdirs_write_dir(SIMPLE_XDG_BDIRS_DATA);

		s2 = getenv("XDG_DATA_DIRS");
		if (!s2 || !s2[0])
			s2 = "/usr/local/share:/usr/share";

		break;

	case SIMPLE_XDG_BDIRS_CONFIG:
		s1 = simple_xdg_bdirs_write_dir(SIMPLE_XDG_BDIRS_CONFIG);

		s2 = getenv("XDG_CONFIG_DIRS");
		if (!s2 || !s2[0])
			s2 = "/etc/xdg";

		break;
	default:
		errno = EINVAL;
		return NULL;
	}

	/* count number of fallback dirs */
	for (flag = true, l1 = 0, l2 = 1; s2[l1]; l1++) {
		if (l1 == SIZE_MAX) {
			errno = EOVERFLOW;
			goto abort;
		}
		if (s2[l1] == ':') {
			if (!flag) {
				l2++;
				flag = true;
			}
		} else {
			flag = false;
		}
	}

	/* make r */
	r = malloc((l2 + (s1 != NULL) + 1) * sizeof(char*));
	if (r == NULL) {
		errno = ENOMEM;
		goto abort;
	}

	/* insert write_dir, if it was found */
	if (s1 != NULL) {
		r[0] = s1;
		cur = r + 1;
	} else {
		cur = r;
	}

	for (l1 = 0, l2 = 0; s2[l1];) {
		/* skip over multiple ':' */
		for (; s2[l2] == ':'; l2++);

		/* check for done */
		l1 = l2;
		if (s2[l1] == '\0')
			break;

		/* find the end of the current dir in the string */
		for (; s2[l2] && s2[l2] != ':'; l2++);

		/* copy the current string into r */
		*cur = malloc((l2 - l1 + 2) * sizeof(char));
		if (*cur == NULL) {
			errno = ENOMEM;
			goto abort;
		}

		if (l2 - l1 != 0)
			strncpy(*cur, s2 + l1, l2 - l1);
		(*cur)[l2 - l1] = '/';
		(*cur)[l2 - l1 + 1] = '\0';

		/* slash dedupe */
		l3 = l2 - l1;

		for (l4 = 0; l4 < l3; l4++) {
			if ((*cur)[l4] == '/' && (*cur)[l4 + 1] == '/') {
				memmove(*cur + l4, *cur + l4 + 1, l3 - l4);
				l3--;
				l4--;
			}
		}

		(*cur)[l3 + 1] = '\0';

		cur++;
	}

	*cur = NULL;

	/* done ^_^ */
	return r;

abort:
	/* whoops, something went wrong :c */
	if (s1 != NULL)
		free(s1);

	if (r == NULL)
		return NULL;

	for (cur = r; *cur != NULL; cur++)
		free(cur);

	free(r);

	return NULL;
}

static char*
sub_simple_xdg_bdirs_join(const char *s1, const char *s2)
{
	size_t l1, l2, l3;
	char *r;
	
	l1 = strlen(s1);
	l2 = strlen(s2);

	for (l3 = l1; l3 != SIZE_MAX && l2 != 0; l3++, l2--);

	if (l3 == SIZE_MAX) {
		errno = EOVERFLOW;
		return NULL;
	}

	r = malloc((l3 + 1) * sizeof(char));
	if (r == NULL) {
		errno = ENOMEM;
		return NULL;
	}

	strcpy(r, s1);
	strcpy(r + l1, s2);

	return r;
}

/* take a relative path and set of directories returned from
 * `simple_xdg_bdirs_read_dirs` and return either a full path to an existing
 * file or, on error, NULL
 *
 * errors
 *   ENOENT: target file not found or readable
 *   EINVAL: bad argument
 *   EOVERFLOW: extremely-large string encountered (unlikely)
 *   ENOMEM: malloc err
 */
static inline char*
simple_xdg_bdirs_fullpath_read(const char *rel_path, char **read_dirs)
{
	char **cur, *path;

	if (rel_path == NULL && read_dirs == NULL) {
		errno = EINVAL;
		return NULL;
	}

	for (cur = read_dirs; *cur != NULL; cur++) {
		path = sub_simple_xdg_bdirs_join(*cur, rel_path);
		if (path == NULL)
			return NULL;

		if (!access(path, R_OK)) {
			return path;
		}

		free(path);
	}

	errno = ENOENT;
	return NULL;
}

/* take a relative path and a write directory returned from
 * `simple_xdg_bdirs_write_dir` and return either a full path to the targeted
 * write path or, on error, NULL 
 *
 * errors
 *   ENOENT: write_dir is not found or not write accessible
 *   EINVAL: bad argument
 *   EOVERFLOW: extremely-large string encountered (unlikely)
 *   ENOMEM: malloc err
 */
static inline char*
simple_xdg_bdirs_fullpath_write(const char *rel_path, char *write_dir)
{
	if (rel_path == NULL && write_dir == NULL) {
		errno = EINVAL;
		return NULL;
	}

	if (access(write_dir, W_OK)) {
		errno = ENOENT;
		return NULL;
	}

	return sub_simple_xdg_bdirs_join(write_dir, rel_path);
}

#endif