/********************************************************
 * File: exec.c
 * Created at Sun Jan 28 22:10:26 MSK 2001 by raorn // raorn@binec.ru
 * Exec stuff
 * $Id: exec.c,v 1.4 2002/10/27 22:47:52 raorn Exp $
 *******************************************************/
#include <machine/defs.h>

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif
#ifndef WEXITSTATUS
# define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
#endif
#ifndef WIFEXITED
# define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
#endif

#include "jblist.h"
#include "exec.h"

/* Returns -1 if dir was not found and errorlevel otherwise */
int ChDirExecute(uchar * dir, uchar * cmd, int *rc)
{
  char olddir[300];
  int res;

  if (!getcwd(olddir, 300))
    return -1;

  if (chdir(dir) != 0)
    return -1;

  res = Execute(cmd, rc);

  chdir(olddir);

  return (res);
}

int Execute(uchar * cmd, int *rc)
{
  int res;

  res = system(cmd);

  if (WIFEXITED(res)) {
    *rc = WEXITSTATUS(res);
    return 0;
  }

  return res;
}


syntax highlighted by Code2HTML, v. 0.9.1