summaryrefslogtreecommitdiffstats
path: root/src/util/pty/dump-utmp.c
blob: b349b9afe614481bdd90381f95b6c8e6c390e612 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdio.h>
#include <sys/file.h>
#include <fcntl.h>

#ifdef UTMPX
#include <utmpx.h>
#endif
#include <utmp.h>

char *ut_typename (t) {
  switch (t) {
#define S(N) case N : return #N
  S(EMPTY);
  S(RUN_LVL);
  S(BOOT_TIME);
  S(OLD_TIME);
  S(NEW_TIME);
  S(INIT_PROCESS);
  S(LOGIN_PROCESS);
  S(USER_PROCESS);
  S(DEAD_PROCESS);
  S(ACCOUNTING);
  default: return "??";
  }
}

int main (argc, argv) int argc; char *argv[]; {
  int f;
  char id[5], user[50];
  char *file = 0;
  int all = 0;

  while (*++argv)
    {
      char *arg = *argv;
      if (!arg)
	break;
      if (!strcmp ("-a", arg))
	all = 1;
      else if (file)
	{
	  fprintf (stderr, "already got a file\n");
	  return 1;
	}
      else
	file = arg;
    }
  f = open (file, O_RDONLY);
  if (f < 0) {
    perror (file);
    exit (1);
  }
  id[4] = 0;
#ifdef UTMPX
  if ('x' != file[strlen(file) - 1]) {
    struct utmpx u;
    while (read (f, &u, sizeof (u)) == sizeof (u)) {
      char c;
      if ((u.ut_type == DEAD_PROCESS
	   || u.ut_type == EMPTY)
	  && !all)
	continue;
      strncpy (id, u.ut_id, 4);
      printf ("%-8s:%-12s:%-4s:%6d %s",
	      u.ut_user, u.ut_line, id,
	      u.ut_pid, ut_typename (u.ut_type));
	      /* ctime (&u.ut_xtime) + 4 */
      if (u.ut_syslen && u.ut_host[0])
	printf (" %s", u.ut_host);
      printf ("\n");
      return 0;
    }
  }
  /* else */
#endif
  {
    struct utmp u;
    user[sizeof(u.ut_user)] = 0;
    while (read (f, &u, sizeof (u)) == sizeof (u)) {
      char c;
      if ((u.ut_type == DEAD_PROCESS
	   || u.ut_type == EMPTY)
	  && !all)
	continue;
      strncpy (id, u.ut_id, 4);
      strncpy (user, u.ut_user, sizeof (u.ut_user));
      printf ("%-8s:%-12s:%-4s:%6d %s", user, u.ut_line, id,
	      u.ut_pid, ut_typename (u.ut_type));
      printf ("\n");
#if 0
      printf ("user: %-32s  id: %s\n", user, id);
      printf ("    line: %-32s  pid:%-6d  type: %s\n",
	      u.ut_line, u.ut_pid, ut_typename (u.ut_type));
      printf ("    exit_status: %d,%d\n",
	      u.ut_exit.e_termination, u.ut_exit.e_exit);
      printf ("    time: %s\n", ctime (&u.ut_time) + 4);
#endif
    }
  }

  return 0;
}