#include <stdio.h>
#include <ctype.h>
#include <utmp.h>
-#include <errno.h>
-#include <malloc.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "c.h"
#include "nls.h"
#include "pathnames.h"
+#include "xalloc.h"
+#include "closestream.h"
#ifndef SHUTDOWN_TIME
# define SHUTDOWN_TIME 254
char **show = NULL; /* What do they want us to show */
char *ufile; /* Filename of this file */
time_t lastdate; /* Last date we've seen */
-char *progname; /* Name of this program */
#if CHOP_DOMAIN
char hostname[256]; /* For gethostbyname() */
char *domainname; /* Our domainname. */
return 0;
o = ((fpos - 1) / UCHUNKSIZE) * UCHUNKSIZE;
if (fseeko(fp, o, SEEK_SET) < 0) {
- fprintf(stderr, "%s: seek failed!\n", progname);
+ warn(_("seek failed: %s"), ufile);
return 0;
}
bpos = (int)(fpos - o);
if (fread(buf, bpos, 1, fp) != 1) {
- fprintf(stderr, "%s: read failed!\n", progname);
+ warn(_("read failed: %s"), ufile);
return 0;
}
fpos = o;
*/
memcpy(tmp + (-bpos), buf, utsize + bpos);
if (fseeko(fp, fpos, SEEK_SET) < 0) {
- perror("fseek");
+ warn(_("seek failed: %s"), ufile);
return 0;
}
* Read another UCHUNKSIZE bytes.
*/
if (fread(buf, UCHUNKSIZE, 1, fp) != 1) {
- perror("fread");
+ warn(_("read failed: %s"), ufile);
return 0;
}
*/
static void int_handler(int sig __attribute__((unused)))
{
- printf("Interrupted %s\n", showdate());
- exit(1);
+ errx(EXIT_FAILURE, _("Interrupted %s"), showdate());
}
/*
*/
static void quit_handler(int sig __attribute__((unused)))
{
- printf("Interrupted %s\n", showdate());
+ warnx(_("Interrupted %s"), showdate());
signal(SIGQUIT, quit_handler);
}
-/*
- * Get the basename of a filename
- */
-static char *mybasename(char *s)
-{
- char *p;
-
- if ((p = strrchr(s, '/')) != NULL)
- p++;
- else
- p = s;
- return p;
-}
-
/*
* Lookup a host with DNS.
*/
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
-
- progname = mybasename(argv[0]);
+ atexit(close_stdout);
while ((c = getopt_long(argc, argv,
"hVf:n:RxadFit:0123456789w", long_opts, NULL)) != -1) {
maxrecs = atoi(optarg);
break;
case 'f':
- if((altufile = malloc(strlen(optarg)+1)) == NULL) {
- fprintf(stderr, "%s: out of memory\n",
- progname);
- exit(1);
- }
- strcpy(altufile, optarg);
+ altufile = xstrdup(optarg);
break;
case 'd':
usedns++;
fulltime++;
break;
case 't':
- if ((until = parsetm(optarg)) == (time_t)-1) {
- fprintf(stderr, "%s: Invalid time value \"%s\"\n",
- progname, optarg);
- usage(stderr);
- }
+ until = parsetm(optarg);
+ if (until == (time_t) -1)
+ errx(EXIT_FAILURE, _("invalid time value \"%s\""), optarg);
break;
case 'w':
if (UT_NAMESIZE > name_len)
/*
* Which file do we want to read?
*/
- if (strcmp(progname, "lastb") == 0) {
- ufile = _PATH_BTMP;
- lastb = 1;
- } else
- ufile = _PATH_WTMP;
- if (altufile)
- ufile = altufile;
+ lastb = !strcmp(program_invocation_short_name, "lastb");
+ ufile = altufile ? altufile : lastb ? _PATH_BTMP : _PATH_WTMP;
+
time(&lastdown);
lastrch = lastdown;
/*
* Open the utmp file
*/
- if ((fp = fopen(ufile, "r")) == NULL) {
- x = errno;
- fprintf(stderr, "%s: %s: %s\n", progname, ufile, strerror(errno));
- if (altufile == NULL && x == ENOENT)
- fprintf(stderr, "Perhaps this file was removed by the "
- "operator to prevent logging %s info.\n", progname);
- exit(1);
- }
+ if ((fp = fopen(ufile, "r")) == NULL)
+ err(EXIT_FAILURE, _("cannot open %s"), ufile);
/*
* Optimize the buffer size.
*/
if (ut.ut_line[0] == 0)
break;
- if ((p = malloc(sizeof(struct utmplist))) == NULL) {
- fprintf(stderr, "%s: out of memory\n",
- progname);
- exit(1);
- }
+ p = xmalloc(sizeof(struct utmplist));
memcpy(&p->ut, &ut, sizeof(struct utmp));
p->next = utmplist;
p->prev = NULL;
down = 0;
}
}
- printf("\n%s begins %s", mybasename(ufile), ctime(&begintime));
+ printf(_("\n%s begins %s"), basename(ufile), ctime(&begintime));
fclose(fp);
-
- /*
- * Should we free memory here? Nah. This is not NT :)
- */
- return 0;
+ return EXIT_SUCCESS;
}