/* Call UserID Message */
/* (c)1992 by Derek Snider */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <utmp.h>

void main(argc, argv)
         char **argv;
{
  FILE *fpin, *fpout;
  struct utmp buff;
  int num;
  char *device;
  char online = 0;
  char devpath[20];
  char *user;
  char TempID[9];

  if (argc < 3) {
     puts("Usage: call UserID Message");
     puts("Sends a message to a user's terminal.");
     puts("(c) 1993 D.S.D. Software, Written by Derek Snider.");
     exit();
  }

  fpin=fopen(UTMP_FILE,"r");
  user=getlogin();   /*getuinfo("LOGNAME");*/

  while (fread(&buff, sizeof(buff), 1, fpin)!=0) {
       strcpy(TempID,buff.ut_user);
       TempID[8]='\0';
       if (strcmp(TempID, argv[1]) == 0)
       {
         strcpy(devpath,"/dev/");
         device=strcat(devpath,buff.ut_line);
         online = 1;
         if (access(device,W_OK) == 0) {
            fpout = fopen(device,"w");
            fprintf(fpout,"\n***");
            for (num = 2; num < argc; num++)
                fprintf(fpout," %s",argv[num]);
            fprintf(fpout," *** (From: %s)%c\n",user,7); 
         }
         else 
            printf("User %s is refusing messages on port %s.\n",argv[1],buff.ut_line);
       }
  }
  if (online != 1)
     printf("User %s is not on the system.\n",argv[1]);
  num=fclose(fpin);
}
