]>
Commit | Line | Data |
---|---|---|
6dbe3af9 | 1 | /* |
4d1d1233 | 2 | * last(1) from sysvinit project, merged into util-linux in Aug 2013. |
6dbe3af9 | 3 | * |
4d1d1233 KZ |
4 | * Copyright (C) 1991-2004 Miquel van Smoorenburg. |
5 | * Copyright (C) 2013 Ondrej Oprala <ooprala@redhat.com> | |
77079432 | 6 | * Karel Zak <kzak@redhat.com> |
6dbe3af9 | 7 | * |
4d1d1233 KZ |
8 | * Re-implementation of the 'last' command, this time for Linux. Yes I know |
9 | * there is BSD last, but I just felt like writing this. No thanks :-). Also, | |
10 | * this version gives lots more info (especially with -x) | |
ce602720 | 11 | * |
ce602720 | 12 | * |
4d1d1233 KZ |
13 | * This program is free software; you can redistribute it and/or modify |
14 | * it under the terms of the GNU General Public License as published by | |
15 | * the Free Software Foundation; either version 2 of the License, or | |
16 | * (at your option) any later version. | |
ba0c9030 | 17 | * |
4d1d1233 KZ |
18 | * This program is distributed in the hope that it will be useful, |
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | * GNU General Public License for more details. | |
ce602720 | 22 | * |
4d1d1233 KZ |
23 | * You should have received a copy of the GNU General Public License |
24 | * along with this program; if not, write to the Free Software | |
25 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
ba0c9030 | 26 | */ |
7eda085c | 27 | #include <sys/types.h> |
ce602720 | 28 | #include <sys/stat.h> |
9d76c6fb | 29 | #include <fcntl.h> |
6dbe3af9 | 30 | #include <time.h> |
6dbe3af9 | 31 | #include <stdio.h> |
ce602720 | 32 | #include <ctype.h> |
b4b919fe | 33 | #include <utmpx.h> |
404fa3f9 | 34 | #include <pwd.h> |
6dbe3af9 KZ |
35 | #include <stdlib.h> |
36 | #include <unistd.h> | |
ce602720 OO |
37 | #include <string.h> |
38 | #include <signal.h> | |
39 | #include <getopt.h> | |
7eda085c | 40 | #include <netinet/in.h> |
ce602720 | 41 | #include <netdb.h> |
6dbe3af9 | 42 | #include <arpa/inet.h> |
c6ba16c6 | 43 | #include <libgen.h> |
7eda085c | 44 | |
293e0c90 KZ |
45 | #include "c.h" |
46 | #include "nls.h" | |
2ea747eb | 47 | #include "optutils.h" |
4c7dfea7 | 48 | #include "pathnames.h" |
a9f789e5 KZ |
49 | #include "xalloc.h" |
50 | #include "closestream.h" | |
2e58cd40 | 51 | #include "carefulputc.h" |
1bd68aab | 52 | #include "strutils.h" |
60f25dea | 53 | #include "timeutils.h" |
cd2876d2 | 54 | #include "monotonic.h" |
293e0c90 | 55 | |
ce602720 | 56 | #ifndef SHUTDOWN_TIME |
4d1d1233 | 57 | # define SHUTDOWN_TIME 254 |
6dbe3af9 | 58 | #endif |
ce602720 | 59 | |
83484375 SK |
60 | #ifndef LAST_LOGIN_LEN |
61 | # define LAST_LOGIN_LEN 8 | |
62 | #endif | |
63 | ||
64 | #ifndef LAST_DOMAIN_LEN | |
65 | # define LAST_DOMAIN_LEN 16 | |
66 | #endif | |
67 | ||
45f11b6b SK |
68 | #ifndef LAST_TIMESTAMP_LEN |
69 | # define LAST_TIMESTAMP_LEN 32 | |
70 | #endif | |
71 | ||
ce602720 OO |
72 | #define UCHUNKSIZE 16384 /* How much we read at once. */ |
73 | ||
83484375 | 74 | struct last_control { |
ee24e58f KZ |
75 | unsigned int lastb :1, /* Is this command 'lastb' */ |
76 | extended :1, /* Lots of info */ | |
77 | showhost :1, /* Show hostname */ | |
78 | altlist :1, /* Hostname at the end */ | |
79 | usedns :1, /* Use DNS to lookup the hostname */ | |
f16d5044 | 80 | useip :1; /* Print IP address in number format */ |
83484375 SK |
81 | |
82 | unsigned int name_len; /* Number of login name characters to print */ | |
83 | unsigned int domain_len; /* Number of domain name characters to print */ | |
84 | unsigned int maxrecs; /* Maximum number of records to list */ | |
85 | ||
86 | char **show; /* Match search list */ | |
87 | ||
0e5218ac | 88 | struct timeval boot_time; /* system boot time */ |
83484375 SK |
89 | time_t since; /* at what time to start displaying the file */ |
90 | time_t until; /* at what time to stop displaying the file */ | |
91 | time_t present; /* who where present at time_t */ | |
45f11b6b | 92 | unsigned int time_fmt; /* time format */ |
83484375 SK |
93 | }; |
94 | ||
ce602720 OO |
95 | /* Double linked list of struct utmp's */ |
96 | struct utmplist { | |
b4b919fe | 97 | struct utmpx ut; |
4d1d1233 KZ |
98 | struct utmplist *next; |
99 | struct utmplist *prev; | |
ce602720 | 100 | }; |
ce602720 OO |
101 | |
102 | /* Types of listing */ | |
e843d047 SK |
103 | enum { |
104 | R_CRASH = 1, /* No logout record, system boot in between */ | |
105 | R_DOWN, /* System brought down in decent way */ | |
106 | R_NORMAL, /* Normal */ | |
107 | R_NOW, /* Still logged in */ | |
108 | R_REBOOT, /* Reboot record. */ | |
109 | R_PHANTOM, /* No logout record but session is stale. */ | |
110 | R_TIMECHANGE /* NEW_TIME or OLD_TIME */ | |
111 | }; | |
ce602720 | 112 | |
45f11b6b SK |
113 | enum { |
114 | LAST_TIMEFTM_NONE = 0, | |
c7eb14d3 KZ |
115 | LAST_TIMEFTM_SHORT, |
116 | LAST_TIMEFTM_CTIME, | |
117 | LAST_TIMEFTM_ISO8601, | |
118 | ||
119 | LAST_TIMEFTM_HHMM, /* non-public */ | |
45f11b6b SK |
120 | }; |
121 | ||
c87e4fbb KZ |
122 | struct last_timefmt { |
123 | const char *name; | |
c7eb14d3 KZ |
124 | int in_len; /* log-in */ |
125 | int in_fmt; | |
126 | int out_len; /* log-out */ | |
127 | int out_fmt; | |
45f11b6b SK |
128 | }; |
129 | ||
c87e4fbb | 130 | static struct last_timefmt timefmts[] = { |
c7eb14d3 KZ |
131 | [LAST_TIMEFTM_NONE] = { .name = "notime" }, |
132 | [LAST_TIMEFTM_SHORT] = { | |
133 | .name = "short", | |
134 | .in_len = 16, | |
135 | .out_len = 7, | |
136 | .in_fmt = LAST_TIMEFTM_CTIME, | |
137 | .out_fmt = LAST_TIMEFTM_HHMM | |
138 | }, | |
139 | [LAST_TIMEFTM_CTIME] = { | |
140 | .name = "full", | |
141 | .in_len = 24, | |
142 | .out_len = 26, | |
143 | .in_fmt = LAST_TIMEFTM_CTIME, | |
144 | .out_fmt = LAST_TIMEFTM_CTIME | |
145 | }, | |
146 | [LAST_TIMEFTM_ISO8601] = { | |
147 | .name = "iso", | |
148 | .in_len = 24, | |
149 | .out_len = 26, | |
150 | .in_fmt = LAST_TIMEFTM_ISO8601, | |
151 | .out_fmt = LAST_TIMEFTM_ISO8601 | |
152 | } | |
45f11b6b SK |
153 | }; |
154 | ||
ce602720 | 155 | /* Global variables */ |
751947b6 | 156 | static unsigned int recsdone; /* Number of records listed */ |
77079432 | 157 | static time_t lastdate; /* Last date we've seen */ |
2e81d998 | 158 | static time_t currentdate; /* date when we started processing the file */ |
ce602720 | 159 | |
45f11b6b | 160 | /* --time-format=option parser */ |
eb2306e6 | 161 | static int which_time_format(const char *s) |
45f11b6b | 162 | { |
c87e4fbb KZ |
163 | size_t i; |
164 | ||
165 | for (i = 0; i < ARRAY_SIZE(timefmts); i++) { | |
eb2306e6 | 166 | if (strcmp(timefmts[i].name, s) == 0) |
c87e4fbb KZ |
167 | return i; |
168 | } | |
eb2306e6 | 169 | errx(EXIT_FAILURE, _("unknown time format: %s"), s); |
45f11b6b SK |
170 | } |
171 | ||
ce602720 OO |
172 | /* |
173 | * Read one utmp entry, return in new format. | |
174 | * Automatically reposition file pointer. | |
175 | */ | |
b4b919fe | 176 | static int uread(FILE *fp, struct utmpx *u, int *quit, const char *filename) |
ce602720 OO |
177 | { |
178 | static int utsize; | |
179 | static char buf[UCHUNKSIZE]; | |
180 | char tmp[1024]; | |
181 | static off_t fpos; | |
182 | static int bpos; | |
183 | off_t o; | |
184 | ||
185 | if (quit == NULL && u != NULL) { | |
186 | /* | |
187 | * Normal read. | |
188 | */ | |
b4b919fe | 189 | return fread(u, sizeof(struct utmpx), 1, fp); |
ce602720 OO |
190 | } |
191 | ||
192 | if (u == NULL) { | |
193 | /* | |
194 | * Initialize and position. | |
195 | */ | |
b4b919fe | 196 | utsize = sizeof(struct utmpx); |
ce602720 OO |
197 | fseeko(fp, 0, SEEK_END); |
198 | fpos = ftello(fp); | |
199 | if (fpos == 0) | |
200 | return 0; | |
201 | o = ((fpos - 1) / UCHUNKSIZE) * UCHUNKSIZE; | |
202 | if (fseeko(fp, o, SEEK_SET) < 0) { | |
744c7fec | 203 | warn(_("seek on %s failed"), filename); |
ce602720 OO |
204 | return 0; |
205 | } | |
206 | bpos = (int)(fpos - o); | |
207 | if (fread(buf, bpos, 1, fp) != 1) { | |
744c7fec | 208 | warn(_("cannot read %s"), filename); |
ce602720 OO |
209 | return 0; |
210 | } | |
211 | fpos = o; | |
212 | return 1; | |
213 | } | |
214 | ||
215 | /* | |
216 | * Read one struct. From the buffer if possible. | |
217 | */ | |
218 | bpos -= utsize; | |
219 | if (bpos >= 0) { | |
b4b919fe | 220 | memcpy(u, buf + bpos, sizeof(struct utmpx)); |
ce602720 | 221 | return 1; |
6dbe3af9 | 222 | } |
db797f24 | 223 | |
ce602720 OO |
224 | /* |
225 | * Oops we went "below" the buffer. We should be able to | |
226 | * seek back UCHUNKSIZE bytes. | |
227 | */ | |
228 | fpos -= UCHUNKSIZE; | |
229 | if (fpos < 0) | |
230 | return 0; | |
231 | ||
232 | /* | |
233 | * Copy whatever is left in the buffer. | |
234 | */ | |
235 | memcpy(tmp + (-bpos), buf, utsize + bpos); | |
236 | if (fseeko(fp, fpos, SEEK_SET) < 0) { | |
744c7fec | 237 | warn(_("seek on %s failed"), filename); |
ce602720 OO |
238 | return 0; |
239 | } | |
240 | ||
241 | /* | |
242 | * Read another UCHUNKSIZE bytes. | |
243 | */ | |
244 | if (fread(buf, UCHUNKSIZE, 1, fp) != 1) { | |
744c7fec | 245 | warn(_("cannot read %s"), filename); |
ce602720 OO |
246 | return 0; |
247 | } | |
248 | ||
249 | /* | |
250 | * The end of the UCHUNKSIZE byte buffer should be the first | |
251 | * few bytes of the current struct utmp. | |
252 | */ | |
253 | memcpy(tmp, buf + UCHUNKSIZE + bpos, -bpos); | |
254 | bpos += UCHUNKSIZE; | |
255 | ||
b4b919fe | 256 | memcpy(u, tmp, sizeof(struct utmpx)); |
ce602720 OO |
257 | |
258 | return 1; | |
6dbe3af9 KZ |
259 | } |
260 | ||
261 | /* | |
ce602720 | 262 | * Print a short date. |
6dbe3af9 | 263 | */ |
ce602720 OO |
264 | static char *showdate(void) |
265 | { | |
266 | char *s = ctime(&lastdate); | |
267 | s[16] = 0; | |
268 | return s; | |
6dbe3af9 KZ |
269 | } |
270 | ||
271 | /* | |
ce602720 | 272 | * SIGINT handler |
6dbe3af9 | 273 | */ |
ce602720 OO |
274 | static void int_handler(int sig __attribute__((unused))) |
275 | { | |
a9f789e5 | 276 | errx(EXIT_FAILURE, _("Interrupted %s"), showdate()); |
ce602720 | 277 | } |
8e5cf69f | 278 | |
ce602720 OO |
279 | /* |
280 | * SIGQUIT handler | |
281 | */ | |
282 | static void quit_handler(int sig __attribute__((unused))) | |
283 | { | |
a9f789e5 | 284 | warnx(_("Interrupted %s"), showdate()); |
ce602720 OO |
285 | signal(SIGQUIT, quit_handler); |
286 | } | |
6dbe3af9 | 287 | |
ce602720 OO |
288 | /* |
289 | * Lookup a host with DNS. | |
290 | */ | |
291 | static int dns_lookup(char *result, int size, int useip, int32_t *a) | |
292 | { | |
293 | struct sockaddr_in sin; | |
294 | struct sockaddr_in6 sin6; | |
295 | struct sockaddr *sa; | |
296 | int salen, flags; | |
297 | int mapped = 0; | |
6dbe3af9 | 298 | |
ce602720 | 299 | flags = useip ? NI_NUMERICHOST : 0; |
6dbe3af9 | 300 | |
ce602720 OO |
301 | /* |
302 | * IPv4 or IPv6 ? | |
303 | * 1. If last 3 4bytes are 0, must be IPv4 | |
304 | * 2. If IPv6 in IPv4, handle as IPv4 | |
305 | * 3. Anything else is IPv6 | |
306 | * | |
307 | * Ugly. | |
308 | */ | |
309 | if (a[0] == 0 && a[1] == 0 && a[2] == (int32_t)htonl (0xffff)) | |
310 | mapped = 1; | |
311 | ||
312 | if (mapped || (a[1] == 0 && a[2] == 0 && a[3] == 0)) { | |
313 | /* IPv4 */ | |
314 | sin.sin_family = AF_INET; | |
315 | sin.sin_port = 0; | |
316 | sin.sin_addr.s_addr = mapped ? a[3] : a[0]; | |
317 | sa = (struct sockaddr *)&sin; | |
318 | salen = sizeof(sin); | |
319 | } else { | |
320 | /* IPv6 */ | |
321 | memset(&sin6, 0, sizeof(sin6)); | |
322 | sin6.sin6_family = AF_INET6; | |
323 | sin6.sin6_port = 0; | |
324 | memcpy(sin6.sin6_addr.s6_addr, a, 16); | |
325 | sa = (struct sockaddr *)&sin6; | |
326 | salen = sizeof(sin6); | |
c07ebfa1 | 327 | } |
ce602720 OO |
328 | |
329 | return getnameinfo(sa, salen, result, size, NULL, 0, flags); | |
6dbe3af9 KZ |
330 | } |
331 | ||
c7eb14d3 | 332 | static int time_formatter(int fmt, char *dst, size_t dlen, time_t *when) |
45f11b6b | 333 | { |
45f11b6b SK |
334 | int ret = 0; |
335 | ||
c7eb14d3 | 336 | switch (fmt) { |
45f11b6b SK |
337 | case LAST_TIMEFTM_NONE: |
338 | *dst = 0; | |
339 | break; | |
c7eb14d3 KZ |
340 | case LAST_TIMEFTM_HHMM: |
341 | { | |
342 | struct tm *tm = localtime(when); | |
343 | if (!snprintf(dst, dlen, "%02d:%02d", tm->tm_hour, tm->tm_min)) | |
344 | ret = -1; | |
45f11b6b | 345 | break; |
c7eb14d3 KZ |
346 | } |
347 | case LAST_TIMEFTM_CTIME: | |
78acfddd SK |
348 | snprintf(dst, dlen, "%s", ctime(when)); |
349 | ret = rtrim_whitespace((unsigned char *) dst); | |
45f11b6b SK |
350 | break; |
351 | case LAST_TIMEFTM_ISO8601: | |
c7eb14d3 | 352 | ret = strtime_iso(when, ISO_8601_DATE|ISO_8601_TIME|ISO_8601_TIMEZONE, dst, dlen); |
45f11b6b SK |
353 | break; |
354 | default: | |
355 | abort(); | |
356 | } | |
357 | return ret; | |
358 | } | |
359 | ||
97bbb5e4 SK |
360 | /* |
361 | * Remove trailing spaces from a string. | |
362 | */ | |
363 | static void trim_trailing_spaces(char *s) | |
364 | { | |
365 | char *p; | |
366 | ||
367 | for (p = s; *p; ++p) | |
368 | continue; | |
369 | while (p > s && isspace(*--p)) | |
370 | continue; | |
371 | if (p > s) | |
372 | ++p; | |
373 | *p++ = '\n'; | |
374 | *p = '\0'; | |
375 | } | |
376 | ||
6dbe3af9 | 377 | /* |
ce602720 | 378 | * Show one line of information on screen |
6dbe3af9 | 379 | */ |
b4b919fe | 380 | static int list(const struct last_control *ctl, struct utmpx *p, time_t logout_time, int what) |
ce602720 | 381 | { |
2e81d998 | 382 | time_t secs, utmp_time; |
45f11b6b SK |
383 | char logintime[LAST_TIMESTAMP_LEN]; |
384 | char logouttime[LAST_TIMESTAMP_LEN]; | |
385 | char length[LAST_TIMESTAMP_LEN]; | |
ce602720 | 386 | char final[512]; |
a924b400 | 387 | char utline[sizeof(p->ut_line) + 1]; |
ce602720 | 388 | char domain[256]; |
328734c0 | 389 | char *s; |
ce602720 OO |
390 | int mins, hours, days; |
391 | int r, len; | |
c87e4fbb | 392 | struct last_timefmt *fmt; |
6dbe3af9 | 393 | |
ce602720 OO |
394 | /* |
395 | * uucp and ftp have special-type entries | |
396 | */ | |
397 | utline[0] = 0; | |
a924b400 | 398 | strncat(utline, p->ut_line, sizeof(p->ut_line)); |
ce602720 OO |
399 | if (strncmp(utline, "ftp", 3) == 0 && isdigit(utline[3])) |
400 | utline[3] = 0; | |
401 | if (strncmp(utline, "uucp", 4) == 0 && isdigit(utline[4])) | |
402 | utline[4] = 0; | |
403 | ||
404 | /* | |
0e65dcde | 405 | * Is this something we want to show? |
ce602720 | 406 | */ |
83484375 | 407 | if (ctl->show) { |
328734c0 | 408 | char **walk; |
83484375 | 409 | for (walk = ctl->show; *walk; walk++) { |
a924b400 | 410 | if (strncmp(p->ut_user, *walk, sizeof(p->ut_user)) == 0 || |
ce602720 OO |
411 | strcmp(utline, *walk) == 0 || |
412 | (strncmp(utline, "tty", 3) == 0 && | |
413 | strcmp(utline + 3, *walk) == 0)) break; | |
414 | } | |
415 | if (*walk == NULL) return 0; | |
7eda085c | 416 | } |
ce602720 OO |
417 | |
418 | /* | |
419 | * Calculate times | |
420 | */ | |
c7eb14d3 KZ |
421 | fmt = &timefmts[ctl->time_fmt]; |
422 | ||
55771f54 | 423 | utmp_time = p->ut_tv.tv_sec; |
37f534a6 | 424 | |
d4ce90d4 SK |
425 | if (ctl->present) { |
426 | if (ctl->present < utmp_time) | |
427 | return 0; | |
428 | if (0 < logout_time && logout_time < ctl->present) | |
429 | return 0; | |
430 | } | |
c7eb14d3 KZ |
431 | |
432 | /* log-in time */ | |
433 | if (time_formatter(fmt->in_fmt, logintime, | |
434 | sizeof(logintime), &utmp_time) < 0) | |
45f11b6b SK |
435 | errx(EXIT_FAILURE, _("preallocation size exceeded")); |
436 | ||
c7eb14d3 | 437 | /* log-out time */ |
d4ce90d4 | 438 | secs = logout_time - utmp_time; |
ce602720 OO |
439 | mins = (secs / 60) % 60; |
440 | hours = (secs / 3600) % 24; | |
441 | days = secs / 86400; | |
31d28e09 | 442 | |
c4482f7b KZ |
443 | strcpy(logouttime, "- "); |
444 | if (time_formatter(fmt->out_fmt, logouttime + 2, | |
445 | sizeof(logouttime) - 2, &logout_time) < 0) | |
446 | errx(EXIT_FAILURE, _("preallocation size exceeded")); | |
447 | ||
2e81d998 | 448 | if (logout_time == currentdate) { |
c7eb14d3 | 449 | if (ctl->time_fmt > LAST_TIMEFTM_SHORT) { |
31d28e09 | 450 | sprintf(logouttime, " still running"); |
d3108db1 RM |
451 | length[0] = 0; |
452 | } else { | |
31d28e09 SK |
453 | sprintf(logouttime, " still"); |
454 | sprintf(length, "running"); | |
455 | } | |
c7eb14d3 | 456 | } else if (days) { |
ce602720 | 457 | sprintf(length, "(%d+%02d:%02d)", days, hours, mins); |
c7eb14d3 | 458 | } else { |
ce602720 | 459 | sprintf(length, " (%02d:%02d)", hours, mins); |
c7eb14d3 | 460 | } |
ce602720 OO |
461 | |
462 | switch(what) { | |
463 | case R_CRASH: | |
464 | sprintf(logouttime, "- crash"); | |
465 | break; | |
466 | case R_DOWN: | |
467 | sprintf(logouttime, "- down "); | |
468 | break; | |
469 | case R_NOW: | |
c7eb14d3 | 470 | if (ctl->time_fmt > LAST_TIMEFTM_SHORT) { |
ce602720 | 471 | sprintf(logouttime, " still logged in"); |
4fcbfaa8 RM |
472 | length[0] = 0; |
473 | } else { | |
ce602720 OO |
474 | sprintf(logouttime, " still"); |
475 | sprintf(length, "logged in"); | |
476 | } | |
6dbe3af9 | 477 | break; |
ce602720 | 478 | case R_PHANTOM: |
c7eb14d3 | 479 | if (ctl->time_fmt > LAST_TIMEFTM_SHORT) { |
ce602720 | 480 | sprintf(logouttime, " gone - no logout"); |
4fcbfaa8 | 481 | length[0] = 0; |
c7eb14d3 | 482 | } else if (ctl->time_fmt == LAST_TIMEFTM_SHORT) { |
ce602720 OO |
483 | sprintf(logouttime, " gone"); |
484 | sprintf(length, "- no logout"); | |
6e69f1ee RM |
485 | } else { |
486 | logouttime[0] = 0; | |
487 | sprintf(length, "no logout"); | |
ce602720 OO |
488 | } |
489 | break; | |
ce602720 OO |
490 | case R_TIMECHANGE: |
491 | logouttime[0] = 0; | |
492 | length[0] = 0; | |
6dbe3af9 | 493 | break; |
ce602720 | 494 | case R_NORMAL: |
c7eb14d3 | 495 | case R_REBOOT: |
6dbe3af9 | 496 | break; |
5cae90f9 SK |
497 | default: |
498 | abort(); | |
ce602720 OO |
499 | } |
500 | ||
501 | /* | |
502 | * Look up host with DNS if needed. | |
503 | */ | |
504 | r = -1; | |
83484375 SK |
505 | if (ctl->usedns || ctl->useip) |
506 | r = dns_lookup(domain, sizeof(domain), ctl->useip, p->ut_addr_v6); | |
ce602720 | 507 | if (r < 0) { |
a924b400 | 508 | len = sizeof(p->ut_host); |
ce602720 OO |
509 | if (len >= (int)sizeof(domain)) len = sizeof(domain) - 1; |
510 | domain[0] = 0; | |
511 | strncat(domain, p->ut_host, len); | |
512 | } | |
513 | ||
c87e4fbb | 514 | |
83484375 SK |
515 | if (ctl->showhost) { |
516 | if (!ctl->altlist) { | |
ce602720 | 517 | len = snprintf(final, sizeof(final), |
438269dc | 518 | "%-8.*s %-12.12s %-16.*s %-*.*s %-*.*s %s\n", |
cfa7fe89 | 519 | ctl->name_len, p->ut_user, utline, |
45f11b6b | 520 | ctl->domain_len, domain, |
c7eb14d3 | 521 | fmt->in_len, fmt->in_len, logintime, fmt->out_len, fmt->out_len, |
c87e4fbb | 522 | logouttime, length); |
ce602720 OO |
523 | } else { |
524 | len = snprintf(final, sizeof(final), | |
45f11b6b | 525 | "%-8.*s %-12.12s %-*.*s %-*.*s %-12.12s %s\n", |
cfa7fe89 | 526 | ctl->name_len, p->ut_user, utline, |
c7eb14d3 | 527 | fmt->in_len, fmt->in_len, logintime, fmt->out_len, fmt->out_len, |
c87e4fbb | 528 | logouttime, length, domain); |
5818a4aa | 529 | } |
ce602720 OO |
530 | } else |
531 | len = snprintf(final, sizeof(final), | |
438269dc | 532 | "%-8.*s %-12.12s %-*.*s %-*.*s %s\n", |
cfa7fe89 | 533 | ctl->name_len, p->ut_user, utline, |
c7eb14d3 | 534 | fmt->in_len, fmt->in_len, logintime, fmt->out_len, fmt->out_len, |
c87e4fbb | 535 | logouttime, length); |
ce602720 OO |
536 | |
537 | #if defined(__GLIBC__) | |
538 | # if (__GLIBC__ == 2) && (__GLIBC_MINOR__ == 0) | |
539 | final[sizeof(final)-1] = '\0'; | |
540 | # endif | |
541 | #endif | |
6dbe3af9 | 542 | |
97bbb5e4 | 543 | trim_trailing_spaces(final); |
ce602720 OO |
544 | /* |
545 | * Print out "final" string safely. | |
546 | */ | |
2e58cd40 | 547 | for (s = final; *s; s++) |
78a3b0af | 548 | fputc_careful(*s, stdout, '*'); |
ce602720 OO |
549 | |
550 | if (len < 0 || (size_t)len >= sizeof(final)) | |
551 | putchar('\n'); | |
552 | ||
553 | recsdone++; | |
83484375 | 554 | if (ctl->maxrecs && ctl->maxrecs <= recsdone) |
ce602720 OO |
555 | return 1; |
556 | ||
557 | return 0; | |
6dbe3af9 KZ |
558 | } |
559 | ||
ce602720 | 560 | |
f06abd22 | 561 | static void __attribute__((__noreturn__)) usage(const struct last_control *ctl, FILE *out) |
ce602720 | 562 | { |
293e0c90 KZ |
563 | fputs(USAGE_HEADER, out); |
564 | fprintf(out, _( | |
b8672754 | 565 | " %s [options] [<username>...] [<tty>...]\n"), program_invocation_short_name); |
293e0c90 | 566 | |
451dbcfa BS |
567 | fputs(USAGE_SEPARATOR, out); |
568 | fputs(_("Show a listing of last logged in users.\n"), out); | |
569 | ||
293e0c90 KZ |
570 | fputs(USAGE_OPTIONS, out); |
571 | fputs(_(" -<number> how many lines to show\n"), out); | |
572 | fputs(_(" -a, --hostlast display hostnames in the last column\n"), out); | |
573 | fputs(_(" -d, --dns translate the IP number back into a hostname\n"), out); | |
574 | fprintf(out, | |
f06abd22 | 575 | _(" -f, --file <file> use a specific file instead of %s\n"), ctl->lastb ? _PATH_BTMP : _PATH_WTMP); |
293e0c90 KZ |
576 | fputs(_(" -F, --fulltimes print full login and logout times and dates\n"), out); |
577 | fputs(_(" -i, --ip display IP numbers in numbers-and-dots notation\n"), out); | |
578 | fputs(_(" -n, --limit <number> how many lines to show\n"), out); | |
579 | fputs(_(" -R, --nohostname don't display the hostname field\n"), out); | |
dd50168b SK |
580 | fputs(_(" -s, --since <time> display the lines since the specified time\n"), out); |
581 | fputs(_(" -t, --until <time> display the lines until the specified time\n"), out); | |
09af3db4 | 582 | fputs(_(" -p, --present <time> display who were present at the specified time\n"), out); |
293e0c90 KZ |
583 | fputs(_(" -w, --fullnames display full user and domain names\n"), out); |
584 | fputs(_(" -x, --system display system shutdown entries and run level changes\n"), out); | |
09af3db4 BS |
585 | fputs(_(" --time-format <format> show timestamps in the specified <format>:\n" |
586 | " notime|short|full|iso\n"), out); | |
293e0c90 KZ |
587 | |
588 | fputs(USAGE_SEPARATOR, out); | |
589 | fputs(USAGE_HELP, out); | |
590 | fputs(USAGE_VERSION, out); | |
30faa202 | 591 | fprintf(out, USAGE_MAN_TAIL("last(1)")); |
293e0c90 KZ |
592 | |
593 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); | |
6dbe3af9 KZ |
594 | } |
595 | ||
b4b919fe | 596 | static int is_phantom(const struct last_control *ctl, struct utmpx *ut) |
404fa3f9 SK |
597 | { |
598 | struct passwd *pw; | |
599 | char path[32]; | |
0e5218ac | 600 | int ret = 0; |
404fa3f9 | 601 | |
55771f54 | 602 | if (ut->ut_tv.tv_sec < ctl->boot_time.tv_sec) |
0e5218ac | 603 | return 1; |
a365953a | 604 | pw = getpwnam(ut->ut_user); |
404fa3f9 SK |
605 | if (!pw) |
606 | return 1; | |
607 | sprintf(path, "/proc/%u/loginuid", ut->ut_pid); | |
8813da1e SK |
608 | if (access(path, R_OK) == 0) { |
609 | unsigned int loginuid; | |
610 | FILE *f = NULL; | |
611 | ||
612 | if (!(f = fopen(path, "r"))) | |
613 | return 1; | |
614 | if (fscanf(f, "%u", &loginuid) != 1) | |
615 | ret = 1; | |
616 | fclose(f); | |
617 | if (!ret && pw->pw_uid != loginuid) | |
618 | return 1; | |
619 | } else { | |
620 | struct stat st; | |
2e118e70 | 621 | |
8813da1e SK |
622 | sprintf(path, "/dev/%s", ut->ut_line); |
623 | if (stat(path, &st)) | |
624 | return 1; | |
625 | if (pw->pw_uid != st.st_uid) | |
626 | return 1; | |
627 | } | |
404fa3f9 SK |
628 | return ret; |
629 | } | |
293e0c90 | 630 | |
744c7fec KZ |
631 | static void process_wtmp_file(const struct last_control *ctl, |
632 | const char *filename) | |
ce602720 | 633 | { |
9e930041 | 634 | FILE *fp; /* File pointer of wtmp file */ |
77079432 | 635 | |
b4b919fe | 636 | struct utmpx ut; /* Current utmp entry */ |
da1a8ed0 | 637 | struct utmplist *ulist = NULL; /* All entries */ |
77079432 KZ |
638 | struct utmplist *p; /* Pointer into utmplist */ |
639 | struct utmplist *next; /* Pointer into utmplist */ | |
640 | ||
641 | time_t lastboot = 0; /* Last boottime */ | |
642 | time_t lastrch = 0; /* Last run level change */ | |
643 | time_t lastdown; /* Last downtime */ | |
644 | time_t begintime; /* When wtmp begins */ | |
645 | int whydown = 0; /* Why we went down: crash or shutdown */ | |
646 | ||
647 | int c, x; /* Scratch */ | |
648 | struct stat st; /* To stat the [uw]tmp file */ | |
649 | int quit = 0; /* Flag */ | |
650 | int down = 0; /* Down flag */ | |
77079432 KZ |
651 | |
652 | time(&lastdown); | |
77079432 KZ |
653 | /* |
654 | * Fill in 'lastdate' | |
655 | */ | |
2e81d998 | 656 | lastdate = currentdate = lastrch = lastdown; |
ce602720 | 657 | |
77079432 KZ |
658 | /* |
659 | * Install signal handlers | |
660 | */ | |
661 | signal(SIGINT, int_handler); | |
662 | signal(SIGQUIT, quit_handler); | |
ce602720 | 663 | |
77079432 KZ |
664 | /* |
665 | * Open the utmp file | |
666 | */ | |
da1a8ed0 KZ |
667 | if ((fp = fopen(filename, "r")) == NULL) |
668 | err(EXIT_FAILURE, _("cannot open %s"), filename); | |
6dbe3af9 KZ |
669 | |
670 | /* | |
77079432 | 671 | * Optimize the buffer size. |
ce602720 | 672 | */ |
77079432 KZ |
673 | setvbuf(fp, NULL, _IOFBF, UCHUNKSIZE); |
674 | ||
ce602720 | 675 | /* |
77079432 | 676 | * Read first structure to capture the time field |
6dbe3af9 | 677 | */ |
744c7fec | 678 | if (uread(fp, &ut, NULL, filename) == 1) |
55771f54 | 679 | begintime = ut.ut_tv.tv_sec; |
ce602720 | 680 | else { |
d879e052 | 681 | if (fstat(fileno(fp), &st) != 0) |
da1a8ed0 | 682 | err(EXIT_FAILURE, _("stat of %s failed"), filename); |
77079432 KZ |
683 | begintime = st.st_ctime; |
684 | quit = 1; | |
685 | } | |
686 | ||
687 | /* | |
688 | * Go to end of file minus one structure | |
689 | * and/or initialize utmp reading code. | |
690 | */ | |
744c7fec | 691 | uread(fp, NULL, NULL, filename); |
77079432 KZ |
692 | |
693 | /* | |
694 | * Read struct after struct backwards from the file. | |
695 | */ | |
696 | while (!quit) { | |
697 | ||
744c7fec | 698 | if (uread(fp, &ut, &quit, filename) != 1) |
77079432 KZ |
699 | break; |
700 | ||
55771f54 | 701 | if (ctl->since && ut.ut_tv.tv_sec < ctl->since) |
dd50168b SK |
702 | continue; |
703 | ||
55771f54 | 704 | if (ctl->until && ctl->until < ut.ut_tv.tv_sec) |
77079432 KZ |
705 | continue; |
706 | ||
55771f54 | 707 | lastdate = ut.ut_tv.tv_sec; |
77079432 | 708 | |
83484375 | 709 | if (ctl->lastb) { |
55771f54 | 710 | quit = list(ctl, &ut, ut.ut_tv.tv_sec, R_NORMAL); |
77079432 KZ |
711 | continue; |
712 | } | |
713 | ||
ce602720 | 714 | /* |
77079432 | 715 | * Set ut_type to the correct type. |
ce602720 | 716 | */ |
77079432 KZ |
717 | if (strncmp(ut.ut_line, "~", 1) == 0) { |
718 | if (strncmp(ut.ut_user, "shutdown", 8) == 0) | |
719 | ut.ut_type = SHUTDOWN_TIME; | |
720 | else if (strncmp(ut.ut_user, "reboot", 6) == 0) | |
721 | ut.ut_type = BOOT_TIME; | |
722 | else if (strncmp(ut.ut_user, "runlevel", 8) == 0) | |
723 | ut.ut_type = RUN_LVL; | |
724 | } | |
725 | #if 1 /*def COMPAT*/ | |
ce602720 | 726 | /* |
77079432 KZ |
727 | * For stupid old applications that don't fill in |
728 | * ut_type correctly. | |
ce602720 | 729 | */ |
77079432 KZ |
730 | else { |
731 | if (ut.ut_type != DEAD_PROCESS && | |
cfa7fe89 SK |
732 | ut.ut_user[0] && ut.ut_line[0] && |
733 | strcmp(ut.ut_user, "LOGIN") != 0) | |
77079432 KZ |
734 | ut.ut_type = USER_PROCESS; |
735 | /* | |
736 | * Even worse, applications that write ghost | |
737 | * entries: ut_type set to USER_PROCESS but | |
cfa7fe89 | 738 | * empty ut_user... |
77079432 | 739 | */ |
cfa7fe89 | 740 | if (ut.ut_user[0] == 0) |
77079432 KZ |
741 | ut.ut_type = DEAD_PROCESS; |
742 | ||
743 | /* | |
744 | * Clock changes. | |
745 | */ | |
cfa7fe89 | 746 | if (strcmp(ut.ut_user, "date") == 0) { |
77079432 KZ |
747 | if (ut.ut_line[0] == '|') |
748 | ut.ut_type = OLD_TIME; | |
749 | if (ut.ut_line[0] == '{') | |
750 | ut.ut_type = NEW_TIME; | |
751 | } | |
6dbe3af9 | 752 | } |
ce602720 | 753 | #endif |
77079432 | 754 | switch (ut.ut_type) { |
ce602720 | 755 | case SHUTDOWN_TIME: |
83484375 | 756 | if (ctl->extended) { |
ce602720 | 757 | strcpy(ut.ut_line, "system down"); |
83484375 | 758 | quit = list(ctl, &ut, lastboot, R_NORMAL); |
ce602720 | 759 | } |
55771f54 | 760 | lastdown = lastrch = ut.ut_tv.tv_sec; |
ce602720 OO |
761 | down = 1; |
762 | break; | |
763 | case OLD_TIME: | |
764 | case NEW_TIME: | |
83484375 | 765 | if (ctl->extended) { |
ce602720 OO |
766 | strcpy(ut.ut_line, |
767 | ut.ut_type == NEW_TIME ? "new time" : | |
768 | "old time"); | |
83484375 | 769 | quit = list(ctl, &ut, lastdown, R_TIMECHANGE); |
ce602720 OO |
770 | } |
771 | break; | |
772 | case BOOT_TIME: | |
773 | strcpy(ut.ut_line, "system boot"); | |
83484375 | 774 | quit = list(ctl, &ut, lastdown, R_REBOOT); |
55771f54 | 775 | lastboot = ut.ut_tv.tv_sec; |
ce602720 OO |
776 | down = 1; |
777 | break; | |
778 | case RUN_LVL: | |
779 | x = ut.ut_pid & 255; | |
83484375 | 780 | if (ctl->extended) { |
ce602720 | 781 | sprintf(ut.ut_line, "(to lvl %c)", x); |
83484375 | 782 | quit = list(ctl, &ut, lastrch, R_NORMAL); |
ce602720 OO |
783 | } |
784 | if (x == '0' || x == '6') { | |
55771f54 | 785 | lastdown = ut.ut_tv.tv_sec; |
ce602720 OO |
786 | down = 1; |
787 | ut.ut_type = SHUTDOWN_TIME; | |
788 | } | |
55771f54 | 789 | lastrch = ut.ut_tv.tv_sec; |
ce602720 | 790 | break; |
6dbe3af9 | 791 | |
ce602720 OO |
792 | case USER_PROCESS: |
793 | /* | |
77079432 KZ |
794 | * This was a login - show the first matching |
795 | * logout record and delete all records with | |
796 | * the same ut_line. | |
ce602720 OO |
797 | */ |
798 | c = 0; | |
da1a8ed0 | 799 | for (p = ulist; p; p = next) { |
ce602720 OO |
800 | next = p->next; |
801 | if (strncmp(p->ut.ut_line, ut.ut_line, | |
a924b400 | 802 | sizeof(ut.ut_line)) == 0) { |
ce602720 OO |
803 | /* Show it */ |
804 | if (c == 0) { | |
55771f54 | 805 | quit = list(ctl, &ut, p->ut.ut_tv.tv_sec, R_NORMAL); |
ce602720 OO |
806 | c = 1; |
807 | } | |
da1a8ed0 KZ |
808 | if (p->next) |
809 | p->next->prev = p->prev; | |
ce602720 OO |
810 | if (p->prev) |
811 | p->prev->next = p->next; | |
812 | else | |
da1a8ed0 | 813 | ulist = p->next; |
ce602720 OO |
814 | free(p); |
815 | } | |
816 | } | |
817 | /* | |
77079432 KZ |
818 | * Not found? Then crashed, down, still |
819 | * logged in, or missing logout record. | |
ce602720 OO |
820 | */ |
821 | if (c == 0) { | |
ee24e58f | 822 | if (!lastboot) { |
ce602720 OO |
823 | c = R_NOW; |
824 | /* Is process still alive? */ | |
0e5218ac | 825 | if (is_phantom(ctl, &ut)) |
ce602720 OO |
826 | c = R_PHANTOM; |
827 | } else | |
828 | c = whydown; | |
83484375 | 829 | quit = list(ctl, &ut, lastboot, c); |
ce602720 OO |
830 | } |
831 | /* FALLTHRU */ | |
832 | ||
833 | case DEAD_PROCESS: | |
834 | /* | |
77079432 KZ |
835 | * Just store the data if it is |
836 | * interesting enough. | |
ce602720 OO |
837 | */ |
838 | if (ut.ut_line[0] == 0) | |
839 | break; | |
a9f789e5 | 840 | p = xmalloc(sizeof(struct utmplist)); |
b4b919fe | 841 | memcpy(&p->ut, &ut, sizeof(struct utmpx)); |
da1a8ed0 | 842 | p->next = ulist; |
ce602720 | 843 | p->prev = NULL; |
da1a8ed0 KZ |
844 | if (ulist) |
845 | ulist->prev = p; | |
846 | ulist = p; | |
ce602720 OO |
847 | break; |
848 | ||
5cae90f9 SK |
849 | case EMPTY: |
850 | case INIT_PROCESS: | |
851 | case LOGIN_PROCESS: | |
b4b919fe | 852 | #ifdef ACCOUNTING |
5cae90f9 | 853 | case ACCOUNTING: |
b4b919fe | 854 | #endif |
5cae90f9 SK |
855 | /* ignored ut_types */ |
856 | break; | |
857 | ||
858 | default: | |
9e930041 | 859 | warnx("unrecognized ut_type: %d", ut.ut_type); |
ce602720 | 860 | } |
77079432 KZ |
861 | |
862 | /* | |
863 | * If we saw a shutdown/reboot record we can remove | |
da1a8ed0 | 864 | * the entire current ulist. |
77079432 KZ |
865 | */ |
866 | if (down) { | |
55771f54 | 867 | lastboot = ut.ut_tv.tv_sec; |
77079432 | 868 | whydown = (ut.ut_type == SHUTDOWN_TIME) ? R_DOWN : R_CRASH; |
da1a8ed0 | 869 | for (p = ulist; p; p = next) { |
77079432 KZ |
870 | next = p->next; |
871 | free(p); | |
872 | } | |
da1a8ed0 | 873 | ulist = NULL; |
77079432 KZ |
874 | down = 0; |
875 | } | |
ce602720 | 876 | } |
ce602720 | 877 | |
78acfddd SK |
878 | if (ctl->time_fmt != LAST_TIMEFTM_NONE) { |
879 | struct last_timefmt *fmt; | |
880 | char timestr[LAST_TIMESTAMP_LEN]; | |
881 | char *tmp = xstrdup(filename); | |
882 | ||
883 | fmt = &timefmts[ctl->time_fmt]; | |
884 | if (time_formatter(fmt->in_fmt, timestr, | |
885 | sizeof(timestr), &begintime) < 0) | |
886 | errx(EXIT_FAILURE, _("preallocation size exceeded")); | |
887 | printf(_("\n%s begins %s\n"), basename(tmp), timestr); | |
c6ba16c6 RM |
888 | free(tmp); |
889 | } | |
78acfddd | 890 | |
77079432 | 891 | fclose(fp); |
da1a8ed0 KZ |
892 | |
893 | for (p = ulist; p; p = next) { | |
bce5163b SK |
894 | next = p->next; |
895 | free(p); | |
896 | } | |
7ec78229 SK |
897 | } |
898 | ||
899 | int main(int argc, char **argv) | |
900 | { | |
83484375 SK |
901 | struct last_control ctl = { |
902 | .showhost = TRUE, | |
903 | .name_len = LAST_LOGIN_LEN, | |
c7eb14d3 | 904 | .time_fmt = LAST_TIMEFTM_SHORT, |
83484375 SK |
905 | .domain_len = LAST_DOMAIN_LEN |
906 | }; | |
744c7fec KZ |
907 | char **files = NULL; |
908 | size_t i, nfiles = 0; | |
83484375 | 909 | int c; |
3a4ae395 | 910 | usec_t p; |
7ec78229 | 911 | |
45f11b6b SK |
912 | enum { |
913 | OPT_TIME_FORMAT = CHAR_MAX + 1 | |
914 | }; | |
7ec78229 SK |
915 | static const struct option long_opts[] = { |
916 | { "limit", required_argument, NULL, 'n' }, | |
917 | { "help", no_argument, NULL, 'h' }, | |
918 | { "file", required_argument, NULL, 'f' }, | |
919 | { "nohostname", no_argument, NULL, 'R' }, | |
920 | { "version", no_argument, NULL, 'V' }, | |
921 | { "hostlast", no_argument, NULL, 'a' }, | |
dd50168b | 922 | { "since", required_argument, NULL, 's' }, |
7ec78229 | 923 | { "until", required_argument, NULL, 't' }, |
37f534a6 | 924 | { "present", required_argument, NULL, 'p' }, |
7ec78229 SK |
925 | { "system", no_argument, NULL, 'x' }, |
926 | { "dns", no_argument, NULL, 'd' }, | |
927 | { "ip", no_argument, NULL, 'i' }, | |
928 | { "fulltimes", no_argument, NULL, 'F' }, | |
929 | { "fullnames", no_argument, NULL, 'w' }, | |
45f11b6b | 930 | { "time-format", required_argument, NULL, OPT_TIME_FORMAT }, |
7ec78229 SK |
931 | { NULL, 0, NULL, 0 } |
932 | }; | |
a7349ee3 | 933 | static const ul_excl_t excl[] = { /* rows and cols in ASCII order */ |
2ea747eb KZ |
934 | { 'F', OPT_TIME_FORMAT }, /* fulltime, time-format */ |
935 | { 0 } | |
936 | }; | |
937 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
7ec78229 SK |
938 | |
939 | setlocale(LC_ALL, ""); | |
940 | bindtextdomain(PACKAGE, LOCALEDIR); | |
941 | textdomain(PACKAGE); | |
942 | atexit(close_stdout); | |
f06abd22 SK |
943 | /* |
944 | * Which file do we want to read? | |
945 | */ | |
946 | ctl.lastb = strcmp(program_invocation_short_name, "lastb") == 0 ? 1 : 0; | |
7ec78229 | 947 | while ((c = getopt_long(argc, argv, |
dd50168b | 948 | "hVf:n:RxadFit:p:s:0123456789w", long_opts, NULL)) != -1) { |
2ea747eb KZ |
949 | |
950 | err_exclusive_options(c, long_opts, excl, excl_st); | |
951 | ||
7ec78229 SK |
952 | switch(c) { |
953 | case 'h': | |
f06abd22 | 954 | usage(&ctl, stdout); |
7ec78229 SK |
955 | break; |
956 | case 'V': | |
957 | printf(UTIL_LINUX_VERSION); | |
958 | return EXIT_SUCCESS; | |
959 | case 'R': | |
ee24e58f | 960 | ctl.showhost = 0; |
7ec78229 SK |
961 | break; |
962 | case 'x': | |
ee24e58f | 963 | ctl.extended = 1; |
7ec78229 SK |
964 | break; |
965 | case 'n': | |
83484375 | 966 | ctl.maxrecs = strtos32_or_err(optarg, _("failed to parse number")); |
7ec78229 SK |
967 | break; |
968 | case 'f': | |
744c7fec KZ |
969 | if (!files) |
970 | files = xmalloc(sizeof(char *) * argc); | |
971 | files[nfiles++] = xstrdup(optarg); | |
7ec78229 SK |
972 | break; |
973 | case 'd': | |
ee24e58f | 974 | ctl.usedns = 1; |
7ec78229 SK |
975 | break; |
976 | case 'i': | |
ee24e58f | 977 | ctl.useip = 1; |
7ec78229 SK |
978 | break; |
979 | case 'a': | |
ee24e58f | 980 | ctl.altlist = 1; |
7ec78229 SK |
981 | break; |
982 | case 'F': | |
c7eb14d3 | 983 | ctl.time_fmt = LAST_TIMEFTM_CTIME; |
7ec78229 | 984 | break; |
37f534a6 | 985 | case 'p': |
3a4ae395 | 986 | if (parse_timestamp(optarg, &p) < 0) |
37f534a6 | 987 | errx(EXIT_FAILURE, _("invalid time value \"%s\""), optarg); |
83484375 | 988 | ctl.present = (time_t) (p / 1000000); |
37f534a6 | 989 | break; |
dd50168b | 990 | case 's': |
dd50168b SK |
991 | if (parse_timestamp(optarg, &p) < 0) |
992 | errx(EXIT_FAILURE, _("invalid time value \"%s\""), optarg); | |
83484375 | 993 | ctl.since = (time_t) (p / 1000000); |
dd50168b | 994 | break; |
7ec78229 | 995 | case 't': |
3a4ae395 | 996 | if (parse_timestamp(optarg, &p) < 0) |
7ec78229 | 997 | errx(EXIT_FAILURE, _("invalid time value \"%s\""), optarg); |
83484375 | 998 | ctl.until = (time_t) (p / 1000000); |
7ec78229 SK |
999 | break; |
1000 | case 'w': | |
b4b919fe RM |
1001 | if (ctl.name_len < sizeof(((struct utmpx *) 0)->ut_user)) |
1002 | ctl.name_len = sizeof(((struct utmpx *) 0)->ut_user); | |
1003 | if (ctl.domain_len < sizeof(((struct utmpx *) 0)->ut_host)) | |
1004 | ctl.domain_len = sizeof(((struct utmpx *) 0)->ut_host); | |
7ec78229 SK |
1005 | break; |
1006 | case '0': case '1': case '2': case '3': case '4': | |
1007 | case '5': case '6': case '7': case '8': case '9': | |
83484375 | 1008 | ctl.maxrecs = 10 * ctl.maxrecs + c - '0'; |
7ec78229 | 1009 | break; |
45f11b6b SK |
1010 | case OPT_TIME_FORMAT: |
1011 | ctl.time_fmt = which_time_format(optarg); | |
45f11b6b | 1012 | break; |
7ec78229 | 1013 | default: |
677ec86c | 1014 | errtryhelp(EXIT_FAILURE); |
7ec78229 SK |
1015 | } |
1016 | } | |
1017 | ||
1018 | if (optind < argc) | |
83484375 | 1019 | ctl.show = argv + optind; |
7ec78229 | 1020 | |
744c7fec KZ |
1021 | if (!files) { |
1022 | files = xmalloc(sizeof(char *)); | |
1023 | files[nfiles++] = xstrdup(ctl.lastb ? _PATH_BTMP : _PATH_WTMP); | |
7ec78229 SK |
1024 | } |
1025 | ||
744c7fec | 1026 | for (i = 0; i < nfiles; i++) { |
0e5218ac | 1027 | get_boot_time(&ctl.boot_time); |
744c7fec KZ |
1028 | process_wtmp_file(&ctl, files[i]); |
1029 | free(files[i]); | |
7ec78229 | 1030 | } |
744c7fec | 1031 | free(files); |
77079432 | 1032 | return EXIT_SUCCESS; |
6dbe3af9 | 1033 | } |