1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id$"; |
3 |
#endif |
4 |
/* |
5 |
* Convert a set of arguments into a command line for pipe() or system() |
6 |
* |
7 |
* Defines in paths.h |
8 |
*/ |
9 |
|
10 |
#include "paths.h" |
11 |
|
12 |
/* Check if any of the characters in str2 are found in str1 */ |
13 |
int |
14 |
matchany(const char *str1, const char *str2) |
15 |
{ |
16 |
while (*str1) { |
17 |
const char *cp = str2; |
18 |
while (*cp) |
19 |
if (*cp++ == *str1) |
20 |
return(*str1); |
21 |
++str1; |
22 |
} |
23 |
return(0); |
24 |
} |
25 |
|
26 |
/* Convert a set of arguments into a command line, being careful of specials */ |
27 |
char * |
28 |
convert_commandline(char *cmd, const int len, char *av[]) |
29 |
{ |
30 |
int match; |
31 |
char *cp; |
32 |
|
33 |
for (cp = cmd; *av != NULL; av++) { |
34 |
const int n = strlen(*av); |
35 |
if (cp+n >= cmd+(len-3)) |
36 |
return(NULL); |
37 |
if (matchany(*av, SPECIALS)) { |
38 |
const int quote = |
39 |
#ifdef ALTQUOT |
40 |
strchr(*av, QUOTCHAR) ? ALTQUOT : |
41 |
#endif |
42 |
QUOTCHAR; |
43 |
*cp++ = quote; |
44 |
strcpy(cp, *av); |
45 |
cp += n; |
46 |
*cp++ = quote; |
47 |
} else { |
48 |
strcpy(cp, *av); |
49 |
cp += n; |
50 |
} |
51 |
*cp++ = ' '; |
52 |
} |
53 |
if (cp <= cmd) |
54 |
return(NULL); |
55 |
*--cp = '\0'; |
56 |
return(cmd); |
57 |
} |