home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / extensions / test / InsPEX / newpgrp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-15  |  2.7 KB  |  96 lines

  1. /* $XConsortium: newpgrp.c,v 5.1 91/02/16 10:04:51 rws Exp $ */
  2.  
  3. /*****************************************************************
  4. Copyright (c) 1989,1990, 1991 by Sun Microsystems, Inc. and the X Consortium.
  5.  
  6.                         All Rights Reserved
  7.  
  8. Permission to use, copy, modify, and distribute this software and its 
  9. documentation for any purpose and without fee is hereby granted, 
  10. provided that the above copyright notice appear in all copies and that
  11. both that copyright notice and this permission notice appear in 
  12. supporting documentation, and that the names of Sun Microsystems,
  13. the X Consortium, and MIT not be used in advertising or publicity 
  14. pertaining to distribution of the software without specific, written 
  15. prior permission.  
  16.  
  17. SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 
  18. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT 
  19. SHALL SUN MICROSYSTEMS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL 
  20. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  22. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  23. SOFTWARE.
  24.  
  25. ******************************************************************/
  26.  
  27. /*
  28.  * newprgp:  disassociate current process from process group and 
  29.  *         control terminal; execute the rest of the command line 
  30.  *          as a command
  31.  */
  32.  
  33. #include "signal.h"
  34.  
  35. #ifdef SIGTSTP /* BSD */
  36. #include "sys/file.h"
  37. #include "sys/ioctl.h"
  38. #endif
  39.  
  40. main(argc,argv)
  41.     int argc;
  42.     char **argv;
  43. {
  44.     int i;
  45.     char command[1024];
  46.     char *charptr;
  47.     int fd;
  48.  
  49.     /* ignore terminal stop signals (BSD) */
  50. #ifdef SIGTOU
  51.     signal(SIGTOU, SIG_IGN);
  52. #endif 
  53. #ifdef SIGTTIN
  54.     signal(SIGTTIN, SIG_IGN);
  55. #endif 
  56. #ifdef SIGTSTP
  57.     signal(SIGTSTP, SIG_IGN);
  58. #endif 
  59.  
  60.     /* to make sure the first child is not the process group leader */
  61.     if (fork() != 0)
  62.         exit(0);  /* parent */
  63.  
  64. #ifdef SIGTSTP /* BSD */
  65.     /* disassociate from process group */
  66.     setpgrp(0,getpid());
  67.  
  68.     /* disassociate from control terminal */
  69.     fd = open("/dev/tty",O_RDWR);
  70.         if (fd >=0) {
  71.                 ioctl(fd, TIOCNOTTY, (char*)0);
  72.                 close(fd);
  73.         }
  74.  
  75. #else            /* SYS 5 */
  76.     /* disassociate from process group and */
  77.      /* control terminal (as a side effect) */
  78.     setpgrp();
  79.     /* to immune from pgrp leader death */
  80.     signal(SIGHUP, SIG_IGN);
  81.     /* first child exit so that the living process will not be a */
  82.      /* process group leader */
  83.     if (fork() != 0)
  84.         exit(0);
  85. #endif
  86.  
  87.     /* assemble command line */
  88.     for (charptr=command, i=1;
  89.             i<argc;
  90.             ++i, charptr+=strlen(argv[i-1])+1)
  91.         sprintf(charptr," %s",argv[i]);
  92.  
  93.     /* execute command line */
  94.     system(command);
  95. }
  96.