CSCI.4210 Operating Systems
Fall, 2003
Programming Assignment 1

Part I (Unix)

One of the criticisms of Unix is that when you delete a file using the command rm (remove), it is gone forever. Rewrite the rm command so that instead of deleting the file, it moves it to a directory called trash in the user's home directory. If trash does not exist, your program should create it. The directory should be created so that the owner has read, write and execute permissions, and no one else has any permissions. The filename in the trash directory should be the same as the name of the deleted file. For example, if the user wanted to delete the file ~/OpSys/proj1/test1.c, your program should create a file called test1.c in trash.

If there is already a file of that name in trash, your delete program should alert the user, and ask the user if he or she wants to replace the earlier version with the new version or rename the file currently being deleted to something else.

For example, on Tuesday, the user deletes a file called foo. Your program moves this to the trash directory and gives it the name foo. On Wednesday, the user tries to delete another file called foo, either from the same directory or from a different directory. Your program should detect that there is already a file called foo in the trash directory so it displays this message.

There is already a file called foo in the trash directory.
To overwrite it, enter 1. 
To rename the file enter 2.  
To cancel the delete, enter 3.

If the user enters 2, the user is prompted for a new name.

The argument passed in to your program must be a file in the current working directory and should be a relative pathname without any slashes.

Your program should do extensive error checking, including but not limited to the wrong number of arguments, trying to remove a nonexistent file, etc.

You are not permitted to use the system system call.

Here are some system calls that you might want to use. You can use the Unix man pages to get more information.

int link(const char *existing, const char *new);
The link() function creates a new link (directory entry) for the existing file and increments its link count by one. The existing argument points to a path name naming an existing file. The new argument points to a pathname naming the new directory entry to be created.

int unlink(const char *path);
The unlink() function removes a link to a file. It removes the link named by the pathname pointed to by path and decrements the link count of the file referenced by the link.

int mkdir(const char *path, mode_t mode);
The mkdir() function creates a new directory named by the path name pointed to by path. The mode of the new directory is initialized from mode (see chmod(2) for values of mode). The protection part of the mode argument is modified by the process's file creation mask (see umask(2)).

Part 2 (Windows)

In the windows world, deleting a file usually copies it to the trash directory. However, as an exercise, you can write the same code for Windows that you wrote for Unix.

Since there is no concept of a home directory in some flavors of Windows, your trash directory should be in C:\. Otherwise the functionality should be the same.

Here are some Win32 APIs that you might want to use.

BOOL CreateDirectory(LPCTSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes);
This creates a new directory. The first argument is the pathname, you can use NULL as the second argument.

BOOL CopyFile(LPCTSTR lpExistingFileName, LPCTSTR lpNewFileName, BOOL bFailIfExists);
Copies an existing file to a new file. If the third argument is TRUE, this will fail if the new file already exists.

BOOL DeleteFile(LPCTSTR lpFileName);
Deletes a file

Note that there is a function called MoveFile, but you are not allowed to use this. It makes the project too easy, and Rensselaer students are insulted if projects are not sufficiently challenging.

Instead of passing the name of the file as an argument, your program should prompt the user to enter a file name.

Grading

Each project will be graded independently.

Compiles (and is a sincere attempt) 40%
Removes the file correctly 10%
Creates the new file correctly 10%
Creates the trash directory if it is not there 10%
Handles duplicate file names correctly 10%
Error handling 10%
Properly commented and properly modularized 10%

Here are some programming guidelines that you must use for this and all other projects for this course and I recommend that you keep these in mind for the rest of your life.