Fix xargs option parsing
This commit is contained in:
parent
1b1cfde1f8
commit
9ae3838657
@ -37,34 +37,39 @@ int xargs_main(int argc, char **argv)
|
|||||||
char traceflag = 0;
|
char traceflag = 0;
|
||||||
int len_args_from_cmdline, len_cmd_to_be_executed, len, opt;
|
int len_args_from_cmdline, len_cmd_to_be_executed, len, opt;
|
||||||
|
|
||||||
while ((opt = getopt(argc, argv, "t")) != EOF) {
|
/* Note that we do not use getopt here, since
|
||||||
switch (opt) {
|
* we only want to interpret initial options,
|
||||||
case 't':
|
* not options passed to commands */
|
||||||
traceflag=1;
|
while (--argc && **(++argv) == '-') {
|
||||||
break;
|
while (*++(*argv)) {
|
||||||
default:
|
switch (**argv) {
|
||||||
fatalError(xargs_usage);
|
case 't':
|
||||||
|
traceflag=1;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
fatalError(xargs_usage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Store the command and arguments to be executed (from the command line) */
|
/* Store the command and arguments to be executed (from the command line) */
|
||||||
if (optind == argc) {
|
if (argc == 1) {
|
||||||
len_args_from_cmdline = 6;
|
len_args_from_cmdline = 6;
|
||||||
args_from_cmdline = xmalloc(len_args_from_cmdline);
|
args_from_cmdline = xmalloc(len_args_from_cmdline);
|
||||||
strcat(args_from_cmdline, "echo ");
|
strcat(args_from_cmdline, "echo ");
|
||||||
} else {
|
} else {
|
||||||
opt=strlen(argv[optind]);
|
opt=strlen(*argv);
|
||||||
len_args_from_cmdline = (opt > 10)? opt : 10;
|
len_args_from_cmdline = (opt > 10)? opt : 10;
|
||||||
args_from_cmdline = xcalloc(len_args_from_cmdline, sizeof(char));
|
args_from_cmdline = xcalloc(len_args_from_cmdline, sizeof(char));
|
||||||
for (; optind < argc; optind++) {
|
while (argc-- > 0) {
|
||||||
if (strlen(argv[optind]) + strlen(args_from_cmdline) >
|
if (strlen(*argv) + strlen(args_from_cmdline) >
|
||||||
len_args_from_cmdline) {
|
len_args_from_cmdline) {
|
||||||
len_args_from_cmdline += strlen(argv[optind]);
|
len_args_from_cmdline += strlen(*argv);
|
||||||
args_from_cmdline =
|
args_from_cmdline =
|
||||||
xrealloc(args_from_cmdline,
|
xrealloc(args_from_cmdline,
|
||||||
len_args_from_cmdline+1);
|
len_args_from_cmdline+1);
|
||||||
}
|
}
|
||||||
strcat(args_from_cmdline, argv[optind]);
|
strcat(args_from_cmdline, *argv);
|
||||||
strcat(args_from_cmdline, " ");
|
strcat(args_from_cmdline, " ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
31
xargs.c
31
xargs.c
@ -37,34 +37,39 @@ int xargs_main(int argc, char **argv)
|
|||||||
char traceflag = 0;
|
char traceflag = 0;
|
||||||
int len_args_from_cmdline, len_cmd_to_be_executed, len, opt;
|
int len_args_from_cmdline, len_cmd_to_be_executed, len, opt;
|
||||||
|
|
||||||
while ((opt = getopt(argc, argv, "t")) != EOF) {
|
/* Note that we do not use getopt here, since
|
||||||
switch (opt) {
|
* we only want to interpret initial options,
|
||||||
case 't':
|
* not options passed to commands */
|
||||||
traceflag=1;
|
while (--argc && **(++argv) == '-') {
|
||||||
break;
|
while (*++(*argv)) {
|
||||||
default:
|
switch (**argv) {
|
||||||
fatalError(xargs_usage);
|
case 't':
|
||||||
|
traceflag=1;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
fatalError(xargs_usage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Store the command and arguments to be executed (from the command line) */
|
/* Store the command and arguments to be executed (from the command line) */
|
||||||
if (optind == argc) {
|
if (argc == 1) {
|
||||||
len_args_from_cmdline = 6;
|
len_args_from_cmdline = 6;
|
||||||
args_from_cmdline = xmalloc(len_args_from_cmdline);
|
args_from_cmdline = xmalloc(len_args_from_cmdline);
|
||||||
strcat(args_from_cmdline, "echo ");
|
strcat(args_from_cmdline, "echo ");
|
||||||
} else {
|
} else {
|
||||||
opt=strlen(argv[optind]);
|
opt=strlen(*argv);
|
||||||
len_args_from_cmdline = (opt > 10)? opt : 10;
|
len_args_from_cmdline = (opt > 10)? opt : 10;
|
||||||
args_from_cmdline = xcalloc(len_args_from_cmdline, sizeof(char));
|
args_from_cmdline = xcalloc(len_args_from_cmdline, sizeof(char));
|
||||||
for (; optind < argc; optind++) {
|
while (argc-- > 0) {
|
||||||
if (strlen(argv[optind]) + strlen(args_from_cmdline) >
|
if (strlen(*argv) + strlen(args_from_cmdline) >
|
||||||
len_args_from_cmdline) {
|
len_args_from_cmdline) {
|
||||||
len_args_from_cmdline += strlen(argv[optind]);
|
len_args_from_cmdline += strlen(*argv);
|
||||||
args_from_cmdline =
|
args_from_cmdline =
|
||||||
xrealloc(args_from_cmdline,
|
xrealloc(args_from_cmdline,
|
||||||
len_args_from_cmdline+1);
|
len_args_from_cmdline+1);
|
||||||
}
|
}
|
||||||
strcat(args_from_cmdline, argv[optind]);
|
strcat(args_from_cmdline, *argv);
|
||||||
strcat(args_from_cmdline, " ");
|
strcat(args_from_cmdline, " ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user