zipcode (part 1 or 9 - repost due to missing files in first posting)
Maj Brian Boyter
boyter at bimbo.uucp
Thu Feb 7 11:28:26 AEST 1991
--- on Feb 6th Joe Evans wrote: ---
> Maj. Boyter:
> I'm having a little trouble getting your zipcode program (posted to
> alt.sources) to compile. I seem to be missing a file called
> zipcode.x, and the man page zipcode.1. I would appreciate any hints you
> can offer.
> Thanks,
------------------------------------
Well, Joe, I guess I screwed that up...
Thanks for pointing it out to me...
Try this one:
Brian
------------------------------------------------------------------
#!/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/07/1991 00:22 UTC by boyter 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
# 1385 -rw-r--r-- zipcode.1
# 351 -rw-r--r-- zipcode.x
# 1537 -rw-r--r-- zipcode.c
#
# ============= 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
# ============= zipcode.1 ==============
if test -f 'zipcode.1' -a X"$1" != X"-c"; then
echo 'x - skipping zipcode.1 (File already exists)'
else
echo 'x - extracting zipcode.1 (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'zipcode.1' &&
.TH ZIPCODE 1 "November 13, 1990"
.SH NAME
zipcode \- look up postal zipcodes
.SH SYNOPSIS
.nf
zipcode [-v][-h server] keyword
.fi
.SH DESCRIPTION
Zipcode searches a small ascii-text database for postal zipcodes.
If the keyword is a zipcode, the city associated with that zipcode
is returned. If the keyword is a city name, the zipcode(s) is
returned. Zipcode uses the Remote Procedure Call (RPC) mechanism.
.SS OPTIONS
.PP
Options can be given in any order.
.IP -v
Return the version number.
.IP "-h server"
The host where the rpc.zipcoded server is running.
If no hostname is given, "localhost" is used (the rpc.zipcoded
must be running locally).
.SH EXAMPLES
.nf
.ta 25m
zipcode 22901 # returns:
X 22900-22910 Charlottesville, VA
zipcode charlottesville # returns:
X 22900-22910 Charlottesville, VA
X 46117 Charlottesville, IN
zipcode "new york" # returns:
X <161 lines>
.ta
.fi
If you are trying to send mail, for example, to Charlottesville, Va,
you will get eleven zipcodes (22900-22910). I am told
(by my sister who works for the post office) that the best thing to
do is to only put "229" (the first three digits of the zipcode)
on the envelope. The first three digits are sufficient to route
the mail to the correct "postal center" where the complete zipcode
will (hopefully) be known.
.PP
.SH AUTHORS
.nf
Maj Brian Boyter, US Army, boyter at fstc-chville.army.mil
.fi
SHAR_EOF
chmod 0644 zipcode.1 ||
echo 'restore of zipcode.1 failed'
Wc_c="`wc -c < 'zipcode.1'`"
test 1385 -eq "$Wc_c" ||
echo 'zipcode.1: original size 1385, current size' "$Wc_c"
fi
# ============= zipcode.x ==============
if test -f 'zipcode.x' -a X"$1" != X"-c"; then
echo 'x - skipping zipcode.x (File already exists)'
else
echo 'x - extracting zipcode.x (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'zipcode.x' &&
/*
X * zipcode.x rpcgen code for zipcode program
X */
X
const MAXDATALEN = 144;
typedef string ZIPDATA<MAXDATALEN>;
typedef struct ZIPLIST *ZIPNEXT;
X
struct ZIPLIST {
X ZIPDATA data;
X ZIPNEXT next;
};
X
struct RESULT {
X int ziperror;
X ZIPNEXT list;
};
X
program ZIPCODEPROC {
X version ZIPCODEVERS {
X RESULT ZIPCODE(string) = 1;
X } = 1;
} = 0x20000088;
SHAR_EOF
chmod 0644 zipcode.x ||
echo 'restore of zipcode.x failed'
Wc_c="`wc -c < 'zipcode.x'`"
test 351 -eq "$Wc_c" ||
echo 'zipcode.x: original size 351, current size' "$Wc_c"
fi
# ============= zipcode.c ==============
if test -f 'zipcode.c' -a X"$1" != X"-c"; then
echo 'x - skipping zipcode.c (File already exists)'
else
echo 'x - extracting zipcode.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'zipcode.c' &&
#include <stdio.h>
#include <string.h>
#include <rpc/rpc.h>
#include "zipcode.h"
#include "copyright.h"
#include "version.h"
X
main(argc,argv)
int argc ;
char *argv[];
{
X char *server;
X char *keyword;
X CLIENT *client;
X RESULT *result;
X int n;
X ZIPLIST *zl;
X extern char *optarg;
X extern int optind;
X
X server = NULL;
X keyword = NULL;
X while ((n = getopt(argc, argv, "h:v")) != -1) {
X switch (n) {
X case 'v':
X printf ("zipcode: version %s\n", VERSION);
X break;
X case 'h':
X server = optarg;
X break;
X case '?':
X fprintf(stderr,
X "Usage: zipcode [-v][-h server] keyword\n");
X exit (1);
X }
X }
X
X keyword = argv[optind];
X if (server == NULL)
X server = strdup("localhost");
X
X if (keyword == NULL) {
X fprintf(stderr, "no keyword specified\n");
X fprintf(stderr,
X "Usage: zipcode [-v][-h server] keyword\n");
X exit (1);
X }
X if (strlen(keyword) > 120) {
X fprintf(stderr, "keyword %s too long\n", keyword);
X exit (1);
X }
X
X /* open rpc connection */
X client = clnt_create(server, ZIPCODEPROC, ZIPCODEVERS, "tcp");
X if (client == NULL) {
X clnt_pcreateerror(server);
X exit (1);
X }
X
X /* call the remote procedure on the server */
X result = zipcode_1(&keyword, client);
X if (result == NULL) {
X clnt_perror (client, server);
X exit (2);
X }
X
X /* print the result */
X switch (result->ziperror) {
X case 0:
X zl = result->list;
X while (zl != NULL) {
X printf ("%s\n", zl->data);
X if (zl->next == NULL)
X break;
X zl = zl->next;
X }
X break;
X default: /* error */
X fprintf (stderr, "zipcode: unknown error\n");
X }
}
SHAR_EOF
chmod 0644 zipcode.c ||
echo 'restore of zipcode.c failed'
Wc_c="`wc -c < 'zipcode.c'`"
test 1537 -eq "$Wc_c" ||
echo 'zipcode.c: original size 1537, 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