#include<stdio.h>
int main()
{
char buf[80];
getcwd(buf, sizeof(buf));
printf("Firstly the current directory is: %s\n", buf);
chdir("/var");
getcwd(buf, sizeof(buf));
printf("Then the current directory is: %s\n", buf);
return 0;
}
Using assembly language embed C to implement system call chdir
#include<stdio.h>static __attribute__((used)) char * dir;
int main()
{
char buf[80];
getcwd(buf, sizeof(buf));
printf("Firstly the current directory is: %s\n", buf);
dir = "/var";
//chdir("/var");asmvolatile(
"mov dir, %ebx\n\t""mov $0xc, %eax\n\t""int $0x80\n\t"
);
getcwd(buf, sizeof(buf));
printf("Then the current directory is: %s\n", buf);
return0;
}