2023-10-01 23:43:07 +03:00
|
|
|
#include <fcntl.h>
|
2023-05-16 00:27:49 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2023-10-01 23:43:07 +03:00
|
|
|
bool cat_file(int fd)
|
2023-05-16 00:27:49 +03:00
|
|
|
{
|
2023-10-01 23:43:07 +03:00
|
|
|
char buffer[1024];
|
|
|
|
while (ssize_t n_read = read(fd, buffer, sizeof(buffer)))
|
2023-05-16 00:27:49 +03:00
|
|
|
{
|
2023-10-01 23:43:07 +03:00
|
|
|
if (n_read == -1)
|
|
|
|
{
|
|
|
|
perror("read");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
write(STDOUT_FILENO, buffer, n_read);
|
2023-05-16 00:27:49 +03:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (argc > 1)
|
|
|
|
{
|
|
|
|
for (int i = 1; i < argc; i++)
|
|
|
|
{
|
2023-10-01 23:43:07 +03:00
|
|
|
int fd = open(argv[i], O_RDONLY);
|
|
|
|
if (fd == -1)
|
2023-05-16 00:27:49 +03:00
|
|
|
{
|
|
|
|
perror(argv[i]);
|
|
|
|
ret = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2023-10-01 23:43:07 +03:00
|
|
|
if (!cat_file(fd))
|
2023-05-16 00:27:49 +03:00
|
|
|
ret = 1;
|
2023-10-01 23:43:07 +03:00
|
|
|
close(fd);
|
2023-05-16 00:27:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-01 23:43:07 +03:00
|
|
|
if (!cat_file(STDIN_FILENO))
|
2023-06-10 17:34:10 +03:00
|
|
|
ret = 1;
|
2023-05-16 00:27:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|