home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume25 / finger / part03 / ttylocfile.c < prev    next >
C/C++ Source or Header  |  1992-04-03  |  6KB  |  270 lines

  1. /*
  2.  * ttylocfile.c -- read /etc/ttyloc and /etc/nttyloc (December 1985)
  3.  *
  4.  * Copyright (C) 1986, 1990  Philip L. Budne
  5.  *
  6.  * This file is part of "Phil's Finger Program".
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 1, or (at your option)
  11.  * any later version.
  12.  *
  13.  */
  14.  
  15. # ifndef lint
  16. static char *rcsid = "$Id: ttylocfile.c,v 3.0 90/07/06 13:12:03 budd Rel $";
  17. # endif /* lint not defined */
  18.  
  19. # include "finger.h"
  20. # include <sys/types.h>
  21. # include <stdio.h>
  22. # ifdef TTYENT
  23. # include <ttyent.h>
  24. # endif /* TTYENT defined */
  25.  
  26. # include "ttylocfile.h"
  27.  
  28. # define BUFSIZE 512
  29.  
  30. LOCAL TTYLOC ttyloc[MAXTTY];
  31. LOCAL int nttyloc;
  32.  
  33. LOCAL int ttyloc_compare( a, b )    /* comparison for qsorting ttyloc[] */
  34.     register struct ttyloc *a, *b;
  35. {
  36.     return strcmp( a->t_name, b->t_name );
  37. } /* ttyloc_compare */
  38.  
  39. GLOBAL int readnewttylocfile() {
  40.     FILE *fp;
  41.     char buf[BUFSIZE];
  42.     char *cp;
  43.     register char *sp;
  44.     register int c, q, l;
  45.     register struct ttyloc *tp;
  46.  
  47.     nttyloc = 0;
  48.     if( (fp = fopen(NLOCFILE,"r")) == NULL )
  49.     return( 0 );            /* found none */
  50.  
  51.     tp = ttyloc;            /* point to start of array */
  52.     while( nttyloc < MAXTTY && fgets(buf, BUFSIZE, fp) != NULL ) {
  53.     cp = buf;
  54.  
  55.     tp->t_name = NULL;
  56.     tp->t_short = NULL;
  57.     tp->t_locn = NULL;
  58.     tp->t_type = LT_UNKNOWN;
  59.  
  60.     l = strlen(cp);            /* smash nl into eos */
  61.     if( l > 0 && (buf[l-1] == '\n') )
  62.         buf[l-1] = EOS;
  63.  
  64.     if( !skipwhite( &cp ) )
  65.         continue;
  66.  
  67.     if( *cp == '#' )        /* comment line? */
  68.         continue;            /*  yes, skip it */
  69.  
  70.     sp = cp;            /* get start of line type */
  71.     if( !skipblack( &cp ) )
  72.         continue;
  73.     *cp++ = EOS;
  74.  
  75.     l = strlen( sp );
  76.     if( strncmp( sp, "hardwire", l ) == 0 )
  77.         tp->t_type = LT_HARD;
  78.     else if( strncmp( sp, "ttyloc", l ) == 0 )
  79.         tp->t_type = LT_TTYLOC;
  80.     else if( strncmp( sp, "dialup", l ) == 0 )
  81.         tp->t_type = LT_DIALUP;
  82.     else if( strncmp( sp, "random", l ) == 0 )
  83.         tp->t_type = LT_UNKNOWN;
  84.     else
  85.         continue;            /* you lose */
  86.  
  87.     if( !skipwhite( &cp ) )
  88.         continue;
  89.     tp->t_name = cp;        /* save start of terminal name */
  90.     if( !skipblack( &cp ) )
  91.         continue;
  92.     *cp++ = EOS;
  93.  
  94.     /* pick up short location */
  95.     if( *cp == '"' || *cp == '\'' ) {
  96.         q = *cp++;
  97.         sp = cp;
  98.         while( (c = *cp) != EOS && c != q )
  99.         cp++;
  100.         if( c == EOS )
  101.         continue;
  102.     }
  103.     else {
  104.         sp = cp;
  105.         if( !skipblack( &cp ) )
  106.         continue;
  107.     }
  108.     *cp++ = EOS;
  109.  
  110.     tp->t_short = sp;
  111.  
  112.     if( !skipwhite( &cp ) )
  113.         continue;
  114.  
  115.     /* pick up long location */
  116.     if( *cp == '"' || *cp == '\'' ) {
  117.         q = *cp++;
  118.         sp = cp;
  119.         while( (c = *cp) != EOS && c != q )
  120.         cp++;
  121.         *cp++ = EOS;
  122.     }
  123.     else
  124.         sp = cp;
  125.  
  126.     if( strlen( sp ) == 0 )        /* no long location? */
  127.         continue;            /* get lost */
  128.  
  129.     tp->t_locn  = savestr( sp );
  130.     tp->t_name  = savestr( tp->t_name );
  131.     if( *tp->t_short != EOS )
  132.         tp->t_short = savestr( tp->t_short );
  133.     else
  134.         tp->t_short = NULL;
  135.  
  136.     tp++;
  137.     nttyloc++;
  138.     } /* while fgets.. */
  139.     fclose(fp);
  140.     qsort( ttyloc, nttyloc, sizeof( ttyloc[0] ), ttyloc_compare );
  141.     return( nttyloc );
  142. } /* readnewttylocfile */
  143.  
  144. GLOBAL int readttylocfile() {
  145.     FILE *fp;
  146.     char buf[BUFSIZE];
  147.     char *cp;
  148.     register int i;
  149.     register char *sp;
  150.     register struct ttyloc *tp;
  151.  
  152.     nttyloc = 0;
  153.     if( (fp = fopen(LOCFILE,"r")) == NULL )
  154.     return( 0 );            /* found none */
  155.  
  156.     tp = ttyloc;            /* point to start of array */
  157.     while( nttyloc < MAXTTY && fgets(buf, BUFSIZE, fp) != NULL ) {
  158.     cp = buf;
  159.  
  160.     i = strlen(cp);            /* smash nl into eos */
  161.     if( i > 0 && (buf[i-1] == '\n') )
  162.         buf[i-1] = EOS;
  163.  
  164.     if( !skipwhite( &cp ) )
  165.         continue;
  166.  
  167.     if( *cp == '#' )        /* comment line? */
  168.         continue;            /*  yes, skip it */
  169.  
  170.     sp = cp;
  171.     if( !skipblack( &cp ))
  172.         continue;
  173.  
  174.     *cp++ = EOS;
  175.  
  176.     tp->t_short = NULL;
  177.     tp->t_locn = NULL;
  178.     tp->t_type = LT_UNKNOWN;
  179.  
  180.        if( strlen( cp ) > 0 ) {
  181.         tp->t_locn = savestr( cp );
  182.         tp->t_name = savestr( sp );
  183.         tp++;
  184.         nttyloc++;
  185.     }
  186.     } /* while fgets.. */
  187.     fclose(fp);
  188.     qsort( ttyloc, nttyloc, sizeof( ttyloc[0] ), ttyloc_compare );
  189.     return( nttyloc );
  190. } /* readttylocfile */
  191.  
  192. # ifdef TTYENT
  193. GLOBAL int readttyents() {        /* 4.3/Ultrix ttys file */
  194.     register struct ttyent *ty;
  195.     register struct ttyloc *tp;
  196.  
  197.     nttyloc = 0;
  198.     tp = ttyloc;
  199.     setttyent();
  200.     while( nttyloc < MAXTTY && (ty = getttyent()) != NULL ) {
  201. # ifndef USE_ALL_TTYENTS
  202.     if( (ty->ty_status & TTY_ON) == 0 )
  203.         continue;
  204. # endif /* USE_ALL_TTYENTS not defined */
  205.  
  206.     tp->t_name = savestr( ty->ty_name ); /* save line name */
  207.  
  208. # ifdef DONT_USE_TTYENT_TYPE_AS_SHORT
  209.     tp->t_short = NULL;
  210. # else  /* DONT_USE_TTYENT_TYPE_AS_SHORT not defined */
  211.     tp->t_short = savestr( ty->ty_type );
  212. # endif /* DONT_USE_TTYENT_TYPE_AS_SHORT not defined */
  213.  
  214.     tp->t_locn = NULL;        /* assume the worst */
  215.     if( ty->ty_comment != NULL ) {
  216.         char *cp;
  217.  
  218.         cp = ty->ty_comment;
  219.         if( *cp == '#' )        /* strip leading pound sign */
  220.         cp++;            /* (needed under uglix) */
  221.  
  222.         while( *cp == ' ' || *cp == '\t' ) /* strip leading white */
  223.         cp++;
  224.  
  225.         if( *cp != EOS )        /* anything left? */
  226.         tp->t_locn = savestr( cp ); /* yes, save as location */
  227.     } /* have comment */
  228.     tp->t_type = LT_UNKNOWN;
  229.     tp++;
  230.     nttyloc++;
  231.     } /* while */
  232.     endttyent();
  233.     qsort( ttyloc, nttyloc, sizeof( ttyloc[0] ), ttyloc_compare );
  234.     return( nttyloc );
  235. } /* readttyents */
  236. # endif /* TTYENT defined */
  237.  
  238. GLOBAL TTYLOC *findttyloc( tty )    /* find entry in ttyloc file */
  239. char *tty;
  240. {
  241.     register int min, max, ptr;
  242.     register TTYLOC *tp;
  243.  
  244.     min = 0;
  245.     max = nttyloc - 1;
  246.     while( min <= max ) {
  247.     int v;
  248.     ptr = (max + min + 1) >> 1;
  249.     tp = &ttyloc[ ptr ];
  250.     v = strcmp( tty, tp->t_name );
  251. # ifdef DEBUG
  252.     fprintf(stderr, "%d %d %d: %s,%s,%d\n",
  253.         min, ptr, max, tty, tp->t_name, v);
  254. # endif /* DEBUG defined */
  255.     if( v == 0 )
  256.         return( tp );
  257.     else if( v > 0 )
  258.         min = ptr + 1;
  259.     else
  260.         max = ptr - 1;
  261.     } /* while */
  262.     return( NULL );
  263. } /* findttyloc */
  264.  
  265. /*
  266.  * Local variables:
  267.  * comment-column: 40
  268.  * End:
  269.  */
  270.