Previous: 13.3 A Small Data Base Example
Up: 13 Files and the Operating System
Next: 13.5 Summary
Previous Page: 13.3 A Small Data Base Example
Next Page: 13.5 Summary

13.4 Operating System Interface

As stated at the beginning of the chapter, all of our programs so far have had minimal interaction with the environment in which they are running, i.e. the operating system, and in particular the shell. One area where we could make use of operating system support is in specifying files to be used in execution of the program. In our previous examples we have either redirected the input or output when running the program (and read or written to the standard input or output in the program code), or prompted the user explicitly for the file names once the program has begun executing. However, this is not the only (nor most convenient) way to specify files to a program. It should also be possible to pass arguments to a program when it is executed. An executable program is invoked by a command to the host operating system consisting of the name of the program. However, the entire command may also include any arguments that are to be passed to the program. For example, the C compiler does not prompt us for the file names to be compiled; instead we simply type the command:

cc filename.c
The entire command is called the command line and may include additional information to the program such as options and file names. The C compiler (and most, if not all, other commands) is also simply a C program.

There must be a way this additional information can be passed to an executing program. There is. The command line arguments are passed to the formal parameters of the function main(). We have always defined the function main() with no formal parameters; however, in reality it does have such parameters. The formal parameters of main() are: an integer, argc, and an array of pointers, argv[]. The full prototype for main() is:

int main(int argc, char * argv[]);
Each word typed by the user on the command line is considered an argument (including the program name). The parameter argc receives the integer count of the number of arguments on the command line, and each word of the line is stored in a string pointed to by the elements of argv[]. So, if the command line consists of just the program name, argc is 1 and argv[0] points to a string containing the program name. If there are other arguments on the command line, argv[1] points to a string containing the first argument after the program name, argv[2] to the next following one, and so forth. In addition, main() has an integer return value passing information back to the environment specified by the return or exit statement terminating main(). Recall, we have always used exit in the form:
exit(0);
A common convention in Unix is that a program terminates with a zero return value if it terminates normally, and with non-zero if it terminates abnormally.

Figure 13.11 shows a program that prints the values of argc and each of the strings pointed to by the array argv[]. The program then uses the first argument passed on the command line as a ``source'' file name, and the second as a ``destination'' file name and copies the source file to the destination. The program returns zero to the environment to indicate normal termination.

Sample Session with a command line:

filecopy filecopy.c xyz

The number of arguments in the command line is 3, and each of the strings pointed to by the array argv[] is then printed. The first argument is the complete path for the program name as interpreted by the host environment. The program then opens the files and copies the file filecopy.c to xyz.

In addition to receiving information from the operating system, a program can also call on the shell to execute commands available in the host environment. This is very simple to do with C using the library function system(). Its prototype is:

int system(const char *cmmdstr);
The function executes the command given by the string cmmdstr. it returns 0 if successful, and returns -1 upon failure. Examples include:
system("date");
     system("time");
     system("clear");
The first prints the current date, the second prints the current time maintained by the system, and the third clears the screen.



Previous: 13.3 A Small Data Base Example
Up: 13 Files and the Operating System
Next: 13.5 Summary
Previous Page: 13.3 A Small Data Base Example
Next Page: 13.5 Summary

tep@wiliki.eng.hawaii.edu
Sat Sep 3 07:17:28 HST 1994