2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <libc.h> /* first to get rid of pre-comp warning */
25 #include <mach/mach.h> /* first to get rid of pre-comp warning */
30 #include "stuff/errors.h"
31 #include "stuff/allocate.h"
32 #include "stuff/execute.h"
33 #include "mach-o/dyld.h"
36 * execute() does an execvp using the argv passed to it. If the parameter
37 * verbose is non-zero the command is printed to stderr. A non-zero return
38 * value indicates success zero indicates failure.
47 int forkpid
, waitpid
, termsig
;
51 union wait waitstatus
;
57 fprintf(stderr
, "+ %s ", name
);
59 while(*p
!= (char *)0)
60 fprintf(stderr
, "%s ", *p
++);
61 fprintf(stderr
, "\n");
66 system_fatal("can't fork a new process to execute: %s", name
);
69 if(execvp(name
, argv
) == -1)
70 system_fatal("can't find or exec: %s", name
);
71 return(1); /* can't get here, removes a warning from the compiler */
74 waitpid
= wait(&waitstatus
);
76 system_fatal("wait on forked process %d failed", forkpid
);
78 termsig
= WTERMSIG(waitstatus
);
80 termsig
= waitstatus
.w_termsig
;
82 if(termsig
!= 0 && termsig
!= SIGINT
)
83 fatal("fatal error in %s", name
);
86 WEXITSTATUS(waitstatus
) == 0 &&
88 waitstatus
.w_retcode
== 0 &&
95 * runlist is used by the routine execute_list() to execute a program and it
96 * contains the command line arguments. Strings are added to it by
97 * add_execute_list(). The routine reset_execute_list() resets it for new use.
106 * This routine is passed a string to be added to the list of strings for
107 * command line arguments.
114 if(runlist
.strings
== (char **)0){
117 runlist
.strings
= allocate(runlist
.size
* sizeof(char **));
119 if(runlist
.next
+ 1 >= runlist
.size
){
120 runlist
.strings
= reallocate(runlist
.strings
,
121 (runlist
.size
* 2) * sizeof(char **));
124 runlist
.strings
[runlist
.next
++] = str
;
125 runlist
.strings
[runlist
.next
] = (char *)0;
129 * This routine is passed a string to be added to the list of strings for
130 * command line arguments and is then prefixed with the path of the executable.
134 add_execute_list_with_prefix(
137 add_execute_list(cmd_with_prefix(str
));
141 * This routine is passed a string of a command name and a string is returned
142 * prefixed with the path of the executable and that command name.
151 char *prefix
, buf
[MAXPATHLEN
], resolved_name
[PATH_MAX
];
152 unsigned long bufsize
;
155 * Construct the prefix to the program running.
157 bufsize
= MAXPATHLEN
;
159 i
= _NSGetExecutablePath(p
, &bufsize
);
161 p
= allocate(bufsize
);
162 _NSGetExecutablePath(p
, &bufsize
);
164 prefix
= realpath(p
, resolved_name
);
165 p
= rindex(prefix
, '/');
169 return(makestr(prefix
, str
, NULL
));
173 * This routine reset the list of strings of command line arguments so that
174 * an new command line argument list can be built.
178 reset_execute_list(void)
184 * This routine calls execute() to run the command built up in the runlist
192 return(execute(runlist
.strings
, verbose
));
194 #endif /* !defined(RLD) */