Use snprintf()
:
/* determine buffer size */
int len = snprintf(NULL, 0, "xmllint, --noout --schema %s %s", xsdName, xmlName);
if (len < 0) {
/* error handling for EILSEQ here */
}
char *buf = malloc(len + 1);
if (buf == NULL) {
/* err handling for malloc() failure here */
}
snprintf(buf, (size_t)len, "xmllint, --noout --schema %s %s", xsdName, xmlName);
system(buf);
free(buf);
On a sufficiently recent system, you could also use asprintf()
to greatly simplify this code:
char *buf = NULL;
asprintf(&buf, "xmllint, --noout --schema %s %s", xsdName, xmlName);
if (buf == NULL) {
/* error handling here */
}
system(buf);
free(buf);
Note that all these approaches fail if xsdName
or xmlName
contain spaces or other special characters. You might want to invoke an exec
function directly to avoid that problem.