Go back to Previous Directory in Linux using a C program

后端 未结 3 1541
不知归路
不知归路 2021-01-24 18:38

I am in the directory /home/destination I need to go back to the /home directory. Any ideas on how to implement this using a C-program?

3条回答
  •  滥情空心
    2021-01-24 19:09

    If you'd like to go level up chdir(".."); will do the work. But if you'd like to have behaviour like cd - then you should use this code:

    char *prev;
    prev = getcwd(prev, 0); /*POSIX.1-2001: will malloc enough memory*/
    /*fail if prev is NULL, do something*/
    chdir(prev);
    free(prev);
    

提交回复
热议问题