home *** CD-ROM | disk | FTP | other *** search
/ ftp.muug.mb.ca / 2014.06.ftp.muug.mb.ca.tar / ftp.muug.mb.ca / pub / src / gopher / gopher1.01 / misc / go500 / detach.c next >
C/C++ Source or Header  |  1992-04-09  |  1KB  |  67 lines

  1. /*
  2.  * Copyright (c) 1990 Regents of the University of Michigan.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that this notice is preserved and that due credit is given
  7.  * to the University of Michigan at Ann Arbor. The name of the University
  8.  * may not be used to endorse or promote products derived from this
  9.  * software without specific prior written permission. This software
  10.  * is provided ``as is'' without express or implied warranty.
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <sys/types.h>
  15. #include <sys/file.h>
  16. #include <sys/ioctl.h>
  17. #include <signal.h>
  18.  
  19. detach( debug )
  20. int    debug;
  21. {
  22.     int    i, sd, nbits;
  23.  
  24.     nbits = getdtablesize();
  25.  
  26.     if ( debug == 0 || !(isatty( 1 )) ) {
  27.         for ( i = 0; i < 5; i++ ) {
  28.             switch ( fork() ) {
  29.             case -1:
  30.                 sleep( 5 );
  31.                 continue;
  32.  
  33.             case 0:
  34.                 break;
  35.  
  36.             default:
  37.                 _exit( 0 );
  38.             }
  39.             break;
  40.         }
  41.  
  42.         for ( i = 3; i < nbits; i++ )
  43.             close( i );
  44.  
  45.         (void) chdir( "/" );
  46.  
  47.         if ( (sd = open( "/dev/null", O_RDWR )) == -1 ) {
  48.             if ( debug ) perror( "/dev/null" );
  49.             exit( 1 );
  50.         }
  51.         if ( isatty( 0 ) )
  52.             (void) dup2( sd, 0 );
  53.         if ( isatty( 1 ) )
  54.             (void) dup2( sd, 1 );
  55.         if ( isatty(2) )
  56.             (void) dup2( sd, 2 );
  57.         close( sd );
  58.  
  59.         if ( (sd = open( "/dev/tty", O_RDWR )) != -1 ) {
  60.             (void) ioctl( sd, TIOCNOTTY, NULL );
  61.             (void) close( sd );
  62.         }
  63.     } 
  64.  
  65.     (void) signal( SIGPIPE, SIG_IGN );
  66. }
  67.