I know that cd
is a shell built-in ,and I can run it by using system()
.
But is that possible to run the cd
command by the
While, as already stated system("cd xxx")
wouldn't change your application current directory, it is not completely useless.
You can still use system exit status to know if changing your current directory to the one stated would succeed or not.
Similarly, if you like complex solutions, you could also do the same with fork/exec, either with exec'ing /bin/sh -c cd xxx
or simply /bin/cd xxx
with OSes that provide an independent cd
executable.
I would however recommend this non overkill faster equivalent access("xxx", X_OK|R_OK)
Note: All POSIX compliant OSes must provide an independent cd executable. This is at least the case with Solaris, AIX, HP-UX and Mac OS/X.