[hackers] [ubase] Convert lastlog(8) to {w,}eprintf() || sin
commit 1aaec6250a5ca8949e30147259f9316d87eb93b5
Author: sin <sin_AT_2f30.org>
Date: Mon Aug 18 21:55:38 2014 +0100
Convert lastlog(8) to {w,}eprintf()
diff --git a/lastlog.c b/lastlog.c
index 0c3cefa..1a27ae4 100644
--- a/lastlog.c
+++ b/lastlog.c
_AT_@ -1,4 +1,5 @@
/* See LICENSE file for copyright and license details. */
+#include <errno.h>
#include <paths.h>
#include <pwd.h>
#include <stdio.h>
_AT_@ -7,6 +8,8 @@
#include <time.h>
#include <utmp.h>
+#include "util.h"
+
#define PASSWD "/etc/passwd"
static FILE *last;
_AT_@ -18,18 +21,20 @@ lastlog(char *user)
struct lastlog ll;
time_t lltime;
+ errno = 0;
if ((pwd = getpwnam(user)) == NULL) {
- fprintf(stderr, "unknown user: %s
", user);
+ if (errno)
+ weprintf("getpwnam %s:", user);
+ else
+ weprintf("unknown user: %s
", user);
return;
}
fseek(last, pwd->pw_uid * sizeof(struct lastlog), 0);
fread(&ll, sizeof(struct lastlog), 1, last);
- if (ferror(last)) {
- perror("error reading lastlog");
- exit(EXIT_FAILURE);
- }
+ if (ferror(last))
+ eprintf("error reading lastlog
");
/* on glibc `ll_time' can be an int32_t with compat32
* avoid compiler warning when calling ctime() */
_AT_@ -44,29 +49,23 @@ main(int argc, char **argv)
FILE *fp;
char line[512], *p;
- if ((last = fopen(_PATH_LASTLOG, "r")) == NULL) {
- perror(_PATH_LASTLOG);
- exit(EXIT_FAILURE);
- }
+ if ((last = fopen(_PATH_LASTLOG, "r")) == NULL)
+ eprintf("fopen %s:", _PATH_LASTLOG);
if (argc > 1) {
while (*++argv)
lastlog(*argv);
} else {
- if ((fp = fopen(PASSWD, "r")) == NULL) {
- perror(PASSWD);
- exit(EXIT_FAILURE);
- }
+ if ((fp = fopen(PASSWD, "r")) == NULL)
+ eprintf("fopen %s:", PASSWD);
while ((fgets(line, sizeof(line), fp)) != NULL) {
- if ((p = strchr(line, ':')) == NULL) {
- fputs("incorrect password file", stderr);
- exit(-1);
- }
+ if ((p = strchr(line, ':')) == NULL)
+ eprintf("invalid passwd entry
");
*p = '
Received on Mon Aug 18 2014 - 22:56:51 CEST
This archive was generated by hypermail 2.3.0
: Mon Aug 18 2014 - 23:00:14 CEST