replace pdksh’s get_phys_path() and do_phys_path() with my new

do_realpath() function – looking fine, review welcome though
This commit is contained in:
tg 2009-08-30 13:30:08 +00:00
parent 31e02e59f7
commit e622d80394
3 changed files with 10 additions and 82 deletions

12
funcs.c
View File

@ -25,7 +25,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.126 2009/08/30 13:22:38 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.127 2009/08/30 13:30:07 tg Exp $");
#if HAVE_KILLPG
/*
@ -417,6 +417,10 @@ c_cd(const char **wp)
return (1);
}
/* allocd (above) => dir, which is no longer used */
afree(allocd, ATEMP);
allocd = NULL;
/* Clear out tracked aliases with relative paths */
flushcom(0);
@ -429,13 +433,13 @@ c_cd(const char **wp)
if (Xstring(xs, xp)[0] != '/') {
pwd = NULL;
} else
if (!physical || !(pwd = get_phys_path(Xstring(xs, xp))))
} else if (!physical || !(pwd = allocd = do_realpath(Xstring(xs, xp))))
pwd = Xstring(xs, xp);
/* Set PWD */
if (pwd) {
char *ptmp = pwd;
set_current_wd(ptmp);
/* Ignore failure (happens if readonly or integer) */
setstr(pwd_s, ptmp, KSH_RETURN_ERROR);
@ -475,7 +479,7 @@ c_pwd(const char **wp)
bi_errorf("too many arguments");
return (1);
}
p = current_wd[0] ? (physical ? get_phys_path(current_wd) :
p = current_wd[0] ? (physical ? allocd = do_realpath(current_wd) :
current_wd) : NULL;
if (p && access(p, R_OK) < 0)
p = NULL;

77
misc.c
View File

@ -29,7 +29,7 @@
#include <grp.h>
#endif
__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.117 2009/08/28 21:07:26 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.118 2009/08/30 13:30:07 tg Exp $");
#undef USE_CHVT
/* XXX conditions correct? */
@ -50,7 +50,6 @@ static const unsigned char *cclass(const unsigned char *, int);
#ifdef USE_CHVT
static void chvt(const char *);
#endif
static char *do_phys_path(XString *, char *, const char *);
/*
* Fast character classes
@ -1273,80 +1272,6 @@ set_current_wd(char *pathl)
afree(p, ATEMP);
}
char *
get_phys_path(const char *pathl)
{
XString xs;
char *xp;
Xinit(xs, xp, strlen(pathl) + 1, ATEMP);
xp = do_phys_path(&xs, xp, pathl);
if (!xp)
return (NULL);
if (Xlength(xs, xp) == 0)
Xput(xs, xp, '/');
Xput(xs, xp, '\0');
return (Xclose(xs, xp));
}
static char *
do_phys_path(XString *xsp, char *xp, const char *pathl)
{
const char *p, *q;
int len, llen, savepos;
char *lbuf;
lbuf = alloc(PATH_MAX, ATEMP);
Xcheck(*xsp, xp);
for (p = pathl; p; p = q) {
while (*p == '/')
p++;
if (!*p)
break;
len = (q = cstrchr(p, '/')) ? q - p : (int)strlen(p);
if (len == 1 && p[0] == '.')
continue;
if (len == 2 && p[0] == '.' && p[1] == '.') {
while (xp > Xstring(*xsp, xp)) {
xp--;
if (*xp == '/')
break;
}
continue;
}
savepos = Xsavepos(*xsp, xp);
Xput(*xsp, xp, '/');
XcheckN(*xsp, xp, len + 1);
memcpy(xp, p, len);
xp += len;
*xp = '\0';
llen = readlink(Xstring(*xsp, xp), lbuf, PATH_MAX - 1);
if (llen < 0) {
if (errno == EINVAL)
/* not a symbolic link */
continue;
xp = NULL;
goto out;
}
lbuf[llen] = '\0';
/* If absolute path, start from scratch.. */
xp = lbuf[0] == '/' ? Xstring(*xsp, xp) :
Xrestpos(*xsp, xp, savepos);
if ((xp = do_phys_path(xsp, xp, lbuf)) == NULL)
break;
}
out:
afree(lbuf, ATEMP);
return (xp);
}
#ifdef USE_CHVT
static void
chvt(const char *fn)

3
sh.h
View File

@ -134,7 +134,7 @@
#endif
#ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.335 2009/08/30 13:22:39 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.336 2009/08/30 13:30:08 tg Exp $");
#endif
#define MKSH_VERSION "R39 2009/08/28"
@ -1578,7 +1578,6 @@ int reset_nonblock(int);
char *ksh_get_wd(size_t *);
int make_path(const char *, const char *, char **, XString *, int *);
void simplify_path(char *);
char *get_phys_path(const char *);
void set_current_wd(char *);
#ifdef MKSH_SMALL
char *strdup_(const char *, Area *);