1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-09 09:15:38 +02:00

Reformat to be Unix format. Check the program

with pfind() to see if executable.
This commit is contained in:
Alain Magloire 2002-10-17 15:52:34 +00:00
parent e1b17f11c1
commit b968757017

View file

@ -1,145 +1,135 @@
#include "exec0.h" #include "exec0.h"
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <libgen.h> #include <libgen.h>
#include <stdlib.h> #include <stdlib.h>
/* from pfind.c */ /* from pfind.c */
char *pfind( char *name ); extern char *pfind(const char *name);
pid_t pid_t
exec0(const char *path, char *const argv[], char *const envp[], exec0(const char *path, char *const argv[], char *const envp[],
const char *dirpath, int channels[3]) const char *dirpath, int channels[3])
{ {
int pipe0[2], pipe1[2], pipe2[2]; int pipe0[2], pipe1[2], pipe2[2];
pid_t childpid; pid_t childpid;
char *full_path; char *full_path;
// /*
// Handle this error case, we need the full path for execve() below. * We use pfind() to check that the program exists and is an executable.
// * If not pass the error up.
if (path[0] != '/' && path[0] != '.') { */
full_path = pfind( path ); full_path = pfind(path);
//full_path = pathfind (getenv ("PATH"), path, "rx"); if (full_path == NULL) {
if (full_path == NULL) { fprintf(stderr, "Unable to find full path for \"%s\"\n", path);
fprintf( stderr, "Unable to find full path for \"%s\"\n", path ); return -1;
return -1; }
}
} else { /*
full_path = strdup(path); * Make sure we can create our pipes before forking.
} */
if (channels != NULL) {
// if (pipe(pipe0) < 0 || pipe(pipe1) < 0 || pipe(pipe2) < 0) {
// Make sure we can create our pipes before forking. fprintf(stderr, "%s(%d): returning due to error.\n",
// __FUNCTION__, __LINE__);
if( channels != NULL ) free(full_path);
{ return -1;
if (pipe(pipe0) < 0 || pipe(pipe1) < 0 || pipe(pipe2) < 0) { }
fprintf(stderr, "%s(%d): returning due to error.\n", }
__FUNCTION__, __LINE__);
free(full_path); childpid = fork1();
return -1;
} if (childpid < 0) {
} fprintf(stderr, "%s(%d): returning due to error: %s\n",
__FUNCTION__, __LINE__, strerror(errno));
childpid = fork1(); free(full_path);
return -1;
if (childpid < 0) { } else if (childpid == 0) { /* child */
fprintf(stderr, "%s(%d): returning due to error: %s\n", char *ptr;
__FUNCTION__, __LINE__, strerror(errno));
free(full_path); chdir(dirpath);
return -1;
} else if (childpid == 0) { /* child */ if (channels != NULL) {
char *ptr; /* Close the write end of pipe0 */
if (close(pipe0[1]) == -1)
chdir(dirpath); perror("close(pipe0[1])");
if( channels != NULL ) /* Close the read end of pipe1 */
{ if (close(pipe1[0]) == -1)
/* Close the write end of pipe0 */ perror("close(pipe1[0])");
if( close(pipe0[1]) == -1 )
perror( "close(pipe0[1])" ); /* Close the read end of pipe2 */
if (close(pipe2[0]) == -1)
/* Close the read end of pipe1 */ perror("close(pipe2[0]))");
if( close(pipe1[0]) == -1 )
perror( "close(pipe1[0])" ); /* redirections */
dup2(pipe0[0], STDIN_FILENO); /* dup stdin */
/* Close the read end of pipe2 */ dup2(pipe1[1], STDOUT_FILENO); /* dup stdout */
if( close(pipe2[0]) == -1 ) dup2(pipe2[1], STDERR_FILENO); /* dup stderr */
perror( "close(pipe2[0]))" ); }
/* redirections */ /* Close all the fd's in the child */
dup2(pipe0[0], STDIN_FILENO); /* dup stdin */ {
dup2(pipe1[1], STDOUT_FILENO); /* dup stdout */ int fdlimit = sysconf(_SC_OPEN_MAX);
dup2(pipe2[1], STDERR_FILENO); /* dup stderr */ int fd = 3;
}
while (fd < fdlimit)
/* Close all the fd's in the child */ close(fd++);
{ }
int fdlimit = sysconf(_SC_OPEN_MAX);
int fd = 3; if (envp[0] == NULL) {
execv(full_path, argv);
while (fd < fdlimit) } else {
close(fd++); execve(full_path, argv, envp);
} }
if( envp[0] == NULL ) _exit(127);
{
execv( full_path, argv ); } else if (childpid != 0) { /* parent */
}
else char b;
{
execve( full_path, argv, envp ); if (channels != NULL) {
} /* close the read end of pipe1 */
if (close(pipe0[0]) == -1)
_exit(127); perror("close(pipe0[0])");
} else if (childpid != 0) { /* parent */ /* close the write end of pipe2 */
if (close(pipe1[1]) == -1)
char b; perror("close(pipe1[1])");
if( channels != NULL ) /* close the write end of pipe2 */
{ if (close(pipe2[1]) == -1)
/* close the read end of pipe1 */ perror("close(pipe2[1])");
if( close(pipe0[0]) == -1 )
perror( "close(pipe0[0])" ); channels[0] = pipe0[1]; /* Output Stream. */
channels[1] = pipe1[0]; /* Input Stream. */
/* close the write end of pipe2 */ channels[2] = pipe2[0]; /* Input Stream. */
if( close(pipe1[1]) == -1 ) }
perror( "close(pipe1[1])" );
free(full_path);
/* close the write end of pipe2 */ return childpid;
if( close(pipe2[1]) == -1 ) }
perror( "close(pipe2[1])" );
free(full_path);
channels[0] = pipe0[1]; /* Output Stream. */ return -1; /*NOT REACHED */
channels[1] = pipe1[0]; /* Input Stream. */ }
channels[2] = pipe2[0]; /* Input Stream. */
}
int wait0(pid_t pid)
free(full_path); {
return childpid; int status;
} int val = -1;
free(full_path); if (pid < 0 || waitpid(pid, &status, 0) < 0)
return -1; /*NOT REACHED */ return -1;
}
if (WIFEXITED(status)) {
val = WEXITSTATUS(status);
int wait0(pid_t pid) }
{
int status; return val;
int val = -1; }
if (pid < 0 || waitpid(pid, &status, 0) < 0)
return -1;
if (WIFEXITED(status)) {
val = WEXITSTATUS(status);
}
return val;
}