add get_current_dir_name function

This commit is contained in:
Rich Felker 2012-02-17 23:10:00 -05:00
parent 414a4cdebc
commit 1611ab0d9b
2 changed files with 13 additions and 0 deletions

View 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

View File

@ -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);
}