Lab overview
Lab 01
Lab 02
Lab 03
Lab 04
Lab 05
Lab 06
Lab 07
Lab 08
Lab 09
Lab 10
Lab 11
Lab 12
Lab 13
Review

[Course home page]

CSC 209 lab 07 exercises, later June

[solutions are available (requires teach.cs authentication)]

No attendance requirement

There are no classes in the latter half of June, and there is thus no attendance requirement relating to this lab.

If you don't want to work on this lab over the break, I would urge you to get started early. On the other end, it won't be due until 18:00 on Tuesday July 5, which is two full days after classes have resumed.


1. Files

Let's look at that rot13.c program from lab 04 yet again (e.g. /u/csc209h/summer/pub/lab/soln/04/rot13.c).

In lab 06 we modified it to take one mandatory file name argument, and to read from there instead of from stdin.

Let's now make it function like a normal "zero or more file names" unix tool. If argc is 1 it processes the standard input (as originally), otherwise it loops through the arguments: fopen(), process it, fclose().

So instead of getchar() in your loop, you use getc(fp). You still use putchar().

Avoid duplicating code. When you go to process stdin, do so with the same code as you use for processing a file by filename, probably in a separate function, by making fp be the value stdin (defined in stdio.h).


1b (optional): Next, change it to take a (mandatory) rotation amount as argv[1] rather than always rotating by 13.

So in this new version, argc is required to be at least 2, and argv[1] is the rotation amount; but after that, if argc == 2 it processes the standard input (as before), otherwise it loops through the remaining arguments (fopen(), process it, fclose()).

(So this is more like grep's command-line processing, in that grep similarly has a mandatory first argument.)


2. pwd (for credit)

Background required for this exercise:

Your task for submission in this lab is to write a program which does most of the work of "pwd", as follows:

First, call lstat() on "." to find out the inode number of the current directory (the "st_ino" member of the struct stat). (Remember that we looked at stat() and lstat() in lab 06.)

Then we change our current directory to the parent directory, with chdir("..").

Then we want to read the parent directory with opendir()/readdir() (using "opendir(".")" because we've now cd'd there), but unlike in the assignment two directory traversal problem, we are mostly interested in the d_ino value in the struct returned by readdir(). We are looking for the original directory in the parent directory. When we find it, we print its name.

Then call chdir("..") again, and repeat until the inode number is 2. At this point, we're at the root directory. Please print "[root]" to indicate this.

So if the real pwd would output "/home/ajr/whatever/example", your pwd will output

example
whatever
ajr
home
[root]

Except that on systems like teach.cs, the above algorithm will often stop short of the real root directory, because there are multiple filesystems which are "mounted" in the directory tree, as we discussed when discussing the unix filesystem; and the root directory of any filesystem has inode number 2. To avoid this problem, pwd has to compare "major and minor device numbers" too, but we won't do that for this exercise today. So your program will show you only part of the pwd data, until it gets to a filesystem mount point. All this is to say that you might have to cd into some subdirectories to make the output interesting for testing.

Remember to include all relevant error checking, with appropriate use of perror() (although the appropriate argument to perror() isn't always clear, but just try to do something appropriate there).

The "real" pwd in linux these days actually uses a totally different algorithm, which is a linux-specific interface for querying the current working directory. But the above is the traditional algorithm. But this means, especially given the fact that mount points cause this simplified version to stop short of the root directory, that you might want to compare your program's behaviour to mine. So I've put a compiled solution in /u/csc209h/summer/pub/lab/07/pwd

(To be clear: You may not use getcwd(). You are to use the above algorithm. An algorithm similar to the above traditionally appears in getcwd(). Your task is to implement this algorithm.)

(Note that you don't need to declare the argc and argv parameters to main (and don't need to check usage).)


To submit

Submit your best version of pwd.c for credit for this lab by 18:00 (sharp) on Tuesday July 5. To get the mark, it has to perform the basic pwd function using the above algorithm, and it has to compile with "gcc −Wall pwd.c" on the teach.cs machines with no errors or warning messages.

Submit with the command

        submit -c csc209h -a lab07 pwd.c
and the other submit commands are as before.