Mercurial > hg > toybox
annotate lib/getmountlist.c @ 1433:00c20f410c46 draft
Patches to commands for issues reported from static analysis tool.
portability.h.patch - it is for O_CLOEXEC, as compiler complained of it.
Makefile.patch - for cleaning generated/*.o files and libopts.dat file
[Fixup to uniq.c from Rob.]
author | Ashwini Sharma <ak.ashwini1981@gmail.com> |
---|---|
date | Tue, 12 Aug 2014 07:09:01 -0500 |
parents | 4d898affda0c |
children | 487716951287 |
rev | line source |
---|---|
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
6
diff
changeset
|
1 /* getmountlist.c - Get a linked list of mount points, with stat information. |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
6
diff
changeset
|
2 * |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
6
diff
changeset
|
3 * Copyright 2006 Rob Landley <rob@landley.net> |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
6
diff
changeset
|
4 */ |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
5 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
6 #include "toys.h" |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
7 #include <mntent.h> |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
8 |
897
849e14fecf2b
Convert getmountlist() to xgetmountlist().
Rob Landley <rob@landley.net>
parents:
882
diff
changeset
|
9 // Get list of mounted filesystems, including stat and statvfs info. |
849e14fecf2b
Convert getmountlist() to xgetmountlist().
Rob Landley <rob@landley.net>
parents:
882
diff
changeset
|
10 // Returns a reversed list, which is good for finding overmounts and such. |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
11 |
1033
11cf9b97fae7
Allow getmountlist to read fstab too.
Rob Landley <rob@landley.net>
parents:
901
diff
changeset
|
12 struct mtab_list *xgetmountlist(char *path) |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
13 { |
1321
4d898affda0c
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.
Rob Landley <rob@landley.net>
parents:
1320
diff
changeset
|
14 struct mtab_list *mtlist = 0, *mt; |
897
849e14fecf2b
Convert getmountlist() to xgetmountlist().
Rob Landley <rob@landley.net>
parents:
882
diff
changeset
|
15 struct mntent *me; |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
442
diff
changeset
|
16 FILE *fp; |
1321
4d898affda0c
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.
Rob Landley <rob@landley.net>
parents:
1320
diff
changeset
|
17 char *p = path ? path : "/proc/mounts"; |
897
849e14fecf2b
Convert getmountlist() to xgetmountlist().
Rob Landley <rob@landley.net>
parents:
882
diff
changeset
|
18 |
1321
4d898affda0c
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.
Rob Landley <rob@landley.net>
parents:
1320
diff
changeset
|
19 if (!(fp = setmntent(p, "r"))) perror_exit("bad %s", p); |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
20 |
901
c44dff160d65
Silence warning and comment a subtle bit.
Rob Landley <rob@landley.net>
parents:
897
diff
changeset
|
21 // The "test" part of the loop is done before the first time through and |
c44dff160d65
Silence warning and comment a subtle bit.
Rob Landley <rob@landley.net>
parents:
897
diff
changeset
|
22 // again after each "increment", so putting the actual load there avoids |
c44dff160d65
Silence warning and comment a subtle bit.
Rob Landley <rob@landley.net>
parents:
897
diff
changeset
|
23 // duplicating it. If the load was NULL, the loop stops. |
c44dff160d65
Silence warning and comment a subtle bit.
Rob Landley <rob@landley.net>
parents:
897
diff
changeset
|
24 |
1321
4d898affda0c
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.
Rob Landley <rob@landley.net>
parents:
1320
diff
changeset
|
25 while ((me = getmntent(fp))) { |
897
849e14fecf2b
Convert getmountlist() to xgetmountlist().
Rob Landley <rob@landley.net>
parents:
882
diff
changeset
|
26 mt = xzalloc(sizeof(struct mtab_list) + strlen(me->mnt_fsname) + |
1320
b2cc738d3cfc
Add mount options to data getmountlist collects.
Rob Landley <rob@landley.net>
parents:
1033
diff
changeset
|
27 strlen(me->mnt_dir) + strlen(me->mnt_type) + strlen(me->mnt_opts) + 4); |
1321
4d898affda0c
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.
Rob Landley <rob@landley.net>
parents:
1320
diff
changeset
|
28 dlist_add_nomalloc((void *)&mtlist, (void *)mt); |
897
849e14fecf2b
Convert getmountlist() to xgetmountlist().
Rob Landley <rob@landley.net>
parents:
882
diff
changeset
|
29 |
1321
4d898affda0c
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.
Rob Landley <rob@landley.net>
parents:
1320
diff
changeset
|
30 // Collect details about mounted filesystem |
4d898affda0c
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.
Rob Landley <rob@landley.net>
parents:
1320
diff
changeset
|
31 // Don't report errors, just leave data zeroed |
4d898affda0c
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.
Rob Landley <rob@landley.net>
parents:
1320
diff
changeset
|
32 if (!path) { |
4d898affda0c
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.
Rob Landley <rob@landley.net>
parents:
1320
diff
changeset
|
33 stat(me->mnt_dir, &(mt->stat)); |
4d898affda0c
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.
Rob Landley <rob@landley.net>
parents:
1320
diff
changeset
|
34 statvfs(me->mnt_dir, &(mt->statvfs)); |
4d898affda0c
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.
Rob Landley <rob@landley.net>
parents:
1320
diff
changeset
|
35 } |
897
849e14fecf2b
Convert getmountlist() to xgetmountlist().
Rob Landley <rob@landley.net>
parents:
882
diff
changeset
|
36 |
849e14fecf2b
Convert getmountlist() to xgetmountlist().
Rob Landley <rob@landley.net>
parents:
882
diff
changeset
|
37 // Remember information from /proc/mounts |
1320
b2cc738d3cfc
Add mount options to data getmountlist collects.
Rob Landley <rob@landley.net>
parents:
1033
diff
changeset
|
38 mt->dir = stpcpy(mt->type, me->mnt_type)+1; |
b2cc738d3cfc
Add mount options to data getmountlist collects.
Rob Landley <rob@landley.net>
parents:
1033
diff
changeset
|
39 mt->device = stpcpy(mt->dir, me->mnt_dir)+1; |
b2cc738d3cfc
Add mount options to data getmountlist collects.
Rob Landley <rob@landley.net>
parents:
1033
diff
changeset
|
40 mt->opts = stpcpy(mt->device, me->mnt_fsname)+1; |
b2cc738d3cfc
Add mount options to data getmountlist collects.
Rob Landley <rob@landley.net>
parents:
1033
diff
changeset
|
41 strcpy(mt->opts, me->mnt_opts); |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
442
diff
changeset
|
42 } |
703 | 43 endmntent(fp); |
897
849e14fecf2b
Convert getmountlist() to xgetmountlist().
Rob Landley <rob@landley.net>
parents:
882
diff
changeset
|
44 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
442
diff
changeset
|
45 return mtlist; |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
46 } |