home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2666 / lzipcode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-07  |  2.0 KB  |  114 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #define ZIPFILE "/usr/local/lib/zipcode.txt"
  4.  
  5. main(argc,argv)
  6. int        argc ;
  7. char    *argv[] ;
  8. {
  9.     FILE *zipfile;
  10.     int keynum;
  11.     char buf[128];
  12.     char grepbuf[128];
  13.     char keywrd[128];
  14.     int num1;
  15.     int num2;
  16.     int i;
  17.     char city[128];
  18.     char *City;
  19.     char *lowercase();
  20.     FILE *grepstream;
  21.  
  22.     if (argc != 2) {
  23.         fprintf(stderr, "Usage: zipcode keyword\n");
  24.         exit (1);
  25.     }
  26.     if (strlen(argv[1]) > 120) {
  27.         fprintf(stderr, "keyword %s too long\n", argv[1]);
  28.         exit (1);
  29.     }
  30.     if ( keynum = isnum(argv[1]) ) {
  31.         zipfile = fopen (ZIPFILE, "r");
  32.         if (zipfile == NULL) {
  33.             fprintf(stderr, "%s: unable to open zipcode.txt file\n", argv[0]);
  34.             exit(1) ;
  35.         }
  36.         while (fgets(buf, 128, zipfile) != NULL) {
  37.             *(buf+strlen(buf)-2) == '\0';
  38.             if (*(buf+5)=='-') {
  39.                 sscanf (buf, "%5d-%5d", &num1, &num2);
  40.                 City = buf+12;
  41.                } else {
  42.                 sscanf (buf, "%5d", &num1);
  43.                 num2 = num1;
  44.                 City = buf+6;
  45.             }
  46.             if (keynum >= num1 && keynum <= num2) {
  47.                 trunc (City);
  48.                 prbuf (num1, num2, City);
  49.                 break;
  50.             }
  51.         }
  52.         fclose (zipfile);
  53.     } else {
  54.         sprintf (grepbuf, "/usr/bin/egrep -i '%s' %s", argv[1], ZIPFILE);
  55.         grepstream = popen (grepbuf, "r");
  56.         if (grepstream == NULL) {
  57.             fprintf(stderr, "%s: can't pipe to grep\n", argv[0] );
  58.             exit( 1 );
  59.         }
  60.  
  61.         while (fgets (buf, 128, grepstream) != NULL) {
  62.             *(buf+strlen(buf)-2) == '\0';
  63.             if (*(buf+5)=='-') {
  64.                 sscanf (buf, "%5d-%5d", &num1, &num2);
  65.                 City = buf+12;
  66.                } else {
  67.                 sscanf (buf, "%5d", &num1);
  68.                 num2 = num1;
  69.                 City = buf+6;
  70.             }
  71.             trunc (City);
  72.             prbuf (num1, num2, City);
  73.         }
  74.         exit( pclose( grepstream ) );
  75.     }
  76. }
  77.  
  78. isnum(keywrd)
  79. char *keywrd;
  80. {
  81.     int i;
  82.     int num;
  83.  
  84.     if (strlen(keywrd) != 5)
  85.         return 0;
  86.     for (i=0; i<5; i++)
  87.         if (!isdigit(*(keywrd+i)))
  88.             return 0;
  89.     sscanf(keywrd, "%d", &num);
  90.     return num;
  91. }
  92.  
  93. trunc(s)
  94. char *s;
  95. {
  96.     char *cp;
  97.     cp = s + strlen(s);
  98.     while (cp-- >= s) {
  99.         if (!isspace(*cp)) break;
  100.         *cp = NULL;
  101.     }
  102. }
  103.  
  104. prbuf (n1, n2, s)
  105. int n1;
  106. int n2;
  107. char *s;
  108. {
  109.     if (n1 == n2)
  110.         printf ("%05d       %s\n", n1, s);
  111.         else
  112.         printf ("%05d-%05d %s\n", n1, n2, s);
  113. }
  114.