[net.sources] number sequence utility

glenn@datamen.UUCP (Glenn R. Sogge) (04/15/87)

This utility arose from a need to occasionally parameterize
various calls in some source modules for easier tracing.  Since
the project is multi-person, we needed a bookkeeper of the numbers
used so far and to assign the next available one(s).  Although
the source still has to have the numbers manually inserted, the
communication and coordination problems have been reduced.

Feedback requested (including flames --- this is my first posting.)

....................................................
UUCP: ...!ihnp4!icom!datamen!glenn	BIX: gsogge
 WHO: Glenn R. Sogge	Datamension Corporation
			615 Academy Drive
  PH: (312) 564-5060	Northbrook, IL 60062
.......................................................

#! /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:
#	getnumber.1
#	getnumber.c
# This archive created: Wed Apr 15 13:38:22 1987
export PATH; PATH=/bin:$PATH
if test -f 'getnumber.1'
then
	echo shar: will not over-write existing file "'getnumber.1'"
else
sed 's/^X//' << \SHAR_EOF > 'getnumber.1'
X.TH GETNUMBER 1 "Local"
X.SH NAME
Xgetnumber \- generate a series of numbers with a history file
X.SH SYNOPSIS
X.B getnumber
X[ -f filename ]
X[ -n count ]
X[ -i initial ]
X.SH DESCRIPTION
X\fIGetnumber\fP generates the next number in a sequence that is remembered in
Xthe file \fI.COUNTER.\fP
XThe returned values are printed to \fIstdout\fP, one to a line.
XThe numbers are printed with leading blanks in a field of 5 characters.
X.sp
XIf the history file does not exist, you are asked whether it should be created.
XIf the initial letter of the reply is \fBy, Y,\fP or \fBnewline\fP,
Xthe file is created.
X(A line is read from the \fIstdin\fP so pressing \fIenter\fP
Xis required.)
XIf the file does not exist and is not created, the numbers are still
Xgenerated to \fIstdout.\fP
XOptions are:
X.sp
X.TP 8
X\fB\-f \fP\fIfilename\fP
XFile to be used instead of the default \fI.COUNTER\fP.
X.TP 8
X\fB-n \fP\fInumber\fP
XNumber of numbers to be returned.
X.TP 8
X\fB\-i \fP\fIinitial
XInitial value to be returned.  The history file is updated
Xappropriately.
X.sp
X.SH FILES
XUnless overridden with the \fB\-f\fP option, the file \fB.COUNTER\fP
Xin the current directory is used.
X.SH DIAGNOSTICS
XShould be self-explanatory.
X.SH BUGS
XUses integers so only the values 1 through 32768 are valid
X(on a 16-bit machine.)
X.SH HISTORY
XWritten by Glenn R. Sogge of Datamension Corporation.
SHAR_EOF
fi # end of overwriting check
if test -f 'getnumber.c'
then
	echo shar: will not over-write existing file "'getnumber.c'"
else
sed 's/^X//' << \SHAR_EOF > 'getnumber.c'
X#include	<stdio.h>
X/*
X *	getnumber.c
X *
X *	get one or more numbers in sequence from a file
X *	and update the file for the next invocation
X *
X *	History:
X *		04/15/87 Initial version
X *
X *	By:	Glenn R. Sogge
X *		Systems Support Manager
X *		Datamension Corporation
X *		615 Academy Drive
X *		Northbrook, IL 60062
X *		(312) 564-5060
X *
X *	UUCP:	...!ihnp4!icom!datamen!glenn
X *
X *
X*/
X
Xextern char *optarg ;
Xextern int optind ;
X
Xchar line[BUFSIZ] ;
X
Xstatic char version[] = "getnumber.c version 1.00 04/15/87" ;
X
Xmain(argc,argv)
Xint argc;
Xchar *argv[] ;
X{
X
X	int c ;
X	char *filename ;
X	int count ;
X	int nextnumber ;
X	int init ;
X	int errflag ;
X	int initflag ;
X	FILE *iofile ;
X
X	errflag = 0 ;
X	filename = ".COUNTER" ;	/* default file in current directory */
X	count = 1 ;
X	init = 1 ;
X	initflag = 0 ;
X
X	while ((c=getopt(argc,argv,"f:n:i:")) != EOF) {
X		switch(c) {
X			case 'f':
X				filename = optarg ;
X				break ;
X			case 'n':
X				count = atoi(optarg) ;
X				break ;
X			case 'i':
X				init = atoi(optarg) ;
X				initflag++ ;
X				break ;
X			case '?':
X				errflag++ ;
X				break ;
X		}
X	}
X	if (errflag) {
X		fprintf(stderr,"usage: %s [-f filename] [-n count] [-i initial]\n",argv[0]) ;
X		exit(errflag) ;
X	}
X
X	if (count<=0) {
X		fprintf(stderr,"%s: count must positive and non-zero\n",argv[0]) ;
X		exit(1) ;
X	}
X
X	if (init<=0) {
X		fprintf(stderr,"%s: initial must positive and non-zero\n",argv[0]) ;
X		exit(1) ;
X	}
X
X	iofile = fopen(filename,"r+") ;	/* for r/w */
X	if (iofile==NULL) {
X		fprintf(stderr,"%s: count file <%s> does not exist, create?",argv[0],filename) ;
X		c = getc(stdin) ;
X		switch(c){
X			case 'y':
X			case 'Y':
X			case '\n':
X				if ((iofile=fopen(filename,"w+"))==NULL) {
X					fprintf(stderr,"%s: error creating <%s>\n",argv[0],filename) ;
X					exit(1) ;
X				}
X				fprintf(iofile,"%05d\n",init) ;	/* initialize to next available */
X				fseek(iofile,0L,0) ;	/* rewind file pointer */
X				break ;
X			default:
X				break ;
X		}
X	}
X	/* file may exist and is open now */
X	if (iofile) {
X		fgets(line,BUFSIZ,iofile) ;
X		nextnumber = atoi(line) ;
X	} else {
X		nextnumber = init ;
X	}
X	if (nextnumber<=0) {	/* "can't happen" */
X		fprintf(stderr,"%s: invalid number in file\n",argv[0]) ;
X		if (iofile)	fclose(iofile) ;
X		exit(1) ;
X	}
X
X	if (initflag)	nextnumber = init ;	/* override if specified */
X	/* unnecessary if file was created, but needed if it exists */
X	for ( ; count ; count-- , nextnumber++ )
X		printf("%5d\n",nextnumber) ;
X
X	if (iofile) {
X		fseek(iofile,0L,0) ;	/* rewind */
X		fprintf(iofile,"%05d\n",nextnumber) ;
X		fclose(iofile) ;
X	}
X	exit(0) ;
X}
SHAR_EOF
fi # end of overwriting check
#	End of shell archive
exit 0
-- 
....................................................
UUCP: ...!ihnp4!icom!datamen!glenn	BIX: gsogge
 WHO: Glenn R. Sogge	Datamension Corporation
			615 Academy Drive
  PH: (312) 564-5060	Northbrook, IL 60062

metzger@garfield.columbia.edu.UUCP (04/16/87)

This is another example of a program that could have been done in a
line or two of shellish without any trouble, and yet we see a long and
drawn out program to do it.

If anyone wants to see the two lines I will bother with them, but I
don't see any reason to; anyone in their right mind knows how.

Perry