Assignment 1: Which


Introduction

In this assignment you will implement a variation of the Unix utility (program) which. To find out what which does, read the man page.

Purpose

Details

For the basic operation of which you must read the man page. Your program will follow the behaviour of which except as described below.

Match substrings

The standard version of which matches exactly the program name given as an argument. Your program will match any program name that contains an argument as a substring. (Remember that which may take more than one program name as an argument.)

For example see the output from which and mywhich below. Note that you may get different results from your account because the output depends on the PATH variable.

   $ which sh
   /bin/sh
   $ mywhich sh
   /local/bin/ssh

Options

You will implement only 3 of the options described in the man page:
--all, -aPrint all matches in PATH, not just the first
--skip-dot Skip directories in PATH that start with a dot.
--skip-tilde   Skip directories in PATH that start with a tilde.

You must use getopt_long() to process the command line parameters.

Tips

When you type in the name of a program (such as ls), the shell uses the PATH environment variable to find the program. Similarly, which searches the directories in the PATH environment variable. (Try printenv PATH.) You will need to figure out how to parse the PATH variable.

You will find the following man pages useful: getopt(3), getenv(3), string(3), and stat(2). The number in parentheses indicates the section of man page to which the command or function belongs. If two commands (or functions) have the same name but come from different sections, you can ensure you get the right one using man [section] name. For example man 1 stat or man stat produces the man page for the program stat while man 2 stat produces the man page for the system call stat.

Reading man pages is not easy. It takes practice and concentration. Don't get discouraged if you find man pages difficult to understand.

What to hand in

A C source code file called mywhich.c. 30\% of the mark will be for style and comments.

Submit your program electronically. Please see assignment submissions for details.


Last modified: Wed Sep 4 23:23:55 EDT 2002