[BusyBox] idea: mini_getopt_long() ?

Glenn bug1 at optushome.com.au
Sun Jul 29 10:50:38 UTC 2001


On Mon, 30 Jul 2001 09:39:58 +1000
The previous one didnt handle arguments for long names, this one will move
the argument following the match to the end if the first char of longname
is a ':'.

e.g. mini_getopt_long(argc, argv, "--header"); will move the --header
argment to the end.

mini_getopt_long(argc, argv, ":--header"); will move --header to second
last and its argument to the end.

b.t.w the list is quiet...


int mini_getopt_long(const int argc, char **argv, char *longname)
{
	char *real_longname = longname;
	char *longname_arg = NULL;
	int match_opt = argc;
	int num_match_args = 1;
	int i;

	if (longname[0] == ':') {
		num_match_args = 2;
		real_longname++;
	}
	for (i = 0; i < argc; i++) {
		if (strcmp(argv[i], real_longname) == 0) {
			match_opt = i;
			break;
		}
	}

	if (match_opt > 0) {
		/* Store the longopts argument if there is one */
		if (num_match_args == 2) {
			longname_arg = argv[i + 1];
		}
		/* move arguments after the found longopt down*/
		for (i = match_opt; i < (argc - num_match_args); i++) { 
			argv[i] = argv[i + num_match_args];
		}
		/* add the found longopt [and argument] to the end */
		argv[argc - num_match_args] = real_longname;
		if (num_match_args == 2) {
			argv[argc] = longname_arg;
		}
		return(TRUE);
	}
	return(FALSE);
}





More information about the busybox mailing list