CS248 Menu Buttons

UNIX Programming

"Chapter Four - The UNIX Environment"

Chapter Outline

Lecture Notes

The UNIX Environment

When we write a program for UNIX we have to take into account that the program will run in a multitasking environment.

We will be looking at:

Program Arguments

When a UNIX progam written in C runs it starts at the function main.

For UNIX programs, main is declared as,

where argc is a count of the program arguments and argv is an array of character strings representing the arguments themselves.

You might als see UNIX programs declaring main as:

Whenever the operating system starts a new program, the parameters argc and argv are set up and passed to main.

For example, if, in the shell, we give the command,

the program myprog will be started at main. with parameters:

Command line arguments are useful for passing information to programs. They can be used to set flags or switches.

For example, the sort program takes a -r switch to reverse the normal sort order:

Command line options can be confussing. For example: If this isn't bad enough, some programs make the option +x perform the opposite function to -x.

Try It Out - Program Arguments

Her's a program, args.c, that examines its arguments:

When we run this program, it just prints out its arguments and detects options.

Other options might also be defined:

How It Works

The program simply uses the argument count, argc, to set up a loop to example all of the program arguments. It detects options by looking for an initial dash..

getopt

Linux gives us the getopt facility, which supports the use of options with and without vlaues and is simple to use.

The getopt function takes the argc and argv paramters as passed to the program's main function and an options specifier string.

The getopts command in bash performs a very similar function. The call,

would be used to handle our example above.

We call getopt repeatedly to get each option in turn. It has the following behavior.

Try It Out - getopt

Let's use getopt for our example and call the new program argopt.c:

Now, when we run the program, we see that all the command line arguments are handled automatically:

How It Works

The program repeatly calls getopt to process option arguments until none remain, when getopt returns -1.

Environment Variables

Environment variables can be used to control the behavior of shell scripts and other programs.

Each user has an environment varible, HOME, that defines his home directory, the default starting place for his or her session.

We can examine environment variables from the shell prompt:

A C program may gain access to environment variables using the putenv and getenv functions.

Try It Out - getenv and puten

The following program is to print out value of any environment variable we choose.

1. The first few lines after the declaration of main ensure that the program, environ.c, has been called correctly.

2. That done, we fetch the value of the variable from the environment, using getenv:

3. We set the variable to the value of the second argument if any, by constructing a string of the form name=value and then calling putenv:

4. Finally, we discover the new value of the varible by calling getenv once again"

When we run this program, we can see and set environment varibles:

Use of Environment Variables

Programs often use environment variables to alter the way they work.

User can set their default environment variables via a .profile or by specifying variables on the shell command line. For example:

The shell takes initial varible assignments as temporary changes to environment variables. Each user could then specify his or her own default, or use a shell command to set it on a run-by-run basis: or $ CDDB=mycds cdapp

The environ Variable

The program environment is made up of strings of the form name=value. This array of strings is made available to programs directly via the rnviron variable which is declard as:

Try It Out - environ

Here's a program, showenv.c, that uses the environ variable to print out the environment varibles:

When we run this program on a Linux system we get the following output, which has been abbreviated a little:

How It Works

This program iterates through the environ variable, a null-terminated array of strings, to print out the whole environment.

Time and Date

It can be useful for a program to be able to determine the time and date.

Times are handled using a defined type, a time_t. The variable and functions for manipulating time values are in the header file time.h.

Try It Out - time

Here is a simple program, envtime.c to demonstrate the time function:

When we run this program, it prints the low-level time value every two seconds for 20 seconds.

How It Works

The program calls time with a null pointer argument, which returns the time and date as a number of seconds. The program sleeps for two seconds and repeates the call to time for a total of ten times.

The function difftime, that will calculate the difference in seconds between two time_t values and return it as a double.

The function gmtime breaks down a low-level time value into a structure containing more usual fields:

The structure tm is defined to contain at least the following members:

The range for tm_sec allows for the occasional leap second, or double leap second.

Try It Out - gmtime

Here's a program, gmtime.c, that prints out the current time and date using the tm structure and gmtime:


When we run this program, we get a good approximation of the time and date:

How It Works

The program calls time to get the low-level time value and then calls gmtime to convert this into a structure with useful time and date values. It then prints it out.

To see the local time, we need to use the function localtime instead.

To convet a broken-down tm structure into a raw time_t value, we can use the functiion mktime.

The functions asctime and ctime give a more friendly output:

The asctime function returns a string that represents the time and date given by the tm structure timeptr. The string returned has a format similar to:

It's always a fixed format, 26 characters long. The function ctime is equivalent to calling:

Try It Out - ctime

Let's see ctime in action, using the following code:

Compile and run the surprisingly named ctime.c and you should see:

How It Works

The ctime.c program calls time to get the low level time value and lets ctime do all the hard work converting it to a readable string, which it then prints.

The strftime function gives more control to the formatting of time and date.

The format string is used to control the characters written to the string

The conversion specifiers include:

So, the usual date as given by the date program corresponds to a strftime format string of :

To help with reading dates, we can use the strptime function to convert a string to a tm structure.

Try It Out - strftime and strptime

Have a look at the selection of conversion specifiers used in the following program:

When we compile and run this program, strftime.c, we get:

How It Works

The strftime program obtains the current local time by calling time and localtime. It then converts it to readable form. It then uses strptime to extract the raw time and date and print them.

Temporary Files

Often, programs will need to make use of temporary storage in the form of files. Temporary files need unique names.

A unique file name can be generated by the tmpnam function:

If the temporary file is to be used immediately, you can name it and oppen it a the same time using the tmpfile function.

Try It Out - tmpnam and tmpfile

Let's see these two functions in action:

When we compile and run this program, tmpnam.c, we can see the unique file name generated by tmpnam: