Question: Part B tucp The second utility you will build is called tucp , a variant of the UNIX tool cp . In this project, this

Part B

tucp

The second utility you will build is called tucp, a variant of the UNIX tool cp. In this project, this command is used to copy files or a group of files. It creates an exact image (copy) of a file on a disk. The tucp command requires at least two filenames in its arguments.

The following are the possible command usages:

tucp Sourcefile Destinationfile

tucp Sourcefile Directory

tucp Sourcefile-1 Sourcefile-2 Sourcefile-3 Sourcefile-n Directory

Details

  • If the tucp command contains two file names, then it copies the contents of the 1st file to the 2nd file. If the 2nd file doesnt exist, then it first creates a file with the specified name and the content of the first file is copied to it. But if the second file already exists, then it is simply overwritten without any warning.
  • If the tucp command has one or more arguments, specifying source file names and following those arguments, an argument specifying a directory name, then the command copies each source file to the destination directory with the same source file name. The destination file is created if did not exist, or overwritten if it already existed.
  • The Linux function stat() can be used to determine information about a file. The stat struct returned by the function includes a field, st_mode , that can be used to determine if the path evaluated by stat() is a file or a directory. Use man to learn about the functions stat() and inode().

Project 0 - Part B: tucp Create a .c file called tucp.c that is compiled with -Wall and -Werror flags gcc -o tucp tucp.c -Wall -Werror how to compile Compiling with -Wall -Werror is not optional! There are several specific ways to run: must have 2 or more command line args: ./tucp [sourcefile] [destinationFile] ./tucp [sourcefile] [directory] ./tucp [sourcefile1] [sourcefile2] [sourcefile3] ... [sourcefileN] [directory]

Examples prompt> ./tucp [sourcefile] [destinationFile] Two files names copy the contents of [sourcefile] into [destinationFile] If [destinationFile] doesnt exist create it, then copy [sourcefile] If [destinationFile] does exist simply overwrite it! Check out mode arguments to fopen() prompt> ./tucp [sourcefile] [directory] prompt> ./tucp [sourcefile1] [sourcefile2] ... [sourcefileN] [directory] With one or more source files and a directory, copy each source file into the named directory with the same name as the source file If the source file is already in the named directory, overwrite it - otherwise create it in the named directory

Things to Think About Think about what would happen to make the program fail Too few arguments what happens if just >> ./tucp Errors with source file what happens if it doesnt exist? Be sure to be robust with testing these error cases! Remember that int argc and char **argv can be used to parse command line arguments See examples from last weeks slides for clarification Remember proper coding practice Error check library function calls before moving onwards Use man pages and return values to your advantage Modular coding - isolate repeated functionality Dont leave streams open - remember to close them after use or error!

Useful Functions int stat(const char *pathname, struct stat *restrict statbuf) Man page Returns information about a file in a buffer that is pointed to by statbuf Information about the file pointed to by pathname is stored in the struct The stored struct contains multiple fields that can be used to determine the type of file look towards utilizing the st_mode field Use in conjunction with inode masks to determine file type Man page

Using stat() example struct stat statPath; int n = stat(path, &statPath); if (n == -1) { // stat returns -1 on error, 0 on success perror("stat"); exit(EXIT_FAILURE); } // lets check to see if the path that we are checking is a file if(S_ISREG(statPath.st_mode)){ printf(File: %s , path); } else { printf(%s is not a file! , path); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!