lastlog.c (1511B)
1 /* see LICENSE file for copyright and license details */ 2 3 #include <errno.h> 4 #include <pwd.h> 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <string.h> 8 #include <time.h> 9 10 #include "paths.h" 11 #include "session.h" 12 #include "text.h" 13 #include "util.h" 14 15 #define PASSWD "/etc/passwd" 16 17 static FILE *last; 18 19 static void 20 lastlog(char *user) 21 { 22 struct passwd *pwd; 23 struct SessionEntry e; 24 25 errno = 0; 26 if ((pwd = getpwnam(user)) == NULL) { 27 if (errno) 28 weprintf("getpwnam %s:", user); 29 else 30 weprintf("unknown user: %s\n", user); 31 return; 32 } 33 34 if (session_lastlog(last, pwd->pw_uid, &e) < 0) 35 eprintf("%s: read error:", ARUU_PATH_LASTLOG); 36 printf("%-8.8s %-8.8s %-16.16s %s", user, e.line, e.host, ctime(&e.time)); 37 } 38 39 // ?man lastlog: report recent logins 40 // ?man display the most recent login times of users 41 int 42 main(int argc, char **argv) 43 { 44 FILE *fp; 45 char *line = NULL, *p; 46 size_t sz = 0; 47 48 if ((last = fopen(ARUU_PATH_LASTLOG, "r")) == NULL) 49 eprintf("fopen %s:", ARUU_PATH_LASTLOG); 50 51 if (argc > 1) { 52 while (*++argv) 53 lastlog(*argv); 54 } else { 55 if ((fp = fopen(PASSWD, "r")) == NULL) 56 eprintf("fopen %s:", PASSWD); 57 while (agetline(&line, &sz, fp) != -1) { 58 if ((p = strchr(line, ':')) == NULL) 59 eprintf("invalid passwd entry\n"); 60 *p = '\0'; 61 lastlog(line); 62 } 63 if (fclose(fp)) 64 eprintf("fclose %s:", PASSWD); 65 free(line); 66 } 67 68 if (fclose(last)) 69 eprintf("fclose %s:", ARUU_PATH_LASTLOG); 70 71 return 0; 72 }