Page:4SIGHT manual- a computer program for modelling degradation of underground low level waste concrete vaults (IA 4sightmanualcomp5612snyd).pdf/105

From Wikisource
Jump to navigation Jump to search
This page needs to be proofread.
27. INITIALIZATION
B.12

27. INITIALIZATION.

28. Redirection of stdin. Allow the user to either use redirection at the command line or to specify the input file directly. If an input file is given at the command line without redirection (<), re-define stdin to come from the input file. The #include files (fcntl.h and sys/stat.h) are for the routine dup2(), and errno.h is required for the global variable errno.

#define STDIN 0
⟨Include files 3⟩ 
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>

29. If redirection is not used, set input file_handle to the file specified at the command line.

⟨Global variables 4⟩ 
 int input_file.handle;     /* file containing input parameters */

30. If an input file was given without redirection, use dup2() to copy the input file handle number to the existing stdin file handle.

⟨Determine STDIN 30⟩ 
 if (argc > 1) {      /* redirect from input file */
   fprintf(stderr, "Calculating...");
   if ((input_file_handle = open (argv[1], 0_RDONLY))==-1) {
      switch (errno) {
      case ENOENT: fprintf (stderr, "No such file: %s\n", argv[1]);
        break;
      case EMFILE: fprintf (stderr, "Too many open files.\n");
        break;
      case EACCES: fprintf(stderr, "Permission denied. \n");
        break;
      case EINVACC: fprintf(stderr, "Invalid accesscode.\n");
        break;
      default: fprintf(stderr, "Error: Unknown error code.\n");
        break;
      }
      exit(0);
   }
   dup2(input_file_handle, STDIN);
 }
 else     /* interactive mode */
   printf("Enter commands: \n");

This code is used in section 5.