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
|
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "runes.h"
void runes_warn(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
/* XXX make this do something else on windows */
vfprintf(stderr, fmt, ap);
va_end(ap);
}
void runes_die(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
/* XXX make this do something else on windows */
vfprintf(stderr, fmt, ap);
va_end(ap);
exit(1);
}
char *runes_get_socket_name()
{
char *home, *runtime_dir, *socket_dir, *socket_file;
home = getenv("HOME");
runtime_dir = getenv("XDG_RUNTIME_DIR");
if (runtime_dir) {
runes_sprintf_dup(&socket_dir, "%s/runes", runtime_dir);
}
else {
runes_sprintf_dup(&socket_dir, "%s/.runes", home);
}
runes_sprintf_dup(&socket_file, "%s/runesd", socket_dir);
free(socket_dir);
return socket_file;
}
int runes_sprintf_dup(char **out, const char *fmt, ...)
{
int outlen = 0;
va_list ap;
va_start(ap, fmt);
outlen = vsnprintf(*out, outlen, fmt, ap);
va_end(ap);
*out = malloc(outlen + 1);
va_start(ap, fmt);
outlen = vsnprintf(*out, outlen + 1, fmt, ap);
va_end(ap);
return outlen;
}
void runes_mkdir_p(char *dir)
{
char *path_component, *save_ptr, *tok_str, *partial_path;
struct stat st;
dir = strdup(dir);
partial_path = strdup("/");
tok_str = dir;
while ((path_component = strtok_r(tok_str, "/", &save_ptr))) {
char *new_path;
runes_sprintf_dup(&new_path, "%s%s/", partial_path, path_component);
if (mkdir(new_path, 0755)) {
if (errno != EEXIST) {
runes_die("couldn't create directory %s: %s\n",
new_path, strerror(errno));
}
}
free(partial_path);
partial_path = new_path;
tok_str = NULL;
}
if (stat(partial_path, &st)) {
runes_die("couldn't stat %s: %s\n", partial_path, strerror(errno));
}
if (!S_ISDIR(st.st_mode)) {
runes_die("couldn't create directory %s: %s\n",
partial_path, strerror(EEXIST));
}
free(partial_path);
free(dir);
}
|