Menu

Hello Need 4 C Functions Need C Functions Implement Unix Comment Cat Grep Zip Unzip Works Q43887985

Hello I need 4 C++ functions. I need those C++ functionsto implement unix comment cat, grep, zip and unzip works. Here isthe lab details.

Lab:

Unix Utilities

Before beginning: Your TA will introduce some useful tips forprogramming in the C environment.

In this project, you’ll build a few different UNIX utilities,simple versions of commonly used commands like cat, ls, etc. We’llcall each of them a slightly different name to avoid confusion; forexample, instead of cat, you’ll be implementing wcat .

Objectives:

  • Re-familiarize yourself with the C programming language

  • Re-familiarize yourself with a shell / terminal / command-lineof UNIX

  • Learn (as a side effect) how to use a proper code editor such asemacs, or vi, or…

  • Learn a little about how UNIX utilities are implemented

    While the project focuses upon writing simple C programs, youcan see from the above that even doing so requires a bunch of otherprevious knowledge, including a basic idea of what a shell is andhow to use the command line on some UNIX-based systems (e.g., Linuxor macOS), how to use an editor such as emacs, and of course abasic understanding of C programming. If you do not have theseskills already, this is not the right place to start (that is , doa refresh before the project).

    Summary of what gets turned in:

  • A bunch of single .c files for each of the utilities below:wcat.c, wgrep.c, wzip.c, and wunzip.c.

  • Each should compile successfully when compiled with the -Walland -Werror flags.

  • Each should (hopefully) pass the tests we will discuss.

    wcat

    The program wcat is a simple program. Generally, it reads a fileas specified by the user and prints its contents. A typical usageis as follows, in which the user wants to see the contents ofmain.c, and thus types:

    prompt> ./wcat main.c #include <stdio.h>

As shown, wcat reads the file main.c and prints out itscontents. The “./” before the wcat above is a UNIX thing; it justtells the system which directory to find wcat in (in this case, inthe “.” (dot) directory, which means the current workingdirectory).

To create the wcat binary, you’ll be creating a single sourcefile, wcat.c, and writing a little C code to implement thissimplified version of cat. To compile this program, you will do thefollowing:

prompt> gcc -o wcat wcat.c -Wall -Werror prompt>

This will make a single executable binary called wcat which youcan then run as above.

You’ll need to learn how to use a few library routines from theC standard library (often called

libc) to implement the source code for this program, which we’llassume is in a file called

wcat.c. All C code is automatically linked with the C library,which is full of useful functions you 1

can call to implement your program. Learn more about the Clibrary here and perhaps here .

For this project, we recommend using the following routines todo file input and output: fopen(), fgets(), and fclose(). Wheneveryou use a new function like this, the first thing you should do isread about it — how else will you learn to use it properly?

On UNIX systems, the best way to read about such functions is touse what are called the man pages (short for manual). In ourHTML/web-driven world, the man pages feel a bit antiquated, butthey are useful and informative and generally quite easy touse.

To access the man page for fopen(), for example, just type thefollowing at your UNIX shell prompt:

prompt> man fopen
Then, read! Reading man pages effectively takes practice; why notstart learning now?

We will also give a simple overview here. The fopen() function”opens” a file, which is a common way in UNIX systems to begin theprocess of file access. In this case, opening a file just gives youback a pointer to a structure of type FILE, which can then bepassed to other routines to read, write, etc.

Here is a typical usage of fopen():

FILE *fp = fopen(“main.c”, “r”); if (fp == NULL) {

printf(“cannot open filen”);

exit(1); }

A couple of points here. First, note that fopen() takes twoarguments: the name of the file and the mode. The latter justindicates what we plan to do with the file. In this case, becausewe wish to read the file, we pass “r” as the second argument. Readthe man pages to see what other options are available.

Second, note the critical checking of whether the fopen()actually succeeded. This is not Java where an exception will bethrown when things goes wrong; rather, it is C, and it is expected(in good programs, i.e., the only kind you’d want to write) thatyou always will check if the call succeeded. Reading the man pagetells you the details of what is returned when an error isencountered; in this case, the macOS man page says:

Upon successful completion fopen(), fdopen(), freopen() andfmemopen() return a FILE pointer. Otherwise, NULL is returned andthe global variable errno is
set to indicate the error.

Thus, as the code above does, please check that fopen() does notreturn NULL before trying to use the FILE pointer it returns.

Third, note that when the error case occurs, the program printsa message and then exits with error status of 1. In UNIX systems,it is traditional to return 0 upon success, and non-zero uponfailure. Here, we will use 1 to indicate failure.

Side note: if fopen() does fail, there are many reasons possibleas to why. You can use the functions perror() or strerror() toprint out more about why the error occurred; learn about those onyour own (using … you guessed it … the man pages!).

Once a file is open, there are many different ways to read fromit. The one we’re suggesting here to you is fgets(), which is usedto get input from files, one line at a time.

To print out file contents, just use printf(). For example,after reading in a line with fgets() into a variable buffer, youcan just print out the buffer as follows:

printf(“%s”, buffer);

Note that you should not add a newline (n) character to theprintf(), because that would be changing the output of the file tohave extra newlines. Just print the exact contents of the read- inbuffer (which, of course, many include a newline).

Finally, when you are done reading and printing, use fclose() toclose the file (thus indicating you no longer need to read fromit).

Details

• Your program wcat can be invoked with one or more files on thecommand line; it should just print out each file in turn.

  • In all non-error cases, wcat should exit with status code 0,usually by returning a 0 from main() (or by calling exit(0)).

  • If no files are specified on the command line, wcat should justexit and return 0. Note that this is slightly different than thebehavior of normal UNIX cat (if you’d like to, figure out thedifference).

  • If the program tries to fopen() a file and fails, it shouldprint the exact message “wcat: cannot open file” (followed by anewline) and exit with status code 1. If multiple files arespecified on the command line, the files should be printed out inorder until the end of the file list is reached or an error openinga file is reached (at which point the error message is printed andwcat exits).

    wgrep

    The second utility you will build is called wgrep, a variant ofthe UNIX tool grep. This tool looks through a file, line by line,trying to find a user-specified search term in the line. If a linehas the word within it, the line is printed out, otherwise it isnot.

    Here is how a user would look for the term foo in the filebar.txt:

    prompt> ./wgrep foo bar.txt
    this line has foo in it
    so does this foolish line; do you see where?
    even this line, which has barfood in it, will be printed.

    Details

  • Your program wgrep is always passed a search term and zero ormore files to grep through (thus, more than one is possible). Itshould go through each line and see if the search term is in it; ifso, the line should be printed, and if not, the line should beskipped.

  • The matching is case sensitive. Thus, if searching for foo,lines with Foo will not match.

  • Lines can be arbitrarily long (that is, you may see many manycharacters before you

    encounter a newline character, n). wgrep should work asexpected even with very long lines. For this, you might want tolook into the getline() library call (instead of fgets()), or rollyour own.

  • If wgrep is passed no command-line arguments, it should print”wgrep: searchterm [file …]” (followed by a newline) and exitwith status 1.

  • If wgrep encounters a file that it cannot open, it should print”wgrep: cannot open file” (followed by a newline) and exit withstatus 1.

  • In all other cases, wgrep should exit with return code 0.

  • If a search term, but no file, is specified, wgrep should work,but instead of reading from

    a file, wgrep should read from standard input. Doing so is easy,because the file stream stdin is already open; you can use fgets()(or similar routines) to read from it.

• For simplicity, if passed the empty string as a search string,wgrep can either match NO lines or match ALL lines, both areacceptable.

wzip and wunzip
The next tools you will build come in a pair, because one (wzip) isa file compression tool, and

the other (wunzip) is a file decompression tool.

The type of compression used here is a simple form ofcompression called run-length encoding (RLE). RLE is quite simple:when you encounter n characters of the same type in a row, thecompression tool (wzip) will turn that into the number n and asingle instance of the character.

Thus, if we had a file with the following contents:aaaaaaaaaabbbb
the tool would turn it (logically) into:
10a4b

However, the exact format of the compressed file is quiteimportant; here, you will write out a 4-byte integer in binaryformat followed by the single character in ASCII. Thus, acompressed file will consist of some number of 5-byte entries, eachof which is comprised of a 4-byte integer (the run length) and thesingle character.

To write out an integer in binary format (not ASCII), you shoulduse fwrite(). Read the man page for more details. For wzip, alloutput should be written to standard output (the stdout filestream, which, as with stdin, is already open when the programstarts running).

Note that typical usage of the wzip tool would thus use shellredirection in order to write the compressed output to a file. Forexample, to compress the file file.txt into a (hopefully smaller)file.z, you would type:

prompt> ./wzip file.txt > file.z

The “greater than” sign is a UNIX shell redirection; in thiscase, it ensures that the output from wzip is written to the filefile.z (instead of being printed to the screen). You’ll learn moreabout how this works a little later in the course.

The wunzip tool simply does the reverse of the wzip tool, takingin a compressed file and writing (to standard output again) theuncompressed results. For example, to see the contents of file.txt,you would type:

prompt> ./wunzip file.z

wunzip should read in the compressed file (likely using fread())and print out the uncompressed output to standard output usingprintf().

Details

• Correct invocation should pass one or more files via thecommand line to the program; if no files are specified, the programshould exit with return code 1 and print “wzip: file1 [file2 …]”(followed by a newline) or “wunzip: file1 [file2 …]” (followed bya newline) for wzip and wunzip respectively.

• The format of the compressed file must match the descriptionabove exactly (a 4-byte integer followed by a character for eachrun).

• Do note that if multiple files are passed to *wzip, they arecompressed into a single compressed output, and when unzipped, willturn into a single uncompressed stream of text (thus, theinformation that multiple files were originally input into wzip islost). The same thing holds for wunzip.Requirements wcat is passed in a file and will output the file on the terminal (use cat to familiarize yourself with how this

Requirements wcat is passed in a file and will output the file on the terminal (use cat to familiarize yourself with how this should work) wgrep is passed 2 arguments, the first is the word or words that the user is looking for and the second argument is the file that should be searched (use grep to familiarize yourself with this) wzip is passed a text file and redirects it to a compressed zip file • wunzip is passed a zip file and prints out the unzipped file in standard output Show transcribed image text Requirements wcat is passed in a file and will output the file on the terminal (use cat to familiarize yourself with how this should work) wgrep is passed 2 arguments, the first is the word or words that the user is looking for and the second argument is the file that should be searched (use grep to familiarize yourself with this) wzip is passed a text file and redirects it to a compressed zip file • wunzip is passed a zip file and prints out the unzipped file in standard output

Expert Answer


Answer to Hello I need 4 C++ functions. I need those C++ functions to implement unix comment cat, grep, zip and unzip works. Here …

OR