1
2
3
4
5
6
7#include "toys.h"
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#if CFG_TOYBOX_FORK
23pid_t xfork(void)
24{
25 pid_t pid = fork();
26
27 if (pid < 0) perror_exit("fork");
28
29 return pid;
30}
31#endif
32
33int xgetrandom(void *buf, unsigned buflen, unsigned flags)
34{
35 int fd;
36
37#if CFG_TOYBOX_GETRANDOM
38 if (buflen == getrandom(buf, buflen, flags&~WARN_ONLY)) return 1;
39 if (errno!=ENOSYS && !(flags&WARN_ONLY)) perror_exit("getrandom");
40#endif
41 fd = xopen(flags ? "/dev/random" : "/dev/urandom",O_RDONLY|(flags&WARN_ONLY));
42 if (fd == -1) return 0;
43 xreadall(fd, buf, buflen);
44 close(fd);
45
46 return 1;
47}
48
49#if defined(__APPLE__)
50extern char **environ;
51
52int clearenv(void)
53{
54 *environ = NULL;
55 return 0;
56}
57#endif
58
59
60#if defined(__APPLE__) || defined(__FreeBSD__)
61
62
63
64
65#else
66
67#include <mntent.h>
68
69static void octal_deslash(char *s)
70{
71 char *o = s;
72
73 while (*s) {
74 if (*s == '\\') {
75 int i, oct = 0;
76
77 for (i = 1; i < 4; i++) {
78 if (!isdigit(s[i])) break;
79 oct = (oct<<3)+s[i]-'0';
80 }
81 if (i == 4) {
82 *o++ = oct;
83 s += i;
84 continue;
85 }
86 }
87 *o++ = *s++;
88 }
89
90 *o = 0;
91}
92
93
94
95
96int mountlist_istype(struct mtab_list *ml, char *typelist)
97{
98 int len, skip;
99 char *t;
100
101 if (!typelist) return 1;
102
103 skip = strncmp(typelist, "no", 2);
104
105 for (;;) {
106 if (!(t = comma_iterate(&typelist, &len))) break;
107 if (!skip) {
108
109 if (strncmp(t, "no", 2)) error_exit("bad typelist");
110 if (!strncmp(t+2, ml->type, len-2)) {
111 skip = 1;
112 break;
113 }
114 } else if (!strncmp(t, ml->type, len) && !ml->type[len]) {
115 skip = 0;
116 break;
117 }
118 }
119
120 return !skip;
121}
122
123
124
125
126struct mtab_list *xgetmountlist(char *path)
127{
128 struct mtab_list *mtlist = 0, *mt;
129 struct mntent *me;
130 FILE *fp;
131 char *p = path ? path : "/proc/mounts";
132
133 if (!(fp = setmntent(p, "r"))) perror_exit("bad %s", p);
134
135
136
137
138
139 while ((me = getmntent(fp))) {
140 mt = xzalloc(sizeof(struct mtab_list) + strlen(me->mnt_fsname) +
141 strlen(me->mnt_dir) + strlen(me->mnt_type) + strlen(me->mnt_opts) + 4);
142 dlist_add_nomalloc((void *)&mtlist, (void *)mt);
143
144
145
146 if (!path) {
147 stat(me->mnt_dir, &(mt->stat));
148 statvfs(me->mnt_dir, &(mt->statvfs));
149 }
150
151
152 mt->dir = stpcpy(mt->type, me->mnt_type)+1;
153 mt->device = stpcpy(mt->dir, me->mnt_dir)+1;
154 mt->opts = stpcpy(mt->device, me->mnt_fsname)+1;
155 strcpy(mt->opts, me->mnt_opts);
156
157 octal_deslash(mt->dir);
158 octal_deslash(mt->device);
159 }
160 endmntent(fp);
161
162 return mtlist;
163}
164
165#endif
166