home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / pty4 / part02 / ttydetach.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-18  |  1.1 KB  |  54 lines

  1. #include <stdio.h>
  2. #include <sys/file.h>
  3. #include <sys/ioctl.h>
  4. #include <errno.h>
  5. extern int errno;
  6. #include "env.h"
  7. #include "config/posix.h"
  8.  
  9. main(argc,argv)
  10. int argc;
  11. char *argv[];
  12. {
  13.  int fd;
  14.  int dummy;
  15.  if (argc < 2)
  16.   {
  17.    fputs("Usage: ttydetach program [ arg ... ]\n",stderr);
  18.    exit(1);
  19.   }
  20.  if (env_unset("PTY") == -1)
  21.   {
  22.    fputs("ttydetach: fatal: out of memory\n",stderr);
  23.    exit(2);
  24.   }
  25.  fd = open("/dev/tty",O_RDWR);
  26.  if (fd == -1)
  27.   {
  28.    if (errno == EBUSY) /* damn! */
  29.     {
  30.      if ((ioctl(0,TIOCNOTTY,0) == -1)
  31.        &&(ioctl(1,TIOCNOTTY,0) == -1)
  32.        &&(ioctl(2,TIOCNOTTY,0) == -1)
  33.        &&(ioctl(3,TIOCNOTTY,0) == -1)
  34.        )
  35.      fputs("ttydetach: warning: unable to detach from tty: exclusive-use set\n",stderr);
  36.        /* but it's not as if we didn't try... */
  37.     }
  38.   }
  39.  else
  40.   {
  41.    if (ioctl(fd,TIOCNOTTY,0) == -1)
  42.      perror("ttydetach: warning: unable to detach from tty");
  43.    close(fd);
  44.   }
  45.  if (ioctl(3,TIOCGPGRP,&dummy) == -1)
  46.    close(3);
  47. #ifdef POSIX_SILLINESS /* XXX: sigh... */
  48.  setsid();
  49. #endif
  50.  execvp(argv[1],argv + 1);
  51.  perror("ttydetach: fatal: cannot execute");
  52.  exit(4);
  53. }
  54.