/* RemUser v1.00 - Removes entires from log files                          */
/*                                                                         */
/* Written by: Harlequin  03/08/96 (UK)                                    */
/* Compiles Under: Unix (CC/GCC) and DOS (tested with Turbo C)             */
/*                                                                         */
/* RemUser will remove entries from log files. This would be most useful   */
/* for removing user records from utmp/wtmp/lastlog files, but it can be   */
/* used for almost any log file as long as: all the records are the same   */
/* size and the username is in ASCII.                                      */
/* This will initially compile to remove records from BSD utmp/wtmp files  */
/* which will work on SunOS, BSD, FreeBSD plus others.                     */
/* Initial values are RECSIZE 36, NAMEST 8, NAMELN 8. To compile it to     */
/* remove records from another format of file (or another platforms utmp   */
/* file for example) change the define values as follows:                  */
/*                                                                         */
/* RECSIZE is the size in bytes of a single record in the file             */
/* NAMEST is the offset of the username field from the beginning of the    */
/*  record MINUS 1, so if the username starts at byte 9 of the record this */
/*  value should be 8 !                                                    */
/* NAMELN is the length of the username field in bytes                     */
/*                                                                         */
/* For utmp/wtmp files this information can be gained from the utmp man    */
/* files. This program will output the file to stdout, so redirect or pipe */
/* it otherwise it will be dumped to your screen which can be messy.       */


#include <stdio.h>
#include <sys/stat.h>

#define RECSIZE 36              /* utmp record size             36bytes   */
#define NAMEST 8                /* Offset of 'username' in file >minus 1< */
#define NAMELN 8                /* Length of 'username'                   */

FILE *instream;
struct stat statbuf;
char rec_array[RECSIZE];


void display_help()
{
 fprintf(stderr,"\r\nRemUser v1.00");
 fprintf(stderr,"\r\nusage: remuser [utmp/wtmp file] [user name]");
 fprintf(stderr,"\r\nWarning ! This program outputs to stdout. Redirect or pipe");
 fprintf(stderr,"\r\nif you don't want the raw log file dumped to your screen.\r\n");
}


init(argv)

char *argv[];

{
 if ((instream=fopen(argv[1],"rb"))==NULL)  /* Open in binary mode */
 {
  perror("Error");
  return(1);
 }
 else
 {
  fstat(fileno(instream),&statbuf);
  return(0);
 }
}


void process_utmp(argv)

char *argv[];

{
 short founduser=0;
 int count=0;
 long bytes_read=0;
 char username[NAMELN];

 while (bytes_read<statbuf.st_size)
 {
  rec_array[count]=fgetc(instream);
  if (count>=NAMEST && count<NAMEST+NAMELN)
   username[count-NAMEST]=rec_array[count];
  count++;
  if (count==RECSIZE)
  {
   bytes_read=bytes_read+RECSIZE;
   if (strcmp(username,argv[2])!=0)
    for (count=0; count<RECSIZE; count++)
     putchar(rec_array[count]);
   else
    founduser=255;
   count=0;
  }
 }
 if (founduser==0)
  fprintf(stderr,"\nThere was no record of user [%s] in logfile [%s]\n",argv[2],argv[1]);
}


void terminate()
{
 fclose(instream);
}


void main(argc, argv)

int argc;
char *argv[];

{
 if (argc!=3)
  display_help();
 else
 {
  if (init(argv)==0)
  {
   process_utmp(argv);
   terminate();
  }
 }
}