syscall_avent/01-cat/cat.c
2022-12-06 23:58:15 +01:00

56 lines
933 B
C

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
const int BUFF_SIZE = 4096;
int main(int argc, char *argv[])
{
char *buffer, *ptr;
int i = 1;
for (; i < argc; i++)
{
int fd = open(argv[i], O_RDONLY);
int ret, nb_bytes = 1, n;
if (fd == -1) perror(argv[i]);
else
{
while (nb_bytes > 0)
{
buffer = (char *) calloc(BUFF_SIZE, sizeof(char));
nb_bytes = read(fd, buffer, BUFF_SIZE);
if (nb_bytes == -1)
{
perror(argv[i]);
break;
}
ptr = buffer;
n = nb_bytes;
while (n > 0)
{
ret = write(STDOUT_FILENO, ptr, n);
if (ret == -1)
{
perror(argv[i]);
return -1;
}
ptr += ret;
n -= ret;
}
free(buffer);
}
close(fd);
}
}
return 0;
}