// Convert text to wtmp
// Example text:
// 12558dvi 2002-01-03 00:07:52.000 2002-01-03 00:12:06.000 line206
#include <sys/param.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <stdio.h>
// #include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <utmp.h>
#define MAX_FULL_LEN MAXHOSTNAMELEN+MAXPATHLEN
int flag_d=0;
char dsp[] = "-";
char path_r[MAX_FULL_LEN];
char path_w[MAX_FULL_LEN];
struct couple { int r; struct utmp i,o; };
void logger(struct utmp ut, char *path)
{
register int fd;
// if (flag_d) printf(" path=<%s>.\n",path);
if ((fd=open(path, O_CREAT|O_WRONLY|O_APPEND, 0)) >= 0)
{
(void) write(fd, &ut, sizeof(ut));
(void) close(fd);
}
}
void seeker(FILE **fd, int suphl)
{
int i,sc;
for (i=0; i<suphl; i++) while ((sc=fgetc(*fd)) != 10);
}
time_t mkdate(char *c_date, char *c_time)
{
time_t td;
struct tm t;
t.tm_year=atoi(strtok(c_date,dsp))-1900;
t.tm_mon=atoi(strtok(NULL,dsp))-1;
t.tm_mday=atoi(strtok(NULL,dsp));
t.tm_hour=atoi(strtok(c_time,":"));
t.tm_min=atoi(strtok(NULL,":"));
t.tm_sec=atoi(strtok(NULL,":"));
td=mktime(&t);
return(td);
}
struct couple getrow(FILE *fd)
{
char uname[UT_NAMESIZE],date_s[11],date_f[11],time_s[13],time_f[13],tline[UT_LINESIZE];
struct couple c;
fscanf(fd,"%s",uname);
if (uname[0]=='(') { c.r=0; return(c); }
else {
fscanf(fd,"%s",date_s);
fscanf(fd,"%s",time_s);
fscanf(fd,"%s",date_f);
fscanf(fd,"%s",time_f);
fscanf(fd,"%s",tline);
if (flag_d) printf("<%s>, <%s><%s>, <%s><%s>, <%s>\n",uname,date_s,time_s,date_f,time_f,tline);
strcpy(c.i.ut_name,uname); if (flag_d) printf("U ");
strcpy(c.i.ut_line,tline); if (flag_d) printf("L ");
strcpy(c.i.ut_host,""); c.i.ut_time=mkdate(date_s,time_s); if (flag_d>1) printf("I ");
strcpy(c.o.ut_name,""); strcpy(c.o.ut_line,tline);
strcpy(c.o.ut_host,""); c.o.ut_time=mkdate(date_f,time_f);
c.r=2; return(c);
}
}
void usage()
{
printf("Usage: t2w [-d datesplitter] [-h headlines] [-v] [-w path_to_wtmp] path_to_text\n");
exit(1);
}
int main(int argc, char *argv[])
{
int c, hlines=2;
char u_line[UT_LINESIZE];
char u_name[UT_NAMESIZE];
struct couple ws;
FILE *fdr;
while ((c = getopt(argc, argv, "?d:h:v:w:")) != -1)
switch (c)
{
case '?': usage();
case 'd': strncpy(dsp,optarg,sizeof(dsp)-1); break;
case 'h': hlines = atoi(optarg); break;
case 'v': flag_d = atoi(optarg); break;
case 'w': strncpy(path_w,optarg,sizeof(path_w)-1); break;
default:
usage();
}
argc -= optind; argv += optind;
if (*argv) {
strncpy(path_r,*argv,sizeof(path_r)-1);
if (flag_d) printf("Source text file = <%s>\nTarget wtmp file = ",path_r);
if (strcmp(path_w,"")) { if (flag_d) printf("<%s>\n",path_w); }
else {
getwd(path_w);
strcat(path_w,"/"); strcat(path_w,path_r); strcat(path_w,".wtmp");
if (flag_d) printf("<%s> (default)\n",path_w);
}
if ((fdr=fopen(path_r,"r")) != NULL) {
seeker(&fdr,hlines);
while (!feof(fdr)) {
ws = getrow(fdr);
if (ws.r==0) break;
if (flag_d) printf("WS <%s>, <%s>, <%s>.\n",ws.i.ut_name,ctime(&(ws.i.ut_time)),ws.i.ut_line);
logger(ws.i,path_w);
logger(ws.o,path_w);
}
fclose(fdr);
}
else { printf("ERROR: File could not be opened!\n"); exit(1); }
}
else { usage(); exit(1); }
}
syntax highlighted by Code2HTML, v. 0.9.1