ubase

suckless linux base utils
git clone git://git.suckless.org/ubase
Log | Files | Refs | README | LICENSE

commit ef78f20dd8dfbe9c72e0477fc620c31ec852d7e1
parent 1aaec6250a5ca8949e30147259f9316d87eb93b5
Author: sin <sin@2f30.org>
Date:   Mon, 18 Aug 2014 22:02:22 +0100

Use agetline() in lastlog(8)

Some other minor changes as well.

Diffstat:
Mlastlog.c | 14+++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/lastlog.c b/lastlog.c @@ -8,6 +8,7 @@ #include <time.h> #include <utmp.h> +#include "text.h" #include "util.h" #define PASSWD "/etc/passwd" @@ -34,7 +35,7 @@ lastlog(char *user) fread(&ll, sizeof(struct lastlog), 1, last); if (ferror(last)) - eprintf("error reading lastlog\n"); + eprintf("%s: read error:", _PATH_LASTLOG); /* on glibc `ll_time' can be an int32_t with compat32 * avoid compiler warning when calling ctime() */ @@ -47,7 +48,8 @@ int main(int argc, char **argv) { FILE *fp; - char line[512], *p; + char *line = NULL, *p; + size_t sz = 0; if ((last = fopen(_PATH_LASTLOG, "r")) == NULL) eprintf("fopen %s:", _PATH_LASTLOG); @@ -58,17 +60,19 @@ main(int argc, char **argv) } else { if ((fp = fopen(PASSWD, "r")) == NULL) eprintf("fopen %s:", PASSWD); - while ((fgets(line, sizeof(line), fp)) != NULL) { + while (agetline(&line, &sz, fp) != -1) { if ((p = strchr(line, ':')) == NULL) eprintf("invalid passwd entry\n"); *p = '\0'; lastlog(line); } if (fclose(fp)) - weprintf("fclose %s:", PASSWD); + eprintf("fclose %s:", PASSWD); + free(line); } - fclose(last); + if (fclose(last)) + eprintf("fclose %s:", _PATH_LASTLOG); return EXIT_SUCCESS; }