-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.c
169 lines (144 loc) · 3.48 KB
/
utils.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
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
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <assert.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include "macros.h"
#include "utils.h"
bool isdir(const char *s)
{
struct stat st;
int result = stat(s, &st);
if (result == -1)
ERROR("stat(%s): %s\n", s, strerror(errno));
return result == 0 && S_ISDIR(st.st_mode);
}
bool isexe(const char *s)
{
struct stat st;
int result = stat(s, &st);
if (result == -1)
ERROR("stat(%s): %s\n", s, strerror(errno));
return result == 0 && !S_ISDIR(st.st_mode) && st.st_mode & S_IXUSR;
}
bool exists(const char *s)
{
struct stat st;
return stat(s, &st) == 0;
}
bool mkfile(const char *s)
{
int fd;
if ((fd = creat(s, 0700)) == -1)
return false;
close(fd);
return true;
}
mode_t getmode(const char *s)
{
struct stat st;
if (stat(s, &st) == -1)
return 0;
return st.st_mode;
}
bool startswith(const char *haystack, const char *needle)
{
return strncmp(haystack, needle, strlen(needle)) == 0;
}
int send_fd(int sock, int fd)
{
struct msghdr msg = {0};
struct iovec iov[1];
struct cmsghdr *cmsg = NULL;
char ctrl_buf[CMSG_SPACE(sizeof(int))];
char data[1];
memset(ctrl_buf, 0, CMSG_SPACE(sizeof(int)));
data[0] = ' ';
iov[0].iov_base = data;
iov[0].iov_len = sizeof(data);
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
msg.msg_controllen = CMSG_SPACE(sizeof(int));
msg.msg_control = ctrl_buf;
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
*((int *)CMSG_DATA(cmsg)) = fd;
return sendmsg(sock, &msg, 0);
}
int recv_fd(int sock)
{
struct msghdr msg = {0};
struct iovec iov[1];
struct cmsghdr *cmsg = NULL;
char ctrl_buf[CMSG_SPACE(sizeof(int))];
char data[1];
memset(ctrl_buf, 0, CMSG_SPACE(sizeof(int)));
iov[0].iov_base = data;
iov[0].iov_len = sizeof(data);
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_control = ctrl_buf;
msg.msg_controllen = CMSG_SPACE(sizeof(int));
msg.msg_iov = iov;
msg.msg_iovlen = 1;
recvmsg(sock, &msg, 0);
cmsg = CMSG_FIRSTHDR(&msg);
return *((int *) CMSG_DATA(cmsg));
}
bool isxbpscommand(const char *s)
{
const char *commands[] = {
"/xbps-install",
"/xbps-remove",
"/xbps-reconfigure",
"/xbps-query",
"/xbps-pkgdb",
};
for (size_t i = 0; i < ARRAY_SIZE(commands); i++) {
const char *command = commands[i];
if (!strcmp(s, command+1))
return true;
char *slash = strrchr(s, '/');
if (slash && !strcmp(slash, command))
return true;
}
return false;
}
bool strarray_append(struct strarray *a, char *s)
{
if (a->end == a->size - 1)
return false;
else
a->list[a->end++] = s;
return true;
}
bool intarray_append(struct intarray *a, int i)
{
if (a->end == a->size - 1)
return false;
else
a->list[a->end++] = i;
return true;
}
void strarray_alloc(struct strarray *a, size_t size)
{
a->end = 0;
a->size = size;
a->list = malloc(sizeof(char *) * size);
assert(a->list != NULL);
}
void intarray_alloc(struct intarray *i, size_t size)
{
i->end = 0;
i->size = size;
i->list = malloc(sizeof(int) * size);
assert(i->list != NULL);
}