home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / demo1.zoo / demo / tests / half.c next >
Encoding:
C/C++ Source or Header  |  1989-01-24  |  2.6 KB  |  118 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: half.c,v 4.1 88/06/21 14:02:04 bianchi Exp $
  9.     $Source: /tmp/mgrsrc/demo/tests/RCS/half.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /tmp/mgrsrc/demo/tests/RCS/half.c,v $$Revision: 4.1 $";
  12.  
  13. /* Start a new window with a command running in it */
  14.  
  15. #include <stdio.h>
  16. #include <sgtty.h>
  17.  
  18. #define WIDE    400
  19. #define HIGH    300
  20. #define X    200
  21. #define Y    200
  22.  
  23. main(argc,argv)
  24. int argc;
  25. char **argv;
  26.    {
  27.    register int i;
  28.    int group;        /* process group for new shell */
  29.    int pid;        /* pid of new shell */
  30.    char name[50];    /* name of tty to open */
  31.    struct sgttyb modes;    /* for tty modes */
  32.  
  33.    /* check arguments */
  34.  
  35.    if (argc < 2) {
  36.       fprintf(stderr,"usage: %s <command> \n",*argv);
  37.       exit(1); 
  38.       }
  39.  
  40.    /* get existing file modes, turn off echo */
  41.  
  42.    ioctl(0,TIOCGETP,&modes);
  43.    modes.sg_flags &= ~ECHO;
  44.    ioctl(0,TIOCSETP,&modes);
  45.       
  46.    /* make the window */
  47.  
  48.    printf("\033%d,%d,%d,%d%c",X,Y,WIDE,HIGH,'z');
  49.    fflush(stdout);
  50.  
  51.    /* get tty name to open */
  52.  
  53.    gets(name);
  54.  
  55.    /* restore modes */
  56.  
  57.    modes.sg_flags |= ECHO;
  58.    ioctl(0,TIOCSETP,&modes);
  59.  
  60.    if (strlen(name) < 4) {
  61.       printf("%s: Sorry, couldn't create window\n",*argv);
  62.       exit(1);
  63.       }
  64.  
  65.    /* make sure we can open tty */
  66.  
  67.    if (open(name,2) < 0) {
  68.       printf("Serious error, can't open %s\n",name);
  69.       exit(1);
  70.       }
  71.  
  72.    /* start new process */
  73.  
  74.    if ((pid=fork()) > 0) {    /* parent */
  75.       printf("starting %s as %d on %s\n",argv[1],pid,name);
  76.       exit(0);
  77.       }
  78.  
  79.    else if (pid == 0) {        /* new shell (child) */
  80.  
  81.       /* remove controlling tty */
  82.  
  83.       ioctl(open("/dev/tty",0),TIOCNOTTY,0);
  84.  
  85.       /* close all files */
  86.  
  87.       for(i=0;i<getdtablesize();i++)
  88.          close(i);
  89.  
  90.       /* open new ones */
  91.  
  92.       open(name,0);    /* stdin */
  93.       open(name,1);    /* stdout */
  94.       open(name,1);    /* stderr */
  95.    
  96.       /* get our own process group */
  97.       
  98.       group = getpid();
  99.       setpgrp(group,group);
  100.       ioctl(0,TIOCSPGRP,&group);      
  101.  
  102.       /* fix the tty modes */
  103.  
  104.       modes.sg_flags = ECHO|CRMOD|EVENP|ODDP;
  105.       ioctl(0,TIOCSETP,&modes);
  106.  
  107.       /* start the command */
  108.  
  109.       execvp(argv[1],argv+1);
  110.       printf("%s: Can't find %s\n",argv[0],argv[1]);
  111.       sleep(2);
  112.       exit(1);
  113.       }
  114.    else {
  115.       printf("Can't fork!!\n");
  116.       }
  117.    }
  118.