当前位置: 技术问答>linux和unix
怎样shell提取wtmp中的时间?
来源: 互联网 发布时间:2016-11-26
本文导语: wtmp是二进制文件,要怎样提取出当中存储的时间? struct utmp { char ut_line[8]; /* tty line: "ttyh0", "ttyd0", "ttyp0", ... */ char ut_name[8]; /* login name */ long ut_time; /* seconds since Epoch */ }; ...
wtmp是二进制文件,要怎样提取出当中存储的时间?
struct utmp {
char ut_line[8]; /* tty line: "ttyh0", "ttyd0", "ttyp0", ... */
char ut_name[8]; /* login name */
long ut_time; /* seconds since Epoch */
};
struct utmp {
char ut_line[8]; /* tty line: "ttyh0", "ttyd0", "ttyp0", ... */
char ut_name[8]; /* login name */
long ut_time; /* seconds since Epoch */
};
|
last.c
/*
* last.c Re-implementation of the 'last' command, this time
* for Linux. Yes I know there is BSD last, but I
* just felt like writing this. No thanks :-).
* Also, this version gives lots more info (especially with -x)
*
* Author: Miquel van Smoorenburg, miquels@cistron.nl
*
* Version: @(#)last 2.85 30-Jul-2004 miquels@cistron.nl
*
* This file is part of the sysvinit suite,
* Copyright 1991-2004 Miquel van Smoorenburg.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "oldutmp.h"
#ifndef SHUTDOWN_TIME
# define SHUTDOWN_TIME 254
#endif
char *Version = "@(#) last 2.85 31-Apr-2004 miquels";
#define CHOP_DOMAIN 0 /* Define to chop off local domainname. */
#define NEW_UTMP 1 /* Fancy & fast utmp read code. */
#define UCHUNKSIZE 16384 /* How much we read at once. */
/* Double linked list of struct utmp's */
struct utmplist {
struct utmp ut;
struct utmplist *next;
struct utmplist *prev;
};
struct utmplist *utmplist = NULL;
/* Types of listing */
#define R_CRASH 1 /* No logout record, system boot in between */
#define R_DOWN 2 /* System brought down in decent way */
#define R_NORMAL 3 /* Normal */
#define R_NOW 4 /* Still logged in */
#define R_REBOOT 5 /* Reboot record. */
#define R_PHANTOM 6 /* No logout record but session is stale. */
#define R_TIMECHANGE 7 /* NEW_TIME or OLD_TIME */
/* Global variables */
int maxrecs = 0; /* Maximum number of records to list. */
int recsdone = 0; /* Number of records listed */
int showhost = 1; /* Show hostname too? */
int altlist = 0; /* Show hostname at the end. */
int usedns = 0; /* Use DNS to lookup the hostname. */
int useip = 0; /* Print IP address in number format */
int oldfmt = 0; /* Use old libc5 format? */
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. */
#endif
/*
* Convert old utmp format to new.
*/
void uconv(struct oldutmp *oldut, struct utmp *utn)
{
memset(utn, 0, sizeof(struct utmp));
utn->ut_type = oldut->ut_type;
utn->ut_pid = oldut->ut_pid;
utn->ut_time = oldut->ut_oldtime;
utn->ut_addr = oldut->ut_oldaddr;
strncpy(utn->ut_line, oldut->ut_line, OLD_LINESIZE);
strncpy(utn->ut_user, oldut->ut_user, OLD_NAMESIZE);
strncpy(utn->ut_host, oldut->ut_host, OLD_HOSTSIZE);
}
#if NEW_UTMP
/*
* Read one utmp entry, return in new format.
* Automatically reposition file pointer.
*/
int uread(FILE *fp, struct utmp *u, int *quit)
{
static int utsize;
static char buf[UCHUNKSIZE];
char tmp[1024];
static off_t fpos;
static int bpos;
struct oldutmp uto;
int r;
off_t o;
if (quit == NULL && u != NULL) {
/*
* Normal read.
*/
if (oldfmt) {
r = fread(&uto, sizeof(uto), 1, fp);
uconv(&uto, u);
} else
r = fread(u, sizeof(struct utmp), 1, fp);
return r;
}
if (u == NULL) {
/*
* Initialize and position.
*/
utsize = oldfmt ? sizeof(uto) : sizeof(struct utmp);
fseeko(fp, 0, SEEK_END);
fpos = ftello(fp);
if (fpos == 0)
return 0;
o = ((fpos - 1) / UCHUNKSIZE) * UCHUNKSIZE;
if (fseeko(fp, o, SEEK_SET) = 0) {
if (oldfmt)
uconv((struct oldutmp *)(buf + bpos), u);
else
memcpy(u, buf + bpos, sizeof(struct utmp));
return 1;
}
/*
* Oops we went "below" the buffer. We should be able to
* seek back UCHUNKSIZE bytes.
*/
fpos -= UCHUNKSIZE;
if (fpos