12.14. Exercises
The programs here are potentially dangerous. Test them in a mostly empty directory to make it difficult to accidentally delete something useful.
See Appendix A for answers to the following exercises:
[12] Write a program to ask the user for a directory name and change to that directory. If the user enters a line with nothing but whitespace, change to his or her home directory as a default. After changing, list the ordinary directory contents (not the items whose names begin with a dot) in alphabetical order. (Hint: Will that be easier to do with a directory handle or with a glob?) If the directory change doesn't succeed, alert the user but don't try show the contents. [4] Modify the program to include all files and not just the ones that don't begin with a dot. [5] If you used a directory handle for the previous exercise, rewrite it to use a glob. If you used a glob, try it now with a directory handle. [6] Write a program that works like rm, deleting any files named on the command line. (You don't need to handle any of the options of rm.) [10] Write a program that works like mv, renaming the first command-line argument to the second command-line argument. (You don't need to handle any of the options of mv or additional arguments.) Allow for the destination to be a directory; if it is, use the same original basename in the new directory. [7] If your operating system supports it, write a program that works like ln, making a hard link from the first command-line argument to the second. (You don't need to handle options of ln or more arguments.) If your system doesn't have hard links, print out a message telling what operation you would perform if it were available. Hint: This program has something in common with the previous one and recognizing that could save you time in coding. [7] If your operating system supports it, fix up the program from the previous exercise to allow an optional -s switch before the other arguments to indicate you want to make a soft link instead of a hard link. (Even if you don't have hard links, see whether you can at least make soft links with this program.) [7] If your operating system supports it, write a program to find any symbolic links in the current directory and print out their values (like ls -l would: name -> value).
|