Programming Assignment 7

Due Thursday, December 13
15 points


For this assignment you will write two short C shell scripts for controlling permissions on you files and directories. One program, mkpriv, denies access for everyone except you to files and directories specified on the command line. The other, mkpub, gives everyone read access, and execute access where appropriate, to files and directories specified on the command line. When you finish, both programs will act recursively so that you can open up or close down permissions on a a directory and all of its contents with a single command.

Setting Up a Test Directory

You will not want to work with the shell scripts in this assignment on directories that contain useful files and subdirectories, so you will need to begin by creating a test directory with some junk files and subdirectories. This will also give you some practice navigating a directory hierarchy.

Start off by creating a directory named test7 using the mkdir command. This can be created anywhere in your directory hierarchy. Give everyone read and execute permission on that directory using the chmod command. Then move into the directory test7.

Inside test7, create subdirectories and files with the following commands.

Now move into test7/d1 and repeat the above steps. Then move into test7/d1/d1 and repeat just the last two steps (no more subdirectories). Also move into test7/d1/d2 and test7/d2, repeating the last two steps in each.

Here is a listing for the completed test7 directory that I created. You should get a similar listing, but the permissions for "group" and "others" may be different.

daneel 83% pwd
daneel 83% ls -lR
total 4
drwxr-xr-x   4 gshute        512 Nov 22 11:42 d1/
drwxr-xr-x   2 gshute        512 Nov 22 11:40 d2/
-rw-r--r--   1 gshute          5 Nov 22 11:29 f
-rwxr-xr-x   1 gshute          2 Nov 22 11:41 x*

total 4
drwxr-xr-x   2 gshute        512 Nov 22 11:43 d1/
drwxr-xr-x   2 gshute        512 Nov 22 11:49 d2/
-rw-r--r--   1 gshute          5 Nov 22 11:30 f
-rwxr-xr-x   1 gshute          2 Nov 22 11:42 x*

total 2
-rw-r--r--   1 gshute          5 Nov 22 11:30 f
-rwxr-xr-x   1 gshute          2 Nov 22 11:43 x*

total 2
-rw-r--r--   1 gshute          5 Nov 22 11:49 f
-rwxr-xr-x   1 gshute          2 Nov 22 11:49 x*

total 2
-rw-r--r--   1 gshute          5 Nov 22 11:32 f
-rwxr-xr-x   1 gshute          2 Nov 22 11:40 x*

When you get a listing similar to this, you can start writing the shell scripts.

Writing the mkpriv Shell Script

Both of the shell scripts should be written at the top level of your test7 directory. Most of what you need to know to write the shell script is described in The C Shell Tutorial. In particular, the section Control Structures describes the foreach loop that you can use for processing all of the command-line arguments. When your program starts, it has a built-in array variable, $*, that contains all of the command-line arguments. With the command "chmod go-rwx file", you can deny access to file for everyone else without changing access permissions for yourself. You can test if a file is a directory using conditions described in Conditional Expressions.

You should initially write the loop without recursion and test it on files and directories at the top level of the test7 directory. When that is working correctly, you can add recursion by adding an if statement that checks if an argument is a directory. If it is, invoke your shell script with "/*" appended to the argument. That will recur into the directory. If you have trouble, try invoking the shell script using the full path name of the script.

Check your recursive program by giving the "ls -lR ." command that you used for checking the test directory. It should list the same files, but all permissions should be denied to everyone but you.

Writing the mkpub Shell Script

The code and testing for mkpub is similar to that for mkpriv except that you need to give execute permission on directories if you want others to be able to search the directory. You should also give them execute permission on files for which you have execute permission. You would normally not want to give write permission to others.

What to Demonstrate and Turn In

You should either demonstrate or turn in a typescript showing the following sequence of commands, given from inside the test7 directory:

Depending on how your executable search path is set up, you may need to invoke your scripts as "./mkpriv" and "./mkpub". You also should turn in copies of both of your C shell scipts.