[parted-devel] clearfat clean-up, ACK requested

Jim Meyering jim at meyering.net
Mon Mar 5 11:10:24 CET 2007


Yeah, yeah, clearfat is only for debugging, but it's the principle.
Now it honors the standard --help and --version options.

I found it very disconcerting to run "partprobe --help" and have it just
sit there -- I started to worry when it didn't even respond immediately
to an interrupt, so I'll fix it, next.

Make clearfat accept --help and --version options.

From: Jim Meyering <jim at meyering.net>

* debug/clearfat/clearfat.c (AUTHORS, PROGRAM_NAME): Define.
Include gettext-related things.
Include closeout.h.
(usage): New function.
(main): Set up for translations, use close_stdout.
* bootstrap.conf: Add long-options, for clearfat.
* debug/clearfat/Makefile.am: Use gnulib.
---

 bootstrap.conf             |    1 
 debug/clearfat/Makefile.am |   10 +++--
 debug/clearfat/clearfat.c  |   91 ++++++++++++++++++++++++++++++++++++--------
 lib/.gitignore             |    2 +
 po/POTFILES.in             |    2 +
 5 files changed, 86 insertions(+), 20 deletions(-)

diff --git a/bootstrap.conf b/bootstrap.conf
index b874d93..93a8eb9 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -46,6 +46,7 @@ gnulib_modules="
 	gnupload
 	inttypes
 	lib-ignore
+	long-options
 	malloc
 	realloc
 	rpmatch
diff --git a/debug/clearfat/Makefile.am b/debug/clearfat/Makefile.am
index 0101ceb..7409f45 100644
--- a/debug/clearfat/Makefile.am
+++ b/debug/clearfat/Makefile.am
@@ -2,11 +2,13 @@ noinst_PROGRAMS	= clearfat
 
 clearfat_SOURCES = clearfat.c
 
-clearfat_LDADD = $(LIBS) $(PARTED_LIBS) \
-               $(top_builddir)/libparted/libparted.la
+clearfat_LDADD = \
+  $(top_builddir)/lib/libparted.la \
+  $(top_builddir)/libparted/libparted.la \
+  $(INTLLIBS) $(LIBS) \
+  $(PARTED_LIBS)
 
-
-partedincludedir	= -I$(top_srcdir)/include
+partedincludedir	= -I$(top_srcdir)/lib -I$(top_srcdir)/include
 
 INCLUDES 		= $(partedincludedir) $(INTLINCS)
 
diff --git a/debug/clearfat/clearfat.c b/debug/clearfat/clearfat.c
index e27c32d..4aba739 100644
--- a/debug/clearfat/clearfat.c
+++ b/debug/clearfat/clearfat.c
@@ -21,29 +21,69 @@
 
 #include <string.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <getopt.h>
+#include "closeout.h"
+#include "configmake.h"
+#include "error.h"
+#include "long-options.h"
+
+#include "../../libparted/fs/fat/fat.h"
+
+#include <locale.h>
+
+/* Take care of NLS matters.  */
+
+#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)
 
 #ifndef DISCOVER_ONLY
 
-#include "../../libparted/fs/fat/fat.h"
+/* The official name of this program (e.g., no `g' prefix).  */
+#define PROGRAM_NAME "clearfat"
+
+#define AUTHORS \
+  "<http://parted.alioth.debian.org/cgi-bin/trac.cgi/browser/AUTHORS>"
 
-static char* help_msg =
-"Usage:  clearfat DEVICE MINOR\n"
-"\n"
-"This program is used to enhance the automated testing.  It is not useful for\n"
-"anything much else.\n";
+/* The name this program was run with. */
+char *program_name;
+
+void
+usage (int status)
+{
+  if (status != EXIT_SUCCESS)
+    fprintf (stderr, _("Try `%s --help' for more information.\n"),
+	     program_name);
+  else
+    {
+      printf (_("\
+Usage: %s [OPTION]\n\
+  or:  %s DEVICE MINOR\n"), PROGRAM_NAME, PROGRAM_NAME);
+      fputs (_("\
+Clear unused space on a FAT partition (a GNU Parted testing tool).\n\
+\n\
+"), stdout);
+      fputs (_("      --help     display this help and exit\n"), stdout);
+      fputs (_("      --version  output version information and exit\n"),
+             stdout);
+      printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
+    }
+  exit (status);
+}
 
 #define CLEAR_BUFFER_SIZE		(1024 * 1024)
 #define CLEAR_BUFFER_SECTORS		(CLEAR_BUFFER_SIZE/512)
 
 static char buffer [CLEAR_BUFFER_SIZE];
 
-static int
-_do_help ()
-{
-	fputs (help_msg, stdout);
-	exit (1);
-}
-
 /* generic clearing code ***************************************************/
 
 static int
@@ -228,8 +268,28 @@ main (int argc, char* argv[])
 	PedPartition*		part;
 	PedFileSystem*		fs;
 
-	if (argc < 3)
-		_do_help ();
+        program_name = argv[0];
+        setlocale (LC_ALL, "");
+        bindtextdomain (PACKAGE, LOCALEDIR);
+        textdomain (PACKAGE);
+
+	atexit (close_stdout);
+
+	parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, VERSION,
+                            usage, AUTHORS, (char const *) NULL);
+	if (getopt_long (argc, argv, "", NULL, NULL) != -1)
+	  usage (EXIT_FAILURE);
+
+	if (argc - optind < 2)
+          {
+            error (0, 0, _("too few arguments"));
+            usage (EXIT_FAILURE);
+          }
+	if (2 < argc - optind)
+          {
+            error (0, 0, _("too many arguments"));
+            usage (EXIT_FAILURE);
+          }
 
 	dev = ped_device_get (argv [1]);
 	if (!dev)
@@ -284,4 +344,3 @@ main()
 }
 
 #endif /* DISCOVER_ONLY */
-
diff --git a/lib/.gitignore b/lib/.gitignore
index 37c2404..4b2978f 100644
--- a/lib/.gitignore
+++ b/lib/.gitignore
@@ -31,6 +31,8 @@ inttypes.h
 inttypes_.h
 localcharset.c
 localcharset.h
+long-options.c
+long-options.h
 malloc.c
 memchr.c
 memcmp.c
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 6c50518..406ad36 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -14,6 +14,8 @@ lib/version-etc.c
 lib/xalloc-die.c
 libparted/arch/beos.c
 
+debug/clearfat/clearfat.c
+
 # libparted
 libparted/arch/gnu.c
 libparted/arch/linux.c



More information about the parted-devel mailing list