fixed xargs
This commit is contained in:
parent
ad0d7d25ff
commit
035d64a2fb
1 changed files with 27 additions and 23 deletions
50
src/xargs.c
50
src/xargs.c
|
@ -14,7 +14,8 @@
|
|||
#define ARG_MAX 10000
|
||||
#endif
|
||||
|
||||
static int args;
|
||||
static int args_main;
|
||||
static int args_added;
|
||||
static char *cmd[ARG_MAX + 1];
|
||||
|
||||
static char *I_flag;
|
||||
|
@ -30,7 +31,7 @@ enum {
|
|||
NORMAL,
|
||||
CEOF,
|
||||
ERROR,
|
||||
I_FLAG
|
||||
STOP
|
||||
};
|
||||
|
||||
enum {
|
||||
|
@ -39,28 +40,33 @@ enum {
|
|||
};
|
||||
|
||||
static void clear_cmd(void) {
|
||||
for (int i = 0; i < args; i++) {
|
||||
for (int i = 0; i < args_main; i++) {
|
||||
if (cmd[i] != NULL) {
|
||||
free(cmd[i]);
|
||||
cmd[i] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
args = 0;
|
||||
args_main = 0;
|
||||
args_added = 0;
|
||||
}
|
||||
|
||||
static int add_arg(const char *str, size_t chars, int flag) {
|
||||
if (args >= ARG_MAX)
|
||||
static int add_arg(const char *str, size_t chars, const char flag) {
|
||||
if (!flag)
|
||||
args_added++;
|
||||
|
||||
int ret = NORMAL;
|
||||
if (args_main >= ARG_MAX)
|
||||
return ERROR;
|
||||
|
||||
else if (n_flag > 0 && args > n_flag && I_flag == NULL)
|
||||
return ERROR;
|
||||
else if (n_flag > 0 && args_added == n_flag && I_flag == NULL)
|
||||
ret = STOP;
|
||||
|
||||
else if (s_flag > 0 && chars > s_flag)
|
||||
return ERROR;
|
||||
else if (s_flag > 0 && chars >= s_flag)
|
||||
ret = STOP;
|
||||
|
||||
if (!flag && I_flag) {
|
||||
for (int i = 0; i < args; i++)
|
||||
for (int i = 0; i < args_main; i++)
|
||||
if (strstr(cmd[i], I_flag)) {
|
||||
char *ptr = NULL;
|
||||
CONTINUE:
|
||||
|
@ -76,13 +82,13 @@ CONTINUE:
|
|||
goto CONTINUE;
|
||||
}
|
||||
|
||||
return I_FLAG;
|
||||
return STOP;
|
||||
}
|
||||
|
||||
cmd[args] = strdup(str);
|
||||
args++;
|
||||
cmd[args_main] = strdup(str);
|
||||
args_main++;
|
||||
|
||||
return NORMAL;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int is_correct(char c) {
|
||||
|
@ -137,7 +143,7 @@ static int xargs(void) {
|
|||
break;
|
||||
}
|
||||
|
||||
else if (r == I_FLAG) {
|
||||
else if (r == STOP) {
|
||||
free(arg);
|
||||
return NORMAL;
|
||||
}
|
||||
|
@ -193,7 +199,7 @@ static int xargs(void) {
|
|||
|
||||
static int spawn(void) {
|
||||
if (t_flag) {
|
||||
for (int i = 0; i < args; i++)
|
||||
for (int i = 0; i < args_main; i++)
|
||||
fprintf(stderr, "%s ", cmd[i]);
|
||||
|
||||
fputc('\n', stderr);
|
||||
|
@ -299,7 +305,7 @@ int main(int argc, char **argv) {
|
|||
ret = 1;
|
||||
|
||||
/* Check NULL */
|
||||
for (int i = 0; i < args; i++) {
|
||||
for (int i = 0; i < args_main; i++) {
|
||||
if (cmd[i] == NULL) {
|
||||
fprintf(stderr, "xargs: strdup failed\n");
|
||||
clear_cmd();
|
||||
|
@ -310,12 +316,10 @@ int main(int argc, char **argv) {
|
|||
}
|
||||
|
||||
/* Run */
|
||||
if (stdin_stat == CEOF && I_flag == NULL) {
|
||||
if (!r_flag)
|
||||
ret = spawn();
|
||||
}
|
||||
if (stdin_stat == NORMAL)
|
||||
ret = spawn();
|
||||
|
||||
else if (stdin_stat == NORMAL)
|
||||
else if (!r_flag && args_added == 0)
|
||||
ret = spawn();
|
||||
|
||||
clear_cmd();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue