2018-11-06 12:46:40 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
2018-10-26 12:16:56 +03:00
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
if (argc != 2) {
|
|
|
|
printf("usage: cat <file>\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2018-10-28 16:11:51 +03:00
|
|
|
int fd = open(argv[1], O_RDONLY);
|
2018-10-26 12:16:56 +03:00
|
|
|
if (fd == -1) {
|
|
|
|
printf("failed to open %s: %s\n", argv[1], strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
for (;;) {
|
2018-10-27 01:14:24 +03:00
|
|
|
char buf[1024];
|
2018-10-26 12:16:56 +03:00
|
|
|
ssize_t nread = read(fd, buf, sizeof(buf));
|
|
|
|
if (nread == 0)
|
|
|
|
break;
|
|
|
|
if (nread < 0) {
|
|
|
|
printf("read() error: %s\n", strerror(errno));
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
for (ssize_t i = 0; i < nread; ++i)
|
|
|
|
putchar(buf[i]);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|