-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
84 lines (67 loc) · 1.61 KB
/
util.c
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
#include "util.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <err.h>
#include <pwd.h>
static char *home_dir_cache = NULL;
static char *joinpath_ap(const char *root, va_list ap)
{
size_t len;
char *ret, *p;
const char *temp;
va_list aq;
va_copy(aq, ap);
if (!root)
return NULL;
len = strlen(root);
while ((temp = va_arg(ap, const char *))) {
size_t temp_len = strlen(temp) + 1;
if (temp_len > ((size_t) -1) - len) {
return NULL;
}
len += temp_len;
}
ret = malloc(len + 1);
if (ret) {
p = stpcpy(ret, root);
while ((temp = va_arg(aq, const char *))) {
p++[0] = '/';
p = stpcpy(p, temp);
}
}
return ret;
}
char *joinpath(const char *root, ...)
{
va_list ap;
char *ret;
va_start(ap, root);
ret = joinpath_ap(root, ap);
va_end(ap);
return ret;
}
void safe_asprintf(char **strp, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (vasprintf(strp, fmt, ap) < 0)
err(EXIT_FAILURE, "failed to allocate memory");
va_end(ap);
}
const char *get_home_dir(void)
{
if (!home_dir_cache) {
home_dir_cache = getenv("HOME");
if (home_dir_cache && home_dir_cache[0])
home_dir_cache = strdup(home_dir_cache);
else {
struct passwd *pwd = getpwuid(getuid());
if (!pwd)
err(EXIT_FAILURE, "failed to get pwd entry for user");
home_dir_cache = strdup(pwd->pw_dir);
}
}
return home_dir_cache;
}