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