1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<unistd.h>
int main(int argc, char *argv[])
{
int fd;
int ecris;
int lu;
char buffer[1024];
char *pathname;
pathname = argv[1];
fd = open(pathname, O_RDONLY);
if (argc != 2)
{
write(2, "error argument\n", sizeof("error argument\n"));
exit(EXIT_FAILURE);
}
if (fd == -1)
{
write(2, "open error\n", strlen("open_error\n"));
exit(EXIT_FAILURE);
}
else
{
do
{
lu = read(fd, buffer, 1);
ecris = write(1, buffer, lu);
if (lu == -1 || ecris == -1)
{
write(2, "read/write error\n", sizeof("read/write error\n"));
close(fd);
exit(EXIT_FAILURE);
}
}while (lu != 0);
}
close(fd);
return (0);
} |