The
shquote() and
shquotev() functions copy strings and transform the copies by adding shell escape and quoting characters. They are used to encapsulate arguments to be included in command strings passed to the
system() and
popen() functions, so that the arguments will have the correct values after being evaluated by the shell.
The exact method of quoting and escaping may vary, and is intended to match the conventions of the shell used by
system() and
popen(). It may not match the conventions used by other shells. In this implementation, the following transformation is applied to each input string:
•
it is surrounded by single quotes ('),
•
any single quotes in the input are escaped by replacing them with the four-character sequence: '\'', and
•
extraneous pairs of single quotes (caused by multiple adjacent single quotes in the input string, or by single quotes at the beginning or end of the input string) are elided.
The
shquote() function transforms the string specified by its
arg argument, and places the result into the memory pointed to by
buf.
The
shquotev() function transforms each of the
argc strings specified by the array
argv independently. The transformed strings are placed in the memory pointed to by
buf, separated by spaces. It does not modify the pointer array specified by
argv or the strings pointed to by the pointers in the array.
Both functions write up to
bufsize - 1 characters of output into the buffer pointed to by
buf, then add a
NUL character to terminate the output string. If
bufsize is given as zero, the
buf parameter is ignored and no output is written.