home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / mint / init_5 / getttyen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-03  |  3.9 KB  |  201 lines

  1. /*
  2.  * Getttyent.c    A routine to parse a ttytab file and return a structure
  3.  *             filled with the information extracted.
  4.  *
  5.  * Version 1.1 (c) S.R.Usher 1992.
  6.  *
  7.  * Changelog:-
  8.  *
  9.  * Date        Version        Programmer    Comments
  10.  * ----        -------        ----------    --------
  11.  * 21/9/91    1.0        S.R.Usher    First version
  12.  *  9/1/92    1.1        S.R.Usher    Allow comment lines starting
  13.  *                            with a #.
  14.  *
  15.  * Licence
  16.  * -------
  17.  *
  18.  * This software may be copied and distributed freely. It may also be modified
  19.  * as long as all changes are logged in the changelog above.
  20.  *
  21.  * If you want to get in contact with me, I can be contacted at:-
  22.  *
  23.  * ucacmsu@ucl.ac.uk or susher@uk.ac.csm
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <ctype.h>
  28. #include <ttyent.h>
  29.  
  30. static FILE *ttyfile;
  31. static char tty_name[32], tty_getty[256], tty_type[32], tty_window[256];
  32. static char tty_comment[256], hmm[256];
  33.  
  34. static struct ttyent entry = {
  35.     tty_name,
  36.     tty_getty,
  37.     tty_type,
  38.     0, 
  39.     tty_window,
  40.     tty_comment
  41. };
  42.  
  43. struct ttyent *getttyent()
  44. {
  45.     char *string;
  46.     char line_of_file[1024];
  47.     int current_pos;
  48.      
  49.     if (ttyfile == NULL)
  50.         if ((ttyfile = fopen("/etc/ttytab", "r")) == NULL)
  51.             return NULL;
  52.  
  53. nextone:
  54.     current_pos = 0;
  55.     
  56.     tty_name[0] = '\0';
  57.     tty_getty[0] = '\0';
  58.     tty_type[0] = '\0';
  59.     tty_window[0] = '\0';
  60.     tty_comment[0] = '\0';
  61.     entry.ty_status = 0;
  62.  
  63.     if ((string = fgets(line_of_file, 1023, ttyfile)) == NULL)
  64.         return NULL;
  65. #ifdef DEBUG
  66.     printf("getttyent: fgets returned '%s'\n", string);
  67. #endif
  68.     current_pos = getatoken(line_of_file, current_pos, tty_name, 31);
  69.  
  70.     if (*tty_name == '#')
  71.         goto nextone;
  72.  
  73.     current_pos = getatoken(line_of_file, current_pos, tty_getty, 255);
  74.     current_pos = getatoken(line_of_file, current_pos, tty_type, 31);
  75.     current_pos = getatoken(line_of_file, current_pos, hmm, 255);
  76.  
  77.     if (!strncmp(hmm, "on", 2))
  78.         entry.ty_status = TTY_ON;
  79.  
  80.     while (current_pos < strlen(line_of_file))
  81.     {
  82.         current_pos = getatoken(line_of_file, current_pos, hmm, 255);
  83.  
  84.         switch (hmm[0])
  85.         {
  86.         case 's':
  87.             entry.ty_status |= TTY_SECURE;
  88.             break;
  89.  
  90.         case 'w':
  91.             strcpy(tty_window, &hmm[8]);
  92.             tty_window[(strlen(tty_window) - 1)] = '\0';
  93.             break;
  94.  
  95.         case '#':
  96.             strcpy(tty_comment, &line_of_file[current_pos]);
  97.             tty_comment[strlen(tty_comment) - 1] = '\0';
  98.             current_pos = strlen(line_of_file);
  99.             break;
  100.         }
  101.     }
  102.  
  103.     return &entry;
  104. }
  105.  
  106. getatoken(input_string, position, output_string, maxlength)
  107. char *input_string;
  108. int position;
  109. char *output_string;
  110. int maxlength;
  111. {
  112.     register int i;
  113.     char tmp_string[2048];
  114.     
  115.     while ((input_string[position] == ' ') || (input_string[position] == '\t'))
  116.     {
  117. #ifdef DEBUG
  118.         printf("Killing white-space at position %d\n", position);
  119. #endif
  120.         position++;
  121.     }
  122.  
  123.     if (input_string[position] == '"')
  124.     {
  125. #ifdef DEBUG
  126.         printf("getatoken: quoted string '");
  127. #endif
  128.         for (position++, i = 0; (input_string[position] != '"') && (input_string[position] != '\0'); i++, position++)
  129.         {
  130.             tmp_string[i] = input_string[position];
  131. #ifdef DEBUG
  132.             printf("%c", tmp_string[i]);
  133. #endif
  134.         }
  135.  
  136.         position++;
  137.  
  138.         tmp_string[i] = '\0';
  139.     }
  140.     else
  141.     {
  142. #ifdef DEBUG
  143.         printf("getatoken: unquoted string '");
  144. #endif
  145.         for (i = 0; !isspace(input_string[position]) && (input_string[position] != '\0'); i++, position++)
  146.         {
  147.             tmp_string[i] = input_string[position];
  148. #ifdef DEBUG
  149.             printf("%c", tmp_string[i]);
  150. #endif
  151.         }
  152.  
  153.         position++;
  154.         
  155.         tmp_string[i] = '\0';
  156.     }
  157.  
  158. #ifdef DEBUG
  159.     printf("'\n");
  160. #endif
  161.  
  162.     strncpy(output_string, tmp_string, maxlength);
  163.  
  164.     return position;
  165. }
  166.  
  167. setttyent()
  168. {
  169.     if (ttyfile != NULL)
  170.     {
  171.         fseek(ttyfile, 0L, 0);
  172.         fflush(ttyfile);
  173.     }
  174. }
  175.  
  176. endttyent()
  177. {
  178.     if (ttyfile != NULL)
  179.     {
  180.         fclose(ttyfile);
  181.         ttyfile = (FILE *) NULL;
  182.     }
  183. }
  184.  
  185. struct ttyent *getttynam(name)
  186. char *name;
  187. {
  188.     struct ttyent *entry;
  189.     
  190.     while((entry = getttyent()) != NULL)
  191.         if (!strcmp(entry->ty_name, name))
  192.         {
  193.             endttyent();
  194.             return entry;
  195.         }
  196.  
  197.     endttyent();
  198.  
  199.     return NULL;
  200. }
  201.