Вы находитесь на странице: 1из 3

CS 149, Summer 2011 Assignment #1

The assignment is to take a mycopy program, supplied by me, and turn it into a true Unix command, executable like any Unix command, directly from the command line. What follows is some marginally helpful background on how commands work in Unix (and how programs work in C). All of this is discussed far more fully in, for example, Ouallines Practical C Programming, or in Kernigan & Ritchies The C Programming Language. Digression & review on hooks in C to facilitate writing Unix-like commands: All C programs require a single main (almost always written more correctly as main()) function, which defines the starting point for the program. The main() function takes optional arguments usually denoted argc and argv (you should do so for this assignment). argc is the number of arguments, and argv is an array of pointers to strings. And, surprise, surprise, the length of argv is argc. The full expression for the main() function in C is then int main (int argc, char *argv[]) where Ive assumed a return value type of int as well. The first entry in argv (argv[0]) is the name of the program youre running. The command line processor will parse your command line and supply main() with all the tokens it will need to execute correctly. This includes the processing of wildcard characters. To take a specific example, if the command line were % args this is a test (% is the Unix command prompt) then we would see: argc = 5 argv[0] = argv[1] = argv[2] = argv[3] = argv[4] = argv[5] = args this is a test NULL // Note: this is not \0

Other notes (end of digression): The program you start with is pretty silly. In fact, all it does is open a file foo, in the current directory and copy it to a file bar, also residing in the current directory. If bar exists it will try to overwrite it (and probably succeed). If for some reason it cant, it will crash badly. Your program should not crash. Instead, your program will print an error message and terminate normally (this is just a copy command, after all, so you dont need to wait for a quit command from the user). Other requirements: Your program should do at least the same sort of error checking that the Unix cp command does. For example, if you forget to enter the correct number of arguments, you should get an error to that effect. Also, if the target file exists, and isnt a directory, you should return a warning message that you are about to overwrite a file. Much of this program in its final form (and much of the work you do) will involve tracing potentially complex directory paths in the destination argument and making sure they create the file, with the right name, in the right place. Obviously, your program will also need to respond correctly if the path for either file is incorrect. Further, you need to consider for example the following form of the standard Unix cp command, and make sure your program can duplicate it: cp <arg1> <arg2> <arg3> <arg4> <arg5> In this case the first four arguments are filenames (with possibly complex paths), and the fifth argument is a directory. For this form of the command to work you need to be able to 1) detect that the fifth argument is in fact a directory (otherwise you return an error), and 2) parse the first four arguments and split off the relevant portion of each filename in order to create the correct full path for all the final files.

Note: you do not have to implement the sort of directory-to-directory copying functionality that is another characteristic of the standard Unix command, since this would involve tracing potentially complex directory paths. unless you want extra credit . This would involve supporting the -r option. How do you tell if a path youve been given is a valid directory? One way is given below (although a kludgy but equally effective way would be to just try to open a target file underneath the supposed directory for writing and then see if you get an error): #include <sys/stat.h> struct stat buffer; int success; char fname [256]; int main() { printf(enter filename or directory name (full path): ); fgets (fname, sizeof(fname), stdin); success = stat (fname, &buffer); if ( (S_ISDIR(buffer.st_mode)) != 0) printf (The file \%s\ is a directory\n, fname); else printf (The file \%s\ is a regular file\n, fname); }

Вам также может понравиться