Sun Unix source code. A calling module.

   The Sun Unix code below, gcore.c, is the calling module for a file filter. A file filter reads an existing file and creates a new file based on the existing file and a transfer function within the filter program. A simple example would be converting all letters in a text file to upper case. Encryption would be a more complex example. In all cases, the existing file is merely read, so it is left unchanged. The newly created output file is the desired result.
   Gcore.c opens the input file, calls the linked application module that performs the filter function, and then creates the output file. Unix portability is an issue, so source is given here. Application modules are on other pages.
    There are no HTML tags in the source code text below.
/* Filespec= gcore.c */ /* PORTABILITY COMMENT: errno values are used to invoke messages. Valid on Sun h/w, they must be checked when ported elsewhere. /* /* In comments, BHM=Being Here Means */ #include <errno.h> #include <fcntl.h> #include "stdio.h" #include <sys/types.h> #include <unistd.h> #define cr 0x0d /* Carriage Return */ #define lf 0x0a /* Line Feed */ #define eof 0x1a /* End of file */ #define io_ar_sz 32768 /* Input-output usable array size. */ /* GENERIC GLOBAL */ void end_pgm(); /* GENERIC GLOBAL VARIABLES */ int version=1; int r_val; /* Generic return value for system calls */ off_t f1_size, f2_size, lerr; /* long */ /* FILE GLOBAL VARIABLES */ /* Input file f1 variables */ int f1; /* File descriptor for input file */ char inpchr[io_ar_sz+1]; /* input array */ long chred=0L; /* # of characters read on fread i. */ long tchred=0L; /* Total chars read so far on all fread's. */ long unread; /* Input chars not fread yet=f1size - tchred. */ int k1=1; /* inpchr subscript */ /* Output file f2 variables */ int f2; /* File descriptor for output file */ char outchr[io_ar_sz+1]; /* output array */ long chsnt; /* # char to be sent out on next write. */ int k2=0; /* outchr subscript */ main(argc,argv) int argc; /* count # of command line paramaters */ char *argv[]; /* pointers to entered strings */ { inpchr[io_ar_sz]=outchr[io_ar_sz]='\0'; /* Null on string ends */ /* Check # of command line parms */ if (argc !=3) { printf("This program requires 2 command line parameters: input"); printf(" and\noutput file names. The input file must exist and "); printf("have read permission.\nThe output file must NOT exist."); printf("\nIf the above rules aren't followed, the program "); printf("terminates.\n\n"); end_pgm(1); } /* open input file */ f1 = open(argv[1],O_RDONLY); if (f1 ==-1) { printf("Input file %s not opened\n", argv[1]); if (errno == EACCES) { printf("File %s does not have read permission.\n",argv[1]); } if (errno == ENOENT) { printf(" File %s was not found, a likely user ",argv[1]); printf("error. This program\nwas designed to read an existing"); printf(" input file.\n\n"); } end_pgm(1); } /* get size of input file */ f1_size=lseek(f1,0L,SEEK_END); if (f1_size == -1) { printf("Error when using lseek to obtain file size\n"); printf("lseek returned a %ld\n\n", lerr); end_pgm(2); } printf("The input file size is %ld\n", f1_size); lerr=lseek(f1,0L,SEEK_SET); if (lerr != 0L) { printf("Error when using lseek() to move read pointer to file start\n"); printf("lseek() returned a %ld\n\n", lerr); end_pgm(2); } /* open output file */ f2=open(argv[2], O_CREAT|O_EXCL|O_WRONLY, 0666); if (f2 ==-1) { printf("Output file %s not opened\n", argv[2]); if (errno == EEXIST) { printf(" File %s already exists, a likely user error. This",argv[2]); printf(" program\nwas designed to create a NEW output file.\n\n"); } end_pgm(3); } /* BHM valid filenames for input & output were entered */ genotp(); } /* END MAIN */ extern char getnc() /* BEGIN GETNC */ { long reed(); char bufchar; /* char from inpchr */ int ichred; /* intchred = (int) of global chred */ /* Initially, globals int k1=1, and long chred=0 */ ichred=(int)chred; if(k1>ichred) /* Both items now of type int */ { chred=reed(); /* chred= chars read by a call to reed() */ --chred; /* Make chred be valued from 0 up, like k1 */ } bufchar = inpchr[k1]; ++k1; /* Set up for the next getnc() on the current getnc() */ return(bufchar); } /* END GETNC */ void ship(chrr) /* BEGIN SHIP */ char chrr; { void wryte(); /* Ship sends 1 char at a time to output buffer outchr. When outchr is full(k2=io_ar_sz), wryte is called to copy it to file f2. wryte is also called when the program ends. wryte resets k2 */ if (k2==io_ar_sz) wryte(); outchr[k2]=chrr; ++k2; } /* END SHIP */ long reed() /* BEGIN REED */ { int intunrd; /* for (int)unread. Global unread is a long */ long errchk; char *inchptr; /* Pointer for input character string */ /* Reed does a read to a global buffer. Reed also ends the program. It is called only from getnc() if the buffer pointer is 1 past the top end. BHM need to read input file f1. Start by seeing how much to read. The amount to be read is <= io_ar_sz bytes. */ unread=f1_size - tchred; /* Input chars not fread yet */ inchptr=inpchr; /* set pointer */ if(unread==0L) end_pgm(0); /* Normal end of program */ if (unread>=io_ar_sz) { intunrd=io_ar_sz; } else { intunrd=(int)unread; } r_val=read(f1, inchptr, intunrd); if (r_val != intunrd) { printf("An error occurred while reading the input file\n\n"); end_pgm(4); } tchred+=intunrd; k1=0; /* k1 is read pointer */ return(r_val); } /* END REED */ void wryte() /* BEGIN WRYTE */ { /* wryte copies outchr to f2 */ long errchk; char *ochrptr; ochrptr=outchr; /* set pointer */ if (k2>io_ar_sz) { printf("Fatal error: K2 = %d, >io_ar_sz. The output array",k2); printf("subscript exceeds array size. Program ended\n\n"); end_pgm(5); } if (k2 > 0) { errchk=write(f2,ochrptr,k2); /* K2 is incremented ST it */ /* = # of bytes to ship */ if (errchk!=k2) { printf("Error occurred doing write() in wryte subroutine.\n"); printf("%d bytes were to have ",k2); printf("been written to the output file.\n"); printf("%ld bytes were written\n\n",errchk); end_pgm(5); } /* BHM Write to output file occurred normally */ f2_size += errchk; k2=0; } } /* END WRYTE */ /* BEGIN END_PGM */ void end_pgm(exit_code) int exit_code; { void final_wryte(); /* Declaration */ int errs_here=0; /* Track errors sensed here in end_pgm */ /* Exit_code definitions: 0=no errors 1=input error, input file not opened at start of program, so neither file needs closing here. 2=input error occurred before output file created. Close input file only. 3=output error, output file not opened, so it needn't be closed here 4=input error, both files to be closed here 5=output error, both files to be closed here */ /* Close input file */ if (exit_code !=1) { if ((r_val=close(f1)) !=0 ) { printf("Input file close returned %d, an error.\n", r_val); printf("Check the input file MANUALLY\n\n"); errs_here++; } } /* Output file management: 1. If there are no errors, call final_wryte to: 1. ship a single eof byte, 1Ah. 2. Do final load from output buffer if needed. 2. Get output file size 3. Close file */ if ( exit_code ==0 ) final_wryte(); /* final wryte */ if (exit_code !=1 && exit_code != 2 && exit_code != 3) { f2_size=lseek(f2,0L,2); /* file size */ if (f2_size != -1L) { printf("\nThe output file size is %ld\n", f2_size); } else { printf("Error when using lseek to obtain output file size\n"); printf("lseek returned a %ld\n\n", lerr); errs_here++; } if ( (r_val=close(f2)) !=0 ) /* Close output file */ { printf("Output file close returned %d, an error.\n", r_val); printf("Check the output file MANUALLY\n\n"); errs_here++; } } if (exit_code == 0 && errs_here==0) { printf("The program ended normally with an exit code of %d.\n", exit_code); } else { printf("The program ended with exit code=%d\n", exit_code+errs_here); } exit(exit_code); } /* END END_PGM */ /* Program over */


Return to previously viewed page
Go to Home Page

Arrow to Top