/*ProfiPacket - packet radio terminal program
  Copyright (C) 1999  Alexander Feigl

  This program 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 of the License, or
  (at your option) any later version.

  This program 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 this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

  Author:

  Alexander Feigl
  Burachstraße 51

  D-88250 Weingarten

  Mail : Alexander.Feigl@gmx.de
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "Fork.h"
#include "TNC.h"
#include <stdlib.h>


Fork::Fork(int (*fx)(TNC *mtnc),TNC *ftnc):KProcess()
 {
  function=fx;
  forktnc=ftnc;
 }

bool Fork::start(RunMode runmode, Communication comm)
{

  if (runs) return (FALSE);

  run_mode = runmode;
  status = 0;

  setupCommunication(comm);

  runs = TRUE;
  pid = fork();

  if (pid==0)
   {
    commSetupDoneC();
    if (run_mode == DontCare) setpgid(0,0);
    exit(function(forktnc));
   }
   else if (pid == -1)
          {
	   runs = FALSE;
	   return FALSE;
          } else {
	          commSetupDoneP();

	          input_data = 0;
	          if (run_mode == Block)
                   {
	            waitpid(pid, &status, 0);
	            processHasExited(status);
	           }
                 }
 return TRUE;
}


bool Fork::writeStdin(char *buffer, int buflen)
{

  if ( runs && communication) {
    ::write(in[1],buffer,buflen);
    return(TRUE);
   }
  return (FALSE);
}
