HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Implementing the 'cat' command

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
commandimplementingcatthe

Problem

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':

Solution

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.

Context

StackExchange Code Review Q#45696, answer score: 4

Revisions (0)

No revisions yet.