banan-os/userspace/tee/main.cpp

65 lines
1.1 KiB
C++
Raw Normal View History

2023-07-10 16:07:53 +03:00
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#define MAX_FILES 20
#define BUF_SIZE 1024
int main(int argc, char** argv)
{
2023-07-10 16:18:08 +03:00
int files[MAX_FILES] {};
size_t file_count = 0;
2023-07-10 16:07:53 +03:00
2023-07-10 16:18:08 +03:00
int arg = 1;
2023-07-10 16:07:53 +03:00
2023-07-10 16:18:08 +03:00
int oflag = O_WRONLY | O_CREAT;
if (arg < argc && strcmp(argv[arg], "-a") == 0)
{
oflag |= O_APPEND;
arg++;
}
else
{
oflag |= O_TRUNC;
}
2023-07-10 16:07:53 +03:00
2023-07-10 16:18:08 +03:00
for (int i = arg; i < argc; i++)
{
files[file_count] = open(argv[i], oflag, 0644);
if (files[file_count] == -1)
perror(argv[i]);
else
file_count++;
if (file_count >= MAX_FILES)
{
fprintf(stderr, "only up to %d files are supported\n", MAX_FILES);
break;
}
}
2023-07-10 16:07:53 +03:00
2023-07-10 16:18:08 +03:00
char* buffer = (char*)malloc(BUF_SIZE);
for (;;)
{
ssize_t nread = read(STDIN_FILENO, buffer, BUF_SIZE);
if (nread == -1)
perror("stdin");
if (nread <= 0)
break;
write(STDOUT_FILENO, buffer, nread);
for (size_t i = 0; i < file_count; i++)
write(files[i], buffer, nread);
}
free(buffer);
2023-07-10 16:07:53 +03:00
2023-07-10 16:18:08 +03:00
if (ferror(stdin))
perror("stdin");
2023-07-10 16:07:53 +03:00
2023-07-10 16:18:08 +03:00
for (size_t i = 0; i < file_count; i++)
close(files[i]);
2023-07-10 16:07:53 +03:00
2023-07-10 16:18:08 +03:00
return 0;
2023-07-10 16:07:53 +03:00
}