Code:
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
int main(void)
{
char new_dir[] = "/home/tmp/";
if (chdir(new_dir) != 0) {
perror("Call to chdir failed! ");
exit(EXIT_FAILURE);
}
printf("chdir to %s successful!\n", new_dir);
return 0;
}
kermit@fastbox ~/cprogs/board $ gcc -Wall -o my_cd my_cd.c
kermit@fastbox ~/cprogs/board $ ./my_cd
Call to chdir failed! : No such file
Change one line to
Code:
char new_dir[] = "/home/kermit/tmp/";
kermit@fastbox ~/cprogs/board $ gcc -Wall -o my_cd my_cd.c
kermit@fastbox ~/cprogs/board $ ./my_cd
chdir to /home/kermit/tmp/ successful!
kermit@fastbox ~/cprogs/board $
There you go. There is an awful lot more you can (and probably should) do with that to make it more robust, but that is the basics of the system call. You can get all of that from the man page for chdir.