patterncMinor
Implementing the 'cat' command
Viewed 0 times
commandimplementingcatthe
Problem
The options for the 'cat' command are as follows:
-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
(octal 0177) prints as
for the low 7 bits.
#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':
-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 characterfor 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':
Solution
You're initializing but not using local variables like
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
Your version assumes that 256 is the maximum length of line in the input file.
You're passing
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.Context
StackExchange Code Review Q#45696, answer score: 4
Revisions (0)
No revisions yet.