commit 36f49ec40686848c079c2f01b5a7ba53d6d6e6b4
parent 8627b3bf15a155263524e361fdc51f5a4738fe4d
Author: qorg11 <qorg@vxempire.xyz>
Date: Sat, 28 Nov 2020 22:29:41 +0100
cat: ignore -u and - can be used for stdin
Diffstat:
1 file changed, 10 insertions(+), 10 deletions(-)
diff --git a/src/cat.c b/src/cat.c
@@ -6,17 +6,20 @@
#include <string.h>
int
-cat(int fd,const char *filename)
+cat(int fd, const char *filename)
{
int c;
char buf[8192];
-
+ if (filename[0] == '-')
+ fd = 0;
+
if(fd != 0)
fd = open(filename, O_RDONLY);
if (fd == -1)
fprintf(stderr,"error opening %s: %s\n",filename,strerror(errno));
+
while((c = read(fd,buf,sizeof(buf))) > 0)
write(1,buf,c);
close(fd);
@@ -25,14 +28,11 @@ cat(int fd,const char *filename)
int
main(int argc, char *argv[])
{
-
- if(argc == 1)
- cat(0,NULL);
- else for(int i = optind; i<argc;i++)
- {
- int c = getopt(argc, argv, "u");
- cat(1,argv[i]);
- }
+ int c = getopt(argc,argv,"u");
+ if (argc == optind)
+ cat(0,"-");
+ for(int i = optind; i<argc;i++)
+ cat(1,argv[i]);
return 0;
}