/*
   Name: main.c
   Language: ANSI/ISO C

   Purpose: Driver for encapsulation test (see stream.c) Tries to open a file
           named "foo.tmp" for reading.  If it succeeds then it closes the
           file before exiting, otherwise it just exits.

   Author: James Blustein <jamie@csd.uwo.ca>
   Created: 1 January 1996
   Last Modified: 7 May 1996 by James Blustein
                  Added some comments and the used the UWOCSD cpp symbol
                  Documented use of EXIT_FAILURE in swerr()
                  Cleared up minor typos.  The input file is still named
                  `outname', oh well.
                  removed `extern int errno' 'cause it's not true under ISO C

   See also: stream.c, stream.h 
*/

#include <stdio.h>
 #ifdef UWOCSD
  void perror(char s);          /* should be declared in <stdio.h> but isn't */
  int fprintf(FILE *stream, char *format, ...); /* stupid libraries don't ...*/
 #endif
#include <stdarg.h>
 #ifdef UWOCSD
  int vfprintf(FILE *stream, char *format, va_list ap); /* ... declare these */
 #endif
#include <stdlib.h>
  #ifndef EXIT_FAILURE
    #define EXIT_FAILURE (1) /* libraries lose again */
  #endif
  #ifndef EXIT_SUCCESS
    #define EXIT_SUCCESS (0)
  #endif
#include <errno.h>

#include "types.h"  /* my standard types and macros */
#include "proto.h"  /* prototypes for global functions declared here */
#include "stream.h" /* definition and declaratin of ADT `Stream' */

/* module global variable */
static string progname;

/* local prototype */
int main (int argc, string argv[]);


/* 
 ***********
 * swerr() *
 ***********
   PURPOSE: Report errors detected during software execution to the standard
       error output device.
   PRE:  An error has been detected 
   POST: A detailed error message has been sent to the standard error
   output device.  The message includes the paramaters as descibed below.
   If the `error_level' was not 0 then the program has been halted with a 
   message and a failure code has been returned to the O/S (in the program's
   exit value).  The program may also exit with the same error indication if 
   this function detects an error while writing to the standard error device.

   NOTE: Adapted from error() (p.174) of K&R II 
*/
void swerr(const char *proc, 
           const int  error_level,
                 char *format, ...)
{
   extern string  progname;
          int     err_num = errno;
          int     status;
          va_list args;

   status = fprintf(stderr, "%s [%s()] ", progname, proc);
   va_start(args, format);
   if (EOF != status) {
      status = vfprintf(stderr, format, args);
   }
   va_end(args);
   if (err_num) {  /* error reported by library call (includes '\n') */
      status = fprintf(stderr, ", ");
      errno = err_num; /* use stored value in case fprintf() changes it */
      perror(NULL);  /* error message from library call */
   } else {
      if (EOF != status) {
         status = fprintf(stderr, "\n");
      }
   }
   if ((! error_level) || (EOF == status)) {
      exit(EXIT_FAILURE);
   }
}/* swerr() */


int main (int argc, string argv[])
{
  const char *proc    = "main";
  const char *outname = "foo.tmp";
        FILE *output;

   progname = argv[0];
   if ((output = Stream.open(proc, outname, "r")) != NULL) {
      return (Stream.close(proc, outname, output))?(EXIT_SUCCESS)
                                                  :(EXIT_FAILURE);
   } else {
      swerr(proc, 1, "exiting with error");
      /* NOTREACHED */
      return(EXIT_FAILURE);
   }
} /* main() */

/* end of "main.c" */

