Thu, 13 Dec 2007

chdir(2)
So today at work another developer many years my senior, with many more years experience than I, came to me with a Unixy problem.
"When I have a program, how can I have it so the current working directory for all processes it starts isn't the one that it started in?"
"chdir."
"No, I want so that if this process starts something like ls, when ls stats 'dot' I want 'dot' to be the directory that process wants it to be, not the directory that process was started from."
After about 15 minutes of me suggesting chdir while he said that's not what he wanted but then describing chdir, I finally wrote something along the lines of the following

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int
main (int argc, char *argv[])
{
  system("/bin/pwd");
  system("/bin/ls");
  chdir("/tmp");
  system("/bin/pwd");
  system("/bin/ls");
  chdir("/etc");
  system("/bin/pwd");
  system("/bin/ls");
  chdir("/");
  system("/bin/pwd");
  system("/bin/ls");
  return 0;
}

I compiled that, ran it, showed him the output. He said, "Yeah, that's what I want to do."
I showed him the code.
"chdir does that?"

writebacks

writebacks...

trackback

TrackBack ping me at:

http://www.rainysoul.net/~mforde/blog/index.pl.trackback

comment...

 
Name:
URL/Email: [http://... or mailto:you@wherever] (optional)
Title: (optional)
Comments:
Save my Name and URL/Email for next time
Captcha: To prevent comment spam, prove you're a human or a close approximation:
<@captcha.get output="yes" />
Enter text here: