annotate toys/pending/last.c @ 1776:7bf68329eb3b draft default tip

Repository switched to git at https://github.com/landley/toybox
author Rob Landley <rob@landley.net>
date Thu, 09 Apr 2015 02:28:32 -0500
parents b0a92be71368
children
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
1265
0ecfaa7022e8 usage: is lower case (the help generator looks for that, might as well be consistent).
Rob Landley <rob@landley.net>
parents: 1163
diff changeset
14 usage: last [-W] [-f FILE]
1163
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 */
1278
324306321d82 Initial cleanup of last: mostly whitespace, move no record test to start of loop, don't bother to stat an empty file to report when an empty log was created (just report current time).
Rob Landley <rob@landley.net>
parents: 1265
diff changeset
21
1163
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
22 #define FOR_last
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;
1278
324306321d82 Initial cleanup of last: mostly whitespace, move no record test to start of loop, don't bother to stat an empty file to report when an empty log was created (just report current time).
Rob Landley <rob@landley.net>
parents: 1265
diff changeset
32
1163
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 struct arg_list *list;
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
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 static void free_list()
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
37 {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
38 if (TT.list) {
1305
b0a92be71368 Ashwini Sharma pointed out I screwed up last.c.
Rob Landley <rob@landley.net>
parents: 1296
diff changeset
39 llist_traverse(TT.list, llist_free_arg);
1163
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 TT.list = NULL;
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
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
44 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
45 {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
46 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
47
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
48 new->arg = (char*)data;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
49 new->next = *old;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
50 *old = new;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
51 }
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 // 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
54 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
55 {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
56 struct arg_list *l = *list;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
57
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
58 while (*list) {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
59 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
60
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
61 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
62 *list = (*list)->next;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
63 return l;
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 list = &(*list)->next;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
66 l = *list;
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 return NULL;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
69 }
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
70
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
71 // 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
72 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
73 {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
74 unsigned days, hours, mins;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
75 double diff = difftime(tm1, tm0);
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 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
78 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
79 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
80 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
81 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
82 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
83 mins = mins%60;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
84 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
85 }
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
86
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
87 void last_main(void)
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
88 {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
89 struct utmp ut;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
90 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
91 char *file = "/var/log/wtmp";
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
92 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
93 off_t loc;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
94
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
95 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
96
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
97 pwidth = (toys.optflags & FLAG_W) ? 46 : 16;
1278
324306321d82 Initial cleanup of last: mostly whitespace, move no record test to start of loop, don't bother to stat an empty file to report when an empty log was created (just report current time).
Rob Landley <rob@landley.net>
parents: 1265
diff changeset
98 *tm = time(tm+1);
1163
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
99 fd = xopen(file, O_RDONLY);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
100 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
101
1278
324306321d82 Initial cleanup of last: mostly whitespace, move no record test to start of loop, don't bother to stat an empty file to report when an empty log was created (just report current time).
Rob Landley <rob@landley.net>
parents: 1265
diff changeset
102 // Loop through file structures in reverse order.
324306321d82 Initial cleanup of last: mostly whitespace, move no record test to start of loop, don't bother to stat an empty file to report when an empty log was created (just report current time).
Rob Landley <rob@landley.net>
parents: 1265
diff changeset
103 for (;;) {
324306321d82 Initial cleanup of last: mostly whitespace, move no record test to start of loop, don't bother to stat an empty file to report when an empty log was created (just report current time).
Rob Landley <rob@landley.net>
parents: 1265
diff changeset
104 loc -= sizeof(ut);
324306321d82 Initial cleanup of last: mostly whitespace, move no record test to start of loop, don't bother to stat an empty file to report when an empty log was created (just report current time).
Rob Landley <rob@landley.net>
parents: 1265
diff changeset
105 if(loc < 0) break;
324306321d82 Initial cleanup of last: mostly whitespace, move no record test to start of loop, don't bother to stat an empty file to report when an empty log was created (just report current time).
Rob Landley <rob@landley.net>
parents: 1265
diff changeset
106 xlseek(fd, loc, SEEK_SET);
324306321d82 Initial cleanup of last: mostly whitespace, move no record test to start of loop, don't bother to stat an empty file to report when an empty log was created (just report current time).
Rob Landley <rob@landley.net>
parents: 1265
diff changeset
107
1296
314b1486d502 Make last use common llist free function, minor cleanups.
Rob Landley <rob@landley.net>
parents: 1279
diff changeset
108 // Read next structure, determine type
1163
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
109 xreadall(fd, &ut, sizeof(ut));
1278
324306321d82 Initial cleanup of last: mostly whitespace, move no record test to start of loop, don't bother to stat an empty file to report when an empty log was created (just report current time).
Rob Landley <rob@landley.net>
parents: 1265
diff changeset
110 *tm = ut.ut_tv.tv_sec;
324306321d82 Initial cleanup of last: mostly whitespace, move no record test to start of loop, don't bother to stat an empty file to report when an empty log was created (just report current time).
Rob Landley <rob@landley.net>
parents: 1265
diff changeset
111 if (*ut.ut_line == '~') {
1163
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
112 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
113 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
114 else if (!strcmp(ut.ut_user, "shutdown")) ut.ut_type = SHUTDOWN_TIME;
1278
324306321d82 Initial cleanup of last: mostly whitespace, move no record test to start of loop, don't bother to stat an empty file to report when an empty log was created (just report current time).
Rob Landley <rob@landley.net>
parents: 1265
diff changeset
115 } else if (!*ut.ut_user) ut.ut_type = DEAD_PROCESS;
324306321d82 Initial cleanup of last: mostly whitespace, move no record test to start of loop, don't bother to stat an empty file to report when an empty log was created (just report current time).
Rob Landley <rob@landley.net>
parents: 1265
diff changeset
116 else if (*ut.ut_user && *ut.ut_line && ut.ut_type != DEAD_PROCESS
324306321d82 Initial cleanup of last: mostly whitespace, move no record test to start of loop, don't bother to stat an empty file to report when an empty log was created (just report current time).
Rob Landley <rob@landley.net>
parents: 1265
diff changeset
117 && strcmp(ut.ut_user, "LOGIN")) ut.ut_type = USER_PROCESS;
1163
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
118 /* 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
119 * 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
120 */
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
121 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
122 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
123 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
124 }
1278
324306321d82 Initial cleanup of last: mostly whitespace, move no record test to start of loop, don't bother to stat an empty file to report when an empty log was created (just report current time).
Rob Landley <rob@landley.net>
parents: 1265
diff changeset
125
324306321d82 Initial cleanup of last: mostly whitespace, move no record test to start of loop, don't bother to stat an empty file to report when an empty log was created (just report current time).
Rob Landley <rob@landley.net>
parents: 1265
diff changeset
126 if ((ut.ut_type == SHUTDOWN_TIME) || ((ut.ut_type == RUN_LVL) &&
324306321d82 Initial cleanup of last: mostly whitespace, move no record test to start of loop, don't bother to stat an empty file to report when an empty log was created (just report current time).
Rob Landley <rob@landley.net>
parents: 1265
diff changeset
127 (((ut.ut_pid & 255) == '0') || ((ut.ut_pid & 255) == '6'))))
324306321d82 Initial cleanup of last: mostly whitespace, move no record test to start of loop, don't bother to stat an empty file to report when an empty log was created (just report current time).
Rob Landley <rob@landley.net>
parents: 1265
diff changeset
128 {
1163
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
129 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
130 free_list();
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
131 curlog_type = RUN_LVL;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
132 } 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
133 seize_duration(tm[0], tm[1]);
1279
29603c5a3317 sizeof("string") treats it as a char array _including_ the null terminator, so strncmp(dest, "string", sizeof("string")) is just strcpy.
Rob Landley <rob@landley.net>
parents: 1278
diff changeset
134 strcpy(ut.ut_line, "system boot");
1163
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
135 free_list();
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
136 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
137 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
138 toybuf, toybuf+18, toybuf+28);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
139 curlog_type = BOOT_TIME;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
140 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
141 } 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
142 struct arg_list *l = find_and_dlink(&TT.list, ut.ut_line);
1278
324306321d82 Initial cleanup of last: mostly whitespace, move no record test to start of loop, don't bother to stat an empty file to report when an empty log was created (just report current time).
Rob Landley <rob@landley.net>
parents: 1265
diff changeset
143
1163
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
144 if (l) {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
145 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
146 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
147 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
148 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
149 toybuf, toybuf+18, toybuf+28);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
150 free(l->arg);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
151 free(l);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
152 } else {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
153 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
154 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
155 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
156 type = INIT_PROCESS;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
157 }
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
158 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
159 switch (type) {
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
160 case EMPTY:
1279
29603c5a3317 sizeof("string") treats it as a char array _including_ the null terminator, so strncmp(dest, "string", sizeof("string")) is just strcpy.
Rob Landley <rob@landley.net>
parents: 1278
diff changeset
161 strcpy(toybuf+18, " still");
29603c5a3317 sizeof("string") treats it as a char array _including_ the null terminator, so strncmp(dest, "string", sizeof("string")) is just strcpy.
Rob Landley <rob@landley.net>
parents: 1278
diff changeset
162 strcpy(toybuf+28, "logged in");
1163
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
163 break;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
164 case RUN_LVL:
1279
29603c5a3317 sizeof("string") treats it as a char array _including_ the null terminator, so strncmp(dest, "string", sizeof("string")) is just strcpy.
Rob Landley <rob@landley.net>
parents: 1278
diff changeset
165 strcpy(toybuf+18, "- down ");
1163
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
166 break;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
167 case BOOT_TIME:
1279
29603c5a3317 sizeof("string") treats it as a char array _including_ the null terminator, so strncmp(dest, "string", sizeof("string")) is just strcpy.
Rob Landley <rob@landley.net>
parents: 1278
diff changeset
168 strcpy(toybuf+18, "- crash");
1163
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
169 break;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
170 case INIT_PROCESS:
1279
29603c5a3317 sizeof("string") treats it as a char array _including_ the null terminator, so strncmp(dest, "string", sizeof("string")) is just strcpy.
Rob Landley <rob@landley.net>
parents: 1278
diff changeset
171 strcpy(toybuf+18, " gone");
29603c5a3317 sizeof("string") treats it as a char array _including_ the null terminator, so strncmp(dest, "string", sizeof("string")) is just strcpy.
Rob Landley <rob@landley.net>
parents: 1278
diff changeset
172 strcpy(toybuf+28, "- no logout");
1163
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
173 break;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
174 default:
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 }
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
177 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
178 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
179 toybuf, toybuf+18, toybuf+28);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
180 }
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
181 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
182 } 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
183 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
184
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
185 loc -= sizeof(ut);
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
186 if(loc < 0) break;
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
187 xlseek(fd, loc, SEEK_SET);
1278
324306321d82 Initial cleanup of last: mostly whitespace, move no record test to start of loop, don't bother to stat an empty file to report when an empty log was created (just report current time).
Rob Landley <rob@landley.net>
parents: 1265
diff changeset
188 }
1163
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
189
1296
314b1486d502 Make last use common llist free function, minor cleanups.
Rob Landley <rob@landley.net>
parents: 1279
diff changeset
190 if (CFG_TOYBOX_FREE) {
314b1486d502 Make last use common llist free function, minor cleanups.
Rob Landley <rob@landley.net>
parents: 1279
diff changeset
191 xclose(fd);
314b1486d502 Make last use common llist free function, minor cleanups.
Rob Landley <rob@landley.net>
parents: 1279
diff changeset
192 free_list();
314b1486d502 Make last use common llist free function, minor cleanups.
Rob Landley <rob@landley.net>
parents: 1279
diff changeset
193 }
1278
324306321d82 Initial cleanup of last: mostly whitespace, move no record test to start of loop, don't bother to stat an empty file to report when an empty log was created (just report current time).
Rob Landley <rob@landley.net>
parents: 1265
diff changeset
194
324306321d82 Initial cleanup of last: mostly whitespace, move no record test to start of loop, don't bother to stat an empty file to report when an empty log was created (just report current time).
Rob Landley <rob@landley.net>
parents: 1265
diff changeset
195 xprintf("\n%s begins %-24.24s\n", basename(file), ctime(tm));
1163
972d4fc0c8a2 Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
196 }