[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    Re: [BusyBox] /bin/ls color
From:       Tito <farmatito () tiscali ! it>
Date:       2005-01-31 13:31:10
Message-ID: 200501311431.11084.farmatito () tiscali ! it
[Download RAW message or body]

On Monday 31 January 2005 00:02, Paul Fox wrote:
> fyi, i've put a patch for the ls coloring issue into the bug
> system.  it's pretty much as i described below:  it implements
> the "--color" option, and also adds a new config option which can
> be used if necessary to preserve the existing default behavior of
> "auto" coloring if none is specified.
> 
> implementing --color was a little more cumbersome than i would
> have liked.   currently there's no way for a long option to be
> parsed by bb_getopt_ulflags() unless it has a corresponding
> short option.  (you can detect the option, but you can't get it's
> value.) if i'm wrong about this, perhaps someone (vodz?) can tell
> me how it's done.  otherwise, what i've done is inefficient, but
> not otherwise horrible.
> 
> paul
Hi, Paul
Try it like this example it's easy:

#define WHOLE_FILE		1
#define PRINT_NAME		2
#define PRINT_OFFSET	4
#define SIZE			8


int strings_main(int argc, char **argv)
{
	int n, c, i = 0, status = EXIT_SUCCESS;
	unsigned long opt;
	unsigned long count;
	FILE *file = stdin;
	char *string;
	const char *fmt = "%s: ";
	char *n_arg = "4";
	int version=0;
	
	/* remove  static const here */
	struct option strings_long_options[] = {
		{ "version", 0, &version, 1 },
		{ 0,         0, 0, 0 }
	};
	
	bb_applet_long_options = strings_long_options;
		
	opt = bb_getopt_ulflags (argc, argv, "afon:", &n_arg);
	/* -a is our default behaviour */
	
	argc -= optind;
	argv += optind;

	if(version == 1) {
		printf("version %d\n", version);
		return 0;
	}

Basically it works like its described in man getopt:

         struct option {
              const char *name;
              int has_arg;
              int *flag;
              int val;
          };

       The meanings of the different fields are:

       name   is the name of the long option.

       has_arg
              is:  no_argument (or 0) if the option does not take an argument,
              required_argument (or 1) if the option requires an argument,  or
              optional_argument  (or  2) if the option takes an optional argu-
              ment.

       flag   specifies how results are returned for a long option.   If  flag
              is  NULL,  then  getopt_long()  returns  val.  (For example, the
              calling program may set val to the equivalent short option char-
              acter.)   Otherwise, getopt_long() returns 0, and flag points to
             a variable which is set to val if the option is found, but  left
              unchanged if the option is not found.

       val    is  the value to return, or to load into the variable pointed to
              by flag.

       The last element of the array has to be filled with zeroes.

Ciao, Tito.

PS :Maybe I should add this to the getopt_ulflags improved comments patch?
_______________________________________________
busybox mailing list
busybox@mail.busybox.net
http://codepoet.org/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic