commit 84715bc3d4181b836da85f1c28952e95ada52701
parent 470c684c82dc071c148283859695235741e3ff54
Author: qorg11 <qorg@vxempire.xyz>
Date: Thu, 14 Jan 2021 07:43:51 +0100
fix cat to return error if the read fails
Diffstat:
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/src/cat.c b/src/cat.c
@@ -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;
}