add get_current_dir_name function
authorRich Felker <dalias@aerifal.cx>
Sat, 18 Feb 2012 04:10:00 +0000 (23:10 -0500)
committerRich Felker <dalias@aerifal.cx>
Sat, 18 Feb 2012 04:10:00 +0000 (23:10 -0500)
include/unistd.h
src/misc/get_current_dir_name.c [new file with mode: 0644]

index 95b514ec474dd38ddafb52d012da95fa5777b8e4..7662e5188936536beaa4c60ed8362cd8931c5284 100644 (file)
@@ -158,6 +158,7 @@ unsigned ualarm(unsigned, unsigned);
 int setgroups(size_t, const gid_t []);
 int setresuid(uid_t, uid_t, uid_t);
 int setresgid(gid_t, gid_t, gid_t);
+char *get_current_dir_name(void);
 #endif
 
 #define _XOPEN_VERSION          700
diff --git a/src/misc/get_current_dir_name.c b/src/misc/get_current_dir_name.c
new file mode 100644 (file)
index 0000000..212edf3
--- /dev/null
@@ -0,0 +1,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);
+}