Unix Systems Programming
Authors: Robbins K.A. Robbins S.
Published year: 2003
Pages: 30-31/274
Buy this book on amazon.com >>
Team-FLY

2.12 Exercise: An env Utility

The env utility examines the environment and modifies it to execute another command. When called without arguments, the env command writes the current environment to standard output. The optional utility argument specifies the command to be executed under the modified environment. The optional -i argument means that env should ignore the environment inherited from the shell when executing utility . Without the -i option, env uses the [ name =value] arguments to modify rather than replace the current environment to execute utility . The env utility does not modify the environment of the shell that executes it.


SYNOPSIS

env [-i] [name=value] ... [utility [argument ...]]

POSIX:Shell and Utilities

Example 2.25

Calling env from the C shell on a machine running Sun Solaris produced the following output.

HOME=/users/srobbins

USER=srobbins

LOGNAME=srobbins

PATH=/bin:/usr/bin:/usr/ucb:/usr/bin/X11:/usr/local/bin

MAIL=/var/mail/srobbins

TZ=US/Central

SSH2_CLIENT=129.115.12.131 41064 129.115.12.131 22

TERM=sun-cmd

DISPLAY=sqr3:12.0

SSH2_SFTP_LOG_FACILITY=-1

PWD=/users/srobbins

Write a program called doenv that behaves in the same way as the env utility when executing another program.

  1. When called with no arguments, the doenv utility calls the getenv function and outputs the current environment to standard output.

  2. When doenv is called with the optional -i argument, the entire environment is replaced by the name=value pairs. Otherwise, the pairs modify or add to the current environment.

  3. If the utility argument is given, use system to execute utility after the environment has been appropriately changed. Otherwise, print the changed environment to standard output, one entry per line.

  4. One way to change the current environment in a program is to overwrite the value of the environ external variable. If you are completely replacing the old environment ( -i option), count the number of name=value pairs, allocate enough space for the argument array (don't forget the extra NULL entry), copy the pointers from argv into the array, and set environ .

  5. If you are modifying the current environment by overwriting environ , allocate enough space to hold the old entries and any new entries to be added. Copy the pointers from the old environ into the new one. For each name=value pair, determine whether the name is already in the old environment. If name appears, just replace the pointer. Otherwise, add the new entry to the array.

  6. Note that it is not safe to just append new entries to the old environ , since you cannot expand the old environ array with realloc . If all name=value pairs correspond to entries already in the environment, just replace the corresponding pointers in environ .

Team-FLY
Team-FLY

2.13 Exercise: Message Logging

The exercise in this section describes a logging library that is similar to the list object defined in listlib.h and listlib.c of Program 2.6 and Program 2.7, respectively. The logging utility allows the caller to save a message at the end of a list. The logger also records the time that the message was logged. Program 2.11 shows the log.h file for the logger.

Program 2.11 log.h

The header file log.h for the logging facility .

#include <time.h>



typedef struct data_struct {

     time_t time;

     char *string;

} data_t;



int addmsg(data_t data);

void clearlog(void);

char *getlog(void);

int savelog(char *filename);

The data_t structure and the addmsg function have the same respective roles as the list_t structure and adddata function of listlib.h . The savelog function saves the logged messages to a disk file. The clearlog function releases all the storage that has been allocated for the logged messages and empties the list of logged messages. The getlog function allocates enough space for a string containing the entire log, copies the log into this string, and returns a pointer to the string. It is the responsibility of the calling program to free this memory when necessary.

If successful, addmsg and savelog return 0. A successful getlog call returns a pointer to the log string. If unsuccessful , addmsg and savelog return “1. An unsuccessful getlog call returns NULL . These three functions also set errno on failure.

Program 2.12 contains templates for the four functions specified in log.h , as well as the static structures for the list itself. Complete the implementation of loglib.c . Use the logging facility to save the messages that were printed by some of your programs. How might you use this facility for program debugging and testing?

Program 2.12 loglib.c

A template for a simple logging facility .

#include <stdlib.h>

#include <string.h>

#include "log.h"



typedef struct list_struct {

     data_t item;

     struct list_struct *next;

} log_t;



static log_t *headptr = NULL;

static log_t *tailptr = NULL;



int addmsg(data_t data) {

   return 0;

}



void clearlog(void) {

}



char *getlog(void) {

   return NULL;

}



int savelog(char *filename) {

   return 0;

}
Team-FLY
Unix Systems Programming
Authors: Robbins K.A. Robbins S.
Published year: 2003
Pages: 30-31/274
Buy this book on amazon.com >>

Similar books on Amazon