/* Apb Message */
/* (c)1992, 1993 by Derek Snider */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <utmp.h>
#include <sys/time.h>
#include <time.h>

void main(argc, argv)
         char **argv;
{
  FILE *fpin, *fpout;
  struct utmp buff;
  int num;
  struct tm tmm;
  time_t tim;
  char *device;
  char devpath[20];
  char *user;
  char TempID[9];

  if (argc < 2) {
     puts("Usage: apb Message");
     puts("Sends a message to all available nodes.");
     puts("(c) 1993 D.S.D. Software, Written by Derek Snider.");
     exit();
  }

  fpin=fopen(UTMP_FILE,"r");
  user=getlogin();

  while ((num=fread(&buff, sizeof(buff), 1, fpin)) != 0) {
       if (buff.ut_type == USER_PROCESS)
       {
         strcpy(devpath,"/dev/");
         device=strcat(devpath,buff.ut_line);
         if (access(device,W_OK) == 0) {
            fpout = fopen(device,"w");
            fprintf(fpout,"\n*** APB:");
            for (num = 1; num < argc; num++)
                fprintf(fpout," %s",argv[num]);
            fprintf(fpout," *** (From: %s)%c\n",user,7); 
            fclose(fpout);
         }
       }
  }
  num=fclose(fpin);

  if ((fpout = fopen("/usr/local/lib/apb/apblog","a")) != NULL) {
     tim=time(0);
     tmm=*gmtime(&tim);
     fprintf(fpout,"*** APB:");
     for (num = 1; num < argc; num++)
         fprintf(fpout," %s",argv[num]);
     fprintf(fpout," *** (From: %s)\nSent at: %02d:%02d:%02d %02d/%02d/%02d\n",
         user,tmm.tm_hour,tmm.tm_min,tmm.tm_sec,tmm.tm_mday,tmm.tm_mon+1,
         tmm.tm_year); 
     fclose(fpout);
  }
}
