<<

CSPP 51086: Systems Programming Pipes and Built-in Functions Assignment 5

Instructors: Todd Nugent Kenneth Harris

Purpose

In this assignment you will extend lgsh to implement pipeslines. The assignment will also extend the built-in commands by adding three additional:

• export

The description of assignment 5 is to be appended to that of assignment 4.

Submission

You must submit your implementation of pipelines before the next class. If you have this week you may also implement the three additional built-in operations; otherwise, the built-ins can be submitted with assignment 6. You will need to include a DOC file with your submission describing the additional features of your you want me to check.

Commandline syntax

Pipelines

There is a single new special token added to lgsh, for this assignment: |, the pipe operator. A commandline will now include both simple commands and pipelines.A pipeline is a sequence of one or simple commands separated by the pipe operator. The of a pipeline is

cmd1 [| cmd2] . . .

1 The standard output of cmd1 is connected to the standard input of cmd2. There is no limit to the length of the pipeline (i.e the number of pipes.) The standard input, standard output or both of a command will be assigned by the pipeline before any redirection as specified by any redirection expressions that are part of the command.

If the pipeline is not in the background (running commands in the background is implemented in assignment 6) then the shell will for the last command in the pipeline to complete and report the status of the entire pipeline by the exit status of the last command in the pipeline. It is up to you whether your shell waits on all processes in the pipeline to finish execution. However you decide, zombies must not be allowed to live.

Hints for pipes

Implementing pipes is not hard, but there are some important issues you need to think carefully about before you :

• Use makeargv with "|" as your delimiter to parse a pipeline. The presence of the pipe operator on a commandline (regardless of surrounding white space) separates commands.

• In a pipeline like

-l /dev | ^p | -l

you will need to create three processes and two pipes. The shell would all three children of the shell–but is this really convenient for your shell? must your shell wait for?

• The following is a legitimate pipeline

hello > bye | cat - < file1 > file2

Your shell needs to handle this correctly. Try this command with the bash shell. How do you explain the output? What is the order the bash shell sets-up I/O?

2 Built-in Commands

Your shell is responsible for three new built-in commands

• umask

• export

• cd

These will be described in this assignment, although you may choose to put-off their implementa- tion until Assignment 6.

These three commands must be implemented as built-ins. Consider cd. This command changes the current working , which is an attribute of a process. If you wrote cd as an external program, then every time you implemented cd the process running cd would change its current directory, but this would not affect your shell, a different process. When you cd the prompt, the shell runs an internal command which actually changes the current of the shell itself. For the same reason, umask must be run as a built-in command–the file creation mask is an attribute of each individual process, so the shell must change its own mask. The environment variables are also an attribute of a process, stored in the environ, so the shell itself is the only process that can access this variable.

umask

The file creation mask is a nine-bit integer that takes effect when a process creates a new file. Suppose a process opens a file for creation with the permissions set as perm and the file creation mask for the process is mask. Then the actual permissions for the file (provided it is created by this open) is perm & ~mask. The effect of the file creation mask is to block certain permissions from taking effect. The mask is set in the shell when you login (either as a default value of the shell, or assigned in your .bashrc file), and all processes executed at the shell prompt inherit the mask. The for changing this mask for a given process is umask(2).

Below is a description of the built-in command, also called umask which your shell will implement. POSIX specifies a -S option, for symbolic input (similar to that given to (1)), but your shell will only accept numeric input.

3 NAME umask : get or set the mode creation mask

SYNOPSIS umask [mask]

DESCRIPTION The umask utility sets the file mode creation mask of the current shell execution environment to the value specified by the mask operand. This value affects the initial value of the file permission bits of subsequently created files. If the mask operand is not specified, the umask utility writes to standard output the value of the invoking process’ file mode creation mask, as obtained by umask(2).

OPTIONS None.

RETURN 0 : The file mode creation mask was successfully changed, or no mask operand was supplied >0 : The argument was not a value between 0000 and 0777 ( notation.) export

Your shell will use export only to display the shell’s environment variables. These are stored in extern char **environ from . environ is a NULL-terminated list of of the form name=value. You can walk down this list just as you would the argv list–use the fact that it ends in NULL. Your shell will only this list. The reason I have included it, is because cd changes the , and I will use your export to check this. Your shell will display the same list as the bash shell (provided you haven’t changed the environment list) but with a slightly different format.

NAME export -- display the environment variables for the shell

SYNOPSIS export

DESCRIPTION export will display the environment variables of the shell, as stored in the global environ variable. It will display these using the format "%s=%s"

4 RETURN export always returns 0 cd

Your shell will implement cd to change the current working directory. The behavior will be close to, but not quite the same as the actual command executed by the bash shell. The real cd when executed with a directory does the following set of checks:

1. If dir is an absolute name (begins with ’/’) then dir is the absolute path. 2. If dir is a relative path name then check to see if it is a directory in the current working directory. 3. If dir is a relative path name, but not a directory in the current working directory, then search the CDPATH variable for dir (just as you would search the PATH variable for an executable.) 4. If the above fail to produce a directory whose absolute path ends in dir, then report failure.

Your shell will skip the third step. (The variable CDPATH is not usually set to a value.)

NAME cd : change the working directory

SYNOPSIS cd [directory]

DESCRIPTION The cd utility changes the working directory of the current shell execution environment using the chdir(2). If no directory is specified, then cd behaves as if the directory named in the HOME environment variable was specified as the directory operand. If the environment variable HOME is not defined no further action is taken. The cd utility sets the PWD environment variable to an absolute path name, as given by getcwd(3), if it successfully executed chdir(2). If the PWD environment variable does not exist, cd will set it to the current working directory, as given by getcwd(3). Otherwise, on an error from chdir(2), cd will display an error message and leave the PWD environment variable as is.

RETURN VALUE 0 on success >0 on error

5 There is no system call for obtaining the current working directory. There is a standard function, getcwd(3), which find the absolute path name of the current working directory. It is prob- ably implemented much like the pwd(1) utility described by Molay in Understanding Unix/ Programming, section 4.5. Do not use the environment variable PWD to access the current working directory, it is safer to use the function getcwd. There are also very convenient standard library functions for getting and setting environment variables getenv(3) and putenv(3). (So, you will not need to access and search environ yourself.)

6