home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
gdead.berkeley.edu
/
gdead.berkeley.edu.tar
/
gdead.berkeley.edu
/
pub
/
cad-tools
/
ciftomann.tar
/
driver_dir
/
call.c
next >
Wrap
C/C++ Source or Header
|
1988-01-28
|
2KB
|
113 lines
/*
* call a program
*
* Four entries:
*
* call, callv - call a program and wait for it, returning status
* back, backv - call a program and don't wait, returning process id
*
* The command's standard input, output and error are passed as FILE *'s.
*/
#include <stdio.h>
#include "string.h"
#define nil(type) ((type) 0)
#define local static
#define MAXNARGS 100 /* unchecked upper limit on max num of arguments */
#define BADEXEC 127 /* exec fails */
#define ischild(pid) ((pid) == 0)
/*VARARGS3*/
int call(name, in, out, err, args)
string name;
FILE *in;
FILE *out;
FILE *err;
string args;
{
register string *ap, *argp;
string argv[MAXNARGS];
argp = &argv[0];
*argp++ = name;
ap = &args;
while (*ap != nil(string)) {
*argp++ = *ap++;
}
*argp = nil(string);
return(callv(name, in, out, err, argv));
}
/*VARARGS3*/
int back(name, in, out, err, args)
string name;
FILE *in;
FILE *out;
FILE *err;
string args;
{
register string *ap, *argp;
string argv[MAXNARGS];
argp = &argv[0];
*argp++ = name;
ap = &args;
while (*ap != nil(string)) {
*argp++ = *ap++;
}
*argp = nil(string);
return(backv(name, in, out, err, argv));
}
int callv(name, in, out, err, argv)
string name;
FILE *in;
FILE *out;
FILE *err;
string *argv;
{
int pid, status;
pid = backv(name, in, out, err, argv);
pwait(pid, &status);
return(status);
}
int backv(name, in, out, err, argv)
string name;
FILE *in;
FILE *out;
FILE *err;
string *argv;
{
int pid;
if (ischild(pid = fork())) {
fswap(0, fileno(in));
fswap(1, fileno(out));
fswap(2, fileno(err));
execvp(name, argv);
exit(BADEXEC);
}
return(pid);
}
/*
* Swap file numbers so as to redirect standard input, output and error.
*/
local fswap(oldfd, newfd)
int oldfd;
int newfd;
{
if (oldfd != newfd) {
close(oldfd);
dup(newfd);
close(newfd);
}
}