annotate toys/pending/last.c @ 1183:0752b2d58909 draft

Rename xmsprintf() to just xmprintf(). Partly because there's no supplied target string ala sprintf, and partly because I can never remember what order the m and s go in.
author Rob Landley <rob@landley.net>
date Thu, 16 Jan 2014 09:26:50 -0600
parents 972d4fc0c8a2
children 0ecfaa7022e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1163
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* last.c - Show listing of last logged in users.
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * Copyright 2013 Ranjan Kumar <ranjankumar.bth@gmail.com>
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 * Copyright 2013 Kyungwan Han <asura321@gmail.com>
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 *
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 * No Standard.
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
7
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
8 USE_LAST(NEWTOY(last, "f:W", TOYFLAG_BIN))
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
9
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
10 config LAST
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 bool "last"
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 default n
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 help
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 Usage: last [-W] [-f FILE]
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
15
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
16 Show listing of last logged in users.
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
17
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
18 -W Display the information without host-column truncation.
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 -f FILE Read from file FILE instead of /var/log/wtmp.
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 */
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
21 #define FOR_last
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
22
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 #include "toys.h"
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
24 #include <utmp.h>
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
25
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 #ifndef SHUTDOWN_TIME
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
27 #define SHUTDOWN_TIME 254
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
28 #endif
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
29
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
30 GLOBALS(
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
31 char *file;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
32 struct arg_list *list;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 )
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
34
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 static void free_node(void *data)
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
37 void *arg = ((struct arg_list*)data)->arg;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
38
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
39 if (arg) free(arg);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 free(data);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 }
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
42
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 static void free_list()
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
44 {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
45 if (TT.list) {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
46 llist_traverse(TT.list, free_node);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
47 TT.list = NULL;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
48 }
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
49 }
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
50
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
51 static void llist_add_node(struct arg_list **old, void *data)
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
52 {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
53 struct arg_list *new = xmalloc(sizeof(struct arg_list));
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
54
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
55 new->arg = (char*)data;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
56 new->next = *old;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
57 *old = new;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
58 }
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
59
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
60 // Find a node and dlink it from the list.
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
61 static struct arg_list *find_and_dlink(struct arg_list **list, char *devname)
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
62 {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
63 struct arg_list *l = *list;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
64
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
65 while (*list) {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
66 struct utmp *ut = (struct utmp *)l->arg;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
67
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
68 if (!strncmp(ut->ut_line, devname, UT_LINESIZE)) {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
69 *list = (*list)->next;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
70 return l;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
71 }
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
72 list = &(*list)->next;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
73 l = *list;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
74 }
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
75 return NULL;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
76 }
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
77
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
78 // Compute login, logout and duration of login.
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
79 static void seize_duration(time_t tm0, time_t tm1)
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
80 {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
81 unsigned days, hours, mins;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
82 double diff = difftime(tm1, tm0);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
83
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
84 diff = (diff > 0) ? (tm1 - tm0) : 0;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
85 toybuf[0] = toybuf[18] = toybuf[28] = '\0';
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
86 strncpy(toybuf, ctime(&tm0), 16); // Login Time.
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
87 snprintf(toybuf+18, 8, "- %s", ctime(&tm1) + 11); // Logout Time.
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
88 days = (mins = diff/60)/(24*60);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
89 hours = (mins = (mins%(24*60)))/60;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
90 mins = mins%60;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
91 sprintf(toybuf+28, "(%u+%02u:%02u)", days, hours, mins); // Duration.
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
92 }
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
93
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
94 void last_main(void)
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
95 {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
96 struct utmp ut;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
97 struct stat sb;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
98 time_t tm[3] = {0,}; //array for time avlues, previous, current
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
99 char *file = "/var/log/wtmp";
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
100 int fd, pwidth, curlog_type = EMPTY;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
101 off_t loc;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
102
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
103 if (toys.optflags & FLAG_f) file = TT.file;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
104
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
105 TT.list = NULL;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
106 pwidth = (toys.optflags & FLAG_W) ? 46 : 16;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
107 time(&tm[1]);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
108 fd = xopen(file, O_RDONLY);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
109 loc = xlseek(fd, 0, SEEK_END);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
110 // in case of empty file or 'filesize < sizeof(ut)'
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
111 fstat(fd, &sb);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
112 if (sizeof(ut) > sb.st_size) {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
113 xclose(fd);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
114 printf("\n%s begins %-24.24s\n", basename(file), ctime(&sb.st_ctime));
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
115 return;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
116 }
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
117 loc = xlseek(fd, loc - sizeof(ut), SEEK_SET);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
118
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
119 while (1) {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
120 xreadall(fd, &ut, sizeof(ut));
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
121 tm[0] = (time_t)ut.ut_tv.tv_sec;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
122 if (ut.ut_line[0] == '~') {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
123 if (!strcmp(ut.ut_user, "runlevel")) ut.ut_type = RUN_LVL;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
124 else if (!strcmp(ut.ut_user, "reboot")) ut.ut_type = BOOT_TIME;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
125 else if (!strcmp(ut.ut_user, "shutdown")) ut.ut_type = SHUTDOWN_TIME;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
126 }
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
127 else if (ut.ut_user[0] == '\0') ut.ut_type = DEAD_PROCESS;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
128 else if (ut.ut_user[0] && ut.ut_line[0] && (ut.ut_type != DEAD_PROCESS)
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
129 && (strcmp(ut.ut_user, "LOGIN")) ) ut.ut_type = USER_PROCESS;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
130 /* The pair of terminal names '|' / '}' logs the
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
131 * old/new system time when date changes it.
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
132 */
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
133 if (!strcmp(ut.ut_user, "date")) {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
134 if (ut.ut_line[0] == '|') ut.ut_type = OLD_TIME;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
135 if (ut.ut_line[0] == '{') ut.ut_type = NEW_TIME;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
136 }
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
137 if ( (ut.ut_type == SHUTDOWN_TIME) || ((ut.ut_type == RUN_LVL) &&
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
138 (((ut.ut_pid & 255) == '0') || ((ut.ut_pid & 255) == '6')))) {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
139 tm[1] = tm[2] = (time_t)ut.ut_tv.tv_sec;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
140 free_list();
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
141 curlog_type = RUN_LVL;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
142 } else if (ut.ut_type == BOOT_TIME) {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
143 seize_duration(tm[0], tm[1]);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
144 strncpy(ut.ut_line, "system boot", sizeof("system boot"));
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
145 free_list();
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
146 printf("%-8.8s %-12.12s %-*.*s %-16.16s %-7.7s %s\n", ut.ut_user,
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
147 ut.ut_line, pwidth, pwidth, ut.ut_host,
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
148 toybuf, toybuf+18, toybuf+28);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
149 curlog_type = BOOT_TIME;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
150 tm[2] = (time_t)ut.ut_tv.tv_sec;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
151 } else if (ut.ut_type == USER_PROCESS && *ut.ut_line) {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
152 struct arg_list *l = find_and_dlink(&TT.list, ut.ut_line);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
153 if (l) {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
154 struct utmp *u = (struct utmp *)l->arg;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
155 seize_duration(tm[0], u->ut_tv.tv_sec);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
156 printf("%-8.8s %-12.12s %-*.*s %-16.16s %-7.7s %s\n", ut.ut_user,
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
157 ut.ut_line, pwidth, pwidth, ut.ut_host,
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
158 toybuf, toybuf+18, toybuf+28);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
159 free(l->arg);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
160 free(l);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
161 } else {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
162 int type = !tm[2] ? EMPTY : curlog_type;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
163 if (!tm[2]) { //check process's current status (alive or dead).
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
164 if ((ut.ut_pid > 0) && (kill(ut.ut_pid, 0)!=0) && (errno == ESRCH))
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
165 type = INIT_PROCESS;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
166 }
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
167 seize_duration(tm[0], tm[2]);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
168 switch (type) {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
169 case EMPTY:
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
170 strncpy(toybuf+18, " still", sizeof(" still"));
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
171 strncpy(toybuf+28, "logged in", sizeof("logged in"));
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
172 break;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
173 case RUN_LVL:
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
174 strncpy(toybuf+18, "- down ", sizeof("- down "));
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
175 break;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
176 case BOOT_TIME:
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
177 strncpy(toybuf+18, "- crash", sizeof("- crash"));
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
178 break;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
179 case INIT_PROCESS:
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
180 strncpy(toybuf+18, " gone", sizeof(" gone"));
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
181 strncpy(toybuf+28, "- no logout", sizeof("- no logout"));
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
182 break;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
183 default:
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
184 break;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
185 }
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
186 printf("%-8.8s %-12.12s %-*.*s %-16.16s %-7.7s %s\n", ut.ut_user,
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
187 ut.ut_line, pwidth, pwidth, ut.ut_host,
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
188 toybuf, toybuf+18, toybuf+28);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
189 }
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
190 llist_add_node(&TT.list, memcpy(xmalloc(sizeof(ut)), &ut, sizeof(ut)));
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
191 } else if (ut.ut_type == DEAD_PROCESS && *ut.ut_line)
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
192 llist_add_node(&TT.list, memcpy(xmalloc(sizeof(ut)), &ut, sizeof(ut)));
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
193
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
194 loc -= sizeof(ut);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
195 if(loc < 0) break;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
196 xlseek(fd, loc, SEEK_SET);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
197 } // End of while.
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
198
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
199 fflush(stdout);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
200 xclose(fd);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
201 if (CFG_TOYBOX_FREE) free_list();
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
202 printf("\n%s begins %-24.24s\n", basename(file), ctime(&tm[0]));
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
203 }