fixed it (duh, rtfm)
This commit is contained in:
49
realpath.c
49
realpath.c
@@ -69,9 +69,9 @@ extern int lstat();
|
|||||||
* Prototypes
|
* Prototypes
|
||||||
*/
|
*/
|
||||||
#ifdef HAVE_FCHDIR
|
#ifdef HAVE_FCHDIR
|
||||||
static char * realpath_ret __P((char *, FILE *));
|
static void realpath_restore __P((FILE *));
|
||||||
#else
|
#else
|
||||||
static char * realpath_ret __P((char *, char *));
|
static void realpath_restore __P((char *));
|
||||||
#endif /* HAVE_FCHDIR */
|
#endif /* HAVE_FCHDIR */
|
||||||
|
|
||||||
|
|
||||||
@@ -97,7 +97,7 @@ char * realpath(old, new)
|
|||||||
#ifdef HAVE_FCHDIR
|
#ifdef HAVE_FCHDIR
|
||||||
FILE * cwd; /* current working dir */
|
FILE * cwd; /* current working dir */
|
||||||
#else
|
#else
|
||||||
static char cwd[MAXPATHLEN]; /* old working dir */
|
char cwd[MAXPATHLEN]; /* old working dir */
|
||||||
#endif /* HAVE_FCHDIR */
|
#endif /* HAVE_FCHDIR */
|
||||||
struct stat statbuf; /* for lstat() */
|
struct stat statbuf; /* for lstat() */
|
||||||
char * temp; /* temporary ptr */
|
char * temp; /* temporary ptr */
|
||||||
@@ -127,8 +127,10 @@ char * realpath(old, new)
|
|||||||
while (!lstat(new, &statbuf) && (statbuf.st_mode & _S_IFMT) == _S_IFLNK) {
|
while (!lstat(new, &statbuf) && (statbuf.st_mode & _S_IFMT) == _S_IFLNK) {
|
||||||
/* it's a link */
|
/* it's a link */
|
||||||
|
|
||||||
if ((len = readlink(new, buf, sizeof(buf))) <= 0)
|
if ((len = readlink(new, buf, sizeof(buf))) <= 0) {
|
||||||
return(realpath_ret(NULL, cwd));
|
realpath_restore(cwd);
|
||||||
|
return(NULL);
|
||||||
|
}
|
||||||
buf[len] = '\0';
|
buf[len] = '\0';
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -143,7 +145,8 @@ char * realpath(old, new)
|
|||||||
|
|
||||||
if (strlen(new) + strlen(buf) >= MAXPATHLEN ) {
|
if (strlen(new) + strlen(buf) >= MAXPATHLEN ) {
|
||||||
errno = ENAMETOOLONG;
|
errno = ENAMETOOLONG;
|
||||||
return(realpath_ret(NULL, cwd));
|
realpath_restore(cwd);
|
||||||
|
return(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
(void) strcat(new, buf);
|
(void) strcat(new, buf);
|
||||||
@@ -151,8 +154,10 @@ char * realpath(old, new)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* did an lstat() fail? */
|
/* did an lstat() fail? */
|
||||||
if (errno)
|
if (errno) {
|
||||||
return(realpath_ret(NULL, cwd));
|
realpath_restore(cwd);
|
||||||
|
return(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* separate the last component from the rest of the path
|
* separate the last component from the rest of the path
|
||||||
@@ -161,7 +166,8 @@ char * realpath(old, new)
|
|||||||
if (!(temp = strrchr(new, '/'))) {
|
if (!(temp = strrchr(new, '/'))) {
|
||||||
/* this should not happen */
|
/* this should not happen */
|
||||||
errno = ENOTDIR;
|
errno = ENOTDIR;
|
||||||
return(realpath_ret(NULL, cwd));
|
realpath_restore(cwd);
|
||||||
|
return(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
(void) strcpy(buf, ++temp);
|
(void) strcpy(buf, ++temp);
|
||||||
@@ -171,11 +177,15 @@ char * realpath(old, new)
|
|||||||
* chdir() to new and go a getcwd() to find real path then
|
* chdir() to new and go a getcwd() to find real path then
|
||||||
* append buf * (last component of the path) and return.
|
* append buf * (last component of the path) and return.
|
||||||
*/
|
*/
|
||||||
if (chdir(new))
|
if (chdir(new)) {
|
||||||
return(realpath_ret(NULL, cwd));
|
realpath_restore(cwd);
|
||||||
|
return(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
if (!(getcwd(new, MAXPATHLEN)))
|
if (!(getcwd(new, MAXPATHLEN))) {
|
||||||
return(realpath_ret(NULL, cwd));
|
realpath_restore(cwd);
|
||||||
|
return(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
/* append "/" and buf to new but watch for double '/' */
|
/* append "/" and buf to new but watch for double '/' */
|
||||||
len = strlen(new);
|
len = strlen(new);
|
||||||
@@ -189,7 +199,8 @@ char * realpath(old, new)
|
|||||||
}
|
}
|
||||||
(void) strcat(new, buf);
|
(void) strcat(new, buf);
|
||||||
|
|
||||||
return(realpath_ret(new, cwd));
|
realpath_restore(cwd);
|
||||||
|
return(new);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -201,21 +212,18 @@ char * realpath(old, new)
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#ifdef HAVE_FCHDIR
|
#ifdef HAVE_FCHDIR
|
||||||
static char * realpath_ret(path, cwd)
|
static void realpath_restore(cwd)
|
||||||
char * path;
|
|
||||||
FILE * cwd;
|
FILE * cwd;
|
||||||
{
|
{
|
||||||
int old_errno = errno; /* so we can restore errno... */
|
int old_errno = errno; /* so we can restore errno... */
|
||||||
|
|
||||||
(void) fchdir(cwd);
|
(void) fchdir(fileno(cwd));
|
||||||
(void) fclose(cwd);
|
(void) fclose(cwd);
|
||||||
|
|
||||||
errno = old_errno;
|
errno = old_errno;
|
||||||
return(path);
|
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
static char * realpath_ret(path, cwd)
|
static void realpath_restore(cwd)
|
||||||
char * path;
|
|
||||||
char * cwd;
|
char * cwd;
|
||||||
{
|
{
|
||||||
int old_errno = errno; /* so we can restore errno... */
|
int old_errno = errno; /* so we can restore errno... */
|
||||||
@@ -223,6 +231,5 @@ static char * realpath_ret(path, cwd)
|
|||||||
(void) chdir(cwd);
|
(void) chdir(cwd);
|
||||||
|
|
||||||
errno = old_errno;
|
errno = old_errno;
|
||||||
return(path);
|
|
||||||
}
|
}
|
||||||
#endif /* HAVE_FCHDIR */
|
#endif /* HAVE_FCHDIR */
|
||||||
|
@@ -69,9 +69,9 @@ extern int lstat();
|
|||||||
* Prototypes
|
* Prototypes
|
||||||
*/
|
*/
|
||||||
#ifdef HAVE_FCHDIR
|
#ifdef HAVE_FCHDIR
|
||||||
static char * realpath_ret __P((char *, FILE *));
|
static void realpath_restore __P((FILE *));
|
||||||
#else
|
#else
|
||||||
static char * realpath_ret __P((char *, char *));
|
static void realpath_restore __P((char *));
|
||||||
#endif /* HAVE_FCHDIR */
|
#endif /* HAVE_FCHDIR */
|
||||||
|
|
||||||
|
|
||||||
@@ -97,7 +97,7 @@ char * realpath(old, new)
|
|||||||
#ifdef HAVE_FCHDIR
|
#ifdef HAVE_FCHDIR
|
||||||
FILE * cwd; /* current working dir */
|
FILE * cwd; /* current working dir */
|
||||||
#else
|
#else
|
||||||
static char cwd[MAXPATHLEN]; /* old working dir */
|
char cwd[MAXPATHLEN]; /* old working dir */
|
||||||
#endif /* HAVE_FCHDIR */
|
#endif /* HAVE_FCHDIR */
|
||||||
struct stat statbuf; /* for lstat() */
|
struct stat statbuf; /* for lstat() */
|
||||||
char * temp; /* temporary ptr */
|
char * temp; /* temporary ptr */
|
||||||
@@ -127,8 +127,10 @@ char * realpath(old, new)
|
|||||||
while (!lstat(new, &statbuf) && (statbuf.st_mode & _S_IFMT) == _S_IFLNK) {
|
while (!lstat(new, &statbuf) && (statbuf.st_mode & _S_IFMT) == _S_IFLNK) {
|
||||||
/* it's a link */
|
/* it's a link */
|
||||||
|
|
||||||
if ((len = readlink(new, buf, sizeof(buf))) <= 0)
|
if ((len = readlink(new, buf, sizeof(buf))) <= 0) {
|
||||||
return(realpath_ret(NULL, cwd));
|
realpath_restore(cwd);
|
||||||
|
return(NULL);
|
||||||
|
}
|
||||||
buf[len] = '\0';
|
buf[len] = '\0';
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -143,7 +145,8 @@ char * realpath(old, new)
|
|||||||
|
|
||||||
if (strlen(new) + strlen(buf) >= MAXPATHLEN ) {
|
if (strlen(new) + strlen(buf) >= MAXPATHLEN ) {
|
||||||
errno = ENAMETOOLONG;
|
errno = ENAMETOOLONG;
|
||||||
return(realpath_ret(NULL, cwd));
|
realpath_restore(cwd);
|
||||||
|
return(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
(void) strcat(new, buf);
|
(void) strcat(new, buf);
|
||||||
@@ -151,8 +154,10 @@ char * realpath(old, new)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* did an lstat() fail? */
|
/* did an lstat() fail? */
|
||||||
if (errno)
|
if (errno) {
|
||||||
return(realpath_ret(NULL, cwd));
|
realpath_restore(cwd);
|
||||||
|
return(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* separate the last component from the rest of the path
|
* separate the last component from the rest of the path
|
||||||
@@ -161,7 +166,8 @@ char * realpath(old, new)
|
|||||||
if (!(temp = strrchr(new, '/'))) {
|
if (!(temp = strrchr(new, '/'))) {
|
||||||
/* this should not happen */
|
/* this should not happen */
|
||||||
errno = ENOTDIR;
|
errno = ENOTDIR;
|
||||||
return(realpath_ret(NULL, cwd));
|
realpath_restore(cwd);
|
||||||
|
return(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
(void) strcpy(buf, ++temp);
|
(void) strcpy(buf, ++temp);
|
||||||
@@ -171,11 +177,15 @@ char * realpath(old, new)
|
|||||||
* chdir() to new and go a getcwd() to find real path then
|
* chdir() to new and go a getcwd() to find real path then
|
||||||
* append buf * (last component of the path) and return.
|
* append buf * (last component of the path) and return.
|
||||||
*/
|
*/
|
||||||
if (chdir(new))
|
if (chdir(new)) {
|
||||||
return(realpath_ret(NULL, cwd));
|
realpath_restore(cwd);
|
||||||
|
return(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
if (!(getcwd(new, MAXPATHLEN)))
|
if (!(getcwd(new, MAXPATHLEN))) {
|
||||||
return(realpath_ret(NULL, cwd));
|
realpath_restore(cwd);
|
||||||
|
return(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
/* append "/" and buf to new but watch for double '/' */
|
/* append "/" and buf to new but watch for double '/' */
|
||||||
len = strlen(new);
|
len = strlen(new);
|
||||||
@@ -189,7 +199,8 @@ char * realpath(old, new)
|
|||||||
}
|
}
|
||||||
(void) strcat(new, buf);
|
(void) strcat(new, buf);
|
||||||
|
|
||||||
return(realpath_ret(new, cwd));
|
realpath_restore(cwd);
|
||||||
|
return(new);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -201,21 +212,18 @@ char * realpath(old, new)
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#ifdef HAVE_FCHDIR
|
#ifdef HAVE_FCHDIR
|
||||||
static char * realpath_ret(path, cwd)
|
static void realpath_restore(cwd)
|
||||||
char * path;
|
|
||||||
FILE * cwd;
|
FILE * cwd;
|
||||||
{
|
{
|
||||||
int old_errno = errno; /* so we can restore errno... */
|
int old_errno = errno; /* so we can restore errno... */
|
||||||
|
|
||||||
(void) fchdir(cwd);
|
(void) fchdir(fileno(cwd));
|
||||||
(void) fclose(cwd);
|
(void) fclose(cwd);
|
||||||
|
|
||||||
errno = old_errno;
|
errno = old_errno;
|
||||||
return(path);
|
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
static char * realpath_ret(path, cwd)
|
static void realpath_restore(cwd)
|
||||||
char * path;
|
|
||||||
char * cwd;
|
char * cwd;
|
||||||
{
|
{
|
||||||
int old_errno = errno; /* so we can restore errno... */
|
int old_errno = errno; /* so we can restore errno... */
|
||||||
@@ -223,6 +231,5 @@ static char * realpath_ret(path, cwd)
|
|||||||
(void) chdir(cwd);
|
(void) chdir(cwd);
|
||||||
|
|
||||||
errno = old_errno;
|
errno = old_errno;
|
||||||
return(path);
|
|
||||||
}
|
}
|
||||||
#endif /* HAVE_FCHDIR */
|
#endif /* HAVE_FCHDIR */
|
||||||
|
Reference in New Issue
Block a user