* src/include/nonposix.h (write, dup, dup2, close) [_MSC_VER]:
[s-roff.git] / src / libs / libgroff / spawnvp.c
blobe185ec3075a6adb6d3cb8c1d7bacaa63b9d93b95
1 /* Copyright (C) 2004
2 Free Software Foundation, Inc.
3 Written by: Keith Marshall (keith.d.marshall@ntlworld.com)
5 This file is part of groff.
7 groff is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 groff is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License along
18 with groff; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 #include <stdio.h>
26 #include <stdlib.h>
28 #ifdef HAVE_PROCESS_H
29 # include <process.h>
30 #endif
32 #define SPAWN_FUNCTION_WRAPPERS 1
34 /* Define the default mechanism, and messages, for error reporting
35 * (user may substitute a preferred alternative, by defining his own
36 * implementation of the macros REPORT_ERROR and ARGV_MALLOC_ERROR,
37 * in the header file `nonposix.h').
40 #include "nonposix.h"
42 #ifndef REPORT_ERROR
43 # define REPORT_ERROR(WHY) fprintf(stderr, "%s:%s\n", program_name, WHY)
44 #endif
45 #ifndef ARGV_MALLOC_ERROR
46 # define ARGV_MALLOC_ERROR "malloc: Allocation for 'argv' failed"
47 #endif
49 extern char *program_name;
51 extern char *quote_arg(char *string);
52 extern void purge_quoted_args(char **argv);
54 int
55 spawnvp_wrapper(int mode, char *path, char **argv)
57 /* Invoke the system `spawnvp' service
58 * enclosing the passed arguments in double quotes, as required,
59 * so that the (broken) default parsing in the MSVC runtime doesn't
60 * split them at whitespace. */
62 char **quoted_argv; /* used to build a quoted local copy of `argv' */
64 int i; /* used as an index into `argv' or `quoted_argv' */
65 int status = -1; /* initialise return code, in case we fail */
66 int argc = 0; /* initialise argument count; may be none */
68 /* First count the number of arguments
69 * which are actually present in the passed `argv'. */
71 if (argv)
72 for (quoted_argv = argv; *quoted_argv; ++argc, ++quoted_argv)
75 /* If we do not now have an argument count,
76 * then we must fall through and fail. */
78 if (argc) {
79 /* We do have at least one argument:
80 * We will use a copy of the `argv', in which to do the quoting,
81 * so we must allocate space for it. */
83 if ((quoted_argv = malloc(++argc * sizeof(char **))) == NULL) {
84 /* If we didn't get enough space,
85 * then complain, and bail out gracefully. */
87 REPORT_ERROR(ARGV_MALLOC_ERROR);
88 exit(1);
91 /* Now copy the passed `argv' into our new vector,
92 * quoting its contents as required. */
94 for (i = 0; i < argc; i++)
95 quoted_argv[i] = quote_arg(argv[i]);
97 /* Invoke the MSVC `spawnvp' service
98 * passing our now appropriately quoted copy of `argv'. */
100 status = spawnvp(mode, path, quoted_argv);
102 /* Clean up our memory allocations
103 * for the quoted copy of `argv', which is no longer required. */
105 purge_quoted_args(quoted_argv);
106 free(quoted_argv);
109 /* Finally,
110 * return the status code returned by `spawnvp',
111 * or a failure code if we fell through. */
113 return status;
116 /* spawnvp.c: end of file */