|
0
|
1 |
// Copyright (C) 1999,2000 Bruce Guenter <bruceg@em.ca>
|
|
|
2 |
//
|
|
|
3 |
// This program is free software; you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation; either version 2 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// This program is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with this program; if not, write to the Free Software
|
|
|
15 |
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
16 |
|
|
|
17 |
#include <config.h>
|
|
|
18 |
#include <errno.h>
|
|
|
19 |
#include <stdlib.h>
|
|
2
|
20 |
#include <string.h>
|
|
0
|
21 |
#include <sys/stat.h>
|
|
|
22 |
#include <sys/types.h>
|
|
|
23 |
#include <sys/wait.h>
|
|
|
24 |
#include <unistd.h>
|
|
|
25 |
#include "exec.h"
|
|
|
26 |
#include "config/configrc.h"
|
|
|
27 |
|
|
|
28 |
int presetenv(const char* prefix, const mystring& value)
|
|
|
29 |
{
|
|
|
30 |
unsigned plen = strlen(prefix);
|
|
|
31 |
char* tmp = new char[plen+value.length()+1];
|
|
|
32 |
strcpy(tmp, prefix);
|
|
|
33 |
strcpy(tmp+plen, value.c_str());
|
|
|
34 |
return putenv(tmp);
|
|
|
35 |
|
|
|
36 |
// Note that tmp is never freed. This was done as a result of problems
|
|
|
37 |
// using putenv with glibc 2.1, where freeing the pointer passed to
|
|
|
38 |
// putenv appeared to cause garbage to enter the environment.
|
|
|
39 |
//mystring tmp = prefix + value;
|
|
|
40 |
//return putenv(tmp.c_str());
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
int execute_one(const char* args[])
|
|
|
44 |
{
|
|
|
45 |
int pid = fork();
|
|
|
46 |
switch(pid) {
|
|
|
47 |
case -1:
|
|
|
48 |
return -1;
|
|
|
49 |
case 0:
|
|
|
50 |
execvp(args[0], (char**)args);
|
|
|
51 |
exit(127);
|
|
|
52 |
default:
|
|
|
53 |
for(;;) {
|
|
|
54 |
int status;
|
|
|
55 |
if(waitpid(pid, &status, 0) == -1) {
|
|
|
56 |
if(errno != EINTR)
|
|
|
57 |
return -1;
|
|
|
58 |
} else {
|
|
|
59 |
if(WIFEXITED(status))
|
|
|
60 |
return WEXITSTATUS(status);
|
|
|
61 |
else
|
|
|
62 |
return -1;
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
}
|
|
|
66 |
return 0;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
static int execute(const configuration* node, const mystring& name)
|
|
|
70 |
{
|
|
|
71 |
if(node->parent) {
|
|
|
72 |
int i = execute(node->parent, name);
|
|
|
73 |
if(i || (node->parent->directory == node->directory))
|
|
|
74 |
return i;
|
|
|
75 |
}
|
|
|
76 |
mystring path = node->directory + "/" + name;
|
|
|
77 |
struct stat buf;
|
|
|
78 |
if(stat(path.c_str(), &buf) != -1 && buf.st_mode & 0111) {
|
|
|
79 |
const char* args[] = { path.c_str(), 0 };
|
|
|
80 |
return execute_one(args);
|
|
|
81 |
}
|
|
|
82 |
else
|
|
|
83 |
return 0;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
int execute(const mystring& name)
|
|
|
87 |
{
|
|
2
|
88 |
return execute(config, name);
|
|
0
|
89 |
}
|