/*
 * This file is part of an snmp query application.
 * This file contains miscellaneous utilities.
 *
 * Copyright 1990 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 *
 * Tom Coppeto
 * MIT Network Services
 * 15 April 1990
 *
 *    $Source: /afs/.net/tools/src/simon/RCS/utils.c,v $
 *    $Author: tom $
 *    $Locker: nocuser $
 *    $Log: utils.c,v $
 * Revision 1.3  1991/02/21  13:55:33  tom
 * *** empty log message ***
 *
 * Revision 1.2  90/07/20  23:48:06  tom
 * *** empty log message ***
 * 
 * Revision 1.1  90/07/20  21:17:52  tom
 * Initial revision
 * 
 *
 */

#ifndef lint
static char *rcsid = "$Header: /afs/.net/tools/src/simon/RCS/utils.c,v 1.3 1991/02/21 13:55:33 tom Exp nocuser $";
#endif

#define _BSD_COMPAT

#include <stdio.h>
#include <signal.h>
#include <ctype.h>
#include <simon.h>
#include <mit-copyright.h>



static char lbuf[BUF_SIZE];
extern int errno;
static int ding;
int alarm_handler();

/*
 * Function:    display_file() 
 * Decsription: displays given file with program given by pager
 * Returns:     SUCCESS/ERROR
 */

int
display_file(pager, filename)
     char *pager;
     char *filename;
{
  FILE *fp;

  fp = fopen(filename, "r");
  if(fp == NULL)
    {
      (void) fprintf(stderr, "Unable to open file: %s\n", filename);
      return(ERROR);
    }

  if (call_program(pager, filename) == ERROR)
    {
      while(fgets(lbuf, BUF_SIZE, fp) != (char *)NULL)
	(void) printf("%s", lbuf);
      (void) printf("\n");
    }

  (void) fclose(fp);
  return(SUCCESS);
}
  

/*
 * Function:    call_program() 
 * Decsription: executes given program with argument- waits for completion
 * Returns:     SUCCESS/ERROR
 */


int
call_program(program, argument)
     char *program;             
     char *argument;            
{
  int pid;              
  int (*func)();

  if ((pid = fork()) == -1)
    {
      perror("call_program");
      return(ERROR);
    }
  else
    if (pid == 0)
      {
        (void) execlp(program, program, argument, 0);
        perror("call_program");
        return(ERROR);
      }
    else
      {
        func = signal(SIGINT, SIG_IGN);
        while (wait(0) != pid)
          {
                        /* ho hum ... (yawn) */
            /* tap tap */
          };
        signal(SIGINT, func);
        return(SUCCESS);
      }
}


	
timer(sec, usec)
     long sec,usec;
{
  static struct itimerval it;
  static int init = 0;

  if(!init)
    {
      bzero((char *) &it, sizeof(it));
      init = TRUE;
    }

  it.it_value.tv_sec = sec;
  it.it_value.tv_usec = usec;
  ding = 0;

  (void) signal(SIGALRM, alarm_handler);
  (void) setitimer(ITIMER_REAL, &it, (struct itimerval *)0);

  while (1)
    {
      (void) sigblock(sigmask(SIGALRM));
      if (ding)                   /* note:  have to block, so that ALRM */
        break;                    /* doesn't occur between 'if (timerdone)'*/
      else
        sigpause(0);              /* and calling sigpause(0) */
    }
  sigblock(0);                    /* turn ALRM blocking off */
  (void) signal(SIGALRM, SIG_DFL);
}



alarm_handler(a)  /*ARGSUSED*/
     int a;
{
  ding = 1;
}



fd_ready(f)
     int f;
{
  fd_set readfds;
  static int init = 0;
  static struct timeval tm;

  errno = 0;

  /*
   * we only need to do this once
   */

  if(!init)
    {
      bzero((char *) &tm, sizeof(struct timeval));
      init = TRUE;
    }

  FD_ZERO(&readfds);
  FD_SET(f, &readfds);

  if(select(f + 1, &readfds, (fd_set *) 0, (fd_set *) 0, &tm) < 1)
      return(0);

  return (1);
}

