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

  1. #include <sys/types.h>
  2. #include <sys/file.h>
  3. #include <utmp.h>
  4. #include "fmt.h"
  5. #include "config/utmpfile.h"
  6. #include "config/wtmpfile.h"
  7. #include "config/sysv.h" /*XXX*/
  8. #include "ptymisc.h"
  9. #include "ptylogs.h"
  10. extern int flagxutmp; /*XXX*/
  11. extern int flagxwtmp; /*XXX*/
  12.  
  13. /* utmp and wtmp make about as much sense as /etc/passwd: not much. */
  14.  
  15. int utmp_on(ext,name,host,date)
  16. char *ext;
  17. char *name;
  18. char *host;
  19. long date;
  20. {
  21.  struct utmp ut;
  22.  struct utmp xt;
  23.  int fd;
  24.  int i;
  25.  char *t;
  26.  
  27.  if (!flagxutmp)
  28.    return 0;
  29.  
  30.  /* XXXX: This uses sequential allocation. See utmpinit. */
  31.  
  32.  t = ut.ut_line;
  33.  t += fmt_strncpy(t,"tty",0);
  34.  *t++ = ext[0];
  35.  *t++ = ext[1];
  36.  *t = 0;
  37.  strncpy(ut.ut_name,name,sizeof(ut.ut_name));
  38. #ifndef SYSV
  39.  strncpy(ut.ut_host,host,sizeof(ut.ut_host));
  40. #endif
  41.  ut.ut_time = date;
  42.  
  43.  if ((fd = open(UTMP_FILE,O_RDWR)) == -1)
  44.    return -1;
  45.  
  46.  i = 0;
  47.  while (bread(fd,(char *) &xt,sizeof(xt)) == sizeof(xt)) /* XXX: should buffer */
  48.   {
  49.    if (!strncmp(xt.ut_line,ut.ut_line,sizeof(ut.ut_line)))
  50.     {
  51.      if (lseek(fd,i * (long) sizeof(xt),L_SET) == -1)
  52.       {
  53.        close(fd);
  54.        return -1;
  55.       }
  56.      i = -1;
  57.      break;
  58.     }
  59.    ++i;
  60.   }
  61.  if (i != -1)
  62.   {
  63.    /* We have to reopen to avoid a race with other end-of-utmp entries. */
  64.    close(fd);
  65.    if ((fd = open(UTMP_FILE,O_RDWR | O_APPEND)) == -1)
  66.      return -1;
  67.   }
  68.  
  69.  if (bwrite(fd,(char *) &ut,sizeof(ut)) < sizeof(ut))
  70.   {
  71.    close(fd);
  72.    return -1;
  73.   }
  74.  close(fd);
  75.  return 0;
  76. }
  77.  
  78. int utmp_off(ext,host,date)
  79. char *ext;
  80. char *host;
  81. long date;
  82. {
  83.  utmp_on(ext,"",host,date);
  84. }
  85.  
  86. int wtmp_on(ext,name,host,date)
  87. char *ext;
  88. char *name;
  89. char *host;
  90. long date;
  91. {
  92.  struct utmp wt;
  93.  int fd;
  94.  char *t;
  95.  
  96.  if (!flagxwtmp)
  97.    return 0;
  98.  
  99.  t = wt.ut_line;
  100.  t += fmt_strncpy(t,"tty",0);
  101.  *t++ = ext[0];
  102.  *t++ = ext[1];
  103.  *t = 0;
  104.  strncpy(wt.ut_name,name,sizeof(wt.ut_name));
  105. #ifndef SYSV
  106.  strncpy(wt.ut_host,host,sizeof(wt.ut_host));
  107. #endif
  108.  wt.ut_time = date;
  109.  
  110.  if ((fd = open(WTMP_FILE,O_WRONLY | O_APPEND)) == -1)
  111.    return -1;
  112.  if (bwrite(fd,(char *) &wt,sizeof(wt)) < sizeof(wt))
  113.   {
  114.    close(fd);
  115.    return -1;
  116.   }
  117.  close(fd);
  118.  return 0;
  119. }
  120.  
  121. int wtmp_off(ext,host,date)
  122. char *ext;
  123. char *host;
  124. long date;
  125. {
  126.  wtmp_on(ext,"",host,date);
  127. }
  128.