How do C programs pass whitespace arguments to the libc system(3) calls? -
when c program calls system()
run unix command, know it's possible pass arguments command, , according stackoverflow answer (from high-rep user), the system()
call uses shell execute command.
it surprised me see system("ls -lh >/dev/null 2>&1");
example system()
call c program, since looks using same whitespace delimited "words" shell uses interactively.
from standpoint sysadmin, i'd understand provisos , pitfalls when system()
call going executed within c program on files or commands on system. passing whitespace-containing filenames shell script very issue-prone; there similar issues when c program calling command?
or make point blunter (though less exact): c program written novice break on whitespace-containing filenames shell script?
there nothing in c source here, string pass system() run in shell context. shell parse string, c program doesn't.
if @ system() function prototype:
#include <stdlib.h> int system(const char *command);
the argument passed system()
string. has nothing whitespaces characters in string, gets string , passes string other system call. same did:
sh -c 'ls -l'
here system() use execve():
$ cat <<\code | gcc -xc - #include <stdlib.h> int main(void) { system("ls -l"); return 0; } code $ strace -fe execve ./a.out execve("./a.out", ["./a.out"], [/* 64 vars */]) = 0 process 16281 attached [pid 16281] execve("/bin/sh", ["sh", "-c", "ls -l"], [/* 64 vars */]) = 0 process 16282 attached [pid 16282] execve("/bin/ls", ["ls", "-l"], [/* 64 vars */]) = 0 ...
Comments
Post a Comment