lazysphere/command/tee.c

84 lines
1.7 KiB
C
Raw Normal View History

2023-05-06 04:39:44 +00:00
#include "command.h"
#include "lslib.h"
2023-05-01 04:31:13 +00:00
#include <signal.h>
2023-05-06 04:39:44 +00:00
#include <stdlib.h>
2023-05-01 04:31:13 +00:00
2023-05-01 22:43:32 +00:00
static struct {
bool append;
bool handle_sigint;
} flags;
static void help(void) {
2023-05-01 04:31:13 +00:00
printf("Usage: tee [-ai] [FILE]...\n\n");
printf("Copy stdin to each FILE, and also to stdout\n\n");
2023-05-12 22:35:41 +00:00
printf("\t-a\tAppend to the given FILEs, don't overwrite\n");
printf("\t-i\tIgnore interrupt signals (SIGINT)\n");
2023-05-01 04:31:13 +00:00
exit(EXIT_SUCCESS);
}
2023-05-01 22:43:32 +00:00
static void handle(int dummy){UNUSED(dummy);}
2023-05-01 04:31:13 +00:00
2023-05-04 20:10:37 +00:00
static void run_tee(int file_count, FILE** files) {
2023-05-01 04:31:13 +00:00
char c;
2023-05-04 20:10:37 +00:00
int i;
2023-05-01 04:31:13 +00:00
while((c = getchar()) != EOF) {
2023-05-04 20:10:37 +00:00
int i;
for (i = 0; i < file_count; i++) {
2023-05-01 04:31:13 +00:00
fwrite(&c, 1, 1, files[i]);
fflush(files[i]);
}
putchar(c);
}
2023-05-04 20:10:37 +00:00
for (i = 0; i < file_count; i++) {
2023-05-01 04:31:13 +00:00
fclose(files[i]);
}
}
2023-05-01 22:43:32 +00:00
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:
2023-05-03 16:17:56 +00:00
return ARG_INVALID;
2023-05-01 22:43:32 +00:00
}
return ARG_UNUSED;
}
2023-05-01 04:31:13 +00:00
2023-05-12 22:35:41 +00:00
COMMAND(tee_cmd) {
2023-05-01 04:31:13 +00:00
2023-05-04 20:10:37 +00:00
int start, i;
FILE** files;
2023-05-01 22:43:32 +00:00
flags.append = false;
flags.handle_sigint = false;
2023-05-01 04:31:13 +00:00
2023-05-04 20:10:37 +00:00
start = parse_args(argc, argv, help, short_arg, NULL);
2023-05-01 04:31:13 +00:00
2023-05-01 22:43:32 +00:00
if (flags.handle_sigint) {
2023-05-01 04:31:13 +00:00
signal(SIGINT, handle);
}
if (argc - start < 1) {
run_tee(0, NULL);
return EXIT_SUCCESS;
}
2023-05-04 20:10:37 +00:00
files = malloc(sizeof(FILE*) * (argc - start));
for (i = start; i < argc; i++) {
2023-05-01 22:43:32 +00:00
FILE* file = get_file(argv[i], flags.append ? "a" : "w");
2023-05-01 04:31:13 +00:00
files[i - start] = file;
}
run_tee(argc - start, files);
return EXIT_SUCCESS;
}