// Convert text to wtmp // Example text: // 12558dvi 2002-01-03 00:07:52.000 2002-01-03 00:12:06.000 line206 #include #include #include #include #include #include // #include #include #include #include #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, <%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); } }