home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / pty4 / part03 / sesslist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-19  |  3.6 KB  |  174 lines

  1. /* XXX: this sort of depends on the dirent interface */
  2. #undef POSIX /* JIC XXX */
  3. #include <sys/types.h>
  4. #include <sys/file.h>
  5. #include <sys/dir.h>
  6. #include <stdio.h>
  7. #include "config/ptydir.h"
  8. #include "config/ptylongname.h"
  9. #include "fmt.h"
  10. #include "scan.h"
  11. #include "getopt.h"
  12. #include "ptycomm.h"
  13.  
  14. main(argc,argv)
  15. int argc;
  16. char *argv[];
  17. {
  18.  int uid;
  19.  DIR *dirp;
  20.  struct direct *dp;
  21.  int opt;
  22.  char sep;
  23.  int flagall;
  24.  
  25.  uid = getuid();
  26.  sep = '\n';
  27.  flagall = 0;
  28.  
  29.  while ((opt = getopt(argc,argv,"0a")) != opteof)
  30.    switch(opt)
  31.     {
  32.      case 'a':
  33.        flagall = 1;
  34.        break;
  35.      case '0':
  36.        sep = 0;
  37.        break;
  38.      case '?':
  39.      default:
  40.        exit(1);
  41.     }
  42.  argc -= optind; argv += optind;
  43.  
  44.  if (chdir(PTYDIR) == -1)
  45.   {
  46.    fprintf(stderr,"%s: fatal: cannot change to session directory %s\n",optprogname,PTYDIR);
  47.    exit(2);
  48.   }
  49.  
  50.  dirp = opendir(".");
  51.  while (dp = readdir(dirp))
  52.   {
  53.    unsigned int duid;
  54.    char dext[2];
  55.    char *t;
  56.    unsigned int len;
  57.  
  58.    t = dp->d_name;
  59.    len = scan_strncmp(t,"comm.",5);
  60.    if (len < 5)
  61.      ; /*XXX*/
  62.    t += len;
  63.    len = scan_uint(t,&duid);
  64.    if (len < 1)
  65.      ; /*XXX*/
  66.    t += len;
  67.    len = scan_strncmp(t,".",1);
  68.    if (len < 1)
  69.      ; /*XXX*/
  70.    t += len;
  71.    dext[0] = *t;
  72.    if (dext[0])
  73.      dext[1] = *++t;
  74.    else
  75.      dext[1] = 0;
  76.  
  77.    if ((duid == uid) || (flagall && !uid)) /*XXXX*/
  78.     {
  79.      int fdcomm;
  80.      int mslavepid;
  81.      int mpid;
  82.      char mrecoext[2];
  83.      char resp6[6];
  84.      int mflagsession;
  85.      char mlongname[PTYLONGNAMELEN];
  86.      static char outbuf[PTYLONGNAMELEN + 200];
  87.      char mext[2];
  88.      int mconn;
  89.      char *t;
  90.  
  91. #define DO6 read(fdcomm,resp6,6);
  92. #define BUMMER { close(fdcomm); continue; }
  93.  
  94.      fdcomm = comm_write(dext,uid);
  95.      if (fdcomm == -1)
  96.        continue;
  97.      if (write(fdcomm,"a",1) < 1)
  98.        BUMMER
  99.      DO6
  100.      if (write(fdcomm,"e",1) < 1)
  101.        BUMMER
  102.      if (read(fdcomm,mext,2) < 2)
  103.        BUMMER
  104.      if (write(fdcomm,"l",1) < 1)
  105.        BUMMER
  106.      DO6
  107.      if (!respeq(resp6,"longnm"))
  108.        BUMMER
  109.      if (read(fdcomm,mlongname,sizeof(mlongname)) < sizeof(mlongname))
  110.        BUMMER
  111.      if (write(fdcomm,"C",1) < 1)
  112.        BUMMER
  113.      DO6
  114.      mconn = respeq(resp6,"owuno?");
  115.      if (write(fdcomm,"p",1) < 1)
  116.        BUMMER
  117.      if (read(fdcomm,&mpid,sizeof(mpid)) < sizeof(mpid))
  118.        BUMMER
  119.      if (write(fdcomm,"P",1) < 1)
  120.        BUMMER
  121.      if (read(fdcomm,&mslavepid,sizeof(mslavepid)) < sizeof(mslavepid))
  122.        BUMMER
  123.      if (write(fdcomm,"D",1) < 1)
  124.        BUMMER
  125.      if (read(fdcomm,&mflagsession,sizeof(mflagsession)) < sizeof(mflagsession))
  126.        BUMMER
  127.      if (write(fdcomm,"S",1) < 1)
  128.        BUMMER
  129.      DO6
  130.      if (!respeq(resp6,"latest"))
  131.        BUMMER
  132.      if (read(fdcomm,mrecoext,2) < 2)
  133.        BUMMER
  134.      close(fdcomm);
  135.  
  136.      if ((mext[0] != dext[0]) || (mext[1] != dext[1]))
  137.        ; /* better to report what the master says */
  138.  
  139.      t = outbuf;
  140.      if (!mflagsession) t += fmt_strncpy(t,"non-",0);
  141.      t += fmt_strncpy(t,"session ",0);
  142.      *t++ = mext[0];
  143.      *t++ = mext[1];
  144.      t += fmt_strncpy(t," pid ",0);
  145.      t += fmt_uint(t,mpid);
  146.      t += fmt_strncpy(t," slave ",0);
  147.      t += fmt_uint(t,mslavepid);
  148.      if (mflagsession)
  149.       {
  150.        *t++ = ' ';
  151.        if (!mconn) t += fmt_strncpy(t,"dis",0);
  152.        t += fmt_strncpy(t,"connected",0);
  153.       }
  154.      if (mrecoext[0])
  155.       {
  156.        t += fmt_strncpy(t," (will drop into ",0);
  157.        *t++ = mrecoext[0];
  158.        *t++ = mrecoext[1];
  159.        *t++ = ')';
  160.       }
  161.      if (mlongname[0])
  162.       {
  163.        *t++ = ':';
  164.        *t++ = ' ';
  165.        t += fmt_strncpy(t,mlongname,0);
  166.       }
  167.      *t++ = sep;
  168.  
  169.      fwrite(outbuf,1,t - outbuf,stdout);
  170.     }
  171.   }
  172.  exit(0);
  173. }
  174.