This is my first post basically I am trying to write a basic version of the linux cat command in c using -e, -b, -n switches.
The -e switch prints out $ at the end of line.
The -b switch doesnt count the number of blank lines and
the -n switch prints out the number of lines. So far the cat program compiled and working without the switches. See code below but need help with implementing the switches. Any code help would be appreciated.
Code:
//Implementing cat command
#include<stdlib.h>
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
int main(int argc,char *argv[])
{
int f=0,n;
char l[80];
struct stat s;
if(argc!=2)
{
printf("Mismatch argument");
exit(1);
}
if(access(argv[1],F_OK))
{
printf("File Exist");
exit(1);
}
if(stat(argv[1],&s)<0)
{
printf("Stat ERROR");
exit(1);
}
if(S_ISREG(s.st_mode)<0)
{
printf("Not a Regular FILE");
exit(1);
}
if(geteuid()==s.st_uid)
if(s.st_mode & S_IRUSR)
f=1;
else if(getegid()==s.st_gid)
if(s.st_mode & S_IRGRP)
f=1;
else if(s.st_mode & S_IROTH)
f=1;
if(!f)
{
printf("Permission denied");
exit(1);
}
while (argc--)
{
if (strcmp(argc,"-i")==0)
printf("-i exists");
}
f=open(argv[1],O_RDONLY);
while((n=read(f,l,80))>0)
write(1,l,n);
}