aboutsummaryrefslogtreecommitdiff
path: root/riemann.fmi.uni-sofia.bg/programs/nlogin-0.3-pre/lastlog_do.c
blob: f840ea1948baf12c86ee8c4c37abf0ab62dc8763 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <unistd.h>
#include <fcntl.h>
#include <utmp.h>
#include <time.h>
#include "time_defs.h"
#include "utmp_defs.h"
#include "prot_defs.h"

#ifndef LASTLOG_FILE
#define LASTLOG_FILE "/var/log/lastlog"
#endif

#define SC(A,B) str_copynz(A, B, sizeof(A))

void lastlog_do(char *buf, unsigned int uid, char *line) /*EXTRACT_INCL*/{
  int fd = open(LASTLOG_FILE, O_RDWR);
  struct lastlog ll;
  off_t	offset = uid * sizeof ll;
  char *p=buf;

  p[0] = 0;
  if (fd<0 || lseek(fd, offset, SEEK_SET) != offset) goto do_close;
  if (read(fd, &ll, sizeof ll) == sizeof ll && ll.ll_time) {
    p += str_copy(p, "Last login: ");
    p += nv_ctime(p, ll.ll_time, 24);
    p += str_copy(p, " on ");
    p += str_add(p, ll.ll_line, sizeof ll.ll_line);
    if (ll.ll_host[0]) {
      p += str_copy(p, " from ");
      p += str_add(p, ll.ll_host, 40);
    }
    *p++ =  '\n';
    *p = 0;
  }

  if (lseek(fd, offset, SEEK_SET) != offset) goto do_close;
  byte_zero(&ll, sizeof ll);
  ll.ll_time=time(0);
  SC(ll.ll_line, line);
  SAFE_IO(write, fd, &ll, sizeof ll);
 do_close:
  close(fd);
}