I am trying to have something like:
Code:
char pdfinfo[1028];
sprintf(pdfinfo, "pdfinfo %s >filout", pdfname);
g_print("%s", pdfinfo);
system((char *) pdfinfo);
This works fine when the filename choosen has no space. If it has space,
as linux reads it as "file\ with\ space", pdfinfo is giving error.
When you call system(), the host environment's command processor (if one
is available) is used to process the string. I believe that on Unix-like
systems it's usually
"/bin/sh -c", which on Linux systems is usually an alias for bash. The
problem is not the presence of a backslash, but the absence of one.
Try these two:
system("pdfinfo file with space");
system("pdfinfo file\\ with\\ space");
On my system, the first fails, the second succeeds. Keep in mind that
'\\' is a C escape sequence that only places a single backslash
character in the resulting string. A space character in bash normally
separates command line arguments. Escaping it with a backslash turns
that special meaning off, with the result that the entire file name,
including the embedded spaces, is parsed as a single command line
argument to be passed to pdfinfo. As Keith pointed out, the following
also works:
system("pdfinfo 'file with space'");
I cannot open pdfinfo (in my knowledge) with fopen, as then,
sprintf(pdfinfo, "pdfinfo %s >filout", pdfname);
will give error, as I am printing a FILE as char.
what is the way out?
One way is to insert a backslash into the file name before any spaces,
or to surround it with single quotes, before calling system().
If you need to look at files with other unusual characters embedded in
them (such as backslashes, single quotes, etc.), you may need a more
complete solution. You might want to look at the exec*() family of POSIX
functions - they pass the specified list of arguments directly to the
program being executed, without any command line processing. You'll need
to use fork() before calling it, and only call the exec*() function in
the child processif there's anything you want the program to do after
calling it.