From: Theodore Ts'o Date: Wed, 14 May 2003 03:03:43 +0000 (-0400) Subject: util.c, ls.c, logdump.c, htree.c, dump.c, debugfs.h, debugfs.c, ChangeLog: X-Git-Tag: E2FSPROGS-1_34-WIP-0521~15 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=88494bb6d440f703db98b6cc4452f63d7aa392b9;p=thirdparty%2Fe2fsprogs.git util.c, ls.c, logdump.c, htree.c, dump.c, debugfs.h, debugfs.c, ChangeLog: util.c (reset_getopt), debugfs.c (do_open_filesys, do_show_super_stats), ls.c (do_list_dir), dump.c (do_dump), htree.c (do_htree_dump, do_dx_hash), logdump.c (do_logdump): Define and use a new function, reset_getopt(), which does whatever is necessary to reset getopt() again. This is different for different implementations, so the portabilty issues are a bit of a nightmare. (Addresses Debian bug #192834) --- diff --git a/debugfs/ChangeLog b/debugfs/ChangeLog index e178944b1..7e1535308 100644 --- a/debugfs/ChangeLog +++ b/debugfs/ChangeLog @@ -1,3 +1,13 @@ +2003-05-13 root + + * util.c (reset_getopt), debugfs.c (do_open_filesys, + do_show_super_stats), ls.c (do_list_dir), dump.c (do_dump), + htree.c (do_htree_dump, do_dx_hash), logdump.c (do_logdump): + Define and use a new function, reset_getopt(), which does whatever + is necessary to reset getopt() again. This is different for + different implementations, so the portabilty issues are a bit of a + nightmare. (Addresses Debian bug #192834) + 2003-05-05 Theodore Ts'o * debugfs.c (do_imap), debugfs.h, debug_cmds.ct, debugfs.8.in: diff --git a/debugfs/debugfs.c b/debugfs/debugfs.c index 651bd7bae..33e20518b 100644 --- a/debugfs/debugfs.c +++ b/debugfs/debugfs.c @@ -20,9 +20,6 @@ extern int optind; extern char *optarg; #endif -#ifdef HAVE_OPTRESET -extern int optreset; /* defined by BSD, but not others */ -#endif #ifdef HAVE_ERRNO_H #include #endif @@ -101,10 +98,7 @@ void do_open_filesys(int argc, char **argv) blk_t blocksize = 0; int open_flags = 0; - optind = 1; -#ifdef HAVE_OPTRESET - optreset = 1; /* Makes BSD getopt happy */ -#endif + reset_getopt(); while ((c = getopt (argc, argv, "iwfcb:s:")) != EOF) { switch (c) { case 'i': @@ -242,10 +236,7 @@ void do_show_super_stats(int argc, char *argv[]) int numdirs = 0; const char *usage = "Usage: show_super [-h]"; - optind = 1; -#ifdef HAVE_OPTRESET - optreset = 1; /* Makes BSD getopt happy */ -#endif + reset_getopt(); while ((c = getopt (argc, argv, "h")) != EOF) { switch (c) { case 'h': @@ -1486,7 +1477,7 @@ void do_imap(int argc, char *argv[]) return; ino = string_to_inode(argv[1]); if (!ino) - return 0; + return; group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super); offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) * diff --git a/debugfs/debugfs.h b/debugfs/debugfs.h index e30d95eb4..e8c77a81a 100644 --- a/debugfs/debugfs.h +++ b/debugfs/debugfs.h @@ -22,6 +22,7 @@ extern ext2_filsys current_fs; extern ext2_ino_t root, cwd; +extern void reset_getopt(void); extern FILE *open_pager(void); extern void close_pager(FILE *stream); extern int check_fs_open(char *name); diff --git a/debugfs/dump.c b/debugfs/dump.c index 303f7e183..0ea6e3541 100644 --- a/debugfs/dump.c +++ b/debugfs/dump.c @@ -24,9 +24,6 @@ extern int optind; extern char *optarg; #endif -#ifdef HAVE_OPTRESET -extern int optreset; /* defined by BSD, but not others */ -#endif #include "debugfs.h" @@ -149,10 +146,7 @@ void do_dump(int argc, char **argv) const char *dump_usage = "Usage: dump_inode [-p] "; char *in_fn, *out_fn; - optind = 1; -#ifdef HAVE_OPTRESET - optreset = 1; /* Makes BSD getopt happy */ -#endif + reset_getopt(); while ((c = getopt (argc, argv, "p")) != EOF) { switch (c) { case 'p': diff --git a/debugfs/htree.c b/debugfs/htree.c index b9403d97c..70842432d 100644 --- a/debugfs/htree.c +++ b/debugfs/htree.c @@ -21,9 +21,6 @@ extern int optind; extern char *optarg; #endif -#ifdef HAVE_OPTRESET -extern int optreset; /* defined by BSD, but not others */ -#endif #include "debugfs.h" @@ -193,10 +190,7 @@ void do_htree_dump(int argc, char *argv[]) pager = open_pager(); - optind = 1; -#ifdef HAVE_OPTRESET - optreset = 1; /* Makes BSD getopt happy */ -#endif + reset_getopt(); while ((c = getopt (argc, argv, "l")) != EOF) { switch (c) { case 'l': @@ -277,10 +271,8 @@ void do_dx_hash(int argc, char *argv[]) __u32 hash_seed[4]; hash_seed[0] = hash_seed[1] = hash_seed[2] = hash_seed[3] = 0; - optind = 1; -#ifdef HAVE_OPTRESET - optreset = 1; /* Makes BSD getopt happy */ -#endif + + reset_getopt(); while ((c = getopt (argc, argv, "h:")) != EOF) { switch (c) { case 'h': diff --git a/debugfs/logdump.c b/debugfs/logdump.c index 1e195ba97..f3f2e5dcb 100644 --- a/debugfs/logdump.c +++ b/debugfs/logdump.c @@ -28,9 +28,6 @@ extern int optind; extern char *optarg; #endif -#ifdef HAVE_OPTRESET -extern int optreset; /* defined by BSD, but not others */ -#endif #include "debugfs.h" #include "blkid/blkid.h" @@ -92,10 +89,6 @@ void do_logdump(int argc, char **argv) struct journal_source journal_source; struct ext2_super_block *es = NULL; - optind = 1; -#ifdef HAVE_OPTRESET - optreset = 1; /* Makes BSD getopt happy */ -#endif journal_source.where = 0; journal_source.fd = 0; journal_source.file = 0; @@ -107,6 +100,7 @@ void do_logdump(int argc, char **argv) inode_block_to_dump = -1; inode_to_dump = -1; + reset_getopt(); while ((c = getopt (argc, argv, "ab:ci:f:")) != EOF) { switch (c) { case 'a': diff --git a/debugfs/ls.c b/debugfs/ls.c index a66482dc7..2c672fb1a 100644 --- a/debugfs/ls.c +++ b/debugfs/ls.c @@ -21,9 +21,6 @@ extern int optind; extern char *optarg; #endif -#ifdef HAVE_OPTRESET -extern int optreset; /* defined by BSD, but not others */ -#endif #include "debugfs.h" @@ -125,10 +122,7 @@ void do_list_dir(int argc, char *argv[]) if (check_fs_open(argv[0])) return; - optind = 1; -#ifdef HAVE_OPTRESET - optreset = 1; /* Makes BSD getopt happy */ -#endif + reset_getopt(); while ((c = getopt (argc, argv, "dl")) != EOF) { switch (c) { case 'l': diff --git a/debugfs/util.c b/debugfs/util.c index a5ee131b5..fd2e99a9b 100644 --- a/debugfs/util.c +++ b/debugfs/util.c @@ -13,9 +13,49 @@ #include #include #include +#ifdef HAVE_GETOPT_H +#include +#else +extern int optind; +extern char *optarg; +#endif +#ifdef HAVE_OPTRESET +extern int optreset; /* defined by BSD, but not others */ +#endif #include "debugfs.h" +/* + * This function resets the libc getopt() function, which keeps + * internal state. Bad design! Stupid libc API designers! No + * biscuit! + * + * BSD-derived getopt() functions require that optind be reset to 1 in + * order to reset getopt() state. This used to be generally accepted + * way of resetting getopt(). However, glibc's getopt() + * has additional getopt() state beyond optind, and requires that + * optind be set zero to reset its state. So the unfortunate state of + * affairs is that BSD-derived versions of getopt() misbehave if + * optind is set to 0 in order to reset getopt(), and glibc's getopt() + * will core ump if optind is set 1 in order to reset getopt(). + * + * More modern versions of BSD require that optreset be set to 1 in + * order to reset getopt(). Sigh. Standards, anyone? + * + * We hide the hair here. + */ +void reset_getopt(void) +{ +#ifdef __GLIBC__ + optind = 0; +#else + optind = 1; +#endif +#ifdef HAVE_OPTRESET + optreset = 1; /* Makes BSD getopt happy */ +#endif +} + FILE *open_pager(void) { FILE *outfile;