mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 23:50:19 +00:00
6fa727a88e
Error messages should go to stderr so they don't get mixed in with the program's output. Also added a check for the return value of write().
33 lines
761 B
C++
33 lines
761 B
C++
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <errno.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <fcntl.h>
|
|
#include <assert.h>
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
int fd = argc > 1 ? open(argv[1], O_RDONLY) : 0;
|
|
if (fd == -1) {
|
|
fprintf(stderr, "Failed to open %s: %s\n", argv[1], strerror(errno));
|
|
return 1;
|
|
}
|
|
for (;;) {
|
|
char buf[4096];
|
|
ssize_t nread = read(fd, buf, sizeof(buf));
|
|
if (nread == 0)
|
|
break;
|
|
if (nread < 0) {
|
|
perror("read");
|
|
return 2;
|
|
}
|
|
ssize_t nwritten = write(1, buf, nread);
|
|
if (nwritten < 0) {
|
|
perror("write");
|
|
return 3;
|
|
}
|
|
ASSERT(nwritten == nread);
|
|
}
|
|
return 0;
|
|
}
|