zipcode (part 1 of 9)

Maj Brian Boyter boyter at bimbo.uucp
Wed Feb 6 14:37:58 AEST 1991


This posting has 9 parts.
Part 1 is a program for looking-up zipcodes. The program has
two flavors: a local version and an rpc version.
Parts 2-8 are the table of zipcodes.
Part 9 is a special table of Desert-Storm army-only zipcodes.
If you don't like my programs you might find another use for the
zipcode file.

Constructive criticism is welcome (this is my first rpc program
so be kind).

boyter at fstc-chville.army.mil

----------------------------------------------------------------
#!/bin/sh
# This is a shell archive (produced by shar 3.49)
# To extract the files from this archive, save it to a file, remove
# everything above the "!/bin/sh" line above, and type "sh file_name".
#
# made 02/06/1991 03:25 UTC by root at bimbo
# Source directory /local/src/zipcode
#
# existing files will NOT be overwritten unless -c is specified
#
# This shar contains:
# length  mode       name
# ------ ---------- ------------------------------------------
#   1122 -rw-r--r-- Makefile
#   1195 -rw-r--r-- README
#   1083 -rw-r--r-- copyright.h
#   2090 -rw-r--r-- lzipcode.c
#   3447 -rw-r--r-- rpc.zipcoded.c
#     29 -rw-r--r-- version.h
#
# ============= Makefile ==============
if test -f 'Makefile' -a X"$1" != X"-c"; then
	echo 'x - skipping Makefile (File already exists)'
else
echo 'x - extracting Makefile (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'Makefile' &&
CFLAGS=-O
X
all: zipcode lzipcode rpc.zipcoded
X
lzipcode: lzipcode.o
X	cc -o lzipcode ${CFLAGS} lzipcode.o
X
clean:
X	rm -f zipcode *.o core lzipcode rpc.zipcoded
X	rm -f zipcode.h zipcode_clnt.c zipcode_svc.c zipcode_xdr.c
X	rm -f zipcode.txt
X
install: zipcode rpc.zipcoded zipcode.txt
X	install -o root -g wheel -m 755 zipcode      /usr/local/bin
X	install -o root -g wheel -m 755 rpc.zipcoded /usr/etc
X	install -o root -g wheel -m 644 zipcode.txt  /usr/local/lib
X	install -o root -g wheel -m 644 zipcode.1    /usr/local/man/man1
X
zipcode.txt: zipcode.txt.1 zipcode.txt.2 zipcode.txt.3 zipcode.txt.4 \
X	     zipcode.txt.5 zipcode.txt.6 zipcode.txt.7 zipcode.txt.8
X	cat zipcode.txt.[1-8] >> zipcode.txt
X
zipcode: zipcode_clnt.o zipcode.o zipcode_xdr.o
X	cc ${CFLAGS} -o zipcode zipcode_clnt.o zipcode.o zipcode_xdr.o
X
rpc.zipcoded: zipcode_svc.o  rpc.zipcoded.o zipcode_xdr.o
X	cc ${CFLAGS} -o rpc.zipcoded zipcode_svc.o rpc.zipcoded.o zipcode_xdr.o
X
zipcode_svc.o:  zipcode.h
zipcode_clnt.o: zipcode.h
rpc.zipcoded.o: zipcode.h
zipcode_xdr.o:  zipcode.h
zipcode.o:      zipcode.h version.h
X
zipcode.h: zipcode.x
X	rpcgen zipcode.x
SHAR_EOF
chmod 0644 Makefile ||
echo 'restore of Makefile failed'
Wc_c="`wc -c < 'Makefile'`"
test 1122 -eq "$Wc_c" ||
	echo 'Makefile: original size 1122, current size' "$Wc_c"
fi
# ============= README ==============
if test -f 'README' -a X"$1" != X"-c"; then
	echo 'x - skipping README (File already exists)'
else
echo 'x - extracting README (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'README' &&
README for zipcode program...
X
This program has 4 main parts:
X  1) zipcode.txt -- An ascii file of known zipcodes. I you don't
X     like my program you can write your own using this file.
X     The file is shipped in 8 segments. The last segment is
X     a special "annex" of zipcodes in use by army units in operation
X     DESERT-STORM.
X  2) lzipcode -- when you make this program, you can search the
X     ascii for zipcodes (given the city name) or the city name
X     (given the zipcode). This program calls "egrep" to do the
X     actual search.
X  3) rpc.zipcoded -- an rpc server to be installed on the same host
X     that the zipcode.txt file resides. You need to start the server
X     by putting "/usr/etc/rpc.zipcoded &" in your /etc/rc.local file
X     and "zipcoded 536871048" in your /etc/rpc file (The
X     assignment of 536871048 was arbitrary. If you use a different
X     number, change the appropriate numbers in the code).
X  4) zipcode -- an rpc client program which calls rpc.zipcoded.
X
This is my first use of RPC, so contact the author if you have
any "improvements".
X
Maj Brian Boyter
US Army Foreign Science and Technology Center
Charlottesville, Va
boyter at fstc-chville.army.mil
SHAR_EOF
chmod 0644 README ||
echo 'restore of README failed'
Wc_c="`wc -c < 'README'`"
test 1195 -eq "$Wc_c" ||
	echo 'README: original size 1195, current size' "$Wc_c"
fi
# ============= copyright.h ==============
if test -f 'copyright.h' -a X"$1" != X"-c"; then
	echo 'x - skipping copyright.h (File already exists)'
else
echo 'x - extracting copyright.h (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'copyright.h' &&
#ifndef _COPYRIGHT_
/*
X * Copyright 1990, Brian Boyter
X *
X * Permission to use, copy, modify, distribute, and sell this software
X * and its documentation for any purpose is hereby granted without fee,
X * provided that the above copyright notice appear in all copies and
X * that both that copyright notice and this permission notice appear
X * in supporting documentation.  The author makes no representations
X * about the suitability of this software for any purpose.  It is
X * provided "as is" without express or implied warranty.
X *
X * THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
X * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
X * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR
X * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
X * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
X * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
X * USE OR PERFORMANCE OF THIS SOFTWARE.
X */
X
static char *Copyright= "Copyright 1990 Brian Boyter";
#define _COPYRIGHT_
#endif
SHAR_EOF
chmod 0644 copyright.h ||
echo 'restore of copyright.h failed'
Wc_c="`wc -c < 'copyright.h'`"
test 1083 -eq "$Wc_c" ||
	echo 'copyright.h: original size 1083, current size' "$Wc_c"
fi
# ============= lzipcode.c ==============
if test -f 'lzipcode.c' -a X"$1" != X"-c"; then
	echo 'x - skipping lzipcode.c (File already exists)'
else
echo 'x - extracting lzipcode.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'lzipcode.c' &&
#include <stdio.h>
#include <ctype.h>
#define ZIPFILE "/usr/local/lib/zipcode.txt"
X
main(argc,argv)
int        argc ;
char    *argv[] ;
{
X	FILE *zipfile;
X	int keynum;
X	char buf[128];
X	char grepbuf[128];
X	char keywrd[128];
X	int num1;
X	int num2;
X	int i;
X	char city[128];
X	char *City;
X	char *lowercase();
X	FILE *grepstream;
X
X	if (argc != 2) {
X		fprintf(stderr, "Usage: zipcode keyword\n");
X		exit (1);
X	}
X	if (strlen(argv[1]) > 120) {
X		fprintf(stderr, "keyword %s too long\n", argv[1]);
X		exit (1);
X	}
X	if ( keynum = isnum(argv[1]) ) {
X		zipfile = fopen (ZIPFILE, "r");
X		if (zipfile == NULL) {
X			fprintf(stderr, "%s: unable to open zipcode.txt file\n", argv[0]);
X			exit(1) ;
X		}
X		while (fgets(buf, 128, zipfile) != NULL) {
X			*(buf+strlen(buf)-2) == '\0';
X			if (*(buf+5)=='-') {
X				sscanf (buf, "%5d-%5d", &num1, &num2);
X				City = buf+12;
X		   	} else {
X				sscanf (buf, "%5d", &num1);
X				num2 = num1;
X				City = buf+6;
X			}
X			if (keynum >= num1 && keynum <= num2) {
X				trunc (City);
X				prbuf (num1, num2, City);
X				break;
X			}
X		}
X		fclose (zipfile);
X	} else {
X		sprintf (grepbuf, "/usr/bin/egrep -i '%s' %s", argv[1], ZIPFILE);
X		grepstream = popen (grepbuf, "r");
X		if (grepstream == NULL) {
X			fprintf(stderr, "%s: can't pipe to grep\n", argv[0] );
X			exit( 1 );
X		}
X
X		while (fgets (buf, 128, grepstream) != NULL) {
X			*(buf+strlen(buf)-2) == '\0';
X			if (*(buf+5)=='-') {
X				sscanf (buf, "%5d-%5d", &num1, &num2);
X				City = buf+12;
X		   	} else {
X				sscanf (buf, "%5d", &num1);
X				num2 = num1;
X				City = buf+6;
X			}
X			trunc (City);
X			prbuf (num1, num2, City);
X		}
X		exit( pclose( grepstream ) );
X	}
}
X
isnum(keywrd)
char *keywrd;
{
X	int i;
X	int num;
X
X	if (strlen(keywrd) != 5)
X		return 0;
X	for (i=0; i<5; i++)
X		if (!isdigit(*(keywrd+i)))
X			return 0;
X	sscanf(keywrd, "%d", &num);
X	return num;
}
X
trunc(s)
char *s;
{
X	char *cp;
X	cp = s + strlen(s);
X	while (cp-- >= s) {
X		if (!isspace(*cp)) break;
X		*cp = NULL;
X	}
}
X
prbuf (n1, n2, s)
int n1;
int n2;
char *s;
{
X	if (n1 == n2)
X		printf ("%05d       %s\n", n1, s);
X	    else
X		printf ("%05d-%05d %s\n", n1, n2, s);
}
SHAR_EOF
chmod 0644 lzipcode.c ||
echo 'restore of lzipcode.c failed'
Wc_c="`wc -c < 'lzipcode.c'`"
test 2090 -eq "$Wc_c" ||
	echo 'lzipcode.c: original size 2090, current size' "$Wc_c"
fi
# ============= rpc.zipcoded.c ==============
if test -f 'rpc.zipcoded.c' -a X"$1" != X"-c"; then
	echo 'x - skipping rpc.zipcoded.c (File already exists)'
else
echo 'x - extracting rpc.zipcoded.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'rpc.zipcoded.c' &&
#include <stdio.h>
#include <ctype.h>
#include <rpc/rpc.h>
#include "zipcode.h"
X
#define ZIPFILE "/usr/local/lib/zipcode.txt"
static RESULT result;	/* must be static */
X
RESULT *zipcode_1(keyword)
char **keyword;
{
X	FILE *zipfile;
X	int keynum;
X	char buffer[128];
X	char errbuf[128];
X	char grepbuf[128];
X	char keywrd[128];
X	int num1;
X	int num2;
X	int i;
X	char city[128];
X	char *City;
X	char *lowercase();
X	FILE *grepstream;
X	ZIPNEXT *zrp;
X	ZIPLIST *zlp;
X	char *pbuf();
X
X	xdr_free(xdr_ZIPLIST, &result);	/* free memory from previous call */
X
X	/* check length of keyword */
X	if (strlen(*keyword) > 120) {
X		sprintf(errbuf, "keyword %s too long", *keyword);
X		send_errmsg (errbuf);
X		return (&result);
X	}
X
X	if ( keynum = isnum(*keyword) ) {
/*
X * This code finds the city given a numeric zipcode
X * (can only be one match)
X */
X		zipfile = fopen (ZIPFILE, "r");		/* open data file */
X		if (zipfile == NULL) {
X			send_errmsg ("rpc.zipcoded: unable to open zipcode.txt file");
X			return (&result);
X		}
X		while (fgets(buffer, 128, zipfile) != NULL) {
X			*(buffer+strlen(buffer)-2) == '\0';
X			if (*(buffer+5)=='-') {
X				sscanf (buffer, "%5d-%5d", &num1, &num2);
X				City = buffer+12;
X		   	} else {
X				sscanf (buffer, "%5d", &num1);
X				num2 = num1;
X				City = buffer+6;
X			}
X			if (keynum >= num1 && keynum <= num2) {
X				trunc (City);
X				zlp        = (ZIPLIST *) malloc(sizeof(ZIPLIST));
X				zlp->data  = (pbuf(num1, num2, City));
X				zlp->next  = NULL;
X				result.ziperror = 0;
X				result.list= zlp;
X				return (&result);
X			}
X		}
X		fclose (zipfile);
X		sprintf(errbuf, "rpc.zipcoded: no match found for zipcode %05d", keynum);
X		send_errmsg (errbuf);
X		return (&result);
X	} else {
/*
X * This code finds the zipcode(s) given a City name
X */
X		sprintf (grepbuf, "/usr/bin/egrep -i '%s' %s", *keyword, ZIPFILE);
X		grepstream = popen (grepbuf, "r");
X		if (grepstream == NULL) {
X			send_errmsg ("rpc.zipcoded: can't pipe to egrep");
X			return (&result);
X		}
X
X		zrp = &result.list;
X		while (fgets (buffer, 128, grepstream) != NULL) {
X			*(buffer+strlen(buffer)-2) == '\0';
X			if (*(buffer+5)=='-') {
X				sscanf (buffer, "%5d-%5d", &num1, &num2);
X				City = buffer+12;
X		   	} else {
X				sscanf (buffer, "%5d", &num1);
X				num2 = num1;
X				City = buffer+6;
X			}
X			trunc (City);
X			zlp       = (ZIPLIST *) malloc(sizeof(ZIPLIST));
X			*zrp      = zlp;
X			zlp->data = pbuf(num1, num2, City);
X			zrp       = &zlp->next;
X		}
X		pclose( grepstream );
X		if (zrp == &result.list) {
X			sprintf(errbuf, 
X				"rpc.zipcoded: no match found for keyword: %s", *keyword);
X			send_errmsg (errbuf);
X			return (&result);
X		} else {
X			*zrp = NULL;
X			result.ziperror = 0;
X		}
X		return (&result);
X	}
}
X
isnum(keywrd)
char *keywrd;
{
X	int i;
X	int num;
X
X	if (strlen(keywrd) != 5)
X		return 0;
X	for (i=0; i<5; i++)
X		if (!isdigit(*(keywrd+i)))
X			return 0;
X	sscanf(keywrd, "%d", &num);
X	return num;
}
X
trunc(s)
char *s;
{
X	char *cp;
X	cp = s + strlen(s);
X	while (cp-- >= s) {
X		if (!isspace(*cp)) break;
X		*cp = NULL;
X	}
}
X
char *pbuf(num1, num2, City)
int num1;
int num2;
char *City;
{
X	char *mp;
X	
X	mp = malloc(15+strlen(City));
X
X	if (num1 == num2)
X		sprintf (mp, "%05d       %s", num1, City);
X	else
X		sprintf (mp, "%05d-%05d %s", num1, num2, City);
X
X	return mp;
}
X
send_errmsg (errmsg)
char *errmsg;
{
X	ZIPLIST *zlp;
X	char *strdup();
X
X	zlp        = (ZIPLIST *) malloc(sizeof(ZIPLIST));
X	zlp->data  = strdup (errmsg);
X	zlp->next  = NULL;
X	result.list= zlp;
X	result.ziperror = 0;
}
SHAR_EOF
chmod 0644 rpc.zipcoded.c ||
echo 'restore of rpc.zipcoded.c failed'
Wc_c="`wc -c < 'rpc.zipcoded.c'`"
test 3447 -eq "$Wc_c" ||
	echo 'rpc.zipcoded.c: original size 3447, current size' "$Wc_c"
fi
# ============= version.h ==============
if test -f 'version.h' -a X"$1" != X"-c"; then
	echo 'x - skipping version.h (File already exists)'
else
echo 'x - extracting version.h (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'version.h' &&
#define VERSION "1.0 (beta)"
SHAR_EOF
chmod 0644 version.h ||
echo 'restore of version.h failed'
Wc_c="`wc -c < 'version.h'`"
test 29 -eq "$Wc_c" ||
	echo 'version.h: original size 29, current size' "$Wc_c"
fi
exit 0
-- 
---------------------------------------------------------------
   Maj. Brian A Boyter
   US Army Foreign Science & Technology Center
   Charlottesville, Va 22901                         __
   off: (804)980-7362                              (    )
   home:     973-9440                             {      }
                                                   (    )
   boyter at fstc-chville.army.mil                      ||
                                                     ||
   Just say glow......                       _______<  >_______



More information about the Alt.sources mailing list