9 System Functions

The C programming language is a good choice if you wish to write operating systems. For example, much of the UNIX operating system is written in C. When writing system programs, it is sometimes necessary to retrieve or modify the environment in which the program is running. This chapter describes the DEC C Run-Time Library (RTL) functions that accomplish this and other system tasks.

Table 9-1 lists and describes all the system functions found in the DEC C RTL. For a more detailed description of each function, see the Reference Section.

Table 9-1 System Functions

Function  Description 
System Functions-Searching and Sorting Utilities 
bsearch  Performs a binary search on an array of sorted objects for a specified object. 
qsort  Sorts an array of objects in place by implementing the quick-sort algorithm. 
System Functions-Retrieving Process Information 
ctermid  Returns a character string giving the equivalence string of SYS$COMMAND, which is the name of the controlling terminal. 
cuserid  Returns a pointer to a character string containing the name of the user who initiated the current process. 
getcwd  Returns a pointer to the file specification for the current working directory. 
getegid, geteuid, getgid, getuid  Returns, in OpenVMS terms, group and member numbers from the user identification code (UIC). 
getenv  Searches the environment array for the current process and returns the value associated with a specified environment. 
getlogin  Gets the login name of the user associated with the current session. 
getpid  Returns the process ID of the current process. 
getppid  Returns the parent process ID of the calling process. 
getpwnam  Accesses user-name information in the user database. 
getpwuid  Accesses user-ID information in the user database. 
System Functions-Changing Process Information 
chdir  Changes the default directory. 
chmod  Changes the file protection of a file. 
chown  Changes the owner user identification code (UIC) of a file. 
mkdir  Creates a directory. 
nice  Increases or decreases the process priority to the process base priority by the amount of the argument. 
putenv  Sets an environmental variable. 
setenv  Inserts or resets the environment variable name in the current environment list. 
setgid, setuid  Implemented for program portability and have no functionality. 
sleep, usleep  Suspends the execution of the current process for at least the number of seconds indicated by its argument. 
umask  Creates a file protection mask that is used whenever a new file is created. It returns the old mask value. 
System Functions-Retrieving and Converting Date/Time Information 
asctime  Converts a broken-down time into a 26-character string. 
clock  Determines the CPU time (in microseconds) used since the beginning of the program execution. 
ctime  Converts a time, in seconds, to an ASCII string in the form generated by the asctime function. 
decc$fix_time  Converts OpenVMS binary system times to UNIX binary times. 
difftime  Computes the difference, in seconds, between the two times specified by its arguments. 
ftime  Returns the elapsed time since 00:00:00, January 1, 1970, in the structure timeb. 
getclock  Gets the current value of the system-wide clock. 
getdate  Converts a formatted string to a time/date structure. 
getitimer  Returns the value of interval timers. 
gettimeofday  Gets date and time. 
gmtime  Converts time units to broken-down UTC time. 
localtime  Converts a time (expressed as the number of seconds elapsed since 00:00:00, January 1, 1970) into hours, minutes, seconds, and so on. 
mktime  Converts a local-time structure into time since the Epoch. 
setitimer  Sets the value of interval timers. 
strftime, wcsftime  Places characters into an array, as controlled by a specified format string. 
strptime  Converts a character string into date and time values. 
time  Returns the time elapsed since 00:00:00, January 1, 1970, in seconds. 
times  Returns the accumulated times of the current process and of its terminated child processes. 
tzset  Sets and accesses time-zone conversion. 
ualarm  Sets or changes the timeout of interval timers.  
wcsftime  Uses date and time information stored in a tm structure to create a wide-character output string. 
System Functions-Miscellaneous 
VAXC$CRTL_INIT  Initializes the run- time environment and establishes an exit and condition handler, which makes it possible for DEC C RTL functions to be called from other languages. 

Example 9-1 shows how the cuserid function is used.

Example 9-1 Accessing the User Name

/*  Using cuserid, this program returns the user name.         */

#include <stdio.h>

main()

{
   static char string[L_cuserid];
   cuserid(string);
   printf("Initiating user: %s\n", string);
}

If a user named TOLLIVER runs the program, the following is displayed on stdout:

$ RUN  EXAMPLE1
Initiating user: TOLLIVER

Example 9-2 shows how the getenv function is used.

Example 9-2 Accessing Terminal Information

cfunc()
{
    printf("Terminal type: %s\n", getenv("TERM"));
}

Running Example 9-2 on a Digital VT100 terminal in 132-column mode displays the following:

$ RUN  EXAMPLE3
Terminal type: vt100-132

Example 9-3 shows how to use getenv to find the user's default login directory and how to use chdir to change to that directory.

Example 9-3 Manipulating the Default Directory

/*  This program performs the equivalent to the DCL command    *
 *  SET DEFAULT SYS$LOGIN. Once the program exits, however,    *
 *  the directory is reset to the directory from which the     *
 *  program was run.                                           */

#include <stdio.h>
#include <unistd.h>

main()
{
   char *dir;
   int i;

   dir = getenv("HOME");
   if ((i = chdir(dir)) != 0)
      {
         perror("Cannot set directory");
         exit();
      }

   printf("Current directory: %s\n", dir);
}

Running Example 9-3 displays the following:

$ RUN EXAMPLE4
Current directory: dba0:[tolliver]
$

Example 9-4 shows how to use the time, localtime, and strftime functions to print the correct date and time at the terminal.

Example 9-4 Printing the Date and Time

/*  The time function returns the time in seconds; the         *
 *  localtime function converts the time to hours, minutes,    *
 *  and so on; and the strftime function uses these values to  */
 *  obtain a string in the desired format.                     */

#include <time.h>
#define MAX_STRING 80

main()
{
   struct  tm  *time_structure;
   time_t time_val;
   char output_str[MAX_STRING];

   time(&time_val);
   time_structure = localtime(&time_val);

   /* Print the date  */

   strftime(output_str, MAX_STRING,
               "Today is %A, %B %d, %Y", time_structure);

   printf("%s\n", output_str);

   /* Print the time using a 12-hour clock format. */

   strftime(output_str, MAX_STRING,
               "The time is %I:%M %p", time_structure);

   printf("%s\n", output_str);

}

Running Example 9-4 displays the following:

$ RUN  EXAMPLE5
Today is Thursday, May 20, 1993
The time is 10:18 AM
$


Previous Page | Next Page | Table of Contents | Index