-\f
-/* Wait for a process to finish, and exit if a nonzero status is found. */
-
-int
-collect_wait (const char *prog, struct pex_obj *pex)
-{
- int status;
-
- if (!pex_get_status (pex, 1, &status))
- fatal_error ("can't get program status: %m");
- pex_free (pex);
-
- if (status)
- {
- if (WIFSIGNALED (status))
- {
- int sig = WTERMSIG (status);
- error ("%s terminated with signal %d [%s]%s",
- prog, sig, strsignal(sig),
- WCOREDUMP(status) ? ", core dumped" : "");
- collect_exit (FATAL_EXIT_CODE);
- }
-
- if (WIFEXITED (status))
- return WEXITSTATUS (status);
- }
- return 0;
-}
-
-static void
-do_wait (const char *prog, struct pex_obj *pex)
-{
- int ret = collect_wait (prog, pex);
- if (ret != 0)
- {
- error ("%s returned %d exit status", prog, ret);
- collect_exit (ret);
- }
-
- if (response_file)
- {
- unlink (response_file);
- response_file = NULL;
- }
-}
-
-\f
-/* Execute a program, and wait for the reply. */
-
-struct pex_obj *
-collect_execute (const char *prog, char **argv, const char *outname,
- const char *errname, int flags)
-{
- struct pex_obj *pex;
- const char *errmsg;
- int err;
- char *response_arg = NULL;
- char *response_argv[3] ATTRIBUTE_UNUSED;
-
- if (HAVE_GNU_LD && at_file_supplied && argv[0] != NULL)
- {
- /* If using @file arguments, create a temporary file and put the
- contents of argv into it. Then change argv to an array corresponding
- to a single argument @FILE, where FILE is the temporary filename. */
-
- char **current_argv = argv + 1;
- char *argv0 = argv[0];
- int status;
- FILE *f;
-
- /* Note: we assume argv contains at least one element; this is
- checked above. */
-
- response_file = make_temp_file ("");
-
- f = fopen (response_file, "w");
-
- if (f == NULL)
- fatal_error ("could not open response file %s", response_file);
-
- status = writeargv (current_argv, f);
-
- if (status)
- fatal_error ("could not write to response file %s", response_file);
-
- status = fclose (f);
-
- if (EOF == status)
- fatal_error ("could not close response file %s", response_file);
-
- response_arg = concat ("@", response_file, NULL);
- response_argv[0] = argv0;
- response_argv[1] = response_arg;
- response_argv[2] = NULL;
-
- argv = response_argv;
- }
-
- if (vflag || debug)
- {
- char **p_argv;
- const char *str;
-
- if (argv[0])
- fprintf (stderr, "%s", argv[0]);
- else
- notice ("[cannot find %s]", prog);
-
- for (p_argv = &argv[1]; (str = *p_argv) != (char *) 0; p_argv++)
- fprintf (stderr, " %s", str);
-
- fprintf (stderr, "\n");
- }
-
- fflush (stdout);
- fflush (stderr);
-
- /* If we cannot find a program we need, complain error. Do this here
- since we might not end up needing something that we could not find. */
-
- if (argv[0] == 0)
- fatal_error ("cannot find '%s'", prog);
-
- pex = pex_init (0, "collect2", NULL);
- if (pex == NULL)
- fatal_error ("pex_init failed: %m");
-
- errmsg = pex_run (pex, flags, argv[0], argv, outname,
- errname, &err);
- if (errmsg != NULL)
- {
- if (err != 0)
- {
- errno = err;
- fatal_error ("%s: %m", _(errmsg));
- }
- else
- fatal_error (errmsg);
- }
-
- free (response_arg);
-
- return pex;
-}
-
-static void
-fork_execute (const char *prog, char **argv)
-{
- struct pex_obj *pex;
-
- pex = collect_execute (prog, argv, NULL, NULL, PEX_LAST | PEX_SEARCH);
- do_wait (prog, pex);
-}