home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1621 / logs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  1.8 KB  |  100 lines

  1. /* Copyright 1990, Daniel J. Bernstein. All rights reserved. */
  2.  
  3. #include "config.h"
  4. #include "logs.h"
  5. #include "file.h"
  6. #include <utmp.h>
  7. #include <strings.h>
  8.  
  9. /* utmp and wtmp are about as sensible as /etc/passwd. */
  10.  
  11. /* We never bother to shrink /etc/utmp. */
  12.  
  13. extern long time();
  14.  
  15. long now()
  16. {
  17.  return time((long *) 0);
  18. }
  19.  
  20. int utmp(line,name,host,date)
  21. char *line;
  22. char *name;
  23. char *host;
  24. long date;
  25. {
  26.  struct utmp ut;
  27.  struct utmp xt;
  28.  int fd;
  29.  int i;
  30.  int j;
  31.  
  32.  /* only use of strncpy, ever */
  33.  (void) strncpy(ut.ut_line,line,sizeof(ut.ut_line));
  34.  (void) strncpy(ut.ut_name,name,sizeof(ut.ut_name));
  35.  (void) strncpy(ut.ut_host,host,sizeof(ut.ut_host));
  36.  ut.ut_time = date;
  37.  
  38.  fd = open(PTYUTMP_FILE,O_RDWR);
  39.  if (fd == -1)
  40.    return -1;
  41.  j = i = 0;
  42.  while (read(fd,(char *) &xt,sizeof(xt)) == sizeof(xt))
  43.   {
  44.    i++;
  45.    if (strncmp(xt.ut_line,ut.ut_line,sizeof(ut.ut_line)) == 0)
  46.     {
  47.      j = i;
  48.      break;
  49.     }
  50.   }
  51.  if (j)
  52.   {
  53.    if (lseek(fd,(long) ((j - 1) * sizeof(xt)),L_SET) == -1)
  54.     {
  55.      (void) close(fd);
  56.      return -1;
  57.     }
  58.   }
  59.  else
  60.   {
  61.    /* We have to reopen to avoid a race with other end-of-utmp entries. */
  62.    (void) close(fd);
  63.    if ((fd = open(PTYUTMP_FILE,O_RDWR | O_APPEND)) == -1)
  64.      return -1;
  65.   }
  66.  if (write(fd,(char *) &ut,sizeof(ut)) < sizeof(ut))
  67.   {
  68.    (void) close(fd);
  69.    return -1;
  70.   }
  71.  (void) close(fd);
  72.  return 0;
  73. }
  74.  
  75. int wtmp(line,name,host,date)
  76. char *line;
  77. char *name;
  78. char *host;
  79. long date;
  80. {
  81.  struct utmp wt;
  82.  int fd;
  83.  
  84.  (void) strncpy(wt.ut_line,line,sizeof(wt.ut_line));
  85.  (void) strncpy(wt.ut_name,name,sizeof(wt.ut_name));
  86.  (void) strncpy(wt.ut_host,host,sizeof(wt.ut_host));
  87.  wt.ut_time = date;
  88.  
  89.  fd = open(PTYWTMP_FILE,O_WRONLY | O_APPEND);
  90.  if (fd == -1)
  91.    return -1;
  92.  if (write(fd,(char *) &wt,sizeof(wt)) < sizeof(wt))
  93.   {
  94.    (void) close(fd);
  95.    return -1;
  96.   }
  97.  (void) close(fd);
  98.  return 0;
  99. }
  100.