From 2728cef0e1b7f91fe1f23b3f85a50820743b75f2 Mon Sep 17 00:00:00 2001 From: Roy Marples Date: Thu, 12 Apr 2007 10:08:42 +0000 Subject: Hug getopt_long even though it's not POSIX. start-stop-daemon has always required this and all our supported platforms have it. --- src/mountinfo.c | 127 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 70 insertions(+), 57 deletions(-) (limited to 'src/mountinfo.c') 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 +#include #include #include #include @@ -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); -- cgit v1.2.3