get_current_dir_name.c 406 B

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