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