Cat program with switches

This is a discussion on Cat program with switches within the Linux Programming forums, part of the Platform Specific Boards category; This is my first post basically I am trying to write a basic version of the linux cat command in ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    1

    Cat program with switches

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

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you are allowed (by yourself or others) to use some GNU tools, you should probably look at getopt.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    32
    Not related to your question, but you still can be interested:

    - Usual Unix convention to print error messages to stderr, not stdout.
    -. When you print error it very helpful to include errno and or errno textual description.
    - most of your tests (but for file is being regular file)) could be replaced with just 'open' - if open succeeded, everything is fine
    if not - just print open's errno
    - tests you make isn't enough: it possible that all you test will be OK, but file still can not be open due to parent directory permissions and/or different additional security measures, such as SELinux

    Valery.

  4. #4
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,611
    to start with, you could loop through the argv array, checking for those flags, setting variables along the way to be used later during the catting

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework Help, simple program, unsure newbie :(
    By Aslan14 in forum C Programming
    Replies: 13
    Last Post: 11-14-2010, 04:07 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21