diff options
Diffstat (limited to 'src/mountinfo.c')
-rw-r--r-- | src/mountinfo.c | 127 |
1 files changed, 70 insertions, 57 deletions
diff --git a/src/mountinfo.c b/src/mountinfo.c index 1e15de4b..50e0716d 100644 --- a/src/mountinfo.c +++ b/src/mountinfo.c @@ -15,6 +15,7 @@ #endif #include <errno.h> +#include <getopt.h> #include <limits.h> #include <regex.h> #include <stdio.h> @@ -26,6 +27,7 @@ #include "rc-misc.h" #include "strlist.h" + #if defined(__FreeBSD__) || defined(__NetBSD__) || defined (__OpenBSD__) static char **find_mounts (regex_t *node_regex, regex_t *fstype_regex, char **mounts, bool list_nodes, bool list_fstype) @@ -34,6 +36,7 @@ static char **find_mounts (regex_t *node_regex, regex_t *fstype_regex, int nmnts; int i; char **list = NULL; + char c; if ((nmnts = getmntinfo (&mnts, MNT_NOWAIT)) == 0) eerrorx ("getmntinfo: %s", strerror (errno)); @@ -140,70 +143,80 @@ int main (int argc, char **argv) bool list_fstype = false; bool reverse = false; char **mounts = NULL; + char c; + + static struct option longopts[] = { + { "fstype-regex", 1, NULL, 'F'}, + { "node-regex", 1, NULL, 'N'}, + { "skip-regex", 1, NULL, 'S'}, + { "fstype", 0, NULL, 'f'}, + { "node", 0, NULL, 'n'}, + { "reverse", 0, NULL, 'r'}, + { NULL, 0, NULL, 0} + }; + + while ((c = getopt_long (argc, argv, "F:N:S;fnr", + longopts, (int *) 0)) != -1) + switch (c) { + case 'F': + if (fstype_regex) + free (fstype_regex); + fstype_regex = rc_xmalloc (sizeof (regex_t)); + if ((result = regcomp (fstype_regex, optarg, + REG_EXTENDED | REG_NOSUB)) != 0) + { + regerror (result, fstype_regex, buffer, sizeof (buffer)); + eerrorx ("%s: invalid regex `%s'", argv[0], buffer); + } + break; + + case 'N': + if (node_regex) + free (node_regex); + node_regex = rc_xmalloc (sizeof (regex_t)); + if ((result = regcomp (node_regex, optarg, + REG_EXTENDED | REG_NOSUB)) != 0) + { + regerror (result, node_regex, buffer, sizeof (buffer)); + eerrorx ("%s: invalid regex `%s'", argv[0], buffer); + } + break; + + case 'R': + if (skip_regex) + free (skip_regex); + skip_regex = rc_xmalloc (sizeof (regex_t)); + if ((result = regcomp (skip_regex, optarg, + REG_EXTENDED | REG_NOSUB)) != 0) + { + regerror (result, skip_regex, buffer, sizeof (buffer)); + eerrorx ("%s: invalid regex `%s'", argv[0], buffer); + } + break; - for (i = 1; i < argc; i++) { - if (strcmp (argv[i], "--fstype-regex") == 0 && (i + 1 < argc)) { - i++; - if (fstype_regex) - free (fstype_regex); - fstype_regex = rc_xmalloc (sizeof (regex_t)); - if ((result = regcomp (fstype_regex, argv[i], - REG_EXTENDED | REG_NOSUB)) != 0) - { - regerror (result, fstype_regex, buffer, sizeof (buffer)); - eerrorx ("%s: invalid regex `%s'", argv[0], buffer); - } - continue; - } - - if (strcmp (argv[i], "--node-regex") == 0 && (i + 1 < argc)) { - i++; - if (node_regex) - free (node_regex); - node_regex = rc_xmalloc (sizeof (regex_t)); - if ((result = regcomp (node_regex, argv[i], - REG_EXTENDED | REG_NOSUB)) != 0) - { - regerror (result, node_regex, buffer, sizeof (buffer)); - eerrorx ("%s: invalid regex `%s'", argv[0], buffer); - } - continue; - } + case 'f': + list_fstype = true; + list_nodes = false; + break; - if (strcmp (argv[i], "--skip-regex") == 0 && (i + 1 < argc)) { - i++; - if (skip_regex) - free (skip_regex); - skip_regex = rc_xmalloc (sizeof (regex_t)); - if ((result = regcomp (skip_regex, argv[i], - REG_EXTENDED | REG_NOSUB)) != 0) - { - regerror (result, skip_regex, buffer, sizeof (buffer)); - eerrorx ("%s: invalid regex `%s'", argv[0], buffer); - } - continue; - } + case 'n': + list_nodes = true; + list_fstype = false; + break; - if (strcmp (argv[i], "--fstype") == 0) { - list_fstype = true; - continue; - } + case 'r': + reverse = true; + break; - if (strcmp (argv[i], "--node") == 0) { - list_nodes = true; - continue; - } - if (strcmp (argv[i], "--reverse") == 0) { - reverse = true; - continue; + default: + exit (EXIT_FAILURE); } - if (argv[i][0] != '/') - eerrorx ("%s: `%s' is not a mount point", argv[0], argv[i]); - - mounts = rc_strlist_add (mounts, argv[i]); + while (optind < argc) { + if (argv[optind][0] != '/') + eerrorx ("%s: `%s' is not a mount point", argv[0], argv[optind]); + mounts = rc_strlist_add (mounts, argv[optind++]); } - nodes = find_mounts (node_regex, fstype_regex, mounts, list_nodes, list_fstype); |