comparison lib/getmountlist.c @ 1321:4d898affda0c draft

Switch mtab_list to doubly linked so we can traverse in either order. Convert umount and df. Add dlist_terminate() to break lists for traversal in either direction.
author Rob Landley <rob@landley.net>
date Thu, 29 May 2014 05:22:02 -0500
parents b2cc738d3cfc
children 487716951287
comparison
equal deleted inserted replaced
1320:b2cc738d3cfc 1321:4d898affda0c
9 // Get list of mounted filesystems, including stat and statvfs info. 9 // Get list of mounted filesystems, including stat and statvfs info.
10 // Returns a reversed list, which is good for finding overmounts and such. 10 // Returns a reversed list, which is good for finding overmounts and such.
11 11
12 struct mtab_list *xgetmountlist(char *path) 12 struct mtab_list *xgetmountlist(char *path)
13 { 13 {
14 struct mtab_list *mtlist, *mt; 14 struct mtab_list *mtlist = 0, *mt;
15 struct mntent *me; 15 struct mntent *me;
16 FILE *fp; 16 FILE *fp;
17 char *p = path ? path : "/proc/mounts";
17 18
18 if (!path) path = "/proc/mounts"; 19 if (!(fp = setmntent(p, "r"))) perror_exit("bad %s", p);
19 if (!(fp = setmntent(path, "r"))) perror_exit("bad %s", path);
20 20
21 // The "test" part of the loop is done before the first time through and 21 // The "test" part of the loop is done before the first time through and
22 // again after each "increment", so putting the actual load there avoids 22 // again after each "increment", so putting the actual load there avoids
23 // duplicating it. If the load was NULL, the loop stops. 23 // duplicating it. If the load was NULL, the loop stops.
24 24
25 for (mtlist = 0; (me = getmntent(fp)); mtlist = mt) { 25 while ((me = getmntent(fp))) {
26 mt = xzalloc(sizeof(struct mtab_list) + strlen(me->mnt_fsname) + 26 mt = xzalloc(sizeof(struct mtab_list) + strlen(me->mnt_fsname) +
27 strlen(me->mnt_dir) + strlen(me->mnt_type) + strlen(me->mnt_opts) + 4); 27 strlen(me->mnt_dir) + strlen(me->mnt_type) + strlen(me->mnt_opts) + 4);
28 mt->next = mtlist; 28 dlist_add_nomalloc((void *)&mtlist, (void *)mt);
29 29
30 // Collect details about mounted filesystem (don't bother for /etc/fstab). 30 // Collect details about mounted filesystem
31 if (stat(me->mnt_dir, &(mt->stat)) || statvfs(me->mnt_dir, &(mt->statvfs))) 31 // Don't report errors, just leave data zeroed
32 perror_msg("stat '%s'"); 32 if (!path) {
33 stat(me->mnt_dir, &(mt->stat));
34 statvfs(me->mnt_dir, &(mt->statvfs));
35 }
33 36
34 // Remember information from /proc/mounts 37 // Remember information from /proc/mounts
35 mt->dir = stpcpy(mt->type, me->mnt_type)+1; 38 mt->dir = stpcpy(mt->type, me->mnt_type)+1;
36 mt->device = stpcpy(mt->dir, me->mnt_dir)+1; 39 mt->device = stpcpy(mt->dir, me->mnt_dir)+1;
37 mt->opts = stpcpy(mt->device, me->mnt_fsname)+1; 40 mt->opts = stpcpy(mt->device, me->mnt_fsname)+1;