/* This program was written in very portable C, */ /* and tested under VMS 4.2 & 5.0 and 4.3 bsd UNIX. */ /* */ /* To compile and run this CIF-CHECKSUM program: */ /* */ /* FOR VMS and C */ /* $ cc checksum.c */ /* $ link checksum.obj */ /* $ define lnk$library sys$library:vaxcrtl.olb */ /* $ checksum :== $your-disk:[your-full-directory-path]checksum.exe */ /* $ checksum your-cif-file */ /* */ /* FOR UNIX and C */ /* % cc -O checksum.c -o checksum */ /* % checksum your-cif-file */ #ifdef VMS #include stdio.h #else #include #endif #define TRUE 1 #define FALSE 0 static char mess[100]; /* Buffer for error messages.*/ /* CKSUM.C */ main(argc,argv) int argc; char *argv[]; /* Calculates the CIF-Checksum. */ /* Reads a CIF file from Standard Input. */ /* Writes the CIF-Checksum on Standard Output. */ { static FILE *in,*out; long chksum; /* Checksum accumulates here. */ long nbyte; /* Byte count accumulates here. */ int sepflg; /* Boolean for previous separator. */ int c; /* Gets a character from the file, or EOF. */ /* Initialization. */ c = command("ckecksum",argc,argv,&in,&out); /* Process command line. */ if (c == 0) errmess("checksum","cannot open files",1); sepflg = TRUE; /* Set the separator flag. */ chksum = 32; /* Initial checksum value. */ nbyte = 1; /* Initial byte count. */ /* Process the data bytes. */ while ((c = getc(in)) != EOF) { c &= 0177; /* Get rid of unwanted bits. */ if (c > ' ') /* Is this a printing character? */ { chksum = chksum + (long) c; nbyte++; sepflg = FALSE; } else if ((c != 0) && (sepflg == FALSE)) { chksum = chksum + 32L; /* Process the first */ nbyte++; /* separator in a row. */ sepflg = TRUE; } } /* Continue reading file. */ /* Process the implied trailing separator. */ if (sepflg == FALSE) { chksum = chksum + 32L; nbyte++; } /* Print our results. */ fprintf (out,"CIF-CHECKSUM: %ld %ld\n", chksum, nbyte); /* fprintf (out,"CIF-CHECKSUM: %ld\n", chksum); */ /* fprintf (out,"BYTE-COUNT: %ld\n", nbyte); */ } /* Process command line for input and output.*/ /* If no file specified, used stdin and stdout.*/ /* If one file specified, use file as input and stdout.*/ /* If both files specified, use first file as input, second file as output.*/ /* If cannot open files, or too many parameters, return error status.*/ /* A return status of 1 indicates sucess, 0 indicates a failure.*/ /* Written by Jeff Deifik @4/16/85.*/ command(func,argc,argv,in,out) register char func[]; register int argc; register char *argv[]; register FILE **in,**out; { FILE *vfopen(); *in = stdin; /* Default input.*/ *out = stdout; /* Default output.*/ if (argc == 1) return(1); /* Nothing specified.*/ *in = vfopen(func,*++argv,"r",0); /* Input specified.*/ if (*in == NULL) return(0); if (argc == 2) return(1); /* Input good.*/ *out = vfopen(func,*++argv,"w",0); /* Output specified.*/ if (*out == NULL) return(0); if (argc == 3) return(1); /* Input and output good.*/ errmess(func,"too many parameters.",0); return(0); } /* This function attempts to do a fopen with the specified parameters. */ /* It returns 1 else it returns 0. */ /* Written by Jeff Deifik 9/18/84. */ /* prog: the name of the calling program */ /* name: the name of the file to be manipulated */ /* fail: a flag to deal with failure. */ /* 0 means issue a warning message and continue */ /* 1 means issue a warning message and die */ /* 2 means be silent and continue */ /* type: the file type (meaninful for VMS only) */ /* 0 means a binary file */ /* 1 means a text file */ /* mode: the file protection mode */ FILE *vfopen(prog,name,mode,fail) register char prog[]; /* The name of the calling program.*/ register char name[]; /* File name.*/ register char mode[]; /* Mode to open file with.*/ register int fail; { FILE *fp; #ifdef VMS /* Specify that we want a stream lf file with maximum record length of 32k.*/ fp = fopen(name,mode,"mrs=32767"); #else fp = fopen(name,mode); #endif if (fp == NULL && (fail == 0 || fail == 1)) { sprintf(mess,"can't fopen %s",name); errmess(prog,mess,fail); } return(fp); } /* A procedure to print an error message on standard error.*/ errmess(name,message,severity) char name[]; /* The name of the calling program.*/ char message[]; /* The text message.*/ int severity; /* True means exit with error status.*/ { fprintf(stderr,"%s: %s\n",name,message); if (severity) exit(1); /* Error exit status.*/ }