/*
 * Utmp 'Editor'
 * Hacked together on 03/23/92
 * By Anon E. Mouse
 */

#include <stdio.h>
#include <utmp.h>
void main(argc, argv)
  int argc;
  char **argv;
{
  FILE *fp;
  struct utmp u, temp;
  char file[40];

  if ((argc == 2) || (argc == 3))  {
    strncpy(temp.ut_name, "", 8);
    strncpy(temp.ut_host, "", 16);
    if(argc == 3) strcpy(file, argv[2]);
  } else if((argc == 5) || (argc == 6)) {
    strncpy(temp.ut_name, argv[2], 8);
    strncpy(temp.ut_line, argv[3], 8);
    strncpy(temp.ut_host, argv[4], 16);
    if(argc == 6) strcpy(file, argv[5]);
  } else {
    printf("Usage: %s <tty> [file]\n", argv[0]);
    printf("       %s <tty> [nuser] [ntty] [nhost] [file]\n\n",
	    argv[0]);
    printf("tty   : on this tty\n");
    printf("nuser : new username to change to\n");
    printf("ntty  : new tty to change to\n");
    printf("nhost : new hostname for user\n");
    printf("file  : full pathname of file if not /etc/utmp\n");
    exit(1);
  }

  if(!*file) strcpy(file, "/etc/utmp");
  if((fp=fopen(file, "r+")) == NULL)  {
    printf("Couldn't open %s, aborting!\n", file);
    exit(1);
  }
  while(fread(&u, sizeof(u), 1, fp)) {
    if(!strcmp(u.ut_line, argv[1])) {
      fseek(fp, -sizeof(u), 1);
      fflush(fp);
      temp.ut_time = u.ut_time;
      if(!temp.ut_line[0]) strcpy(temp.ut_line, u.ut_line);
      if(!fwrite(&temp, sizeof(u), 1, fp)) {
	printf("Error writing modification, aborting!\n");
	exit(1);
      } else {
	if(temp.ut_name[0])
	  printf("Changed %.8s on %.8s to: %.8s - %.8s - %.16s\n", u.ut_name,
	          argv[1], argv[2], argv[3], argv[4]);
	else 
	  printf("Removed %s on %s...\n", u.ut_name, argv[1]);
	fclose(fp);
        exit(1);
      }
    }
  }
  printf("No user on %s!\n", argv[1]);
}
