5
\$\begingroup\$

The options for the 'cat' command are as follows:

 -b      Number the non-blank output lines, starting at 1.

 -e      Display non-printing characters (see the -v option), and display
         a dollar sign (`$') at the end of each line.

 -n      Number the output lines, starting at 1.

 -s      Squeeze multiple adjacent empty lines, causing the output to be
         single spaced.

 -t      Display non-printing characters (see the -v option), and display
         tab characters as `^I'.

 -u      Disable output buffering.

 -v      Display non-printing characters so they are visible.  Control
         characters print as `^X' for control-X; the delete character
         (octal 0177) prints as `^?'.  Non-ASCII characters (with the high
         bit set) are printed as `M-' (for meta) followed by the character
         for the low 7 bits.

#include 'stdlib.h'
#include 'stdio.h'
#include 'string.h'
#include 'unistd.h'
#define len 256;

/*function declarations*/

void non_blank (FILE *fin, FILE *fout, int writeLineNumbers);
void dollar_sign(FILE *fin, FILE *fout, int writeLineNumbers);  //test comment   
void output_line(FILE *fin, FILE *fout, int writeLineNumbers);
void squeeze (FILE *fin, FILE *fout, int writeLineNumbers);
void non_printing (FILE *fin, FILE *fout, int writeLineNumbers);
void non_printingvisible ((FILE *fin, FILE *fout, int writeLineNumbers);

int main(int argc, char *argv[])
{
int opt=0;
int bflag = 0;
int eflag = 0;
int nflag = 0;
int sflag = 0;
int tflag = 0;
int vflag = 0;


//Not sure what value is stored in opt?

while((opt=getopt(argc, argv, "benstv"))!=-1)
switch (opt)
{
    case 'b':
        bflag=1;
        non_blank(stdin, stdout, opt);
        break;
    case 'e':
        eflag=1;
        dollar_sign(stdin, stdout, opt);
        break;
    case 'n':  //test comment   
        nflag=1;
        output_line(stdin, stdout, opt);
        break;
    case 's':
        sflag=1;
        squeeze(stdin, stdout, opt);
        break;
    case 't':
        tflag=1;
        non_printing(stdin, stdout, opt);
        break;
    case 'v':
        vflag=1;
        non_printingvisible(stdin, stdout, opt);
        break;
    default:
        abort();
}
       //test comment   

//implementing -b functionality 
void non_blank (FILE *fin, FILE *fout, int writeLineNumbers)
{
    char line[len];
    int linenumber=1;

    while (fgets(line,len,fin))
    {
        if(writeLineNumbers)
        printf("%d",linenumber);
        if (fputs(line, fout)==EOF)
        {  //test comment   
            printf(stderr,"Write to stdout failed.");
            return;
        }
        if(line[0]!='\0')
        ++linenumber;
    }
}

// implementing -e functionality.                          
void dollar_sign (FILE *fin, FILE *fout, int writeLineNumbers)
{
    char line[len];
    int linenumber=1;

    while (fgets(line,len,fin))  //test comment   
    {
        if(writeLineNumbers)
        printf("  $");
        if (fputs(line, fout)==EOF)
        {
            printf(stderr,"Write to stdout failed.");
            return;
        }
        ++linenumber;
    }
}

//implementing -n functionality                            
void output_line (FILE *fin, FILE *fout, int writeLineNumbers)
{
    char line[len];
    int linenumber=1;
      //test comment   
    while (fgets(line,len,fin))
    {
        if(writeLineNumbers)
            printf("%d",linenumber);
        if (fputs(line, fout)==EOF)
        {
            printf(stderr,"Write to stdout failed.");
            return;
        }
        ++linenumber;
    }
}

/*     void output_line (FILE *fin, FILE *fout, int writeLineNumbers)
{
    char line[len];
    int linenumber=1;  //test comment   

    while (fgets(line,len,fin))
    {
        if(writeLineNumbers)
        printf("%d",linenumber);
        if (fputs(line, fout)==EOF)
        {
            printf(stderr,"Write to stdout failed.");
            return;
        }
        ++linenumber;
    }
}
          */

//implementing -s functionality            

void squeeze ( FILE *fin, FILE *fout, int writeLineNumbers)
{
    char line[len] = {0};
   // int linenumber=1;
      //test comment   
    while (fgets(line,len,fin))
    {
        if(line[0]=='\n')
            fin--=fin;
        else if (fputs(line, fout)==EOF)
        {
            printf(stderr,"Write to stdout failed.");
            return;
        }
      //  ++linenumber;
    }
}                              
  //test comment   
/*should converting each char to int  (for ASCII value) and then printing back in char                     fix it? */

void non_printing ( FILE *fin, FILE *fout, int writeLineNumbers)
{
   char line[len];
   int linenumber= 1;
   int i;

   while (fgets(line,len,fin))
    {
       i=strlen(line);  //test comment   
       for(i=0;i<strlen;i++)  //test comment   
       line[i]

//test comment         
      if(line==NULL)
          fin--;
      if (fputs(line, fout)==EOF)
      {
          printf(stderr,"Write to stdout failed.");
          return;
      }
      ++linenumber;
 }
}
\$\endgroup\$
1
  • 2
    \$\begingroup\$ You seem to be missing some code in main(), as well as a level of indentation. \$\endgroup\$ Commented Mar 29, 2014 at 15:45

1 Answer 1

4
\$\begingroup\$

You're initializing but not using local variables like bflag.

Is the user allowed to specify more than one option? Source code for GNU's cat found here says "Yes, multiple options can be specified". So instead of calling a different routine for each option, you should call the same routine for all options, and apply any/all specified options for each line read.

You're always reading from stdin however cat allows the input filename to be specified on the command-line.

Your version assumes that 256 is the maximum length of line in the input file.

You're passing writeLineNumbers as a parameter to all subroutines but not using it in most of the subroutines.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.