fix cat to return error if the read fails

This commit is contained in:
qorg11 2021-01-14 07:43:51 +01:00
parent 470c684c82
commit 84715bc3d4
No known key found for this signature in database
GPG Key ID: 343FC20A4ACA62B9
1 changed files with 7 additions and 2 deletions

View File

@ -16,12 +16,17 @@ cat(int fd, const char *filename)
if(fd != 0)
fd = open(filename, O_RDONLY);
if (fd == -1)
if (fd == -1) {
fprintf(stderr,"error opening %s: %s\n",
filename,strerror(errno));
return -1;
}
while((c = read(fd,buf,sizeof(buf))) > 0)
while((c = read(fd,buf,sizeof(buf))) > 0) {
if(c == -1)
return -1;
write(1,buf,c);
}
close(fd);
return 0;
}