v07i086: Whales and Plankton, part 13/13
Brandon S. Allbery - comp.sources.misc
allbery at uunet.UU.NET
Wed Jul 19 11:44:02 AEST 1989
Posting-number: Volume 7, Issue 86
Submitted-by: loy at gtx.UUCP (Bob Loy)
Archive-name: whpl/part13
# whpl13of13.shar
#---cut here---cut here---cut here---cut here---cut here---
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files.
# This archive created: Sat Jan 14 04:05:29 MST 1989
export PATH; PATH=/bin:$PATH
echo shar: extracting "'sortcount.c'" '(22016 characters)'
if test -f 'sortcount.c'
then
echo shar: will not over-write existing file "'sortcount.c'"
else
sed 's/^X//' << \SHAR_EOF > 'sortcount.c'
X
X#ifndef lint
X static char sccsid[] = " %M% %I% ";
X static char dateid[] = " %E% ";
X#endif
X
X/*@#==========================================================================*
X@@#
X@@@#@^ sortcount.c
X@#
X@# Purpose:
X@@# Count live, dead, male &/or female whales in files, & optionally
X@@# create output files of same.
X@#
X@#@^Options:
X@#@^ -l count all live whales in input file(s)
X@#@^ -d count all dead whales in input file(s)
X@#@^ -m count all male whales in input file(s)
X@#@^ -f count all female whales in input file(s)
X@#@^ -s creates sorted output files, otherwise, only count is reported
X@#@^
X@# Examples:
X@# sortcount -l whn010101 -f
X@# count live female whales in file whn010101
X@# sortcount -d whn010101 whn010102 whn010103 -s
X@# count dead whales in the three files, and create an output file
X@# of only those dead whales.
X@# sortcount -m `cat wfile`
X@# count all male whales in all the whale files listed in "wfile"
X@# sortcount -s `cat anotherfile` -l -d -m -f
X@# count live, dead, male & female whales in files listed in "an-
X@# otherfile", and create four output files of: live dead, male &
X@# female whales. Names of the output files will appear on stdout.
X@# sortcount `cat anotherfile` -s
X@# same as above (none of the opts -l, -d, -m or -f defaults to all)
X@#
X@# Compiling:
X@# cc -O sortcount.c -o sortcount
X@#
X@# Functions:
X@# void read_infile();
X@# void write_outheads();
X@# void next_whale();
X@# void write_outfile();
X@# void pick_outfiles();
X@# void open_outfiles();
X@# void close_outfiles();
X@# void print_count();
X@# int skip_arg();
X@# short cmd_line();
X@# void main();
X@#
X@# General Comments:
X@# Note: Header information for output files will be taken from FIRST
X@# command line argument opened for reading.
X@# There is no checking to see if the output files were opened properly.
X@#
X@#---------------------------------------------------------------------------*/
X
X/*---------------------------------------------------------------------------*
X Top-level Declaration - includes, defines, externs & globals.
X*----------------------------------------------------------------------------*/
X
X#include <stdio.h>
X
X#define Ushort unsigned short
X
X /* EXTERNS FROM */
X /* Functions From libraries: */
X extern FILE *fopen();
X extern int scanf();
X extern int fprintf();
X extern int strncmp();
X extern char *strcpy();
X extern void exit();
X
X /* Functions From : */
X /* Variables From : */
X
X /* EXTERNS TO */
X /* Functions To : */
X /* Variables To : */
X
X /* GLOBALS */
X static Ushort masterflg = 0;
X static Ushort sortflg = 0;
X static Ushort liveflg = 0;
X static Ushort deadflg = 0;
X static Ushort malflg = 0;
X static Ushort femflg = 0;
X static FILE *outfp1;
X static FILE *outfp2;
X static FILE *outfp3;
X static FILE *outfp4;
X static FILE *infp;
X static char whinstr[99];
X static char *test = whinstr;
X static long tell;
X static long livethis = 0;
X static long deadthis = 0;
X static long malthis = 0;
X static long femthis = 0;
X static long whthis = 0;
X static long livetot = 0;
X static long deadtot = 0;
X static long maltot = 0;
X static long femtot = 0;
X static long whtot = 0;
X
X
X/*---------------------------------------------------------------------------*
X@
X@@ void read_infile(); Reads second line of each whale record to determine
X@@ whether whale is alive or dead, male or female.
X@ caller: main()
X@ calls : sscanf()
X@
X@*---------------------------------------------------------------------------*/
X
Xvoid
Xread_infile()
X{
X char die[2];
X char sex[2];
X
X whthis++;
X sscanf(whinstr, "%s%s", die, sex);
X if(die[0] == 'L')
X liveflg = 1;
X if(die[0] == 'D')
X deadflg = 1;
X if(sex[0] == 'M')
X malflg = 1;
X if(sex[0] == 'F')
X femflg = 1;
X}
X
X/*---------------------------------------------------------------------------*
X@
X@@ void write_outheads(); Writes header information to output files.
X@ caller: main()
X@ calls : fputs()
X@
X@*---------------------------------------------------------------------------*/
X
Xvoid
Xwrite_outheads()
X{
X switch(masterflg)
X {
X case 0:
X case 15:
X fputs(whinstr, outfp3);
X fputs(whinstr, outfp4);
X /* Falls thru on purpose */
X case 3:
X case 7:
X case 11:
X case 12:
X case 13:
X case 14:
X fputs(whinstr, outfp2);
X /* Falls thru on purpose */
X case 1:
X case 2:
X case 4:
X case 5:
X case 6:
X case 8:
X case 9:
X case 10:
X fputs(whinstr, outfp1);
X break;
X default:
X fprintf(stderr, "\nwrite_outheads(): switch error\n");
X break;
X }
X}
X
X/*---------------------------------------------------------------------------*
X@
X@@ void next_whale(); Finds next whale in input file, keeps present whale
X@@ from being written out to an output file.
X@ caller: pick_outfiles()
X@ calls : fgets()
X@
X@*---------------------------------------------------------------------------*/
X
Xvoid
Xnext_whale()
X{
X fseek(infp, tell, 0);
X test = fgets(whinstr, 97, infp);
X do{
X tell = ftell(infp);
X test = fgets(whinstr, 97, infp);
X if(whinstr[0] == '#')
X break;
X } while(test != NULL);
X}
X
X/*---------------------------------------------------------------------------*
X@
X@@ void write_outfile(); Writes out present whale to an output file, and
X@@ finds next whale in input file.
X@ caller: pick_outfiles()
X@ calls : fgets(), fputs()
X@
X@*---------------------------------------------------------------------------*/
X
Xvoid
Xwrite_outfile(filep)
X FILE *filep;
X{
X fseek(infp, tell, 0);
X test = fgets(whinstr, 97, infp);
X do{
X if(sortflg)
X fputs(whinstr, filep);
X tell = ftell(infp);
X test = fgets(whinstr, 97, infp);
X if(whinstr[0] == '#')
X break;
X } while(test != NULL);
X}
X
X/*---------------------------------------------------------------------------*
X@
X@@ void pick_outfiles(); Tests flags set by read_infile() to determine
X@@ whether to write out whale, and if so, to which output file.
X@ caller: main()
X@ calls : write_outfile(), next_whale()
X@
X@*---------------------------------------------------------------------------*/
X
Xvoid
Xpick_outfiles()
X{
X switch(masterflg)
X {
X case 0:
X case 15:
X if(liveflg)
X {
X if(malflg)
X {
X write_outfile(outfp1);
X livethis++;
X malthis++;
X }
X if(femflg)
X {
X write_outfile(outfp2);
X livethis++;
X femthis++;
X }
X }
X if(deadflg)
X {
X if(malflg)
X {
X write_outfile(outfp3);
X deadthis++;
X malthis++;
X }
X if(femflg)
X {
X write_outfile(outfp4);
X deadthis++;
X femthis++;
X }
X }
X break;
X case 1:
X if(liveflg)
X {
X write_outfile(outfp1);
X livethis++;
X }
X else
X next_whale();
X break;
X case 2:
X if(deadflg)
X {
X write_outfile(outfp1);
X deadthis++;
X }
X else
X next_whale();
X break;
X case 3:
X if(liveflg)
X {
X write_outfile(outfp1);
X livethis++;
X }
X if(deadflg)
X {
X write_outfile(outfp2);
X deadthis++;
X }
X else
X next_whale();
X break;
X case 4:
X if(malflg)
X {
X write_outfile(outfp1);
X malthis++;
X }
X else
X next_whale();
X break;
X case 5:
X if(liveflg && malflg)
X {
X write_outfile(outfp1);
X livethis++;
X malthis++;
X }
X else
X next_whale();
X break;
X case 6:
X if(deadflg && malflg)
X {
X write_outfile(outfp1);
X deadthis++;
X malthis++;
X }
X else
X next_whale();
X break;
X case 7:
X if(malflg)
X {
X if(liveflg)
X {
X write_outfile(outfp1);
X livethis++;
X malthis++;
X }
X if(deadflg)
X {
X write_outfile(outfp2);
X deadthis++;
X malthis++;
X }
X }
X else
X next_whale();
X break;
X case 8:
X if(femflg)
X {
X write_outfile(outfp1);
X femthis++;
X }
X else
X next_whale();
X break;
X case 9:
X if(liveflg && femflg)
X {
X write_outfile(outfp1);
X livethis++;
X femthis++;
X }
X else
X next_whale();
X break;
X case 10:
X if(deadflg && femflg)
X {
X write_outfile(outfp1);
X deadthis++;
X femthis++;
X }
X else
X next_whale();
X break;
X case 11:
X if(femflg)
X {
X if(liveflg)
X {
X write_outfile(outfp1);
X livethis++;
X femthis++;
X }
X if(deadflg)
X {
X write_outfile(outfp2);
X deadthis++;
X femthis++;
X }
X }
X else
X next_whale();
X break;
X case 12:
X if(malflg)
X {
X write_outfile(outfp1);
X malthis++;
X }
X if(femflg)
X {
X write_outfile(outfp2);
X femthis++;
X }
X else
X next_whale();
X break;
X case 13:
X if(liveflg)
X {
X if(malflg)
X {
X write_outfile(outfp1);
X livethis++;
X malthis++;
X }
X if(femflg)
X {
X write_outfile(outfp2);
X livethis++;
X femthis++;
X }
X }
X else
X next_whale();
X break;
X case 14:
X if(deadflg)
X {
X if(malflg)
X {
X write_outfile(outfp1);
X deadthis++;
X malthis++;
X }
X if(femflg)
X {
X write_outfile(outfp2);
X deadthis++;
X femthis++;
X }
X }
X else
X next_whale();
X break;
X default:
X fprintf(stderr, "\npick_outfiles(): switch error\n");
X break;
X }
X}
X
X/*---------------------------------------------------------------------------*
X@
X@@ void open_outfiles(); Opens output files based on command line flags.
X@ caller: main()
X@ calls : fopen()
X@
X@*---------------------------------------------------------------------------*/
X
Xvoid
Xopen_outfiles()
X{
X switch(masterflg)
X {
X case 0:
X case 15:
X outfp1 = fopen("whlm", "w");
X outfp2 = fopen("whlf", "w");
X outfp3 = fopen("whdm", "w");
X outfp4 = fopen("whdf", "w");
X fprintf(stderr, " Output file: \"whlm\" opened.\n");
X fprintf(stderr, " Output file: \"whlf\" opened.\n");
X fprintf(stderr, " Output file: \"whdm\" opened.\n");
X fprintf(stderr, " Output file: \"whdf\" opened.\n");
X break;
X case 1:
X outfp1 = fopen("whl", "w");
X fprintf(stderr, " Output file: \"whl\" opened.\n");
X break;
X case 2:
X outfp1 = fopen("whd", "w");
X fprintf(stderr, " Output file: \"whd\" opened.\n");
X break;
X case 3:
X outfp1 = fopen("whl", "w");
X outfp2 = fopen("whd", "w");
X fprintf(stderr, " Output file: \"whl\" opened.\n");
X fprintf(stderr, " Output file: \"whd\" opened.\n");
X break;
X case 4:
X outfp1 = fopen("whxm", "w");
X fprintf(stderr, " Output file: \"whxm\" opened.\n");
X break;
X case 5:
X outfp1 = fopen("whlm", "w");
X fprintf(stderr, " Output file: \"whlm\" opened.\n");
X break;
X case 6:
X outfp1 = fopen("whdm", "w");
X fprintf(stderr, " Output file: \"whdm\" opened.\n");
X break;
X case 7:
X outfp1 = fopen("whlm", "w");
X outfp2 = fopen("whdm", "w");
X fprintf(stderr, " Output file: \"whlm\" opened.\n");
X fprintf(stderr, " Output file: \"whdm\" opened.\n");
X break;
X case 8:
X outfp1 = fopen("whxf", "w");
X fprintf(stderr, " Output file: \"whxf\" opened.\n");
X break;
X case 9:
X outfp1 = fopen("whlf", "w");
X fprintf(stderr, " Output file: \"whlf\" opened.\n");
X break;
X case 10:
X outfp1 = fopen("whdf", "w");
X fprintf(stderr, " Output file: \"whdf\" opened.\n");
X break;
X case 11:
X outfp1 = fopen("whlf", "w");
X outfp2 = fopen("whdf", "w");
X fprintf(stderr, " Output file: \"whlf\" opened.\n");
X fprintf(stderr, " Output file: \"whdf\" opened.\n");
X break;
X case 12:
X outfp1 = fopen("whxm", "w");
X outfp2 = fopen("whxf", "w");
X fprintf(stderr, " Output file: \"whxm\" opened.\n");
X fprintf(stderr, " Output file: \"whxf\" opened.\n");
X break;
X case 13:
X outfp1 = fopen("whlm", "w");
X outfp2 = fopen("whlf", "w");
X fprintf(stderr, " Output file: \"whlm\" opened.\n");
X fprintf(stderr, " Output file: \"whlf\" opened.\n");
X break;
X case 14:
X outfp1 = fopen("whdm", "w");
X outfp2 = fopen("whdf", "w");
X fprintf(stderr, " Output file: \"whdm\" opened.\n");
X fprintf(stderr, " Output file: \"whdf\" opened.\n");
X break;
X default:
X fprintf(stderr, "\nopen_outfiles(): switch error\n");
X break;
X }
X fprintf(stderr, "\n");
X}
X
X/*---------------------------------------------------------------------------*
X@
X@@ void close_outfiles(); Closes output files based on command line flags.
X@ caller: main()
X@ calls : fclose()
X@
X@*---------------------------------------------------------------------------*/
X
Xvoid
Xclose_outfiles()
X{
X switch(masterflg)
X {
X case 0:
X case 15:
X fclose(outfp3);
X fclose(outfp4);
X /* Falls thru on purpose */
X case 3:
X case 7:
X case 11:
X case 12:
X case 13:
X case 14:
X fclose(outfp2);
X /* Falls thru on purpose */
X case 1:
X case 2:
X case 4:
X case 5:
X case 6:
X case 8:
X case 9:
X case 10:
X fclose(outfp1);
X break;
X default:
X fprintf(stderr, "\nclose_outfiles(): switch error\n");
X break;
X }
X}
X
X/*---------------------------------------------------------------------------*
X@
X@@ void print_count(); Outputs whale counts to stdout based on command
X@@ line flags.
X@ caller: main()
X@ calls : fprintf()
X@
X@*---------------------------------------------------------------------------*/
X
Xvoid
Xprint_count(lw, dw, mw, fw)
X long lw, dw, mw, fw;
X{
X switch(masterflg)
X {
X case 0:
X case 15:
X fprintf(stdout, " Live = %4d\n", lw);
X fprintf(stdout, " Dead = %4d\n", dw);
X fprintf(stdout, " Male = %4d\n", mw);
X fprintf(stdout, " Female = %4d\n", fw);
X break;
X case 1:
X fprintf(stdout, " Live = %4d\n", lw);
X break;
X case 2:
X fprintf(stdout, " Dead = %4d\n", dw);
X break;
X case 3:
X fprintf(stdout, " Live = %4d\n", lw);
X fprintf(stdout, " Dead = %4d\n", dw);
X break;
X case 4:
X fprintf(stdout, " Male = %4d\n", mw);
X break;
X case 5:
X fprintf(stdout, " Live Male =%4d\n", lw);
X break;
X case 6:
X fprintf(stdout, " Dead Male =%4d\n", dw);
X break;
X case 7:
X fprintf(stdout, " Live Male =%4d\n", lw);
X fprintf(stdout, " Dead Male =%4d\n", dw);
X break;
X case 8:
X fprintf(stdout, " Female = %4d\n", fw);
X break;
X case 9:
X fprintf(stdout, " Live Fem =%4d\n", lw);
X break;
X case 10:
X fprintf(stdout, " Dead Fem =%4d\n", dw);
X break;
X case 11:
X fprintf(stdout, " Live Fem =%4d\n", lw);
X fprintf(stdout, " Dead Fem =%4d\n", dw);
X break;
X case 12:
X fprintf(stdout, " Male = %4d\n", mw);
X fprintf(stdout, " Female = %4d\n", fw);
X break;
X case 13:
X fprintf(stdout, " Live Male =%4d\n", mw);
X fprintf(stdout, " Live Fem =%4d\n", fw);
X break;
X case 14:
X fprintf(stdout, " Dead Male =%4d\n", mw);
X fprintf(stdout, " Dead Fem =%4d\n", fw);
X break;
X default:
X fprintf(stderr, "\nprint_count(): switch error\n");
X break;
X }
X}
X
X/*---------------------------------------------------------------------------*
X@
X@@ int skip_arg(); Signals arguments beginning "-" so program won't try
X@@ to open them as filenames.
X@ caller: main()
X@ return: 1 if function finds arg beginning with "-"
X@ 0 otherwise
X@
X@*---------------------------------------------------------------------------*/
X
Xint
Xskip_arg(j, argv)
X int j;
X char *argv[];
X{
X if(argv[j][0] == '-')
X return(1);
X else
X return(0);
X}
X
X/*---------------------------------------------------------------------------*
X@
X@@ short cmd_line(count, strings); Gets filename(s) and args from cmnd line.
X@ output: Opens named input file(s)
X@ caller: main()
X@ return: 0 if function makes it to end
X@ 1 if file opens with error
X@
X@*---------------------------------------------------------------------------*/
X
Xshort
Xcmd_line(argc, argv)
X int argc;
X char *argv[];
X{
X int namepos = 0;
X int j;
X
X fprintf(stderr, "\n");
X /* Check for file name, occurs when first char is not '-' */
X for(j = 0; j < argc; j++)
X {
X if(strncmp(argv[j], "-s", 2) == 0)
X sortflg = 1;
X if(strncmp(argv[j], "-l", 2) == 0)
X masterflg |= 0x1;
X if(strncmp(argv[j], "-d", 2) == 0)
X masterflg |= 0x2;
X if(strncmp(argv[j], "-m", 2) == 0)
X masterflg |= 0x4;
X if(strncmp(argv[j], "-f", 2) == 0)
X masterflg |= 0x8;
X if(*argv[j] != '-')
X namepos = j;
X }
X /* If nothing on command line besides program name or -'s */
X if(!namepos)
X {
X fprintf(stderr, "\n No input filename(s)!\n\n");
X return(1);
X }
X return(0);
X}
X
X/*---------------------------------------------------------------------------*
X@
X@@ void main(); Calling module for sortcount.c
X@ input : As many input files as are on command line
X@ calls : cmd_line(), open_outfiles(), skip_arg(), write_outheads(),
X@ read_infile(), pick_outfiles(), print_count(), close_outfiles();
X@ fopen(), fclose(), ftell(), exit()
X@
X@*---------------------------------------------------------------------------*/
X
Xvoid
Xmain(argc, argv)
X int argc;
X char *argv[];
X{
X static short once = 1;
X int j;
X
X if(cmd_line(argc, argv))
X {
X fprintf(stderr, " Problems with command line input. Exiting.\n\n");
X exit(9);
X }
X if(sortflg)
X open_outfiles();
X j = 1;
X while(skip_arg(j, argv))
X j++;
X while((infp = fopen(argv[j], "r")) != NULL)
X {
X tell = ftell(infp);
X test = fgets(whinstr, 97, infp);
X while(whinstr[0] != '#')
X {
X if(once)
X if(sortflg)
X write_outheads();
X tell = ftell(infp);
X test = fgets(whinstr, 97, infp);
X if(test == NULL)
X break;
X }
X while(test != NULL)
X {
X once = 0;
X if(whinstr[0] == '#')
X {
X fgets(whinstr, 97, infp);
X fgets(whinstr, 97, infp);
X read_infile();
X pick_outfiles();
X liveflg = deadflg = malflg = femflg = 0;
X }
X }
X whtot += whthis;
X livetot += livethis;
X deadtot += deadthis;
X maltot += malthis;
X femtot += femthis;
X fprintf(stdout, "Whales this file =%4d\n", whthis);
X print_count(livethis, deadthis, malthis, femthis);
X whthis = livethis = deadthis = malthis = femthis = 0;
X j++;
X if(j >= argc)
X break;
X while(skip_arg(j, argv))
X {
X j++;
X if(j + 1 >= argc)
X break;
X }
X }
X fprintf(stdout, "Whales all files =%4d\n", whtot);
X print_count(livetot, deadtot, maltot, femtot);
X fprintf(stdout, "\n");
X fclose(infp);
X close_outfiles();
X exit(0);
X}
SHAR_EOF
if test 22016 -ne "`wc -c < 'sortcount.c'`"
then
echo shar: error transmitting "'sortcount.c'" '(should have been 22016 characters)'
fi
fi # end of overwriting check
# End of shell archive
exit 0
---
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
Bob Loy | Life is the detour you take on the
...!sun!sunburn!gtx!loy | way to where you're really going.
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
More information about the Comp.sources.misc
mailing list