home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / lib / util / gettys.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-28  |  2.9 KB  |  175 lines

  1. #
  2. /*  A set of routines in the style of getpw() to read the ttys
  3.  *  file and return the tty info.
  4.  *
  5.  *  Steve Manion
  6.  *
  7. */
  8.  
  9. #include    <stdio.h>
  10. #include        "gettys.h"
  11.  
  12. #define        ETCTTYS        "/etc/ttys"
  13.  
  14. static int getline(), process();
  15.  
  16. /*  the channel to read the data from  */
  17. static FILE *input = NULL;
  18.  
  19. /*  the line number last read from the file  */
  20. static int linenum = 0;
  21.  
  22. getttyent(userdata)
  23.   struct ttys *userdata;
  24.     {
  25.     char buff[128];
  26.  
  27.     /*  if the input file is not yet open, do it  */
  28.     if (input == NULL)
  29.     if (openinput(ETCTTYS) < 0)
  30.         return(BADDATA);
  31.  
  32.     /*  read the next line from the input  */
  33.     if (getline(buff) == EOF)
  34.     {
  35.     rewind(input);
  36.     linenum = 0;
  37.     return(EOF);
  38.     }
  39.  
  40.     /*  transfer the data into the user struct  */
  41.     if (process(buff, userdata) < 0)
  42.     return(BADDATA);
  43.  
  44.     /*  return the line number we are on  */
  45.     return(linenum);
  46. }
  47.  
  48.  
  49.  
  50.  
  51.  
  52. openinput(file)
  53.   char *file;
  54.     {
  55.  
  56.     input = fopen(file, "r");
  57.     if (input == NULL)
  58.     {
  59.     printf("File '%s': ", file);
  60.     fflush(stdout);
  61.     perror("");
  62.     fflush(stderr);
  63.     return(-1);
  64.     }
  65.  
  66.     return(0);
  67. }
  68.  
  69.  
  70.  
  71.  
  72.  
  73. static int getline(buff)
  74.   char *buff;
  75.     {
  76.     register int c;
  77.  
  78.     for(;;)
  79.     {
  80.     c = getc(input);
  81.  
  82.     /*  look for the true end of the file  */
  83.     if( (c == EOF) && (feof(input) != 0)   )
  84.     {
  85.         *buff = '\0';
  86.         return(EOF);
  87.     }
  88.  
  89.     /*  transfer character to the buffer and check for the
  90.      *  end of the line.
  91.      */
  92.     *buff++ = c;
  93.     if (c == '\n')
  94.     {
  95.         *buff = '\0';
  96.         linenum++;
  97.         return(0);
  98.     }
  99.     }
  100. }
  101.  
  102.  
  103.  
  104.  
  105.  
  106. static int process(buff, userdata)
  107.   char *buff;
  108.   struct ttys *userdata;
  109.     {
  110.     register char *cp;
  111.  
  112.     /*  check the first char of the line.  It should be 0 or 1 as
  113.      *  the line is invalid or valid.
  114.      */
  115.     switch(*buff++)
  116.     {
  117.     case '0':
  118.         userdata->t_valid = 0;
  119.         break;
  120.  
  121.     case '1':
  122.         userdata->t_valid = 1;
  123.         break;
  124.  
  125.     default:
  126.         fflush(stdout);
  127.         fprintf(stderr, "Invalid tty file - notify system manager.\n");
  128.         fflush(stderr);
  129.         return(-1);
  130.     }
  131.  
  132.     /*  transfer the control code directly  */
  133.     userdata->t_code = *buff++;
  134.  
  135.     /*  transfer the tty name to the buffer  */
  136.     for(cp = userdata->t_name;  *buff != '\n';  )
  137.     *cp++ = *buff++;
  138.     *cp = '\0';
  139.  
  140.     return(0);
  141. }
  142.  
  143.  
  144.  
  145.  
  146. getttynam(userdata, name)
  147.   struct ttys *userdata;
  148.   char *name;
  149.     {
  150.     register int curline, result;
  151.  
  152.     /*  the strategy of this routine is to cycle through the
  153.      *  tty file, starting from the current position, until
  154.      *  the tty 'name' is located.  Note that the getttyent()
  155.      *  routine will automaticly rewind the input when appropriate.
  156.      *  Note also that the call to it ultimately updates the
  157.      *  variable 'linenum'.
  158.      */
  159.  
  160.     curline = linenum;
  161.     do
  162.     {
  163.     result = getttyent(userdata);
  164.     if(result == EOF)
  165.         continue;
  166.     if(result == BADDATA)
  167.         break;
  168.     if(strcmp(name, userdata->t_name) == 0)
  169.         return(result);
  170.     }
  171.     while (curline != linenum);
  172.  
  173.     return(BADDATA);
  174. }
  175.