319 lines
4.9 KiB
C
319 lines
4.9 KiB
C
#include <stdio.h>
|
|
#include <errno.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <signal.h>
|
|
#include <sys/wait.h>
|
|
#include <limits.h>
|
|
#include <ctype.h>
|
|
|
|
#ifndef ARG_MAX
|
|
#define ARG_MAX 10000
|
|
#endif
|
|
|
|
int args;
|
|
char *cmd[ARG_MAX + 1];
|
|
|
|
char *I_flag;
|
|
char t_flag;
|
|
char r_flag;
|
|
char nl_flag;
|
|
int n_flag;
|
|
size_t s_flag;
|
|
|
|
enum {
|
|
NORMAL,
|
|
CEOF,
|
|
ERROR,
|
|
I_FLAG
|
|
};
|
|
|
|
enum {
|
|
NONE,
|
|
QUOTE
|
|
};
|
|
|
|
void clear_cmd(void) {
|
|
for (int i = 0; i < args; i++) {
|
|
if (cmd[i] != NULL) {
|
|
free(cmd[i]);
|
|
cmd[i] = NULL;
|
|
}
|
|
}
|
|
|
|
args = 0;
|
|
}
|
|
|
|
int Iflag_push(const char *str, char **arg) {
|
|
size_t arg_len = strlen(*arg);
|
|
size_t str_len = strlen(str);
|
|
size_t Iflag_len = strlen(I_flag);
|
|
|
|
char *buf = malloc(arg_len - Iflag_len + str_len + 1);
|
|
if (buf == NULL) {
|
|
fprintf(stderr, "xargs: malloc failed\n");
|
|
return 1;
|
|
}
|
|
|
|
strcpy(buf, *arg);
|
|
|
|
/* Replace */
|
|
char *ptr = strstr(buf, I_flag);
|
|
memmove(ptr + str_len, ptr + Iflag_len, strlen(ptr + Iflag_len) + 1);
|
|
strncpy(ptr, str, str_len);
|
|
|
|
free(*arg);
|
|
*arg = buf;
|
|
return 0;
|
|
}
|
|
|
|
int add_arg(const char *str, size_t chars, int flag) {
|
|
if (args >= ARG_MAX)
|
|
return ERROR;
|
|
|
|
else if (n_flag > 0 && args > n_flag && I_flag == NULL)
|
|
return ERROR;
|
|
|
|
else if (s_flag > 0 && chars > s_flag)
|
|
return ERROR;
|
|
|
|
if (!flag && I_flag) {
|
|
for (int i = 0; i < args; i++) {
|
|
while (1) {
|
|
if (strstr(cmd[i], I_flag)) {
|
|
if (Iflag_push(str, &cmd[i]))
|
|
return ERROR;
|
|
}
|
|
|
|
else
|
|
break;
|
|
}
|
|
}
|
|
|
|
return I_FLAG;
|
|
}
|
|
|
|
cmd[args] = strdup(str);
|
|
args++;
|
|
|
|
return NORMAL;
|
|
}
|
|
|
|
int is_correct(char c) {
|
|
if (nl_flag)
|
|
return c == '\0';
|
|
|
|
else
|
|
return isspace(c);
|
|
}
|
|
|
|
int xargs(void) {
|
|
size_t arg_size = 0;
|
|
char *arg = malloc(1);
|
|
if (arg == NULL) {
|
|
fprintf(stderr, "xargs: malloc failed\n");
|
|
return ERROR;
|
|
}
|
|
|
|
size_t index = 0;
|
|
int flag = NONE;
|
|
int ret = NORMAL;
|
|
size_t chars = 0;
|
|
int args_passed = 0;
|
|
|
|
while (1) {
|
|
int c = getchar();
|
|
if (c == EOF) {
|
|
if (flag == QUOTE) {
|
|
fprintf(stderr, "xargs: unterminated quote\n");
|
|
ret = ERROR;
|
|
}
|
|
|
|
ret = CEOF;
|
|
break;
|
|
}
|
|
|
|
if (flag == NONE && is_correct(c) && index > 0) {
|
|
arg[index] = '\0';
|
|
|
|
int r = add_arg(arg, chars, 0);
|
|
if (r == ERROR) {
|
|
ret = ERROR;
|
|
break;
|
|
}
|
|
|
|
else if (r == I_FLAG) {
|
|
free(arg);
|
|
return NORMAL;
|
|
}
|
|
|
|
index = 0;
|
|
arg_size = 0;
|
|
args_passed++;
|
|
free(arg);
|
|
|
|
arg = malloc(1);
|
|
if (arg == NULL) {
|
|
fprintf(stderr, "xargs: malloc failed\n");
|
|
return ERROR;
|
|
}
|
|
}
|
|
|
|
else {
|
|
if (c == '"' || c == '\'') {
|
|
if (flag == QUOTE)
|
|
flag = NONE;
|
|
|
|
else
|
|
flag = QUOTE;
|
|
|
|
continue;
|
|
}
|
|
|
|
else if (is_correct(c) && flag == NONE)
|
|
continue;
|
|
|
|
char *tmp = realloc(arg, (++arg_size) + 1);
|
|
if (tmp == NULL) {
|
|
free(arg);
|
|
|
|
fprintf(stderr, "xargs: realloc failed\n");
|
|
return ERROR;
|
|
}
|
|
|
|
arg = tmp;
|
|
arg[index] = (char)c;
|
|
|
|
index++;
|
|
chars++;
|
|
}
|
|
}
|
|
|
|
if (args_passed == 0 && ret != ERROR)
|
|
ret = CEOF;
|
|
|
|
free(arg);
|
|
return ret;
|
|
}
|
|
|
|
int spawn(void) {
|
|
if (t_flag) {
|
|
for (int i = 0; i < args; i++)
|
|
fprintf(stderr, "%s ", cmd[i]);
|
|
|
|
fputc('\n', stderr);
|
|
}
|
|
|
|
pid_t pid;
|
|
if ((pid = fork()) == 0) {
|
|
execvp(cmd[0], cmd);
|
|
fprintf(stderr, "xargs: exec: %s\n", strerror(errno));
|
|
exit(1);
|
|
}
|
|
|
|
int status = 0;
|
|
waitpid(pid, &status, 0);
|
|
|
|
if (status == 255)
|
|
status = 124;
|
|
|
|
else if (status >= 0x180)
|
|
status = 125;
|
|
|
|
return status;
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
/* For -s flag */
|
|
char *p = NULL;
|
|
|
|
int opt;
|
|
while ((opt = getopt(argc, argv, "tn:s:rP:0I:")) != -1) {
|
|
switch (opt) {
|
|
case 't':
|
|
t_flag = 1;
|
|
break;
|
|
|
|
case 'n':
|
|
n_flag = atoi(optarg);
|
|
if (n_flag <= 0) {
|
|
fprintf(stderr, "xargs: -n: invalid number: %s\n", optarg);
|
|
return 1;
|
|
}
|
|
|
|
break;
|
|
|
|
case 's':
|
|
s_flag = strtoul(optarg, &p, 0);
|
|
if (s_flag <= 0 || *p) {
|
|
fprintf(stderr, "xargs: -s: invalid number: %zu%s\n", s_flag, (p) ? p : "");
|
|
return 1;
|
|
}
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
r_flag = 1;
|
|
break;
|
|
|
|
case '0':
|
|
nl_flag = 1;
|
|
break;
|
|
|
|
case 'I':
|
|
I_flag = optarg;
|
|
break;
|
|
|
|
default:
|
|
printf("xargs [tnsrP0I] [cmd [arg1] [arg2...]\n\t-t Print the command before start\n\t-n Pass no more than N args\n\t-r Don't run command if input is empty\n\t-s Pass command line of no more than N bytes\n\t-0 NUL terminated input\n\t-I STR Replace STR within PROG ARGS with input line\n");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
argv += optind;
|
|
argc -= optind;
|
|
|
|
int ret = 0;
|
|
while (1) {
|
|
|
|
/* Arg */
|
|
if (argc) {
|
|
for (int i = 0; i < argc; i++)
|
|
if (add_arg(argv[i], 0, 1))
|
|
break;
|
|
}
|
|
|
|
else
|
|
add_arg("echo", 0, 1);
|
|
|
|
int stdin_stat = xargs();
|
|
if (stdin_stat == ERROR)
|
|
ret = 1;
|
|
|
|
/* Check NULL */
|
|
for (int i = 0; i < args; i++) {
|
|
if (cmd[i] == NULL) {
|
|
fprintf(stderr, "xargs: strdup failed\n");
|
|
clear_cmd();
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
/* Run */
|
|
if (stdin_stat == CEOF && I_flag == NULL) {
|
|
if (!r_flag)
|
|
ret = spawn();
|
|
}
|
|
|
|
else if (stdin_stat == NORMAL)
|
|
ret = spawn();
|
|
|
|
clear_cmd();
|
|
if (stdin_stat)
|
|
break;
|
|
}
|
|
|
|
return ret;
|
|
}
|