| 1 | 
greg | 
3.2 | 
#ifndef lint | 
| 2 | 
greg | 
3.3 | 
static const char RCSid[] = "$Id: cvtcmd.c,v 3.2 2016/02/02 18:05:31 greg Exp $"; | 
| 3 | 
greg | 
3.2 | 
#endif | 
| 4 | 
greg | 
3.1 | 
/* | 
| 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 | 
  | 
  | 
        char    *cp; | 
| 31 | 
  | 
  | 
 | 
| 32 | 
  | 
  | 
        for (cp = cmd; *av != NULL; av++) { | 
| 33 | 
  | 
  | 
                const int       n = strlen(*av); | 
| 34 | 
  | 
  | 
                if (cp+n >= cmd+(len-3)) | 
| 35 | 
  | 
  | 
                        return(NULL); | 
| 36 | 
  | 
  | 
                if (matchany(*av, SPECIALS)) { | 
| 37 | 
  | 
  | 
                        const int       quote = | 
| 38 | 
  | 
  | 
#ifdef ALTQUOT | 
| 39 | 
  | 
  | 
                                strchr(*av, QUOTCHAR) ? ALTQUOT : | 
| 40 | 
  | 
  | 
#endif | 
| 41 | 
  | 
  | 
                                        QUOTCHAR; | 
| 42 | 
  | 
  | 
                        *cp++ = quote; | 
| 43 | 
  | 
  | 
                        strcpy(cp, *av); | 
| 44 | 
  | 
  | 
                        cp += n; | 
| 45 | 
  | 
  | 
                        *cp++ = quote; | 
| 46 | 
  | 
  | 
                } else { | 
| 47 | 
  | 
  | 
                        strcpy(cp, *av); | 
| 48 | 
  | 
  | 
                        cp += n; | 
| 49 | 
  | 
  | 
                } | 
| 50 | 
  | 
  | 
                *cp++ = ' '; | 
| 51 | 
  | 
  | 
        } | 
| 52 | 
  | 
  | 
        if (cp <= cmd) | 
| 53 | 
  | 
  | 
                return(NULL); | 
| 54 | 
  | 
  | 
        *--cp = '\0'; | 
| 55 | 
  | 
  | 
        return(cmd); | 
| 56 | 
  | 
  | 
} |