home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume38 / shadow / part13 / ttytype.c < prev    next >
C/C++ Source or Header  |  1993-08-14  |  2KB  |  85 lines

  1. /*
  2.  * Copyright 1989, 1990, 1991, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Permission is granted to copy and create derivative works for any
  6.  * non-commercial purpose, provided this copyright notice is preserved
  7.  * in all copies of source code, or included in human readable form
  8.  * and conspicuously displayed on all copies of object code or
  9.  * distribution media.
  10.  */
  11.  
  12. #include <stdio.h>
  13. #ifndef    BSD
  14. #include <string.h>
  15. #include <memory.h>
  16. #else
  17. #include <strings.h>
  18. #define    strchr    index
  19. #define    strrchr    rindex
  20. #endif
  21. #include "config.h"
  22.  
  23. #ifndef    lint
  24. static    char    _sccsid[] = "@(#)ttytype.c    3.3    19:40:04    28 Dec 1991";
  25. #endif
  26.  
  27. extern    char    *getdef_str();
  28.  
  29. /*
  30.  * ttytype - set ttytype from port to terminal type mapping database
  31.  */
  32.  
  33. void
  34. ttytype (line)
  35. char    *line;
  36. {
  37.     FILE    *fp;
  38.     char    buf[BUFSIZ];
  39.     char    termvar[BUFSIZ];
  40.     char    *typefile;
  41.     char    *cp;
  42.     char    *type;
  43.     char    *port;
  44.     char    *getenv ();
  45.  
  46.     if (getenv ("TERM"))
  47.         return;
  48.     if ((typefile=getdef_str("TTYTYPE_FILE")) == NULL )
  49.         return;
  50.     if (access (typefile, 0))
  51.         return;
  52.  
  53.     if (! (fp = fopen (typefile, "r"))) {
  54.         perror (typefile);
  55.         return;
  56.     }
  57.     while (fgets (buf, BUFSIZ, fp)) {
  58.         if (buf[0] == '#')
  59.             continue;
  60.  
  61.         if (cp = strchr (buf, '\n'))
  62.             *cp = '\0';
  63.  
  64. #if defined(SUN) || defined(BSD) || defined(SUN4)
  65.         if ((port = strtok (buf, "\t"))
  66.                 && (type = strtok ((char *) 0, "\t"))
  67.                 && (type = strtok ((char *) 0, "\t"))) {
  68.             if (strcmp (line, port) == 0)
  69.                 break;
  70.         }    
  71. #else    /* USG */
  72.         if ((type = strtok (buf, " \t"))
  73.                 && (port = strtok ((char *) 0, " \t"))) {
  74.             if (strcmp (line, port) == 0)
  75.                 break;
  76.         }
  77. #endif
  78.     }
  79.     if (! feof (fp) && ! ferror (fp)) {
  80.         strcat (strcpy (termvar, "TERM="), type);
  81.         addenv (termvar);
  82.     }
  83.     fclose (fp);
  84. }
  85.