1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00
cde/src/cmd/ksh93/bltins/cd_pwd.c
Martijn Dekker b590a9f155 [shp cleanup 01..20] all the rest (re: 2d3ec8b6)
This combines 20 cleanup commits from the dev branch.

All changed files:
- Clean up pointer defererences to sh.
- Remove shp arguments from functions.

Other notable changes:

src/cmd/ksh93/include/shell.h,
src/cmd/ksh93/sh/init.c:
- On second thought, get rid of the function version of
  sh_getinterp() as libshell ABI compatibility is moot. We've
  already been breaking that by reordering the sh struct, so there
  is no way it's going to work without recompiling.

src/cmd/ksh93/sh/name.c:
- De-obfuscate the relationship between nv_scan() and scanfilter().
  The former just calls the latter as a static function, there's no
  need to do that via a function pointer and void* type conversions.

src/cmd/ksh93/bltins/typeset.c,
src/cmd/ksh93/sh/name.c,
src/cmd/ksh93/sh/nvdisc.c:
- 'struct adata' and 'struct tdata', defined as local struct types
  in these files, need to have their first three fields in common,
  the first being a pointer to sh. This is because scanfilter() in
  name.c accesses these fields via a type conversion. So the sh
  field needed to be removed in all three at the same time.
  TODO: de-obfuscate: good practice definition via a header file.

src/cmd/ksh93/sh/path.c:
- Naming consistency: reserve the path_ function name prefix for
  externs and rename statics with that prefix.
- The default path was sometimes referred to as the standard path.
  To use one term, rename std_path to defpath and onstdpath() to
  ondefpath().
- De-obfuscate SHOPT_PFSH conditional code by only calling
  pf_execve() (was path_pfexecve()) if that is compiled in.

src/cmd/ksh93/include/streval.h,
src/cmd/ksh93/sh/streval.c:
- Rename extern strval() to arith_strval() for consistency.

src/cmd/ksh93/sh/string.c:
- Remove outdated/incorrect isxdigit() fallback; '#ifnded isxdigit'
  is not a correct test as isxdigit() is specified as a function.
  Plus, it's part of C89/C90 which we now require. (re: ac8991e5)

src/cmd/ksh93/sh/suid_exec.c:
- Replace an incorrect reference to shgd->current_pid with
  getpid(); it cannot work as (contrary to its misleading directory
  placement) suid_exec is an independent libast program with no
  link to ksh or libshell at all. However, no one noticed because
  this was in fallback code for ancient systems without
  setreuid(2). Since that standard function was specified in POSIX
  Issue 4 Version 2 from 1994, we should remove that fallback code
  sometime as part of another obsolete code cleanup operation to
  avoid further bit rot. (re: 843b546c)

src/cmd/ksh93/bltins/print.c: genformat():
- Remove preformat[] which was always empty and had no effect.

src/cmd/ksh93/shell.3:
- Minor copy-edit.
- Remove documentation for nonexistent sh.infile_name. A search
  through ast-open-archive[*] reveals this never existed at all.
- Document sh.savexit (== $?).

src/cmd/ksh93/shell.3,
src/cmd/ksh93/include/shell.h,
src/cmd/ksh93/sh/init.c:
- Remove sh.gd/shgd; this is now unused and was never documented
  or exposed in the shell.h public interface.
- sh_sigcheck() was documented in shell.3 as taking no arguments
  whereas in the actual code it took a shp argument. I decided to
  go with the documentation.
- That leaves sh_parse() as the only documented function that still
  takes an shp argument. I'm just going to go ahead and remove it
  for consistency, reverting sh_parse() to its pre-2003 spec.
- Remove undocumented/unused sh_bltin_tree() function which simply
  returned sh.bltin_tree.
- Bump SH_VERSION to 20220106.
2022-01-07 16:16:31 +00:00

306 lines
8.1 KiB
C

/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1982-2012 AT&T Intellectual Property *
* Copyright (c) 2020-2021 Contributors to ksh 93u+m *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.eclipse.org/org/documents/epl-v10.html *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* David Korn <dgk@research.att.com> *
* *
***********************************************************************/
/*
* cd [-L] [-Pe] [dirname]
* cd [-L] [-Pe] [old] [new]
* pwd [-LP]
*
* David Korn
* AT&T Labs
* research!dgk
*
*/
#include "defs.h"
#include <stak.h>
#include <error.h>
#include "variables.h"
#include "path.h"
#include "name.h"
#include "builtins.h"
#include <ls.h>
#include "test.h"
/*
* Invalidate path name bindings to relative paths
*/
static void rehash(register Namval_t *np,void *data)
{
Pathcomp_t *pp = (Pathcomp_t*)np->nvalue.cp;
if(pp && *pp->name!='/')
nv_rehash(np,data);
}
int b_cd(int argc, char *argv[],Shbltin_t *context)
{
register char *dir;
Pathcomp_t *cdpath = 0;
register const char *dp;
int saverrno=0;
int rval,pflag=0,eflag=0,ret=1;
char *oldpwd;
Namval_t *opwdnod, *pwdnod;
NOT_USED(context);
while((rval = optget(argv,sh_optcd))) switch(rval)
{
case 'e':
eflag = 1;
break;
case 'L':
pflag = 0;
break;
case 'P':
pflag = 1;
break;
case ':':
if(sh_isoption(SH_RESTRICTED))
break;
errormsg(SH_DICT,2, "%s", opt_info.arg);
break;
case '?':
if(sh_isoption(SH_RESTRICTED))
break;
errormsg(SH_DICT,ERROR_usage(2), "%s", opt_info.arg);
UNREACHABLE();
}
if(pflag && eflag)
ret = 2; /* exit status is 2 if -eP are both on and chdir failed */
if(sh_isoption(SH_RESTRICTED))
{
/* restricted shells cannot change the directory */
errormsg(SH_DICT,ERROR_exit(ret),e_restricted+4);
UNREACHABLE();
}
argv += opt_info.index;
argc -= opt_info.index;
dir = argv[0];
if(error_info.errors>0 || argc>2)
{
errormsg(SH_DICT,ERROR_usage(2),"%s",optusage((char*)0));
UNREACHABLE();
}
oldpwd = path_pwd();
opwdnod = sh_scoped(OLDPWDNOD);
pwdnod = sh_scoped(PWDNOD);
if(oldpwd == e_dot && pwdnod->nvalue.cp)
oldpwd = (char*)pwdnod->nvalue.cp; /* if path_pwd() failed to get the pwd, use $PWD */
if(sh.subshell)
{
/* clone $OLDPWD and $PWD into the subshell's scope */
opwdnod = sh_assignok(opwdnod,1);
pwdnod = sh_assignok(pwdnod,1);
}
if(argc==2)
dir = sh_substitute(oldpwd,dir,argv[1]);
else if(!dir)
dir = nv_getval(HOME);
else if(*dir == '-' && dir[1]==0)
dir = nv_getval(opwdnod);
if(!dir || *dir==0)
{
errormsg(SH_DICT,ERROR_exit(ret),argc==2?e_subst+4:e_direct);
UNREACHABLE();
}
/*
* If sh_subshell() in subshell.c cannot use fchdir(2) to restore the PWD using a saved file descriptor,
* we must fork any virtual subshell now to avoid the possibility of ending up in the wrong PWD on exit.
*/
if(sh.subshell && !sh.subshare)
{
#if _lib_fchdir
if(!test_inode(nv_getval(pwdnod),e_dot))
#endif
sh_subfork();
}
/*
* Do $CDPATH processing, except if the path is absolute or the first component is '.' or '..'
*/
if(dir[0] != '/'
#if _WINIX
&& dir[1] != ':' /* on Windows, an initial drive letter plus ':' denotes an absolute path */
#endif /* _WINIX */
&& !(dir[0]=='.' && (dir[1]=='/' || dir[1]==0))
&& !(dir[0]=='.' && dir[1]=='.' && (dir[2]=='/' || dir[2]==0)))
{
if((dp=sh_scoped(CDPNOD)->nvalue.cp) && !(cdpath = (Pathcomp_t*)sh.cdpathlist))
{
if(cdpath=path_addpath((Pathcomp_t*)0,dp,PATH_CDPATH))
sh.cdpathlist = (void*)cdpath;
}
}
if(*dir!='/')
{
/* check for leading .. */
char *cp;
sfprintf(sh.strbuf,"%s",dir);
cp = sfstruse(sh.strbuf);
pathcanon(cp, 0);
if(cp[0]=='.' && cp[1]=='.' && (cp[2]=='/' || cp[2]==0))
{
if(!sh.strbuf2)
sh.strbuf2 = sfstropen();
sfprintf(sh.strbuf2,"%s/%s",oldpwd,cp);
dir = sfstruse(sh.strbuf2);
pathcanon(dir, 0);
}
}
rval = -1;
do
{
dp = cdpath?cdpath->name:"";
cdpath = path_nextcomp(cdpath,dir,0);
#if _WINIX
if(*stakptr(PATH_OFFSET+1)==':' && isalpha(*stakptr(PATH_OFFSET)))
{
*stakptr(PATH_OFFSET+1) = *stakptr(PATH_OFFSET);
*stakptr(PATH_OFFSET)='/';
}
#endif /* _WINIX */
if(*stakptr(PATH_OFFSET)!='/')
{
char *last=(char*)stakfreeze(1);
stakseek(PATH_OFFSET);
stakputs(oldpwd);
/* don't add '/' of oldpwd is / itself */
if(*oldpwd!='/' || oldpwd[1])
stakputc('/');
stakputs(last+PATH_OFFSET);
stakputc(0);
}
if(!pflag)
{
register char *cp;
stakseek(PATH_MAX+PATH_OFFSET);
if(*(cp=stakptr(PATH_OFFSET))=='/')
if(!pathcanon(cp,PATH_DOTDOT))
continue;
}
if((rval=chdir(path_relative(stakptr(PATH_OFFSET)))) >= 0)
goto success;
if(errno!=ENOENT && saverrno==0)
saverrno=errno;
}
while(cdpath);
if(rval<0 && *dir=='/' && *(path_relative(stakptr(PATH_OFFSET)))!='/')
rval = chdir(dir);
/* use absolute chdir() if relative chdir() fails */
if(rval<0)
{
if(saverrno)
errno = saverrno;
errormsg(SH_DICT,ERROR_system(ret),"%s:",dir);
UNREACHABLE();
}
success:
if(dir == nv_getval(opwdnod) || argc==2)
dp = dir; /* print out directory for cd - */
if(pflag)
{
dir = stakptr(PATH_OFFSET);
if (!(dir=pathcanon(dir,PATH_PHYSICAL)))
{
dir = stakptr(PATH_OFFSET);
errormsg(SH_DICT,ERROR_system(ret),"%s:",dir);
UNREACHABLE();
}
stakseek(dir-stakptr(0));
}
dir = (char*)stakfreeze(1)+PATH_OFFSET;
if(*dp && (*dp!='.'||dp[1]) && strchr(dir,'/'))
sfputr(sfstdout,dir,'\n');
nv_putval(opwdnod,oldpwd,NV_RDONLY);
if(*dir == '/')
{
size_t len = strlen(dir);
/* delete trailing '/' */
while(--len>0 && dir[len]=='/')
dir[len] = 0;
nv_putval(pwdnod,dir,NV_RDONLY);
nv_onattr(pwdnod,NV_EXPORT);
if(sh.pwd)
free((void*)sh.pwd);
sh.pwd = sh_strdup(pwdnod->nvalue.cp);
}
else
{
/* pathcanon() failed to canonicalize the directory, which happens when 'cd' is invoked from a
nonexistent PWD with a relative path as the argument. Reinitialize $PWD as it will be wrong. */
if(sh.pwd)
free((void*)sh.pwd);
sh.pwd = NIL(const char*);
path_pwd();
if(*sh.pwd != '/')
{
errormsg(SH_DICT,ERROR_system(ret),e_direct);
UNREACHABLE();
}
}
nv_scan(sh_subtracktree(1),rehash,(void*)0,NV_TAGGED,NV_TAGGED);
path_newdir(sh.pathlist);
path_newdir(sh.cdpathlist);
if(pflag && eflag)
{
/* Verify the current working directory matches $PWD */
return(!test_inode(e_dot,nv_getval(pwdnod)));
}
return(0);
}
int b_pwd(int argc, char *argv[],Shbltin_t *context)
{
register int n, flag = 0;
register char *cp;
NOT_USED(argc);
NOT_USED(context);
while((n = optget(argv,sh_optpwd))) switch(n)
{
case 'L':
flag = 0;
break;
case 'P':
flag = 1;
break;
case ':':
errormsg(SH_DICT,2, "%s", opt_info.arg);
break;
case '?':
errormsg(SH_DICT,ERROR_usage(2), "%s", opt_info.arg);
UNREACHABLE();
}
if(error_info.errors)
{
errormsg(SH_DICT,ERROR_usage(2),"%s",optusage((char*)0));
UNREACHABLE();
}
if(*(cp = path_pwd()) != '/')
{
errormsg(SH_DICT,ERROR_system(1), e_pwd);
UNREACHABLE();
}
if(flag)
{
cp = strcpy(stakseek(strlen(cp)+PATH_MAX),cp);
pathcanon(cp,PATH_PHYSICAL);
}
sfputr(sfstdout,cp,'\n');
return(0);
}