Question: Write a C program to implement the wc utility in Unix. Name the file mywc.c. Your program should simulate the real wc command. Your program
Write a C program to implement the wc utility in Unix. Name the file mywc.c. Your program should simulate the real wc command. Your program will be tested with input/output pairs generated by the real wc command. However here are few specifications: Your version of wc only works for one file, so you dont have to worry about multiple file inputs.
You need to take inputs from the command line (as you did for shell assignment)
You are highly recommended to use getopt() function to parse the command line options. Writing your own function would be too tedious and error prone.
As for the options, your program should implement three options namely w, c, and l options. Look at wcs man page for more information about what these options do.
More importantly, your version of wc should exactly mimic what wc prints. To compare your output with real wcs output, you can use commands like sdiff, diff or cmp. For example you could do something like this: sdiff <(./mywc newfile) <(wc newfile) This command compares output generated by mywc and wc on a file called newfile.
Also you are not expected to implement the stdin functionality. Try running wc without any file argument and type some stuff and then press ctrl + d(to send an EOF). wc would work for such inputs: stdin(standard input) as well, however you are not expected to program that.
Also your program will only be tested with text files.
Also for all the invalid ways your program can be called, you must prompt some kind of error message.
Below are some(only some) of the valid calls(separated by commas) for your program(which should generate the same output that the real wc command does).
./mywc file, ./mywc -w file, ./mywc -c file, ./mywc -l file, ./mywc -wc file, ./mywc -cw file, ./mywc -cl file, ./mywc -lc file, ./mywc -wl file, ./mywc -lw file, ./mywc -lwc file, ./mywc -wcl file, ./mywc -w -c -l file, ./mywc -w -cl file etc (there could be more)
Below are some(only some) of the invalid calls(separated by commas) for your program wherein you have to prompt the user a message(which could be anything of your choice, you dont have to mimic wc in this regard).
./mywc noFile, ./mywc -q file, ./mywc fileDoesNotExist etc
Note:
Here file refers to an arbitrary filename.
Whereas noFile would mean that the program was run without any arguments, which would be incorrect for our case, so you have to prompt an appropriate error message.
fileDoesNotExist refers to a filename passed as an argument that actually does not exist. You can do error checking for this by checking the return from fopen meaning if fopen fails, you can be sure that file does not exist(at least for our purposes since there can be other ways fopen may fail).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
