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

  1. #include <stdio.h>
  2. #include "ptymisc.h"
  3. #include "ptycomm.h"
  4. #include "config/ptydir.h"
  5. #include "getopt.h"
  6. #include "env.h"
  7.  
  8. char noreco[2] = { 0, 0 };
  9.  
  10. main(argc,argv)
  11. int argc;
  12. char *argv[];
  13. {
  14.  int opt;
  15.  int uid;
  16.  char *ext;
  17.  char *reco;
  18.  int fdcomm;
  19.  char resp6[6];
  20.  
  21.  uid = getuid();
  22.  ext = env_get("PTY");
  23.  while ((opt = getopt(argc,argv,"s:")) != opteof)
  24.    switch(opt)
  25.     {
  26.      case 's':
  27.        ext = optarg;
  28.        break;
  29.      case '?':
  30.      default:
  31.        exit(1);
  32.     }
  33.  argc -= optind;
  34.  argv += optind;
  35.  
  36.  if (!ext)
  37.   {
  38.    fprintf(stderr,"%s: fatal: no -s specified, and PTY not set; are we under a session?\n",optprogname);
  39.    exit(2);
  40.   }
  41.  
  42.  reco = *argv;
  43.  if (!reco)
  44.    reco = noreco;
  45.  
  46.  if (chdir(PTYDIR) == -1)
  47.   {
  48.    fprintf(stderr,"%s: fatal: cannot change to session directory %s\n",optprogname,PTYDIR);
  49.    exit(2);
  50.   }
  51.  
  52.  fdcomm = comm_write(ext,uid);
  53.  if (fdcomm == -1)
  54.   {
  55.    fprintf(stderr,"%s: fatal: cannot find session %s; if it exists, do you own it?\n",optprogname,ext);
  56.    exit(2);
  57.   }
  58.  if (bwrite(fdcomm,"s",1) < 1)
  59.   {
  60.    close(fdcomm);
  61.    fprintf(stderr,"%s: weird: session %s refuses to listen\n",optprogname,ext);
  62.    exit(2);
  63.   }
  64.  if (bwrite(fdcomm,reco,2) < 2)
  65.   {
  66.    close(fdcomm);
  67.    fprintf(stderr,"%s: weird: session %s refuses to listen\n",optprogname,ext);
  68.    exit(2);
  69.   }
  70.  if (bread(fdcomm,resp6,6) < 6)
  71.   {
  72.    close(fdcomm);
  73.    fprintf(stderr,"%s: weird: session %s refuses to respond\n",optprogname,ext);
  74.    exit(2);
  75.   }
  76.  close(fdcomm);
  77.  
  78.  if (respeq(resp6,"nosglr"))
  79.   {
  80.    fprintf(stderr,"%s: fatal: session %s not connected\n",optprogname,ext);
  81.    exit(2);
  82.   }
  83.  
  84.  if (!respeq(resp6,"sglrok"))
  85.    ; /* unrecognized reply code */
  86.  
  87.  fprintf(stderr,"%s: will connect to session %c%c when session %c%c is done\n"
  88.    ,optprogname,reco[0],reco[1],ext[0],ext[1]);
  89.  exit(0);
  90. }
  91.