[BusyBox] calling getopt() centrally

Vladimir N. Oleynik dzo at simtreas.ru
Wed Dec 18 08:52:03 UTC 2002


I wrote:
 
> Bob,
> 
> My idea:
> All applet use small count options. 'ls' applet have many
> options, but original GNU 'ls' use ~45 options.
> So... We can using 64 bit flags.
> For each applet making macros:
> example mkdir getopt(m:p")

Oops. Have incomplete realization example.

> applets.c:
> 
> static int applet_getopts(int argc, char * const argv[], const char *optstring)
> {
+	int i, colon;
>        const char *t;
>         int nopt = 0;
> 
>         for(t = optstring; *t; t++) {
>                 if(*t != ':') {
>                         nopt++;
>                 }
>         }
>        applet_opts = xcalloc(nopt, sizeof(char *));
>         nopt = 0;
>        while ((opt = getopt(argc, argv, optstring)) > 0) {
>                t = strchr(optstring, opt);
>                if (t == NULL) {
>                        show_usage();
>                }
+		for(colon = i = 0; optstring[i] != *t; i++) {
+			if(optstring[i] == ':') colon++;
+		}
+		nopt = i - colon;
>                 opts_setted |= 1 << nopt;
>                if (*(t + 1) == ':') {
>                        applet_opts[nopt] = optarg;
>                }
-                 nopt++;
>        }
>        return optind;
> }
> 
> mkdir.c:
> 
> /* glogal getopt "m:p" */
> #define MKDIR_OPT_m 0
> #define MKDIR_OPT_p 1
> 
> extern int mkdir_main (int argc, char **argv)
> {
>        if (tst_opt_setted(MKDIR_OPT_m)) {
>                mode = 0777;
>                if (!parse_mode (applet_opts[MKDIR_OPT_m], &mode)) {
>                        error_msg_and_die ("invalid mode `%s'",
> applet_opts[MKDIR_OPT_m]);
>                 }
>                umask(0);
>         }
>        if (tst_opt_setted(MKDIR_OPT_p))
>                flags |= FILEUTILS_RECUR;

 
> This algorithm can set privileged option: if option 'a' can drop option 'b',
> we can use optstring "ba", not "ab", and check first 'b' and twice 'a'.

But require many addition code :(


--w
vodz



More information about the busybox mailing list