[REWRITE] hexdump.c rewritten to use getopt32

Denis Vlasenko vda.linux at googlemail.com
Sat Apr 7 01:33:14 UTC 2007


Hi Mats,

Thanks for your work!

On Tuesday 03 April 2007 20:43, Mats Erik Andersson wrote:
> Fellow code workers,
> 
> let me post a recoding of hexdump.c that now uses
> the functionality of getopt32. Since the difference
> file is slightly larger than the rewritten code,
> I post the full source.
> 
> Using bloat-o-meter, a decrease of 47 bytes is calculated.

getopt32() is cute, eh? :)

> However, due to the inherent construction of getopt32,
> which scrambles any order of option flags, this version
> of hexdump will only allow one hard coded ordering for
> output: at present -b -c -d -o -x, one row after the other.
> Is this really a serious shortcoming?

There bound to be people who depend on that.

I'm afraid getting rid of getopt() in this applet is not worth
the regression.

If we will ever have getopt32 without getopt used beneath,
hexdump can be either deselected in .config or, if getopt-less
compilation can be detected (introduce CONFIG_GETOPT?),
it can be coditionally compiled:

#if ENABLE_GETOPT
        while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
                p = strchr(hexdump_opts, ch);
                if (!p)
                        bb_show_usage();
                if ((p - hexdump_opts) < 5) {
                        bb_dump_add(add_first);
                        bb_dump_add(add_strings[(int)(p - hexdump_opts)]);
                } else if (ch == 'C') {
...
#else
        ch = getopt32(argc, argv, "bcdoxCe:f:n:s:v", // hexdump_opts,
                        &e_arg, &f_arg, &n_arg, &s_arg);

        for (pcnt=0, oflg=1; pcnt < 5; pcnt++, oflg <<= 1) {
                if (ch & oflg) {
                        bb_dump_add(add_first);
                        bb_dump_add(add_fmthead);
                        bb_dump_add(add_strings[pcnt]);
                }
        }

        if (ch & OPT_C) {
                bb_dump_add("\"%08.8_Ax\n\"");
                bb_dump_add("\"%08.8_ax  \" 8/1 \"%02x \" \"  \" 8/1 \"%02x \" ");
                bb_dump_add("\"  |\" 16/1 \"%_p\" \"|\\n\"");
        }
...
#endif

Care to do it?
--
vda



More information about the busybox mailing list