home *** CD-ROM | disk | FTP | other *** search
/ Phoenix CD 2.0 / Phoenix_CD.cdr / 02a / pctj1186.zip / ENABLE.C < prev    next >
Text File  |  1986-07-28  |  2KB  |  88 lines

  1. /*
  2.  *  enable.c
  3.  *  copyright 1986 Maple Lawn Farm, Inc.
  4.  * 
  5.  *  drops/adds terminals from/to multi-user status
  6.  *  usage: "enable ttyxx"  or  "disable ttyxx"
  7.  *
  8.  *  compile:  cc -O -s -o enable enable.c
  9.  *            ln enable disable
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <signal.h>
  14.  
  15. #define    LEN    40
  16. #define INIT    1            /* init is pid 1 */
  17. #define TTYS    "/etc/ttys"
  18. #define TMP    "/etc/ttys.tmpXXXXXX"
  19.  
  20. char    *mktemp();
  21.  
  22. main(argc, argv)
  23. int  argc;
  24. char **argv;
  25. {
  26.     char    **p, buf[LEN], *last, 
  27.         *tmp = mktemp(TMP),
  28.         set = (**argv=='d') ? '0': '1';
  29.     int    i, change = 0;
  30.     static    int    (*sigs[NSIG-1])();
  31.  
  32.     if (!freopen(TTYS, "r", stdin))
  33.         perror(TTYS), exit(1);
  34.     if (!freopen(tmp, "w", stdout))
  35.         perror(tmp), exit(1);
  36.  
  37.     while (fgets(buf, sizeof buf, stdin)) {
  38.                 /* use last char as tty id */
  39.         last = buf + strlen(buf) - 1;
  40.         *last = '\0';
  41.                 /* check argv for match */
  42.         for (p = argv+1; *p; ++p)
  43.             if (**p && !strcmp(last-strlen(*p), *p))
  44.                 break;
  45.                 /* check status in file */
  46.         if (*p && buf[0] != set) {
  47.             ++change;
  48.             buf[0] = set;
  49.         }
  50.         puts(buf);
  51.     }
  52.  
  53.     if (change) {
  54.                 /* trap all interrupts */
  55.         for (i=1; i<NSIG; ++i)
  56.             if (i != SIGKILL)
  57.                 sigs[i-1] = signal(i, SIG_IGN);
  58.         fflush(stdout);
  59.                 /* you're committed now */
  60.         rename(TTYS, tmp);
  61.                 /* make init reread TTYS */
  62.         if (kill(INIT, SIGINT) == -1)
  63.             perror("init");
  64.                 /* let life go on */
  65.         fputs("Please do not enable/disable for one minute.\n", 
  66.             stderr);
  67.         for (i=1; i<NSIG; ++i)
  68.             if (i != SIGKILL)
  69.                 signal(i, sigs[i-1]);
  70.         exit(1);
  71.     }
  72.     else {
  73.         fputs("no change to /etc/ttys\n", stderr);
  74.         unlink(tmp);
  75.     }
  76. }
  77.  
  78.  
  79. rename(new,old)
  80. char  *new, *old;
  81. {
  82.     unlink(new);
  83.     if (link(old, new) == -1)
  84.         perror(new), exit(1);
  85.     if (unlink(old) == -1)
  86.         perror(old), exit(1);
  87. }
  88.