Laboratory Exercises For Computer Science 213

More C and Unix-Style Shell Programming

More C and Unix-Style Shell Programming

Summary: This lab continues work with the my-shell program of the previous lab. The lab also explores perspectives of the assigment operator =, in order to clarify string manipulation in C.

Strings and Assignment: Strings in C may be accessed in either of two ways:

These two situations are illustrated in the following diagram:

In general, a string variable might be either of these, depending upon the specific declaration and the langauge.

Program ~walker/c/shell-programs/string-example.c illustrates both types of variables. In that program, variables s and c are arrays, where characters are stored directly into memory beginning at the designated locations. In contrast, variables a, b, and d, are pointers.

  1. What is the effect of the statements a = s;, b = a;, and d = c; ?

  2. Draw a diagram similar to the one above, showing the state of memory after initialization in string-example.c . (Note that C procedure strcpy just copies the string given in the second parameter to the array with base address given by the first parameter.)

  3. Copy program string-example.c to your account and run it. Then, use your answers to steps 1 and/or 2 to explain why you get the given output.

  4. After initialization, add the statement c = a; to the program, and recompile. Explain what happens. (Why do you think the assignment operator = applies to single values and pointers, but not to arrays?)

As a contrast, consider Program ~walker/java/stringExample.java, which is similar to string-example.c translated to Java. In Java, one cannot change a string directly (strings are said to be immutable or read-only), but one can reset a string variable to another value.

  1. Copy ~walker/java/stringExample.java to your account. Then run it with the following commands:

    
    /net/jdk1.2.2/bin/javac stringExample.java
    /net/jdk1.2.2/bin/java stringExample
    
  2. Compare the output of string-example.c and stringExample.java. How are the results similar or different?

  3. In Java, almost all variables are pointer variables, but an assignment operator often creates a copy of an object. Use this information about language design to explain the output in Java.

A Shell Shell: The lab on simple Unix-Style shells discusses a simple shell program contained program ~walker/c/shell-programs/my-shell.c . In that program, code already existed to set a variable PATH, and you added code to set variable PWD. The resulting program looked something like ~walker/c/shell-programs/my-new-shell.c . Thus, my-new-shell.c processes three commands for shell variables:

my-shell.c also contains a facility for handling commands that do not affect environmental variables.

However, that the my-new-shell.c program does not use either its PWD or PATH programs in processing -- referring instead to the corresponding variables of the original window.

  1. Either use your my-shell.c program from the previous lab, or copy ~walker/c/shell-programs/my-shell.c to your account, compile it, and run it.

  2. Modify my-new-shell.c, so that searching is done based on my-new-shell.c's variables PWD and PATH, rather than those set in the terminal window.

    One approach is to use strtok to obtain successive directories from the PATH, use a directory name together with the command name to obtain a potential full_command_string, and check a command actually exists for the full_command_string. If the command exists, it is executed; if not, the search of directories continues until all directories on the path are searched.

    More details of this approach are given in the following steps

    1. Declare the following variables:

      • working_str -- an array of characters (to be used for searching the PATH),
      • full_command_string -- an array of characters (to be used to store the full command name, including the command's full path), and
      • dir_str -- a pointer to a string (to be used to identify a directory during the search of directories).

    2. After reading the command_line, initialize working_str to the PATH variable by using strcpy (working_str, PATH); or string_copy .

    3. Use a loop to search the PATH to find the directory containing the desired command.(Actual processing should use working_str rather than PATH, however. Why?)

      Within the loop,

      • Use strtok and the token : to extract a directory name (stored through dir_str). Initially, strtok uses parameter working_str to start the search. Thereafter, the first parameter to strtok should be NULL to indicate that searching continues where it left off.

      • Combine the directory string dir_str and the command_name to get a full_command_string. For example, given directory /bin and command ls, you would obtain the full command string /bin/ls. Note that an extra slash character / may be needed between the directory name and the command.

        Such work can be done by starting with the strcpy or string_copy procedures to insert the dir_str into full_command_string, then adding the slash character /, and then concatenating the command_name . See the on-line manual for C procedure strcat to use that existing function for concatenation, or write your own concatenation procedure for that purpose.

      • Use the fopen statement -- opening for reading -- to test if the command is located at a specified location (e.g., full_command_string). (An error on opening indicates the file does note exist.) As with other commands, details of fopen may be found by typing man fopen.

      • If the full path name of the desired command is found, the search is over. If not the search continues within the search loop.

    4. Upon finding the full path name for a command using fopen, close the file, and call execv -- which is just like execvp, except that the first parameter should be the full path name rather than just the program name. (If the command is not found, an error message should be returned instead.)

    5. Adjust your program as necessary, so that the directory <dot> (.) on the search path refers to the directory given by PWD, rather than any directory that might be specified in the terminal window.

Work to be turned in: Note: For most steps, it is expected that the answer will be short.


This document is available on the World Wide Web as

     http://www.math.grin.edu/~walker/courses/213.fa00/lab-c-and-shell.html

created October 2, 2000
last revised October 2, 2000