GNU programs should accept --help and --version options,
if for no other reason than not to "surprise" the user,
especially ones like partprobe that try to do low-level
things with disks even when given an unrecognized option.

With the change below, partprobe --unrecognized-option will
fail immediately rather than trying to probe all partitions.

Also, there was a bug:

  partprobe /dev/bad /dev/bad2 /dev/ok

would exit successfully for the above, not reflecting errors
on the first two partitions.  Now it exits nonzero if process_dev
fails for any device, not just for the last one.

Here's the new --help:

    $ ./partprobe --help
    Usage: partprobe [OPTION] [DEVICE]...
    Inform the OS of partition table changes.

      -d, --no-update  don't update the kernel
      -s, --summary    print a summary of contents
      -h, --help       display this help and exit
      -v, --version    output version information and exit

    With no DEVICE, probe all partitions.

    Report bugs to <[email protected]>.


-------------------------------------------------------------------------------
partprobe-help-version
-------------------------------------------------------------------------------
Make partprobe accept --help and --version options.
Add long options: --no-update (same as existing -d), and
--summary (same as existing -s).
* partprobe/partprobe.c
Include configmake.h, getopt.h, and NLS-related things.
(main): Rewrite option handling.
Along the way, fix a bug whereby "partprobe DEV1 DEV2 ... DEVN" would
exit successfully whenever process_dev (DEVN) returns nonzero, even
when that function fails for each of the preceding devices.
---

 partprobe/partprobe.c |  127 +++++++++++++++++++++++++++++++------------------
 1 files changed, 81 insertions(+), 46 deletions(-)

diff --git a/partprobe/partprobe.c b/partprobe/partprobe.c
index 9a26e9a..78257d4 100644
--- a/partprobe/partprobe.c
+++ b/partprobe/partprobe.c
@@ -31,16 +31,40 @@
 
 #include <stdio.h>
 #include <string.h>
+#include <getopt.h>
 
 #include "closeout.h"
+#include "configmake.h"
 #include "version-etc.h"
 
+#include <locale.h>
+#include "gettext.h"
+#if ! ENABLE_NLS
+# undef textdomain
+# define textdomain(Domainname) /* empty */
+# undef bindtextdomain
+# define bindtextdomain(Domainname, Dirname) /* empty */
+#endif
+
+#undef _
+#define _(msgid) gettext (msgid)
+
 #define AUTHORS \
   "<http://parted.alioth.debian.org/cgi-bin/trac.cgi/browser/AUTHORS>"
 
 /* The official name of this program (e.g., no `g' prefix).  */
 #define PROGRAM_NAME "partprobe"
 
+static struct option const long_options[] =
+  {
+    {"no-update", no_argument, NULL, 'd'},
+    {"summary", no_argument, NULL, 's'},
+    {"help", no_argument, NULL, 'h'},
+    {"version", no_argument, NULL, 'v'},
+    {NULL, 0, NULL, 0}
+  };
+
+
 char *program_name;
 
 /* initialized to 0 according to the language lawyers */
@@ -106,70 +130,81 @@ error:
        return 0;
 }
 
-static void
-help ()
-{
-       printf ("usage: %s [-d] [-h] [-s] [-v] [DEVICES...]\n\n"
-               "-d     don't update the kernel\n"
-               "-s     print a summary of contents\n"
-               "-v     version info\n", PROGRAM_NAME);
-}
-
-static void
-version ()
+void
+usage (int status)
 {
-       version_etc (stdout, PROGRAM_NAME, PACKAGE_NAME, VERSION, AUTHORS,
-                     (char *) NULL);
+  if (status != EXIT_SUCCESS)
+    fprintf (stderr, _("Try `%s --help' for more information.\n"),
+            program_name);
+  else
+    {
+      printf (_("Usage: %s [OPTION] [DEVICE]...\n"), PROGRAM_NAME);
+      fputs (_("\
+Inform the OS of partition table changes.\n\
+\n\
+  -d, --no-update  don't update the kernel\n\
+  -s, --summary    print a summary of contents\n\
+  -h, --help       display this help and exit\n\
+  -v, --version    output version information and exit\n\
+"), stdout);
+      fputs (_("\
+\n\
+With no DEVICE, probe all partitions.\n\
+"), stdout);
+      printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
+    }
+  exit (status);
 }
 
 int
 main (int argc, char* argv[])
 {
-       int             dev_passed = 0;
-       int             i;
-       PedDevice*      dev;
-       int             status = 1;
+       int             status = 0;
 
        program_name = argv[0];
        atexit (close_stdout);
 
-       for (i = 1; i < argc; i++) {
-               if (argv[i][0] != '-') {
-                       dev_passed = 1;
-                       continue;
-               }
-               switch (argv[i][1]) {
-                       case '?':
-                       case 'h':
-                               help();
-                               return 0;
+       int c;
+       while ((c = getopt_long (argc, argv, "dhsv", long_options, NULL)) != -1)
+               switch (c) {
+                       case 'd':
+                               opt_no_probe = 1;
+                               break;
 
-                       case 'd': opt_no_probe = 1; break;
-                       case 's': opt_summary = 1; break;
+                       case 's':
+                               opt_summary = 1;
+                               break;
 
-                       case 'v':
-                               version();
-                               return 0;
-               }
-       }
-
-       if (dev_passed) {
-               for (i = 1; i < argc; i++) {
-                       if (argv[i][0] == '-')
-                               continue;
+                       case 'h':
+                               usage (EXIT_SUCCESS);
+                               break;
 
-                       dev = ped_device_get (argv[i]);
-                       if (dev)
-                               status &= process_dev (dev);
-                       else
-                               status = 0;
+                       case 'v':
+                               version_etc (stdout, PROGRAM_NAME, PACKAGE_NAME,
+                                            VERSION, AUTHORS, (char *) NULL);
+                               exit (EXIT_SUCCESS);
+                               break;
+
+                       default:
+                               usage (EXIT_FAILURE);
+                }
+
+        int n_dev = argc - optind;
+       if (n_dev != 0) {
+               int i;
+               for (i = optind; i < argc; i++) {
+                       PedDevice *dev = ped_device_get (argv[i]);
+                       if (dev == NULL || process_dev (dev) == 0)
+                               status = 1;
                }
        } else {
                ped_device_probe_all ();
+               PedDevice *dev;
                for (dev = ped_device_get_next (NULL); dev;
                     dev = ped_device_get_next (dev))
-                       status &= process_dev (dev);
+                       if (process_dev (dev) == 0)
+                               status = 1;
        }
 
-       return !status;
+       return status;
 }

_______________________________________________
parted-devel mailing list
[email protected]
http://lists.alioth.debian.org/mailman/listinfo/parted-devel

Reply via email to