1 /* getmountlist.c - Get a linked list of mount points, with stat information.
3 * Copyright 2006 Rob Landley <rob@landley.net>
9 // Traverse arg_list of csv, calling callback on each value
10 void comma_args(struct arg_list *al, void *data, char *err,
11 char *(*callback)(void *data, char *str, int len))
16 if (CFG_TOYBOX_DEBUG && !err) err = "INTERNAL";
20 while ((next = comma_iterate(&arg, &len)))
21 if ((next = callback(data, next, len)))
22 error_exit("%s '%s'\n%*c", err, al->arg,
23 (int)(5+strlen(toys.which->name)+strlen(err)+next-al->arg), '^');
28 // Realloc *old with oldstring,newstring
30 void comma_collate(char **old, char *new)
32 char *temp, *atold = *old;
34 // Only add a comma if old string didn't end with one
35 if (atold && *atold) {
38 if (atold[strlen(atold)-1] == ',') comma = "";
39 temp = xmprintf("%s%s%s", atold, comma, new);
40 } else temp = xstrdup(new);
45 // iterate through strings in a comma separated list.
46 // returns start of next entry or NULL if none
47 // sets *len to length of entry (not including comma)
48 // advances *list to start of next entry
49 char *comma_iterate(char **list, int *len)
51 char *start = *list, *end;
53 if (!*list || !**list) return 0;
55 if (!(end = strchr(*list, ','))) {
58 } else *list += (*len = end-start)+1;
63 static void octal_deslash(char *s)
71 for (i = 1; i < 4; i++) {
72 if (!isdigit(s[i])) break;
73 oct = (oct<<3)+s[i]-'0';
87 // check all instances of opt and "no"opt in optlist, return true if opt
88 // found and last instance wasn't no. If clean, remove each instance from list.
89 int comma_scan(char *optlist, char *opt, int clean)
91 int optlen = strlen(opt), len, no, got = 0;
93 if (optlist) for (;;) {
94 char *s = comma_iterate(&optlist, &len);
97 no = 2*(*s == 'n' && s[1] == 'o');
98 if (optlen == len-no && !strncmp(opt, s+no, optlen)) {
100 if (clean && optlist) memmove(s, optlist, strlen(optlist)+1);
107 // return true if all scanlist options enabled in optlist
108 int comma_scanall(char *optlist, char *scanlist)
112 while (scanlist && *scanlist) {
113 char *opt = comma_iterate(&scanlist, &i), *s = xstrndup(opt, i);
115 i = comma_scan(optlist, s, 0);
123 // Check if this type matches list.
124 // Odd syntax: typelist all yes = if any, typelist all no = if none.
126 int mountlist_istype(struct mtab_list *ml, char *typelist)
131 if (!typelist) return 1;
133 skip = strncmp(typelist, "no", 2);
136 if (!(t = comma_iterate(&typelist, &len))) break;
138 // If one -t starts with "no", the rest must too
139 if (strncmp(t, "no", 2)) error_exit("bad typelist");
140 if (!strncmp(t+2, ml->type, len-2)) {
144 } else if (!strncmp(t, ml->type, len) && !ml->type[len]) {
153 // Get list of mounted filesystems, including stat and statvfs info.
154 // Returns a reversed list, which is good for finding overmounts and such.
156 struct mtab_list *xgetmountlist(char *path)
158 struct mtab_list *mtlist = 0, *mt;
161 char *p = path ? path : "/proc/mounts";
163 if (!(fp = setmntent(p, "r"))) perror_exit("bad %s", p);
165 // The "test" part of the loop is done before the first time through and
166 // again after each "increment", so putting the actual load there avoids
167 // duplicating it. If the load was NULL, the loop stops.
169 while ((me = getmntent(fp))) {
170 mt = xzalloc(sizeof(struct mtab_list) + strlen(me->mnt_fsname) +
171 strlen(me->mnt_dir) + strlen(me->mnt_type) + strlen(me->mnt_opts) + 4);
172 dlist_add_nomalloc((void *)&mtlist, (void *)mt);
174 // Collect details about mounted filesystem
175 // Don't report errors, just leave data zeroed
177 stat(me->mnt_dir, &(mt->stat));
178 statvfs(me->mnt_dir, &(mt->statvfs));
181 // Remember information from /proc/mounts
182 mt->dir = stpcpy(mt->type, me->mnt_type)+1;
183 mt->device = stpcpy(mt->dir, me->mnt_dir)+1;
184 mt->opts = stpcpy(mt->device, me->mnt_fsname)+1;
185 strcpy(mt->opts, me->mnt_opts);
187 octal_deslash(mt->dir);
188 octal_deslash(mt->device);