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

  1. #include <stdio.h>
  2. #include "fmt.h"
  3. #include "getopt.h"
  4. #include "sessconnlog.h"
  5. #include "config/sessconnfile.h"
  6.  
  7. main(argc,argv)
  8. int argc;
  9. char *argv[];
  10. {
  11.  FILE *fisf;
  12.  struct sessconnlog sl;
  13.  int opt;
  14.  char *file;
  15.  int flaglogouts;
  16.  int flagreverse;
  17.  int revnum;
  18.  
  19.  file = SESSCONNNOW_FILE;
  20.  flaglogouts = 0;
  21.  flagreverse = 0;
  22.  
  23.  while ((opt = getopt(argc,argv,"rRlLf:")) != opteof)
  24.    switch(opt)
  25.     {
  26.      case 'R':
  27.        flagreverse = 1;
  28.        break;
  29.      case 'r':
  30.        flagreverse = 1;
  31.        break;
  32.      case 'l':
  33.        flaglogouts = 1;
  34.        break;
  35.      case 'L':
  36.        flaglogouts = 0;
  37.        break;
  38.      case 'f':
  39.        file = optarg;
  40.        break;
  41.      case '?':
  42.      default:
  43.        exit(1);
  44.     }
  45.  
  46.  fisf = fopen(file,"r");
  47.  if (!fisf)
  48.   {
  49.    perror("sesswhere: fatal: cannot open current session-connection file");
  50.    exit(2);
  51.   }
  52.  if (flagreverse)
  53.   {
  54.    fseek(fisf,0,2);
  55.    revnum = ftell(fisf) / sizeof(sl);
  56.    fseek(fisf,sizeof(sl) * --revnum,0);
  57.   }
  58.  
  59.  while (fread(&sl,sizeof(sl),1,fisf) == 1)
  60.   {
  61.    static char outbuf[SESSCONNLOG_REMOTELEN + 100];
  62.    if (sl.ext[0] && (sl.siglerpid || flaglogouts))
  63.     {
  64.      char *t; t = outbuf;
  65.      *t++ = sl.ext[0]; *t++ = sl.ext[1];
  66.      t += fmt_strncpy(t,"  ",0);
  67.      t += fmt_strncpy(t,asctime(localtime(&sl.date)) + 4,12);
  68.      switch(sl.siglerpid)
  69.       {
  70.        case -1:
  71.      t += fmt_strncpy(t,"  connect ",0);
  72.      t += fmt_vis(t,sl.remote,strlen(sl.remote));
  73.      break;
  74.        case 1:
  75.      t += fmt_strncpy(t,"  disconnect",0);
  76.      break;
  77.        case 0:
  78.      t += fmt_strncpy(t,"  cleanup",0);
  79.      break;
  80.       }
  81.      *t++ = '\n';
  82.      *t = 0;
  83.      fwrite(outbuf,1,t - outbuf,stdout);
  84.     }
  85.    if (flagreverse)
  86.     {
  87.      if (fseek(fisf,-2 * sizeof(sl),1) == -1) /*XXX*/
  88.        break;
  89.      --revnum; /*XXX: why do we care? */
  90.     }
  91.   }
  92.  exit(0);
  93. }
  94.