cmExec.h/c, Makefile.am : Initial commit.
This commit is contained in:
parent
834502f09f
commit
d72ce9d0bb
@ -9,8 +9,8 @@ cmSRC += src/libcm/cmErr.c src/libcm/cmCtx.c src/libcm/cmRpt.c src/libcm/cmGloba
|
|||||||
cmHDR += src/libcm/cmSerialize.h src/libcm/cmSymTbl.h src/libcm/cmHashTbl.h src/libcm/cmFileSys.h src/libcm/cmFile.h
|
cmHDR += src/libcm/cmSerialize.h src/libcm/cmSymTbl.h src/libcm/cmHashTbl.h src/libcm/cmFileSys.h src/libcm/cmFile.h
|
||||||
cmSRC += src/libcm/cmSerialize.c src/libcm/cmSymTbl.c src/libcm/cmHashTbl.c src/libcm/cmFileSys.c src/libcm/cmFile.c
|
cmSRC += src/libcm/cmSerialize.c src/libcm/cmSymTbl.c src/libcm/cmHashTbl.c src/libcm/cmFileSys.c src/libcm/cmFile.c
|
||||||
|
|
||||||
cmHDR += src/libcm/cmMem.h src/libcm/cmTime.h src/libcm/cmPgmOpts.h
|
cmHDR += src/libcm/cmMem.h src/libcm/cmTime.h src/libcm/cmExec.h src/libcm/cmPgmOpts.h
|
||||||
cmSRC += src/libcm/cmMem.c src/libcm/cmTime.c src/libcm/cmPgmOpts.c
|
cmSRC += src/libcm/cmMem.c src/libcm/cmTime.c src/libcm/cmExec.c src/libcm/cmPgmOpts.c
|
||||||
|
|
||||||
cmHDR += src/libcm/cmData.h src/libcm/cmLib.h src/libcm/cmText.h src/libcm/cmTextTemplate.h
|
cmHDR += src/libcm/cmData.h src/libcm/cmLib.h src/libcm/cmText.h src/libcm/cmTextTemplate.h
|
||||||
cmSRC += src/libcm/cmData.c src/libcm/cmLib.c src/libcm/cmText.c src/libcm/cmTextTemplate.c
|
cmSRC += src/libcm/cmData.c src/libcm/cmLib.c src/libcm/cmText.c src/libcm/cmTextTemplate.c
|
||||||
|
81
cmExec.c
Normal file
81
cmExec.c
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
#include "cmGlobal.h"
|
||||||
|
#include "cmRpt.h"
|
||||||
|
#include "cmErr.h"
|
||||||
|
#include "cmExec.h"
|
||||||
|
#include <sys/wait.h>
|
||||||
|
|
||||||
|
cmExRC_t cmExecV( cmErr_t* err, int* returnValRef, const cmChar_t* pgmFn, va_list vl0 )
|
||||||
|
{
|
||||||
|
cmExRC_t rc = kOkExRC;
|
||||||
|
int n = 0;
|
||||||
|
int i = 0;
|
||||||
|
pid_t pid;
|
||||||
|
va_list vl1;
|
||||||
|
|
||||||
|
if( pgmFn == NULL )
|
||||||
|
return cmErrMsg(err,kInvalidPgmFnExRC,"No executable program file name given in call to %s.",__FUNCTION__);
|
||||||
|
|
||||||
|
// get the count of arguments
|
||||||
|
va_copy(vl1,vl0);
|
||||||
|
while( va_arg(vl1,cmChar_t*)!=NULL )
|
||||||
|
++n;
|
||||||
|
va_end(vl1);
|
||||||
|
|
||||||
|
|
||||||
|
// load argv with ptrs to the args
|
||||||
|
cmChar_t* argv[n+2];
|
||||||
|
|
||||||
|
argv[0] = (cmChar_t*)pgmFn;
|
||||||
|
for(i=0; i<n+1; ++i)
|
||||||
|
argv[i+1] = va_arg(vl0,cmChar_t*);
|
||||||
|
argv[n+1] = NULL;
|
||||||
|
|
||||||
|
errno = 0;
|
||||||
|
|
||||||
|
switch( pid = fork())
|
||||||
|
{
|
||||||
|
case -1:
|
||||||
|
rc = cmErrSysMsg(err,kForkFailExRC,errno,"Fork failed.");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 0:
|
||||||
|
// we are in the child process - never to return
|
||||||
|
execvp(pgmFn,argv);
|
||||||
|
|
||||||
|
// under normal conditions execlp() does not return so we should never get here.
|
||||||
|
rc = cmErrSysMsg(err,kExecFailExRC,errno,"Fork to '%s' failed. Is '%s' installed and on the execution path?",pgmFn,pgmFn);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
int rv;
|
||||||
|
int wrc;
|
||||||
|
|
||||||
|
// we are in the parent process - wait for the child to return
|
||||||
|
if((wrc = waitpid(pid,&rv,0))==-1)
|
||||||
|
{
|
||||||
|
rc = cmErrSysMsg(err,kWaitFailExRC,errno,"Wait failed on call to '%s'.",pgmFn);
|
||||||
|
goto errLabel;
|
||||||
|
}
|
||||||
|
|
||||||
|
if( returnValRef != NULL )
|
||||||
|
*returnValRef = rv;
|
||||||
|
|
||||||
|
if( WEXITSTATUS(rv) != 0 )
|
||||||
|
rc = cmErrMsg(err,kPgmFailExRC,"'%s' failed.",pgmFn);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
errLabel:
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
cmExRC_t cmExec( cmErr_t* err, int* returnValRef, const cmChar_t* pgmFn, ... )
|
||||||
|
{
|
||||||
|
va_list vl;
|
||||||
|
va_start(vl,pgmFn);
|
||||||
|
cmExRC_t rc = cmExecV(err,returnValRef,pgmFn,vl);
|
||||||
|
va_end(vl);
|
||||||
|
return rc;
|
||||||
|
}
|
29
cmExec.h
Normal file
29
cmExec.h
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#ifndef cmExec_h
|
||||||
|
#define cmExec_h
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
kOkExRC,
|
||||||
|
kInvalidPgmFnExRC, // pgmFn was NULL
|
||||||
|
kForkFailExRC, // internal fork() failed
|
||||||
|
kExecFailExRC, // internal exec() failed.
|
||||||
|
kPgmFailExRC, // pgm returned a non-zero exit status
|
||||||
|
kWaitFailExRC // internal waitpid() failed
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef unsigned cmExRC_t;
|
||||||
|
|
||||||
|
// If returnValRef is non-NULL *returnValRef is set to the program return value.
|
||||||
|
cmExRC_t cmExecV( cmErr_t* err, int* returnValRef, const cmChar_t* pgmFn, va_list vl );
|
||||||
|
cmExRC_t cmExec( cmErr_t* err, int* returnValRef, const cmChar_t* pgmFn, ... );
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue
Block a user