Skip to content

Commit

Permalink
last: Add support for time-format option
Browse files Browse the repository at this point in the history
  • Loading branch information
Nykseli committed Jan 3, 2024
1 parent 1c9247a commit e34c5a2
Showing 1 changed file with 44 additions and 5 deletions.
49 changes: 44 additions & 5 deletions src/wtmpdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@

static char *wtmpdb_path = _PATH_WTMPDB;

#define TIMEFMT_CTIME 1
#define TIMEFMT_SHORT 2
#define TIMEFMT_HHMM 3
#define TIMEFMT_CTIME 1
#define TIMEFMT_SHORT 2
#define TIMEFMT_HHMM 3
#define TIMEFMT_NOTIME 4
#define TIMEFMT_ISO 5

#define TIMEFMT_VALUE 255

#define LOGROTATE_DAYS 60

Expand Down Expand Up @@ -153,6 +157,21 @@ parse_time (const char *str, time_t *arg)
return 0;
}

static int
time_format (const char *fmt)
{
if (strcmp (fmt, "notime") == 0)
return TIMEFMT_NOTIME;
if (strcmp (fmt, "short") == 0)
return TIMEFMT_SHORT;
if (strcmp (fmt, "full") == 0)
return TIMEFMT_CTIME;
if (strcmp (fmt, "iso") == 0)
return TIMEFMT_ISO;

return -1;
}

static void
format_time (int fmt, char *dst, size_t dstlen, uint64_t time)
{
Expand All @@ -179,6 +198,16 @@ format_time (int fmt, char *dst, size_t dstlen, uint64_t time)
strftime (dst, dstlen, "%H:%M", tm);
break;
}
case TIMEFMT_ISO:
{
time_t t = (time_t)time;
struct tm *tm = localtime (&t);
strftime (dst, dstlen, "%FT%T%z", tm); /* Same ISO8601 format original last command uses */
break;
}
case TIMEFMT_NOTIME:
*dst = '\0';
break;
default:
abort ();
}
Expand Down Expand Up @@ -596,8 +625,10 @@ main_last (int argc, char **argv)
{"since", required_argument, NULL, 's'},
{"system", no_argument, NULL, 'x'},
{"until", required_argument, NULL, 'u'},
{"time-format", required_argument, NULL, TIMEFMT_VALUE},
{NULL, 0, NULL, '\0'}
};
int time_fmt = TIMEFMT_CTIME;
char *error = NULL;
int c;

Expand Down Expand Up @@ -659,6 +690,14 @@ main_last (int argc, char **argv)
case 'x':
xflag = 1;
break;
case TIMEFMT_VALUE:
time_fmt = time_format (optarg);
if (time_fmt == -1)
{
fprintf (stderr, "Invalid time format '%s'\n", optarg);
exit (EXIT_FAILURE);
}
break;
default:
usage (EXIT_FAILURE);
break;
Expand Down Expand Up @@ -707,10 +746,10 @@ main_last (int argc, char **argv)

if (wtmp_start == UINT64_MAX)
printf ("%s has no entries\n", wtmpdb_path);
else
else if (time_fmt != TIMEFMT_NOTIME)
{
char wtmptime[32];
format_time (TIMEFMT_CTIME, wtmptime, sizeof (wtmptime),
format_time (time_fmt, wtmptime, sizeof (wtmptime),
wtmp_start/USEC_PER_SEC);
printf ("\n%s begins %s\n", wtmpdb_path, wtmptime);
}
Expand Down

0 comments on commit e34c5a2

Please sign in to comment.