/*********************************************************************
 *
 *            EZFM---EZWGL based file manager for X11
 *
 *             Copyright (C) 1998  Maorong Zou
 *
 *
 *  This file is part of EZFM.
 *
 *  EZFM is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * EZFM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with EZFM; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *
 *************************************************************************/


#define _SIGNAL_C_
#include "ezfm.h"

/**************************************************************/
void  handleSigChild();
void exitHandler();
/**************************************************************/

static void *rSignal  MY_ANSIARGS((int signo,  void *handler));
static void  sigchildHandler  MY_ANSIARGS((int signo));
static void  watchMount  MY_ANSIARGS((int signo));

/**************************************************************/

void  handleSigChild()
{
  rSignal(SIGCHLD, (void *)sigchildHandler);
}

void  handleOtherSig()
{
  int i; 
  for(i = 1; i <= 15; i++) rSignal(i,  (void *)watchMount);
}

/**************************************************************/
/* POSIX.1 signal */
void *rSignal(signo, handler)
     int signo; void *handler;
{
  struct sigaction     act, oact;

  act.sa_handler = (void *) handler;
  sigemptyset(&act.sa_mask); 
#ifdef SA_INTERRUPT
  act.sa_flags = SA_INTERRUPT;
#else
  act.sa_flags = SA_RESTART;
#endif

  if(sigaction(signo, &act, &oact) < 0) return(SIG_ERR);
  return((void *)oact.sa_handler);
}
/**************************************************************/
void sigchildHandler(signo)
     int signo;
{
  pid_t      pid;
  int        status;

  /* wait for all terminated children */
  while( (pid = waitpid(-1, &status, WNOHANG)) > 0) ;
}
/**************************************************************/
void watchMount(signo)
     int signo;
{
  exitHandler();
}
/**************************************************************/
void exitHandler()
{
  int i;
  char command[256];
  fprintf(stderr, "existing, please wait ... ");
  sleep(3);
  for(i = 0; i < 8; i++)
    {
      if(ezfmDrives[i].status == 1) /* mounted */
        {
          sprintf(command, "%s %s&", BIN_UMOUNT, ezfmDrives[i].mntPt);
          system(command);
          ezfmDrives[i].status = 0;
        }
    }
  fprintf(stderr, "Done\n");
}
/**************************************************************/

#undef _SIGNAL_C_
