/*----------------------------------------------------------------------- 
 * Description: Source of the program . /equipe/sysadmin/bin/wtmpcut  
 *				which permits the reduction of the file /var/adm/wtmp 
 *				by removing entries older than the date specified.
 *				The number of days prior to today is the only
 *				pararamter.  If no paramter is specified, a default
 *				of DELAY is used.
 *----------------------------------------------------------------------*/ 
 
#include <stdio.h> 
#include <sys/types.h> 
#include <fcntl.h>
#include <utmp.h> 
#include <time.h> 
#include <errno.h> 
#include <unistd.h> 

#define ORIGFILE "/var/adm/wtmp"
#define TEMPFILE "/export/local/sbin/wtmp_temp"
#define DELFILE "/export/local/sbin/wtmp_del"
 
#define DELAY  (time_t) (45)                           /* in days */ 
 
int main(argc, argv) 
int argc; 
char **argv; 
{ 
  struct  utmp wtmp_rec; 
  int     old_fd, new_fd, del_fd; 
  int     wtmp_rec_sz = sizeof(struct utmp); 
  time_t  periode, temps_limite; 
  char garbage [80];
 
 
 
  if (argc > 1) 
      periode = (time_t) atoi(argv[1]); 
  else 
      periode = DELAY; 
 
  /* 
   * Copy /var/adm/wtmp to /tmp/wtmp 
   */ 
  putenv("IFS=' \t\n'");                        /* Security */ 
  putenv("PATH=/usr/bin");                      /* Security */ 
  sprintf (garbage, "cp %s %s", ORIGFILE, TEMPFILE);
  if (system(garbage) < 0) { 
    fprintf(stderr,"%s: Error in copying %s into %s.\nBye.\n",argv[0],
		ORIGFILE, TEMPFILE); 
    exit(1); 
  } /* if */ 
 
  /* 
   *  Calculate the time limit in time_t   
   */ 
  temps_limite = time(NULL) - (periode * 24 * 3600); 
  fprintf(stdout, 
  "\n%s: The Date Limits is %s\n", argv[0], asctime(localtime(&temps_limite))); 
 
  /* 
   *  Open the new file for writing
   */ 
  if ((new_fd = open(ORIGFILE, O_WRONLY | O_TRUNC)) < 0) { 
    fprintf(stderr, "%s: Error %d in opening %s .\nBye.\n", 
            argv[0], errno, ORIGFILE); 
    exit(1); 
  } /* if */ 
 
  /* 
   *  Open the new del file for writing
   */ 
  if ((del_fd = open(DELFILE, O_WRONLY | O_TRUNC | O_CREAT )) < 0) { 
    fprintf(stderr, "%s: Error %d in opening %s .\nBye.\n", 
            argv[0], errno, DELFILE); 
    exit(1); 
  } /* if */ 
 
  /* 
   *  Open the copy of wtmp for reading
   */ 
  if ((old_fd = open(TEMPFILE, O_RDONLY)) < 0) { 
    fprintf(stderr, "%s: Error %d in opening %s.\nBye.\n", 
            argv[0], errno, TEMPFILE); 
    exit(1); 
  } /* if */ 
 
  /* 
   * Until the end of file, compare dates, if older, Put them in del file
   * When they are in the period, put them in /var/adm/wtmp. 
   */ 
  while (read(old_fd, &wtmp_rec, wtmp_rec_sz) == wtmp_rec_sz) { 
    if (wtmp_rec.ut_time >= temps_limite) { 
        if (write(new_fd, &wtmp_rec, wtmp_rec_sz) != wtmp_rec_sz) { 
          fprintf(stderr, "%s: Error %d n writing the record.\nBye.",  
                  argv[0], errno); 
          exit(1); 
        } /* if */ 
    } /* if */ 
	else /* Del File Written */
	{
        if (write(del_fd, &wtmp_rec, wtmp_rec_sz) != wtmp_rec_sz) { 
          fprintf(stderr, "%s: Error %d n writing the record.\nBye.",  
                  argv[0], errno); 
          exit(1); 
        } /* if */ 
	}
  } /* while */ 
  unlink ( TEMPFILE );
  exit(0);                                      /* Nice Exit */
 
} /* main() */ 

