summaryrefslogtreecommitdiff
path: root/src/commands/tee.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/commands/tee.c')
-rw-r--r--src/commands/tee.c53
1 files changed, 27 insertions, 26 deletions
diff --git a/src/commands/tee.c b/src/commands/tee.c
index c500508..ced5b06 100644
--- a/src/commands/tee.c
+++ b/src/commands/tee.c
@@ -2,7 +2,12 @@
#include <signal.h>
-static void help() {
+static struct {
+ bool append;
+ bool handle_sigint;
+} flags;
+
+static void help(void) {
printf("Usage: tee [-ai] [FILE]...\n\n");
printf("Copy stdin to each FILE, and also to stdout\n\n");
printf("\t-a Append to the given FILEs, don't overwrite\n");
@@ -10,7 +15,7 @@ static void help() {
exit(EXIT_SUCCESS);
}
-static void handle(){}
+static void handle(int dummy){UNUSED(dummy);}
static void run_tee(int file_count, FILE* files[file_count]) {
char c;
@@ -26,33 +31,29 @@ static void run_tee(int file_count, FILE* files[file_count]) {
}
}
-COMMAND(tee) {
+static int short_arg(char c, char* next) {
+ UNUSED(next);
+ switch (c) {
+ case 'a':
+ flags.append = true;
+ break;
+ case 'i':
+ flags.handle_sigint = true;
+ break;
+ default:
+ error("error: unkown option: %c", c);
+ }
+ return ARG_UNUSED;
+}
- bool append = false;
- bool handle_sigint = false;
+COMMAND(tee) {
- int start = 0;
- for (int i = 0; i < argc; i++) {
- if (!prefix("-", argv[i])) break;
- if (streql("--help", argv[i])) help();
+ flags.append = false;
+ flags.handle_sigint = false;
- start++;
- for (size_t j = 1; j < strlen(argv[i]); j++) {
- char o = argv[i][j];
- switch (o) {
- case 'a':
- append = true;
- break;
- case 'i':
- handle_sigint = true;
- break;
- default:
- error("error: unkown option: %c", o);
- }
- }
- }
+ int start = parse_args(argc, argv, help, short_arg, NULL);
- if (handle_sigint) {
+ if (flags.handle_sigint) {
signal(SIGINT, handle);
}
@@ -63,7 +64,7 @@ COMMAND(tee) {
FILE* files[argc - start];
for (int i = start; i < argc; i++) {
- FILE* file = get_file(argv[i], append ? "a" : "w");
+ FILE* file = get_file(argv[i], flags.append ? "a" : "w");
files[i - start] = file;
}