]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - misc-utils/findfs.c
Make the ways of using output stream consistent in usage()
[thirdparty/util-linux.git] / misc-utils / findfs.c
index 18608b47060afbe8cd00d4caa566a17a3e3e62ea..7b32dbda33188b2eae7ef802ae91ef6def8e7ec0 100644 (file)
@@ -8,53 +8,71 @@
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
-#include <err.h>
+#include <getopt.h>
 
 #include <blkid.h>
 
 #include "nls.h"
+#include "closestream.h"
+#include "c.h"
 
-static void __attribute__((__noreturn__)) usage(int rc)
+/* Exit codes used by findfs. */
+#define FINDFS_SUCCESS         0       /* no errors */
+#define FINDFS_NOT_FOUND       1       /* label or uuid cannot be found */
+#define FINDFS_USAGE_ERROR     2       /* user did something unexpected */
+
+static void __attribute__((__noreturn__)) usage(void)
 {
-       const char *p = program_invocation_short_name;
+       FILE *out = stdout;
+       fputs(USAGE_HEADER, out);
+       fprintf(out, _(" %s [options] {LABEL,UUID,PARTUUID,PARTLABEL}=<value>\n"),
+               program_invocation_short_name);
 
-       if (!p)
-               p = "findfs";
+       fputs(USAGE_SEPARATOR, out);
+       fputs(_("Find a filesystem by label or UUID.\n"), out);
 
-       fprintf(stderr, _("Usage: %s LABEL=<label>|UUID=<uuid>\n"), p);
-       exit(rc);
+       fputs(USAGE_OPTIONS, out);
+       fprintf(out, USAGE_HELP_OPTIONS(16));
+       fprintf(out, USAGE_MAN_TAIL("findfs(8)"));
+       exit(FINDFS_SUCCESS);
 }
 
 int main(int argc, char **argv)
 {
-       char    *dev, *tk, *vl;
+       char    *dev;
+       int     c;
+       static const struct option longopts[] = {
+               {"version", no_argument, NULL, 'V'},
+               {"help", no_argument, NULL, 'h'},
+               {NULL, 0, NULL, 0}
+       };
 
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
+       close_stdout_atexit();
 
-       if (argc != 2)
+       if (argc != 2) {
                /* we return '2' for backward compatibility
                 * with version from e2fsprogs */
-               usage(2);
-
-       if (!strncmp(argv[1], "LABEL=", 6)) {
-               tk = "LABEL";
-               vl = argv[1] + 6;
-       } else if (!strncmp(argv[1], "UUID=", 5)) {
-               tk = "UUID";
-               vl = argv[1] + 5;
-       } else if (!strcmp(argv[1], "-h") == 0 ||
-                  !strcmp(argv[1], "--help") == 0) {
-               usage(EXIT_SUCCESS);
-       } else
-               usage(2);
-
-       dev = blkid_evaluate_tag(tk, vl, NULL);
+               warnx(_("bad usage"));
+               errtryhelp(FINDFS_USAGE_ERROR);
+       }
+
+       while ((c = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1)
+               switch (c) {
+               case 'V':
+                       print_version(FINDFS_SUCCESS);
+               case 'h':
+                       usage();
+               default:
+                       errtryhelp(FINDFS_USAGE_ERROR);
+               }
+
+       dev = blkid_evaluate_tag(argv[1], NULL, NULL);
        if (!dev)
-               errx(EXIT_FAILURE, _("unable to resolve '%s'"), argv[1]);
+               errx(FINDFS_NOT_FOUND, _("unable to resolve '%s'"), argv[1]);
 
        puts(dev);
-       exit(EXIT_SUCCESS);
+       return FINDFS_SUCCESS;
 }
-