summaryrefslogtreecommitdiff
path: root/src/misc/get_current_dir_name.c
blob: e0f463b5caa21b2702dd9b4ba25994a6ab177c66 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <sys/stat.h>

char *get_current_dir_name(void) {
	struct stat a, b;
	char buf[PATH_MAX];
	char *res = getenv("PWD");
	if (res && *res && !stat(res, &a) && !stat(".", &b)
	    && (a.st_dev == b.st_dev) && (a.st_ino == b.st_ino))
		return strdup(res);
	if(!getcwd(buf, sizeof(buf))) return NULL;
	return strdup(buf);
}