summaryrefslogtreecommitdiff
path: root/src/commands/xargs.c
blob: 01fb6ce70053faa60c6280a378e29af887fe5c4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "../command.h"
#include <stdio.h>
#include <stdlib.h>

static struct {
    bool null_seperated;
    bool ignore_empty;
    bool print_command;
    bool prompt_command;
    int max_args;
    FILE* file;
} flags;

static void help(void) {
    printf("Usage: xargs [OPTIONS] [PROG ARGS]\n\n");
    printf("Run PROG on every item given by stdin\n\n");
	printf("\t-0\tInput is separated by NULs\n");
	printf("\t-a FILE\tRead from FILE instead of stdin\n");
	printf("\t-r\tDon't run command if input is empty\n");
	printf("\t-t\tPrint the command on stderr before execution\n");
	printf("\t-p\tAsk user whether to run each command\n");
	printf("\t-n N\tPass no more than N args to PROG\n");
}

static int short_arg(char c, char* next) {
    UNUSED(next);
    switch (c) {
        case '0':
            flags.null_seperated = true;
            break;
        case 'a':
            check_arg(next);
            flags.file = get_file(next, "r");
            return ARG_USED;
        case 'r':
            flags.ignore_empty = true;
            break;
        case 't':
            flags.print_command = true;
            break;
        case 'p':
            flags.prompt_command = true;
            break;
        case 'n':
            check_arg(next);
            long int n = get_number(next);
            if (n < 1) {
                error("error: max arg count must be at least 1");
            }
            flags.max_args = n;
            return ARG_USED;
    }
    return ARG_UNUSED;
}

char* read_next(FILE* file, int arg_count) {

    if (flags.max_args != -1 && arg_count == flags.max_args) return NULL;

    int size = 0;
    int capacity = 8;
    char* buf = malloc(sizeof(char) * capacity);

    char c;
    while(c = getc(file), true) {
        if (c == EOF && size == 0) {
            free(buf);
            return NULL;
        }

        if (size == capacity) {
            capacity *= 2;
            buf = realloc(buf, sizeof(char) * capacity);
        }

        if (c == '\0' || c == EOF || (!flags.null_seperated && c == '\n')) {
            buf[size++] = '\0';
            return buf;
        } else {
            buf[size++] = c;
        }
    }
}

void read_args(FILE* file, char*** args, int* size, int* capacity) {
    char* arg;
    static int read = 0;
    while (arg = read_next(file, read), true) {
        if (*size == *capacity) {
            *capacity *= 2;
            *args = realloc(*args, sizeof(char*) * *capacity);
        }
        (*args)[(*size)++] = arg;
        read++;
        if (arg == NULL) break;
    }
}

COMMAND(xargs) {
    flags.null_seperated = false;
    flags.ignore_empty = false;
    flags.print_command = false;
    flags.print_command = false;
    flags.max_args = -1;
    flags.file = stdin;

    int start = parse_args(argc, argv, help, short_arg, NULL);

    char* command;
    if (start >= argc) {
        command = "echo";
    } else {
        command = argv[start]; 
    }

    int arg_start = start + 1;
    int arg_on_stack_count;

    if (arg_start >= argc) {
        arg_on_stack_count = 0;
        arg_start = argc - 1;
    } else {
        arg_on_stack_count = argc - arg_start;
    }

    int size = arg_on_stack_count + 1;
    int capacity = size + 8;
    char** args = malloc(sizeof(char*) * capacity);
    args[0] = command;
    memcpy(&args[1], &argv[arg_start], arg_on_stack_count * sizeof(char*));
    read_args(flags.file, &args, &size, &capacity);

    if (flags.ignore_empty && size < 2) goto cleanup;

    if (flags.prompt_command || flags.print_command) {
        for (int i = 0; i < size - 1; i++) {
            fprintf(stderr, "%s ", args[i]);
        }
        fprintf(stderr, "\b\n");
    }

    if (flags.prompt_command) {
        fprintf(stderr, "Run command? ");
        fflush(stderr);
        FILE* in = get_tty_stream("r");
        char c = getc(in);
        fclose(in);
        if (c != 'y' && c != 'Y') {
            fprintf(stderr, "Skipping...\n");
            goto cleanup;
        }
    }

    if (execvp(command, args) == -1) {
        error("error: failed to execute command: %s", strerror(errno));
    }

cleanup:

    for (int i = arg_on_stack_count + 1; i < size - 1; i++) {
        free(args[i]);
    }
    fclose(flags.file);

    return EXIT_SUCCESS;
}