Skip to content

Commit

Permalink
Merge branch 'dev' into fix-builtins-useafterfree
Browse files Browse the repository at this point in the history
  • Loading branch information
McDutchie committed Jan 5, 2025
2 parents 6583833 + b711e5d commit 57f491d
Show file tree
Hide file tree
Showing 58 changed files with 414 additions and 471 deletions.
1 change: 1 addition & 0 deletions COPYRIGHT
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ ksh 93u+m general copyright notice
# Chase <nicetrynsa@protonmail.ch> #
# rymrg <54061433+rymrg@users.noreply.github.com> #
# hyousatsu <118750527+hyousatsu@users.noreply.github.com> #
# dnewhall <dnewhall@users.noreply.github.com> #
# Trey Valenta <t@trey.net> #
# Sterling Jensen <5555776+sterlingjensen@users.noreply.github.com> #
# Marc Wilson <posguy99@gmail.com> #
Expand Down
12 changes: 12 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ This documents significant changes in the dev branch of ksh 93u+m.
For full details, see the git log at: https://github.com/ksh93/ksh
Uppercase BUG_* IDs are shell bug IDs as used by the Modernish shell library.

2025-01-03:

- The performance of virtual subshells has been significantly improved by
avoiding unnecessary syscalls to save and restore the working directory.
Thanks to Johnothan King for backporting and improving the AT&T 93v- code.

2024-12-30:

- The KEYBD trap should now be fully functional for multibyte characters
(for example, non-Latin characters in UTF-8 locales). This fixes a bug
inherited from AT&T and worked around on 2022-02-12.

2024-12-29:

- [v1.1] The emacs ^Y command now accepts a numeric parameter. For example,
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/INIT/include/link_ar.mam
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ exec - esac
note * Some old systems do not automatically update the symbol table upon ad
note * adding or deleting object files (contra POSIX), so update it manually

exec - %{AR} -s %{@}
exec - %{AR} -s %{@} 2>/dev/null || ranlib %{@} 2>/dev/null || true
12 changes: 6 additions & 6 deletions src/cmd/INIT/mamake.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* *
* This software is part of the ast package *
* Copyright (c) 1990-2013 AT&T Intellectual Property *
* Copyright (c) 2020-2024 Contributors to ksh 93u+m *
* Copyright (c) 2020-2025 Contributors to ksh 93u+m *
* and is licensed under the *
* Eclipse Public License, Version 2.0 *
* *
Expand All @@ -28,7 +28,7 @@
* coded for portability
*/

#define RELEASE_DATE "2024-12-03"
#define RELEASE_DATE "2025-01-02"
static char id[] = "\n@(#)$Id: mamake (ksh 93u+m) " RELEASE_DATE " $\0\n";

#if _PACKAGE_ast
Expand All @@ -43,7 +43,7 @@ static const char usage[] =
"[-author?Martijn Dekker <martijn@inlv.org>]"
"[-author?Contributors to https://github.com/ksh93/ksh]"
"[-copyright?(c) 1994-2013 AT&T Intellectual Property]"
"[-copyright?(c) 2020-2024 Contributors to ksh 93u+m]"
"[-copyright?(c) 2020-2025 Contributors to ksh 93u+m]"
"[-license?https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html]"
"[+NAME?mamake - make abstract machine make]"
"[+DESCRIPTION?\bmamake\b reads \amake abstract machine\a target and"
Expand Down Expand Up @@ -112,10 +112,10 @@ static const char usage[] =
* must be enabled to prevent the build from failing at the very start.
*/

#define _POSIX_C_SOURCE 21000101L /* generic */
#define _XOPEN_SOURCE 9900 /* generic */
#define _DARWIN_C_SOURCE 1 /* macOS */
#define _GNU_SOURCE 1 /* GNU/Linux, Cygwin */
#define _FILE_OFFSET_BITS 64 /* 32-bit Linux 5.6+ (glibc 2.32+) */
#define _TIME_BITS 64 /* 32-bit Linux 5.6+ (glibc 2.32+) */
#define _QNX_SOURCE 1 /* QNX */
#define __EXTENSIONS__ 1 /* Solaris/illumos */

Expand Down Expand Up @@ -409,7 +409,7 @@ static void report(int level, char *text, char *item, Rule_t *r)
fprintf(stderr, "%s", text);
if (r && r->time && state.debug <= -2)
#if __STDC_VERSION__ >= 199901L
fprintf(stderr, " %lld", (long long)r->time);
fprintf(stderr, " %llu", (unsigned long long)r->time);
#else
fprintf(stderr, " %lu", (unsigned long)r->time);
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/builtin/Mamfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ setv IFFEFLAGS
setv LDFLAGS

make install virtual
bind -lcmd
bind -lutil dontcare

note *
note * Build pty
note *

make pty
bind -lcmd
bind -lutil dontcare
make pty.o
make pty.c
make FEATURE/pty
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/builtin/pty.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ mkpty(int* master, int* minion)
static Proc_t*
runcmd(char** argv, int minion, int session)
{
long ops[4];
int64_t ops[4];

if (session)
{
Expand Down
4 changes: 0 additions & 4 deletions src/cmd/ksh93/bltins/alarm.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,9 @@ static void putval(Namval_t* np, const char* val, int flag, Namfun_t* fp)
if(val)
{
double now;
#ifdef timeofday
struct timeval tmp;
timeofday(&tmp);
now = tmp.tv_sec + 1.e-6*tmp.tv_usec;
#else
now = (double)time(NULL);
#endif /* timeofday */
nv_putv(np,val,flag,fp);
d = nv_getnum(np);
if(*val=='+')
Expand Down
86 changes: 73 additions & 13 deletions src/cmd/ksh93/bltins/cd_pwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* *
* This software is part of the ast package *
* Copyright (c) 1982-2012 AT&T Intellectual Property *
* Copyright (c) 2020-2024 Contributors to ksh 93u+m *
* Copyright (c) 2020-2025 Contributors to ksh 93u+m *
* and is licensed under the *
* Eclipse Public License, Version 2.0 *
* *
Expand Down Expand Up @@ -46,15 +46,37 @@ static void rehash(Namval_t *np,void *data)
nv_rehash(np,data);
}

#if _lib_openat
/*
* Obtain a file handle to the directory "path" relative to directory "dir"
*/
int sh_diropenat(int dir, const char *path)
{
int fd,shfd;
if((fd = openat(dir, path, O_DIRECTORY|O_NONBLOCK|O_cloexec)) < 0)
#if O_SEARCH
if(errno != EACCES || (fd = openat(dir, path, O_SEARCH|O_DIRECTORY|O_NONBLOCK|O_cloexec)) < 0)
#endif
return fd;
/* Move fd to a number > 10 and register the fd number with the shell */
shfd = sh_fcntl(fd, F_dupfd_cloexec, 10);
close(fd);
return shfd;
}
#endif /* _lib_openat */

int b_cd(int argc, char *argv[],Shbltin_t *context)
{
char *dir;
Pathcomp_t *cdpath = 0;
const char *dp;
int saverrno=0;
int rval,pflag=0,eflag=0,ret=1;
char *oldpwd;
char *oldpwd, *cp;
Namval_t *opwdnod, *pwdnod;
#if _lib_openat
int newdirfd;
#endif /* _lib_openat */
NOT_USED(context);
while((rval = optget(argv,sh_optcd))) switch(rval)
{
Expand Down Expand Up @@ -118,13 +140,13 @@ int b_cd(int argc, char *argv[],Shbltin_t *context)
* 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(sh.pwd,e_dot))
#endif
sh_subfork();
}
#if _lib_openat
if(sh.subshell && !sh.subshare && (!sh_validate_subpwdfd() || !test_inode(sh.pwd,e_dot)))
sh_subfork();
#else
if(sh.subshell && !sh.subshare && !test_inode(sh.pwd,e_dot))
sh_subfork();
#endif /* _lib_openat */
/*
* Do $CDPATH processing, except if the path is absolute or the first component is '.' or '..'
*/
Expand All @@ -144,7 +166,6 @@ int b_cd(int argc, char *argv[],Shbltin_t *context)
if(*dir!='/')
{
/* check for leading .. */
char *cp;
sfprintf(sh.strbuf,"%s",dir);
cp = sfstruse(sh.strbuf);
pathcanon(cp, 0);
Expand Down Expand Up @@ -181,21 +202,60 @@ int b_cd(int argc, char *argv[],Shbltin_t *context)
}
if(!pflag)
{
char *cp;
stkseek(sh.stk,PATH_MAX+PATH_OFFSET);
if(*(cp=stkptr(sh.stk,PATH_OFFSET))=='/')
if(!pathcanon(cp,PATH_DOTDOT))
continue;
}
#if _lib_openat
cp = path_relative(stkptr(sh.stk,PATH_OFFSET));
rval = newdirfd = sh_diropenat((sh.pwdfd>0)?sh.pwdfd:AT_FDCWD,cp);
if(newdirfd>0)
{
/* chdir for directories on HSM/tapeworms may take minutes */
if((rval=fchdir(newdirfd)) >= 0)
{
sh_pwdupdate(newdirfd);
goto success;
}
sh_close(newdirfd);
}
#if !O_SEARCH
else if((rval=chdir(cp)) >= 0)
sh_pwdupdate(sh_diropenat(AT_FDCWD,cp));
#endif
if(saverrno==0)
saverrno=errno;
#else
if((rval=chdir(path_relative(stkptr(sh.stk,PATH_OFFSET)))) >= 0)
goto success;
if(errno!=ENOENT && saverrno==0)
saverrno=errno;
#endif /* _lib_openat */
}
while(cdpath);
if(rval<0 && *dir=='/' && *(path_relative(stkptr(sh.stk,PATH_OFFSET)))!='/')
{
#if _lib_openat
rval = newdirfd = sh_diropenat((sh.pwdfd>0)?sh.pwdfd:AT_FDCWD,dir);
if(newdirfd>0)
{
/* chdir for directories on HSM/tapeworms may take minutes */
if((rval=fchdir(newdirfd)) >= 0)
{
sh_pwdupdate(newdirfd);
goto success;
}
sh_close(newdirfd);
}
#if !O_SEARCH
else if((rval=chdir(dir)) >= 0)
sh_pwdupdate(sh_diropenat(AT_FDCWD,dir));
#endif
#else
rval = chdir(dir);
/* use absolute chdir() if relative chdir() fails */
#endif /* _lib_openat */
}
if(rval<0)
{
if(saverrno)
Expand All @@ -221,7 +281,7 @@ int b_cd(int argc, char *argv[],Shbltin_t *context)
if(*dp && (*dp!='.'||dp[1]) && strchr(dir,'/'))
sfputr(sfstdout,dir,'\n');
nv_putval(opwdnod,oldpwd,NV_RDONLY);
free((void*)sh.pwd);
free(sh.pwd);
if(*dir == '/')
{
size_t len = strlen(dir);
Expand Down
24 changes: 12 additions & 12 deletions src/cmd/ksh93/bltins/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -520,13 +520,13 @@ int b_jobs(int n,char *argv[],Shbltin_t *context)
*/
static void print_times(struct timeval utime, struct timeval stime)
{
int ut_min = utime.tv_sec / 60;
int ut_sec = utime.tv_sec % 60;
int ut_ms = utime.tv_usec / 1000;
int st_min = stime.tv_sec / 60;
int st_sec = stime.tv_sec % 60;
int st_ms = stime.tv_usec / 1000;
sfprintf(sfstdout, sh_isoption(SH_POSIX) ? "%dm%d%c%03ds %dm%d%c%03ds\n" : "%dm%02d%c%03ds %dm%02d%c%03ds\n",
Sfulong_t ut_min = utime.tv_sec / 60;
Sfulong_t ut_sec = utime.tv_sec % 60;
Sfulong_t ut_ms = utime.tv_usec / 1000;
Sfulong_t st_min = stime.tv_sec / 60;
Sfulong_t st_sec = stime.tv_sec % 60;
Sfulong_t st_ms = stime.tv_usec / 1000;
sfprintf(sfstdout, sh_isoption(SH_POSIX) ? "%jum%ju%c%03jus %jum%ju%c%03jus\n" : "%jum%02ju%c%03jus %jum%02ju%c%03jus\n",
ut_min, ut_sec, sh.radixpoint, ut_ms, st_min, st_sec, sh.radixpoint, st_ms);
}
#if _lib_getrusage
Expand All @@ -545,23 +545,23 @@ static void print_cpu_times(void)
static void print_cpu_times(void)
{
struct timeval utime, stime;
double dtime;
Sfdouble_t dtime;
int clk_tck = sh.lim.clk_tck;
struct tms cpu_times;
times(&cpu_times);
/* Print the time (user & system) consumed by the shell. */
dtime = (double)cpu_times.tms_utime / clk_tck;
dtime = (Sfdouble_t)cpu_times.tms_utime / clk_tck;
utime.tv_sec = dtime / 60;
utime.tv_usec = 1000000 * (dtime - utime.tv_sec);
dtime = (double)cpu_times.tms_stime / clk_tck;
dtime = (Sfdouble_t)cpu_times.tms_stime / clk_tck;
stime.tv_sec = dtime / 60;
stime.tv_usec = 1000000 * (dtime - utime.tv_sec);
print_times(utime, stime);
/* Print the time (user & system) consumed by the child processes of the shell. */
dtime = (double)cpu_times.tms_cutime / clk_tck;
dtime = (Sfdouble_t)cpu_times.tms_cutime / clk_tck;
utime.tv_sec = dtime / 60;
utime.tv_usec = 1000000 * (dtime - utime.tv_sec);
dtime = (double)cpu_times.tms_cstime / clk_tck;
dtime = (Sfdouble_t)cpu_times.tms_cstime / clk_tck;
stime.tv_sec = dtime / 60;
stime.tv_usec = 1000000 * (dtime - utime.tv_sec);
print_times(utime, stime);
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/ksh93/bltins/trap.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ int b_suspend(int argc,char *argv[],Shbltin_t *context)
}
if(kill(sh.pid, SIGSTOP) != 0)
{
errormsg(SH_DICT, ERROR_exit(1), "could not signal main shell at PID %d", sh.pid);
errormsg(SH_DICT, ERROR_exit(1), "could not signal main shell at PID %jd", (Sflong_t)sh.pid);
UNREACHABLE();
}
return 0;
Expand Down
9 changes: 3 additions & 6 deletions src/cmd/ksh93/edit/edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -831,14 +831,11 @@ int ed_getchar(Edit_t *ep,int mode)
{
if(mode<=0 && -c == ep->e_intr)
killpg(getpgrp(),SIGINT);
if(mode<=0 && sh.st.trap[SH_KEYTRAP]
/* workaround for <https://github.com/ksh93/ksh/issues/307>:
* do not trigger KEYBD for non-ASCII in multibyte locale */
&& (!mbwide() || c > -128))
if(mode<=0 && sh.st.trap[SH_KEYTRAP])
{
ep->e_keytrap = 1;
n=1;
if((readin[0]= -c) == ESC)
n = mbconv(readin, -c);
if(n==1 && readin[0]==ESC)
{
while(1)
{
Expand Down
Loading

0 comments on commit 57f491d

Please sign in to comment.