cat: ignore -u and - can be used for stdin

This commit is contained in:
qorg11 2020-11-28 22:29:41 +01:00
parent 8627b3bf15
commit 36f49ec406
No known key found for this signature in database
GPG Key ID: 343FC20A4ACA62B9
1 changed files with 10 additions and 10 deletions

View File

@ -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;
}