Convert lastlog(8) to {w,}eprintf()

This commit is contained in:
sin 2014-08-18 21:55:38 +01:00
parent 6af24e35f9
commit 1aaec6250a
2 changed files with 18 additions and 19 deletions

View File

@ -1,4 +1,5 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <errno.h>
#include <paths.h> #include <paths.h>
#include <pwd.h> #include <pwd.h>
#include <stdio.h> #include <stdio.h>
@ -7,6 +8,8 @@
#include <time.h> #include <time.h>
#include <utmp.h> #include <utmp.h>
#include "util.h"
#define PASSWD "/etc/passwd" #define PASSWD "/etc/passwd"
static FILE *last; static FILE *last;
@ -18,18 +21,20 @@ lastlog(char *user)
struct lastlog ll; struct lastlog ll;
time_t lltime; time_t lltime;
errno = 0;
if ((pwd = getpwnam(user)) == NULL) { if ((pwd = getpwnam(user)) == NULL) {
fprintf(stderr, "unknown user: %s\n", user); if (errno)
weprintf("getpwnam %s:", user);
else
weprintf("unknown user: %s\n", user);
return; return;
} }
fseek(last, pwd->pw_uid * sizeof(struct lastlog), 0); fseek(last, pwd->pw_uid * sizeof(struct lastlog), 0);
fread(&ll, sizeof(struct lastlog), 1, last); fread(&ll, sizeof(struct lastlog), 1, last);
if (ferror(last)) { if (ferror(last))
perror("error reading lastlog"); eprintf("error reading lastlog\n");
exit(EXIT_FAILURE);
}
/* on glibc `ll_time' can be an int32_t with compat32 /* on glibc `ll_time' can be an int32_t with compat32
* avoid compiler warning when calling ctime() */ * avoid compiler warning when calling ctime() */
@ -44,29 +49,23 @@ main(int argc, char **argv)
FILE *fp; FILE *fp;
char line[512], *p; char line[512], *p;
if ((last = fopen(_PATH_LASTLOG, "r")) == NULL) { if ((last = fopen(_PATH_LASTLOG, "r")) == NULL)
perror(_PATH_LASTLOG); eprintf("fopen %s:", _PATH_LASTLOG);
exit(EXIT_FAILURE);
}
if (argc > 1) { if (argc > 1) {
while (*++argv) while (*++argv)
lastlog(*argv); lastlog(*argv);
} else { } else {
if ((fp = fopen(PASSWD, "r")) == NULL) { if ((fp = fopen(PASSWD, "r")) == NULL)
perror(PASSWD); eprintf("fopen %s:", PASSWD);
exit(EXIT_FAILURE);
}
while ((fgets(line, sizeof(line), fp)) != NULL) { while ((fgets(line, sizeof(line), fp)) != NULL) {
if ((p = strchr(line, ':')) == NULL) { if ((p = strchr(line, ':')) == NULL)
fputs("incorrect password file", stderr); eprintf("invalid passwd entry\n");
exit(-1);
}
*p = '\0'; *p = '\0';
lastlog(line); lastlog(line);
} }
if (fclose(fp)) if (fclose(fp))
perror(PASSWD); weprintf("fclose %s:", PASSWD);
} }
fclose(last); fclose(last);

View File

@ -79,7 +79,7 @@ main(int argc, char *argv[])
pw = getpwnam(user); pw = getpwnam(user);
if (!pw) { if (!pw) {
if (errno) if (errno)
eprintf("getpwnam: %s:", user); eprintf("getpwnam %s:", user);
else else
eprintf("who are you?\n"); eprintf("who are you?\n");
} }