How to get to get the path to the current file (pwd) in Linux from C?

I would like to know if it is possible to run system("pwd") in the current DIR. So, for example, let this folder structure.

 example >test >>file >test2 >>file3 >>file4 

And with opendir() and readdir() I will go to file3 , and I want to use system("pwd") to get the path: ..../example/test2/file3 . Is this possible, or will pwd return the path to main.c all the time?

+5
source share
5 answers

Simply opening and reading directories does not change the current working directory. However, a directory change in your program will be.

for reference,

 #include <unistd.h> #include <stdio.h> int main() { char cwd[1024]; chdir("/path/to/change/directory/to"); getcwd(cwd, sizeof(cwd)); printf("Current working dir: %s\n", cwd); } 
+11
source

You can use chdir (2) to change the dir from C, then system("pwd"); will provide you with what has ever been a chdir ed to directory.

C-equivivalent of the pwd getcwd (3) pwd .

+3
source

For POSIX systems, I found three solutions:

Get value from "PWD" environment variables

 #include <stdio.h> #include <stdlib.h> #ifdef __unix__ #define IS_POSIX 1 #else #define IS_POSIX 0 #endif int main (int argv, char **argc) { if (IS_POSIX == 1) { puts("Path info by use environment variable PWD:"); printf("\tWorkdir: %s\n", getenv("PWD")); printf("\tFilepath: %s/%s\n", getenv("PWD"), __FILE__); } return 0; } 

Result:

 Path info by use environment variable PWD: Workdir: /media/setivolkylany/WorkDisk/Programming/Projects/c-utils Filepath: /media/setivolkylany/WorkDisk/Programming/Projects/c-utils/main.c 

Use getcwd ()

 #include <stdio.h> #include <stdlib.h> #ifdef __unix__ #define IS_POSIX 1 #include <unistd.h> #else #define IS_POSIX 0 #endif int main (int argv, char **argc) { if (IS_POSIX == 1) { char cwd[1024]; getcwd(cwd, sizeof(cwd)); puts("Path info by use getcwd():"); printf("\tWorkdir: %s\n", cwd); printf("\tFilepath: %s/%s\n", cwd, __FILE__); } return 0; } 

Result

 Path info by use getcwd(): Workdir: /media/setivolkylany/WorkDisk/Programming/Projects/c-utils Filepath: /media/setivolkylany/WorkDisk/Programming/Projects/c-utils/main.c 

Run the system command "pwd" and read the output

 #ifdef __unix__ #define IS_POSIX 1 #define _BSD_SOURCE #else #define IS_POSIX 0 #endif #include <stdio.h> #include <stdlib.h> #include <string.h> int main (int argv, char **argc) { if (IS_POSIX == 1) { char buffer[500]; FILE *output; // read output of a command output = popen("/bin/pwd", "r"); char *pwd = fgets(buffer, sizeof(buffer), output); // strip '\n' on ending of a line pwd = strtok(pwd, "\n"); puts("Path info by execute shell command 'pwd':"); printf("\tWorkdir: %s\n", pwd); printf("\tFilepath: %s/%s\n", pwd, __FILE__); } return 0; } 

Result:

 Path info by execute shell command 'pwd': Workdir: /media/setivolkylany/WorkDisk/Programming/Projects/c-utils Filepath: /media/setivolkylany/WorkDisk/Programming/Projects/c-utils/main.c 
+3
source

When you use the system call (...) on Windows and Linux, it just executes one command. You can do the same with the command file (you can create it using the C code), but my oppinion is that you have to use nftw () to get the directories and then use opendir () / readdir ().

0
source

How to not set path length hard using pathconf

I believe this is the right way to do this:

 #define _XOPEN_SOURCE 700 #include <assert.h> #include <stdlib.h> #include <stdio.h> #include <unistd.h> int main(void) { long n; char *buf; n = pathconf(".", _PC_PATH_MAX); assert(n != -1); buf = malloc(n * sizeof(*buf)); assert(buf); if (getcwd(buf, n) == NULL) { perror("getcwd"); exit(EXIT_FAILURE); } else { printf("%s\n", buf); } free(buf); return EXIT_SUCCESS; } 

GitHub upstream .

Compile and run:

 gcc -Wall -Wextra -std=c11 -pedantic-errors -o getcwd.out getcwd.c ./getcwd.out 

POSIX describes _PC_PATH_MAX as :

The value returned for the {PATH_MAX} variable indicates the longest relative path name that can be specified if the specified directory is the current working directory of the process. A process cannot always generate such a long name and use it if the subdirectory in the path name goes to a more strict file system.

Tested on Ubuntu 18.10, n == 4096 in this implementation.

0
source

All Articles