CIT 042 Index > Operating System Interface

Programming Assignment
Interfacing to the Operating System

Write a “mini-file manager” program that will process commands typed from the command line. Note that the perl program is executed once only, with no command line arguments. The program prompts you for commands repeatedly until you tell it to quit. See sample output from a run of this program.


Prints the current working directory. Use this code:

use Cwd;                    # this goes at the top of your file
my $working_dir;            # variable declaration
$working_dir = getcwd();    # this gives you what you want
cd [name]

Changes directory to the given name. If no name is given, then changes directory to the home directory. See more details.

list name [name...]

For each name given on the command line: if it’s a simple file, the command will simply print the file name and its file size. If the name is a directory, it will list all the files in that directory and their file sizes. The command must give an error message if a file name does not exist. You may choose to have the . and .. directories appear in a directory listing or not, as you wish.

del name [name...]

Deletes the given files. If a file doesn’t exist, the command gives an error message. This command does not ask the user if she wishes to delete the files; it just goes ahead and deletes them.

create name [name...]

Creates the given files if they don’t already exist. To create a file, just open it and close it immediately afterwards. If you can’t create a file, give an error message. (This will happen if you try to create a file in a directory that doesn’t exist or where you don’t have permission). If a file already exists, the program does nothing to it.

Exits the program.

If the user enters an invalid command, give an error message. Blank lines and lines beginning with a # should be ignored. You do not need to handle file names with blanks in them.

There is no single part of this program that is particularly difficult; it just has a lot of parts.

Finding the home directory

Unfortunately, the %ENV entries for home directory are different on UNIX and Windows. Here is code that will reliably get the name of your home directory:

if (exists($ENV{"HOME"}))
	$home_dir = $ENV{"HOME"}; # UNIX
   $home_dir = $ENV{"HOMEDRIVE"} . $ENV{"HOMEPATH"}; # Windows

When you are finished:

Name your file and email it to the instructor.