1 2 3 4 5 6 7 8 9 10 11 12
#include <stdlib.h> #include <string.h> #include <limits.h> #include <unistd.h> char *get_current_dir_name(void) { char buf[PATH_MAX]; char* res = getenv("PWD"); if(res && *res) return strdup(res); if(!getcwd(buf, sizeof(buf))) return NULL; return strdup(buf); }