2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: portproc.c /main/3 1996/06/19 17:16:49 drk $ */
26 Semi-portable implementation of run_process().
28 Written by James Clark (jjc@jclark.com).
39 /* This code shows how you might use system() to implement run_process().
40 ANSI C says very little about the behaviour of system(), and so this
41 is necessarily system dependent. */
43 /* Characters that are significant to the shell and so need quoting. */
44 #define SHELL_MAGIC "$\\\"';&()|<>^ \t\n"
45 /* Character with which to quote shell arguments. */
46 #define SHELL_QUOTE_CHAR '\''
47 /* String that can be used to get SHELL_QUOTE_CHAR into a quoted argument. */
48 #define SHELL_ESCAPE_QUOTE "'\\''"
49 /* Character that can be used to separate arguments to the shell. */
50 #define SHELL_ARG_SEP ' '
52 static UNS shell_quote P((char *, char *));
62 for (p = argv; *p; p++)
63 len += shell_quote(*p, (char *)0);
65 s = command = xmalloc(len);
66 for (p = argv; *p; ++p) {
69 s += shell_quote(*p, s);
73 ret = system(command);
75 appl_error(E_EXEC, argv[0], strerror(errno));
80 /* Quote a string so that it appears as a single argument to the
81 shell (as used for system()). Store the quoted argument in result, if
82 result is not NULL. Return the length. */
85 UNS shell_quote(s, result)
91 if (strpbrk(s, SHELL_MAGIC)) {
95 result[0] = SHELL_QUOTE_CHAR;
98 if (*s == SHELL_QUOTE_CHAR) {
100 strcpy(result + len, SHELL_ESCAPE_QUOTE);
101 len += strlen(SHELL_ESCAPE_QUOTE);
111 result[len] = SHELL_QUOTE_CHAR;
117 #endif /* SUPPORT_SUBDOC */
122 c-continued-statement-offset: 5