Roland Mainz
2013-09-01 03:13:37 UTC
Hi!
----
Attached (as "astksh20130829_libcmd_at001.diff.txt") is a patch (the
first in a series of patches) which switches some of the libcmd APIs
over to the |*at()| APIs.
* Main purposes are:
- We need the patches so a |Shell_t| can (in the future) operate
independently from the process's cwd
- Olga needs this at least for her "ndbx" (New dbx, which should be
available as shared library and thus not rely on the global cwd) and
"kshdbx" (Korn Shell Debugger, which uses two |Shell_t| internally
(one for the shell which is to be debugged and one for the shell
hosting the debugger)) work
- Irek has expressed keen interest in this for GE's "biosh"
- get a tick more performance (on platforms where |*at()| is native...
on Solaris this mainly comes from less locking overhead and for
NFSv4.1 better directory cache utilisation (if more than one directory
fd+cwd are in use))
- we need this for OpenCDE's dtksh work- We need the patch so that in
the future a threaded shell can run a pwdfd per |Shell_t| (each
representing a shell thread)
* Notes:
- I've avoided builtins which use fts for now to avoid crashes between
other work like the fts |*at()| work
- The changes for tr(1) include a change from |qsort()| to
|qsort_r()|, mostly to avoid issues with thread-safety on some
platforms
- Some changes s/int/bool/ are included... I couldn't resist in some
very easy cases
- If I'm not too sleepy we need a |*at()| version of |pathtmp()| ...
----
Bye,
Roland
--
__ . . __
(o.\ \/ /.o) roland.mainz at nrubsig.org
\__\/\/__/ MPEG specialist, C&&JAVA&&Sun&&Unix programmer
/O /==\ O\ TEL +49 641 3992797
(;O/ \/ \O;)
-------------- next part --------------
diff -r -u original/src/lib/libcmd/cat.c build_libcmd_at/src/lib/libcmd/cat.c
--- src/lib/libcmd/cat.c 2012-06-01 00:04:06.000000000 +0200
+++ src/lib/libcmd/cat.c 2013-09-01 04:06:42.821751526 +0200
@@ -395,6 +395,7 @@
int
b_cat(int argc, char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register int n;
register int flags = 0;
register char* cp;
@@ -522,7 +523,7 @@
if (flags&D_FLAG)
sfopen(fp, NiL, mode);
}
- else if (!(fp = sfopen(NiL, cp, mode)))
+ else if (!(fp = sfopenat(pwdfd, NiL, cp, mode)))
{
if (!(flags&F_FLAG))
error(ERROR_system(0), "%s: cannot open", cp);
diff -r -u original/src/lib/libcmd/cksum.c build_libcmd_at/src/lib/libcmd/cksum.c
--- src/lib/libcmd/cksum.c 2012-04-20 08:06:55.000000000 +0200
+++ src/lib/libcmd/cksum.c 2013-09-01 04:10:54.912321421 +0200
@@ -118,6 +118,7 @@
typedef struct State_s /* program state */
{
+ int pwdfd; /* workin directory fd */
int all; /* list all items */
Sfio_t* check; /* check previous output */
int flags; /* sumprint() SUM_* flags */
@@ -146,7 +147,7 @@
*/
static Sfio_t*
-openfile(const char* path, const char* mode)
+openfile(int pwdfd, const char* path, const char* mode)
{
Sfio_t* sp;
@@ -155,7 +156,7 @@
sp = sfstdin;
sfopen(sp, NiL, mode);
}
- else if (!(sp = sfopen(NiL, path, mode)))
+ else if (!(sp = sfopenat(pwdfd, NiL, path, mode)))
error(ERROR_SYSTEM|2, "%s: cannot read", path);
return sp;
}
@@ -303,7 +304,7 @@
}
}
}
- if (sp = openfile(file, "rb"))
+ if (sp = openfile(state->pwdfd, file, "rb"))
{
pr(state, rp, sp, file, -1, NiL, NiL);
if (!(t = sfstruse(rp)))
@@ -409,7 +410,7 @@
register Sfio_t* sp;
while (file = sfgetr(lp, '\n', 1))
- if (sp = openfile(file, state->check ? "rt" : "rb"))
+ if (sp = openfile(state->pwdfd, file, state->check ? "rt" : "rb"))
{
pr(state, sfstdout, sp, file, state->permissions, NiL, state->check);
closefile(sp);
@@ -441,6 +442,7 @@
int
b_cksum(int argc, register char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register int flags;
char* file;
char* method;
@@ -456,6 +458,7 @@
flags = fts_flags() | FTS_META | FTS_TOP | FTS_NOPOSTORDER;
state.flags = SUM_SIZE;
state.warn = 1;
+ state.pwdfd = pwdfd;
logical = 1;
method = 0;
optinit(&optdisc, optinfo);
@@ -575,13 +578,13 @@
if (*argv)
{
while (file = *argv++)
- if (sp = openfile(file, "rt"))
+ if (sp = openfile(pwdfd, file, "rt"))
{
list(&state, sp);
closefile(sp);
}
}
- else if (sp = openfile(NiL, "rt"))
+ else if (sp = openfile(pwdfd, NiL, "rt"))
{
list(&state, sp);
closefile(sp);
@@ -601,7 +604,7 @@
fts_set(NiL, ent, FTS_FOLLOW);
break;
case FTS_F:
- if (sp = openfile(ent->fts_accpath, "rb"))
+ if (sp = openfile(pwdfd, ent->fts_accpath, "rb"))
{
pr(&state, sfstdout, sp, ent->fts_path, state.permissions, ent->fts_statp, state.check);
closefile(sp);
diff -r -u original/src/lib/libcmd/cmp.c build_libcmd_at/src/lib/libcmd/cmp.c
--- src/lib/libcmd/cmp.c 2012-01-10 19:50:49.000000000 +0100
+++ src/lib/libcmd/cmp.c 2013-09-01 03:33:43.254043620 +0200
@@ -247,6 +247,7 @@
int
b_cmp(int argc, register char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
char* s;
char* e;
char* file1;
@@ -315,7 +316,7 @@
n = 2;
if (streq(file1, "-"))
f1 = sfstdin;
- else if (!(f1 = sfopen(NiL, file1, "r")))
+ else if (!(f1 = sfopenat(pwdfd, NiL, file1, "r")))
{
if (!(flags & CMP_SILENT))
error(ERROR_system(0), "%s: cannot open", file1);
@@ -323,7 +324,7 @@
}
if (streq(file2, "-"))
f2 = sfstdin;
- else if (!(f2 = sfopen(NiL, file2, "r")))
+ else if (!(f2 = sfopenat(pwdfd, NiL, file2, "r")))
{
if (!(flags & CMP_SILENT))
error(ERROR_system(0), "%s: cannot open", file2);
diff -r -u original/src/lib/libcmd/comm.c build_libcmd_at/src/lib/libcmd/comm.c
--- src/lib/libcmd/comm.c 2012-01-10 19:50:55.000000000 +0100
+++ src/lib/libcmd/comm.c 2013-09-01 04:18:01.335558771 +0200
@@ -147,9 +147,10 @@
int
b_comm(int argc, char *argv[], Shbltin_t* context)
{
- register int mode = C_FILE1|C_FILE2|C_COMMON;
- register char *cp;
- Sfio_t *f1, *f2;
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
+ register int mode = C_FILE1|C_FILE2|C_COMMON;
+ register char *cp;
+ Sfio_t *f1, *f2;
cmdinit(argc, argv, context, ERROR_CATALOG, 0);
for (;;)
@@ -181,12 +182,12 @@
cp = *argv++;
if(streq(cp,"-"))
f1 = sfstdin;
- else if(!(f1 = sfopen(NiL, cp,"r")))
+ else if(!(f1 = sfopenat(pwdfd, NiL, cp,"r")))
error(ERROR_system(1),"%s: cannot open",cp);
cp = *argv;
if(streq(cp,"-"))
f2 = sfstdin;
- else if(!(f2 = sfopen(NiL, cp,"r")))
+ else if(!(f2 = sfopenat(pwdfd, NiL, cp,"r")))
error(ERROR_system(1),"%s: cannot open",cp);
if(mode)
{
diff -r -u original/src/lib/libcmd/cut.c build_libcmd_at/src/lib/libcmd/cut.c
--- src/lib/libcmd/cut.c 2012-05-29 15:07:42.000000000 +0200
+++ src/lib/libcmd/cut.c 2013-09-01 03:35:59.938857386 +0200
@@ -568,6 +568,7 @@
int
b_cut(int argc, char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register char* cp = 0;
register Sfio_t* fp;
char* s;
@@ -682,7 +683,7 @@
{
if(!cp || streq(cp,"-"))
fp = sfstdin;
- else if(!(fp = sfopen(NiL,cp,"r")))
+ else if(!(fp = sfopenat(pwdfd, NiL, cp, "r")))
{
error(ERROR_system(0),"%s: cannot open",cp);
continue;
diff -r -u original/src/lib/libcmd/date.c build_libcmd_at/src/lib/libcmd/date.c
--- src/lib/libcmd/date.c 2012-01-10 19:52:21.000000000 +0100
+++ src/lib/libcmd/date.c 2013-09-01 03:37:37.229705753 +0200
@@ -276,6 +276,7 @@
int
b_date(int argc, register char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register int n;
register char* s;
register Fmt_t* f;
@@ -440,7 +441,7 @@
n = argv[1] != 0;
while (s = *argv++)
{
- if (stat(s, &st))
+ if (fstatat(pwdfd, s, &st, 0))
error(2, "%s: not found", s);
else
{
diff -r -u original/src/lib/libcmd/fmt.c build_libcmd_at/src/lib/libcmd/fmt.c
--- src/lib/libcmd/fmt.c 2012-01-10 19:53:20.000000000 +0100
+++ src/lib/libcmd/fmt.c 2013-09-01 04:05:08.558049682 +0200
@@ -564,6 +564,7 @@
int
b_fmt(int argc, char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register int n;
char* cp;
Fmt_t fmt;
@@ -618,7 +619,7 @@
do {
if (!cp || streq(cp, "-"))
fmt.in = sfstdin;
- else if (!(fmt.in = sfopen(NiL, cp, "r")))
+ else if (!(fmt.in = sfopenat(pwdfd, NiL, cp, "r")))
{
error(ERROR_system(0), "%s: cannot open", cp);
error_info.errors = 1;
diff -r -u original/src/lib/libcmd/fold.c build_libcmd_at/src/lib/libcmd/fold.c
--- src/lib/libcmd/fold.c 2012-01-10 19:53:36.000000000 +0100
+++ src/lib/libcmd/fold.c 2013-09-01 03:39:37.707709439 +0200
@@ -169,6 +169,7 @@
int
b_fold(int argc, char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register int n, width=WIDTH;
register Sfio_t *fp;
register char *cp;
@@ -225,7 +226,7 @@
{
if(!cp || streq(cp,"-"))
fp = sfstdin;
- else if(!(fp = sfopen(NiL,cp,"r")))
+ else if(!(fp = sfopenat(pwdfd, NiL, cp, "r")))
{
error(ERROR_system(0),"%s: cannot open",cp);
error_info.errors = 1;
diff -r -u original/src/lib/libcmd/head.c build_libcmd_at/src/lib/libcmd/head.c
--- src/lib/libcmd/head.c 2012-06-01 00:04:49.000000000 +0200
+++ src/lib/libcmd/head.c 2013-09-01 04:03:50.055133127 +0200
@@ -72,6 +72,7 @@
{
static const char header_fmt[] = "\n==> %s <==\n";
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register Sfio_t* fp;
register char* cp;
register off_t keep = 10;
@@ -129,7 +130,7 @@
fp = sfstdin;
sfset(fp, SF_SHARE, 1);
}
- else if (!(fp = sfopen(NiL, cp, "r")))
+ else if (!(fp = sfopenat(pwdfd, NiL, cp, "r")))
{
error(ERROR_system(0), "%s: cannot open", cp);
continue;
diff -r -u original/src/lib/libcmd/iconv.c build_libcmd_at/src/lib/libcmd/iconv.c
--- src/lib/libcmd/iconv.c 2012-10-15 10:19:27.000000000 +0200
+++ src/lib/libcmd/iconv.c 2013-09-01 04:03:04.217848207 +0200
@@ -144,6 +144,7 @@
int
b_iconv(int argc, register char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
char* file;
char* from;
char* to;
@@ -239,7 +240,7 @@
file = "/dev/stdin";
ip = sfstdin;
}
- else if (!(ip = sfopen(NiL, file, "r")))
+ else if (!(ip = sfopenat(pwdfd, NiL, file, "r")))
{
error(ERROR_SYSTEM|2, "%s: cannot open", file);
continue;
diff -r -u original/src/lib/libcmd/join.c build_libcmd_at/src/lib/libcmd/join.c
--- src/lib/libcmd/join.c 2013-08-15 07:26:47.000000000 +0200
+++ src/lib/libcmd/join.c 2013-09-01 04:01:57.642526716 +0200
@@ -814,6 +814,7 @@
int
b_join(int argc, char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register int n;
register char* cp;
register Join_t* jp;
@@ -941,7 +942,7 @@
}
jp->file[0].iop = sfstdin;
}
- else if (!(jp->file[0].iop = sfopen(NiL, cp, "r")))
+ else if (!(jp->file[0].iop = sfopenat(pwdfd, NiL, cp, "r")))
{
done(jp);
error(ERROR_system(1),"%s: cannot open",cp);
@@ -958,7 +959,7 @@
}
jp->file[1].iop = sfstdin;
}
- else if (!(jp->file[1].iop = sfopen(NiL, cp, "r")))
+ else if (!(jp->file[1].iop = sfopenat(pwdfd, NiL, cp, "r")))
{
done(jp);
error(ERROR_system(1),"%s: cannot open",cp);
diff -r -u original/src/lib/libcmd/mkdir.c build_libcmd_at/src/lib/libcmd/mkdir.c
--- src/lib/libcmd/mkdir.c 2012-01-10 19:54:47.000000000 +0100
+++ src/lib/libcmd/mkdir.c 2013-09-01 04:18:41.712569380 +0200
@@ -64,14 +64,15 @@
int
b_mkdir(int argc, char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register char* path;
register int n;
register mode_t mode = DIRMODE;
register mode_t mask = 0;
- register int mflag = 0;
- register int pflag = 0;
- register int vflag = 0;
- int made;
+ bool mflag = false;
+ bool pflag = false;
+ bool vflag = false;
+ bool made;
char* part;
mode_t dmode;
struct stat st;
@@ -82,16 +83,16 @@
switch (optget(argv, usage))
{
case 'm':
- mflag = 1;
+ mflag = true;
mode = strperm(opt_info.arg, &part, mode);
if (*part)
error(ERROR_exit(0), "%s: invalid mode", opt_info.arg);
continue;
case 'p':
- pflag = 1;
+ pflag = true;
continue;
case 'v':
- vflag = 1;
+ vflag = true;
continue;
case ':':
error(2, "%s", opt_info.arg);
@@ -121,11 +122,11 @@
}
while (path = *argv++)
{
- if (!mkdir(path, mode))
+ if (!mkdirat(pwdfd, path, mode))
{
if (vflag)
error(0, "%s: directory created", path);
- made = 1;
+ made = true;
}
else if (!pflag || !(errno == ENOENT || errno == EEXIST || errno == ENOTDIR))
{
@@ -141,7 +142,7 @@
* first eliminate trailing /'s
*/
- made = 0;
+ made = false;
n = strlen(path);
while (n > 0 && path[--n] == '/');
path[n + 1] = 0;
@@ -154,7 +155,7 @@
while ((n = *part) && n != '/')
part++;
*part = 0;
- if (mkdir(path, n ? dmode : mode) < 0 && errno != EEXIST && access(path, F_OK) < 0)
+ if (mkdirat(pwdfd, path, n ? dmode : mode) < 0 && errno != EEXIST && access(path, F_OK) < 0)
{
error(ERROR_system(0), "%s: cannot create intermediate directory", path);
*part = n;
@@ -164,14 +165,14 @@
error(0, "%s: directory created", path);
if (!(*part = n))
{
- made = 1;
+ made = true;
break;
}
}
}
if (made && (mode & (S_ISVTX|S_ISUID|S_ISGID)))
{
- if (stat(path, &st))
+ if (fstatat(pwdfd, path, &st, 0))
{
error(ERROR_system(0), "%s: cannot stat", path);
break;
diff -r -u original/src/lib/libcmd/mkfifo.c build_libcmd_at/src/lib/libcmd/mkfifo.c
--- src/lib/libcmd/mkfifo.c 2012-01-10 19:54:56.000000000 +0100
+++ src/lib/libcmd/mkfifo.c 2013-09-01 03:59:33.506337341 +0200
@@ -52,10 +52,11 @@
int
b_mkfifo(int argc, char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register char* arg;
register mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
register mode_t mask = 0;
- register int mflag = 0;
+ bool mflag = false;
cmdinit(argc, argv, context, ERROR_CATALOG, 0);
for (;;)
@@ -63,7 +64,7 @@
switch (optget(argv, usage))
{
case 'm':
- mflag = 1;
+ mflag = true;
mode = strperm(arg = opt_info.arg, &opt_info.arg, mode);
if (*opt_info.arg)
error(ERROR_exit(0), "%s: invalid mode", arg);
@@ -88,7 +89,7 @@
mask = 0;
}
while (arg = *argv++)
- if (mkfifo(arg, mode) < 0)
+ if (mkfifoat(pwdfd, arg, mode) < 0)
error(ERROR_system(0), "%s:", arg);
if (mask)
umask(mask);
diff -r -u original/src/lib/libcmd/od.c build_libcmd_at/src/lib/libcmd/od.c
--- src/lib/libcmd/od.c 2012-10-19 14:40:51.000000000 +0200
+++ src/lib/libcmd/od.c 2013-09-01 03:57:17.807567179 +0200
@@ -236,6 +236,7 @@
size_t size;
} dup;
unsigned char* eob;
+ int pwdfd;
char* file;
Format_t* form;
Format_t* last;
@@ -949,7 +950,7 @@
state->file = "/dev/stdin";
ip = sfstdin;
}
- else if (!(ip = sfopen(NiL, state->file, "r")))
+ else if (!(ip = sfopenat(state->pwdfd, NiL, state->file, "r")))
{
error(ERROR_system(0), "%s: cannot open", state->file);
error_info.errors = 1;
@@ -1303,6 +1304,7 @@
int
b_od(int argc, char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register int n;
register char* s;
register Format_t* fp;
@@ -1319,7 +1321,8 @@
error(ERROR_SYSTEM|2, "out of space");
return 1;
}
- state.context = context;
+ state.context = context;
+ state.pwdfd = pwdfd;
optinit(&optdisc, optinfo);
per = 0;
state.map = ccmap(CC_ASCII, CC_ASCII);
diff -r -u original/src/lib/libcmd/paste.c build_libcmd_at/src/lib/libcmd/paste.c
--- src/lib/libcmd/paste.c 2012-01-10 19:55:31.000000000 +0100
+++ src/lib/libcmd/paste.c 2013-09-01 03:53:47.602076053 +0200
@@ -174,7 +174,9 @@
int
b_paste(int argc, char** argv, Shbltin_t* context)
{
- register int n, sflag=0;
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
+ register int n;
+ bool sflag = false;
register Sfio_t *fp, **streams;
register char *cp, *delim;
char *ep;
@@ -192,7 +194,7 @@
delim = opt_info.arg;
continue;
case 's':
- sflag++;
+ sflag = true;
continue;
case ':':
error(2, "%s", opt_info.arg);
@@ -261,7 +263,7 @@
{
if(!cp || streq(cp,"-"))
fp = sfstdin;
- else if(!(fp = sfopen(NiL,cp,"r")))
+ else if(!(fp = sfopenat(pwdfd, NiL, cp, "r")))
error(ERROR_system(0),"%s: cannot open",cp);
if(fp && sflag)
{
diff -r -u original/src/lib/libcmd/rev.c build_libcmd_at/src/lib/libcmd/rev.c
--- src/lib/libcmd/rev.c 2012-01-10 19:55:48.000000000 +0100
+++ src/lib/libcmd/rev.c 2013-09-01 03:51:16.046513593 +0200
@@ -116,6 +116,7 @@
int
b_rev(int argc, register char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register Sfio_t *fp;
register char *cp;
register int n, line=0;
@@ -148,7 +149,7 @@
{
if(!cp || streq(cp,"-"))
fp = sfstdin;
- else if(!(fp = sfopen((Sfio_t*)0,cp,"r")))
+ else if(!(fp = sfopenat(pwdfd, (Sfio_t*)0, cp, "r")))
{
error(ERROR_system(0),"%s: cannot open",cp);
n=1;
diff -r -u original/src/lib/libcmd/tail.c build_libcmd_at/src/lib/libcmd/tail.c
--- src/lib/libcmd/tail.c 2012-10-10 06:16:54.000000000 +0200
+++ src/lib/libcmd/tail.c 2013-09-01 03:48:51.297298287 +0200
@@ -141,6 +141,7 @@
long dev;
long ino;
int fifo;
+ int pwdfd;
};
static const char header_fmt[] = "\n==> %s <==\n";
@@ -274,7 +275,7 @@
tp->name = "/dev/stdin";
tp->sp = sfstdin;
}
- else if (!(tp->sp = sfopen(tp->sp, tp->name, "r")))
+ else if (!(tp->sp = sfopenat(tp->pwdfd, tp->sp, tp->name, "r")))
{
error(ERROR_system(0), "%s: cannot open", tp->name);
return -1;
@@ -400,6 +401,7 @@
int
b_tail(int argc, char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register Sfio_t* ip;
register int n;
register int i;
@@ -617,6 +619,7 @@
{
fp->name = s;
fp->sp = 0;
+ fp->pwdfd = pwdfd;
if (!init(fp, number, delim, flags, &format))
{
fp->expire = timeout ? (NOW + timeout + 1) : 0;
@@ -685,7 +688,7 @@
if (flags & LOG)
{
i = 3;
- while (--i && stat(fp->name, &st))
+ while (--i && fstatat(pwdfd, fp->name, &st, 0))
if (sh_checksig(context))
{
error_info.errors++;
@@ -735,7 +738,7 @@
file = "/dev/stdin";
ip = sfstdin;
}
- else if (!(ip = sfopen(NiL, file, "r")))
+ else if (!(ip = sfopenat(pwdfd, NiL, file, "r")))
{
error(ERROR_system(0), "%s: cannot open", file);
continue;
diff -r -u original/src/lib/libcmd/tee.c build_libcmd_at/src/lib/libcmd/tee.c
--- src/lib/libcmd/tee.c 2012-06-19 21:40:02.000000000 +0200
+++ src/lib/libcmd/tee.c 2013-09-01 03:45:40.047766906 +0200
@@ -109,6 +109,7 @@
int
b_tee(int argc, register char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register Tee_t* tp = 0;
register int oflag = O_WRONLY|O_TRUNC|O_CREAT|O_BINARY|O_cloexec;
register int* hp;
@@ -177,7 +178,7 @@
hp = tp->fd;
while (cp = *argv++)
{
- while ((*hp = open(cp, oflag, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0 && errno == EINTR)
+ while ((*hp = openat(pwdfd, cp, oflag, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0 && errno == EINTR)
errno = 0;
if (*hp < 0)
error(ERROR_system(0), "%s: cannot create", cp);
diff -r -u original/src/lib/libcmd/tr.c build_libcmd_at/src/lib/libcmd/tr.c
--- src/lib/libcmd/tr.c 2013-07-18 15:47:52.000000000 +0200
+++ src/lib/libcmd/tr.c 2013-09-01 03:44:31.316342345 +0200
@@ -468,7 +468,7 @@
col[n].chr = set[n];
wcsxfrm(col[n].seq, w, sizeof(col[n].seq));
}
- qsort(col, c, sizeof(col[0]), (Compare_f)wcscmp);
+ qsort_r(col, c, sizeof(col[0]), (Compare_f)wcscmp, NULL);
for (n = 0; n < c; n++)
set[n] = col[n].chr;
}
diff -r -u original/src/lib/libcmd/uniq.c build_libcmd_at/src/lib/libcmd/uniq.c
--- src/lib/libcmd/uniq.c 2012-03-08 02:38:36.000000000 +0100
+++ src/lib/libcmd/uniq.c 2013-09-01 03:41:42.351921772 +0200
@@ -245,6 +245,7 @@
int
b_uniq(int argc, char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register int mode=0;
register char *cp;
int fields=0, chars=0, width=-1;
@@ -314,7 +315,7 @@
error(ERROR_usage(2), "%s", optusage(NiL));
if((cp = *argv) && (argv++,!streq(cp,"-")))
{
- if(!(fpin = sfopen(NiL,cp,"r")))
+ if(!(fpin = sfopenat(pwdfd, NiL, cp, "r")))
error(ERROR_system(1),"%s: cannot open",cp);
}
else
@@ -322,7 +323,7 @@
if(cp = *argv)
{
argv++;
- if(!(fpout = sfopen(NiL,cp,"w")))
+ if(!(fpout = sfopenat(pwdfd, NiL, cp, "w")))
error(ERROR_system(1),"%s: cannot create",cp);
}
else
diff -r -u original/src/lib/libcmd/wc.c build_libcmd_at/src/lib/libcmd/wc.c
--- src/lib/libcmd/wc.c 2012-11-21 22:25:48.000000000 +0100
+++ src/lib/libcmd/wc.c 2013-09-01 03:38:54.381552281 +0200
@@ -93,6 +93,7 @@
int
b_wc(int argc,register char **argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register char *cp;
register int mode=0, n;
register Wc_t *wp;
@@ -162,7 +163,7 @@
{
if (!cp || streq(cp,"-"))
fp = sfstdin;
- else if (!(fp = sfopen(NiL,cp,"r")))
+ else if (!(fp = sfopenat(pwdfd, NiL, cp, "r")))
{
error(ERROR_system(0),"%s: cannot open",cp);
continue;
----
Attached (as "astksh20130829_libcmd_at001.diff.txt") is a patch (the
first in a series of patches) which switches some of the libcmd APIs
over to the |*at()| APIs.
* Main purposes are:
- We need the patches so a |Shell_t| can (in the future) operate
independently from the process's cwd
- Olga needs this at least for her "ndbx" (New dbx, which should be
available as shared library and thus not rely on the global cwd) and
"kshdbx" (Korn Shell Debugger, which uses two |Shell_t| internally
(one for the shell which is to be debugged and one for the shell
hosting the debugger)) work
- Irek has expressed keen interest in this for GE's "biosh"
- get a tick more performance (on platforms where |*at()| is native...
on Solaris this mainly comes from less locking overhead and for
NFSv4.1 better directory cache utilisation (if more than one directory
fd+cwd are in use))
- we need this for OpenCDE's dtksh work- We need the patch so that in
the future a threaded shell can run a pwdfd per |Shell_t| (each
representing a shell thread)
* Notes:
- I've avoided builtins which use fts for now to avoid crashes between
other work like the fts |*at()| work
- The changes for tr(1) include a change from |qsort()| to
|qsort_r()|, mostly to avoid issues with thread-safety on some
platforms
- Some changes s/int/bool/ are included... I couldn't resist in some
very easy cases
- If I'm not too sleepy we need a |*at()| version of |pathtmp()| ...
----
Bye,
Roland
--
__ . . __
(o.\ \/ /.o) roland.mainz at nrubsig.org
\__\/\/__/ MPEG specialist, C&&JAVA&&Sun&&Unix programmer
/O /==\ O\ TEL +49 641 3992797
(;O/ \/ \O;)
-------------- next part --------------
diff -r -u original/src/lib/libcmd/cat.c build_libcmd_at/src/lib/libcmd/cat.c
--- src/lib/libcmd/cat.c 2012-06-01 00:04:06.000000000 +0200
+++ src/lib/libcmd/cat.c 2013-09-01 04:06:42.821751526 +0200
@@ -395,6 +395,7 @@
int
b_cat(int argc, char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register int n;
register int flags = 0;
register char* cp;
@@ -522,7 +523,7 @@
if (flags&D_FLAG)
sfopen(fp, NiL, mode);
}
- else if (!(fp = sfopen(NiL, cp, mode)))
+ else if (!(fp = sfopenat(pwdfd, NiL, cp, mode)))
{
if (!(flags&F_FLAG))
error(ERROR_system(0), "%s: cannot open", cp);
diff -r -u original/src/lib/libcmd/cksum.c build_libcmd_at/src/lib/libcmd/cksum.c
--- src/lib/libcmd/cksum.c 2012-04-20 08:06:55.000000000 +0200
+++ src/lib/libcmd/cksum.c 2013-09-01 04:10:54.912321421 +0200
@@ -118,6 +118,7 @@
typedef struct State_s /* program state */
{
+ int pwdfd; /* workin directory fd */
int all; /* list all items */
Sfio_t* check; /* check previous output */
int flags; /* sumprint() SUM_* flags */
@@ -146,7 +147,7 @@
*/
static Sfio_t*
-openfile(const char* path, const char* mode)
+openfile(int pwdfd, const char* path, const char* mode)
{
Sfio_t* sp;
@@ -155,7 +156,7 @@
sp = sfstdin;
sfopen(sp, NiL, mode);
}
- else if (!(sp = sfopen(NiL, path, mode)))
+ else if (!(sp = sfopenat(pwdfd, NiL, path, mode)))
error(ERROR_SYSTEM|2, "%s: cannot read", path);
return sp;
}
@@ -303,7 +304,7 @@
}
}
}
- if (sp = openfile(file, "rb"))
+ if (sp = openfile(state->pwdfd, file, "rb"))
{
pr(state, rp, sp, file, -1, NiL, NiL);
if (!(t = sfstruse(rp)))
@@ -409,7 +410,7 @@
register Sfio_t* sp;
while (file = sfgetr(lp, '\n', 1))
- if (sp = openfile(file, state->check ? "rt" : "rb"))
+ if (sp = openfile(state->pwdfd, file, state->check ? "rt" : "rb"))
{
pr(state, sfstdout, sp, file, state->permissions, NiL, state->check);
closefile(sp);
@@ -441,6 +442,7 @@
int
b_cksum(int argc, register char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register int flags;
char* file;
char* method;
@@ -456,6 +458,7 @@
flags = fts_flags() | FTS_META | FTS_TOP | FTS_NOPOSTORDER;
state.flags = SUM_SIZE;
state.warn = 1;
+ state.pwdfd = pwdfd;
logical = 1;
method = 0;
optinit(&optdisc, optinfo);
@@ -575,13 +578,13 @@
if (*argv)
{
while (file = *argv++)
- if (sp = openfile(file, "rt"))
+ if (sp = openfile(pwdfd, file, "rt"))
{
list(&state, sp);
closefile(sp);
}
}
- else if (sp = openfile(NiL, "rt"))
+ else if (sp = openfile(pwdfd, NiL, "rt"))
{
list(&state, sp);
closefile(sp);
@@ -601,7 +604,7 @@
fts_set(NiL, ent, FTS_FOLLOW);
break;
case FTS_F:
- if (sp = openfile(ent->fts_accpath, "rb"))
+ if (sp = openfile(pwdfd, ent->fts_accpath, "rb"))
{
pr(&state, sfstdout, sp, ent->fts_path, state.permissions, ent->fts_statp, state.check);
closefile(sp);
diff -r -u original/src/lib/libcmd/cmp.c build_libcmd_at/src/lib/libcmd/cmp.c
--- src/lib/libcmd/cmp.c 2012-01-10 19:50:49.000000000 +0100
+++ src/lib/libcmd/cmp.c 2013-09-01 03:33:43.254043620 +0200
@@ -247,6 +247,7 @@
int
b_cmp(int argc, register char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
char* s;
char* e;
char* file1;
@@ -315,7 +316,7 @@
n = 2;
if (streq(file1, "-"))
f1 = sfstdin;
- else if (!(f1 = sfopen(NiL, file1, "r")))
+ else if (!(f1 = sfopenat(pwdfd, NiL, file1, "r")))
{
if (!(flags & CMP_SILENT))
error(ERROR_system(0), "%s: cannot open", file1);
@@ -323,7 +324,7 @@
}
if (streq(file2, "-"))
f2 = sfstdin;
- else if (!(f2 = sfopen(NiL, file2, "r")))
+ else if (!(f2 = sfopenat(pwdfd, NiL, file2, "r")))
{
if (!(flags & CMP_SILENT))
error(ERROR_system(0), "%s: cannot open", file2);
diff -r -u original/src/lib/libcmd/comm.c build_libcmd_at/src/lib/libcmd/comm.c
--- src/lib/libcmd/comm.c 2012-01-10 19:50:55.000000000 +0100
+++ src/lib/libcmd/comm.c 2013-09-01 04:18:01.335558771 +0200
@@ -147,9 +147,10 @@
int
b_comm(int argc, char *argv[], Shbltin_t* context)
{
- register int mode = C_FILE1|C_FILE2|C_COMMON;
- register char *cp;
- Sfio_t *f1, *f2;
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
+ register int mode = C_FILE1|C_FILE2|C_COMMON;
+ register char *cp;
+ Sfio_t *f1, *f2;
cmdinit(argc, argv, context, ERROR_CATALOG, 0);
for (;;)
@@ -181,12 +182,12 @@
cp = *argv++;
if(streq(cp,"-"))
f1 = sfstdin;
- else if(!(f1 = sfopen(NiL, cp,"r")))
+ else if(!(f1 = sfopenat(pwdfd, NiL, cp,"r")))
error(ERROR_system(1),"%s: cannot open",cp);
cp = *argv;
if(streq(cp,"-"))
f2 = sfstdin;
- else if(!(f2 = sfopen(NiL, cp,"r")))
+ else if(!(f2 = sfopenat(pwdfd, NiL, cp,"r")))
error(ERROR_system(1),"%s: cannot open",cp);
if(mode)
{
diff -r -u original/src/lib/libcmd/cut.c build_libcmd_at/src/lib/libcmd/cut.c
--- src/lib/libcmd/cut.c 2012-05-29 15:07:42.000000000 +0200
+++ src/lib/libcmd/cut.c 2013-09-01 03:35:59.938857386 +0200
@@ -568,6 +568,7 @@
int
b_cut(int argc, char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register char* cp = 0;
register Sfio_t* fp;
char* s;
@@ -682,7 +683,7 @@
{
if(!cp || streq(cp,"-"))
fp = sfstdin;
- else if(!(fp = sfopen(NiL,cp,"r")))
+ else if(!(fp = sfopenat(pwdfd, NiL, cp, "r")))
{
error(ERROR_system(0),"%s: cannot open",cp);
continue;
diff -r -u original/src/lib/libcmd/date.c build_libcmd_at/src/lib/libcmd/date.c
--- src/lib/libcmd/date.c 2012-01-10 19:52:21.000000000 +0100
+++ src/lib/libcmd/date.c 2013-09-01 03:37:37.229705753 +0200
@@ -276,6 +276,7 @@
int
b_date(int argc, register char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register int n;
register char* s;
register Fmt_t* f;
@@ -440,7 +441,7 @@
n = argv[1] != 0;
while (s = *argv++)
{
- if (stat(s, &st))
+ if (fstatat(pwdfd, s, &st, 0))
error(2, "%s: not found", s);
else
{
diff -r -u original/src/lib/libcmd/fmt.c build_libcmd_at/src/lib/libcmd/fmt.c
--- src/lib/libcmd/fmt.c 2012-01-10 19:53:20.000000000 +0100
+++ src/lib/libcmd/fmt.c 2013-09-01 04:05:08.558049682 +0200
@@ -564,6 +564,7 @@
int
b_fmt(int argc, char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register int n;
char* cp;
Fmt_t fmt;
@@ -618,7 +619,7 @@
do {
if (!cp || streq(cp, "-"))
fmt.in = sfstdin;
- else if (!(fmt.in = sfopen(NiL, cp, "r")))
+ else if (!(fmt.in = sfopenat(pwdfd, NiL, cp, "r")))
{
error(ERROR_system(0), "%s: cannot open", cp);
error_info.errors = 1;
diff -r -u original/src/lib/libcmd/fold.c build_libcmd_at/src/lib/libcmd/fold.c
--- src/lib/libcmd/fold.c 2012-01-10 19:53:36.000000000 +0100
+++ src/lib/libcmd/fold.c 2013-09-01 03:39:37.707709439 +0200
@@ -169,6 +169,7 @@
int
b_fold(int argc, char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register int n, width=WIDTH;
register Sfio_t *fp;
register char *cp;
@@ -225,7 +226,7 @@
{
if(!cp || streq(cp,"-"))
fp = sfstdin;
- else if(!(fp = sfopen(NiL,cp,"r")))
+ else if(!(fp = sfopenat(pwdfd, NiL, cp, "r")))
{
error(ERROR_system(0),"%s: cannot open",cp);
error_info.errors = 1;
diff -r -u original/src/lib/libcmd/head.c build_libcmd_at/src/lib/libcmd/head.c
--- src/lib/libcmd/head.c 2012-06-01 00:04:49.000000000 +0200
+++ src/lib/libcmd/head.c 2013-09-01 04:03:50.055133127 +0200
@@ -72,6 +72,7 @@
{
static const char header_fmt[] = "\n==> %s <==\n";
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register Sfio_t* fp;
register char* cp;
register off_t keep = 10;
@@ -129,7 +130,7 @@
fp = sfstdin;
sfset(fp, SF_SHARE, 1);
}
- else if (!(fp = sfopen(NiL, cp, "r")))
+ else if (!(fp = sfopenat(pwdfd, NiL, cp, "r")))
{
error(ERROR_system(0), "%s: cannot open", cp);
continue;
diff -r -u original/src/lib/libcmd/iconv.c build_libcmd_at/src/lib/libcmd/iconv.c
--- src/lib/libcmd/iconv.c 2012-10-15 10:19:27.000000000 +0200
+++ src/lib/libcmd/iconv.c 2013-09-01 04:03:04.217848207 +0200
@@ -144,6 +144,7 @@
int
b_iconv(int argc, register char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
char* file;
char* from;
char* to;
@@ -239,7 +240,7 @@
file = "/dev/stdin";
ip = sfstdin;
}
- else if (!(ip = sfopen(NiL, file, "r")))
+ else if (!(ip = sfopenat(pwdfd, NiL, file, "r")))
{
error(ERROR_SYSTEM|2, "%s: cannot open", file);
continue;
diff -r -u original/src/lib/libcmd/join.c build_libcmd_at/src/lib/libcmd/join.c
--- src/lib/libcmd/join.c 2013-08-15 07:26:47.000000000 +0200
+++ src/lib/libcmd/join.c 2013-09-01 04:01:57.642526716 +0200
@@ -814,6 +814,7 @@
int
b_join(int argc, char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register int n;
register char* cp;
register Join_t* jp;
@@ -941,7 +942,7 @@
}
jp->file[0].iop = sfstdin;
}
- else if (!(jp->file[0].iop = sfopen(NiL, cp, "r")))
+ else if (!(jp->file[0].iop = sfopenat(pwdfd, NiL, cp, "r")))
{
done(jp);
error(ERROR_system(1),"%s: cannot open",cp);
@@ -958,7 +959,7 @@
}
jp->file[1].iop = sfstdin;
}
- else if (!(jp->file[1].iop = sfopen(NiL, cp, "r")))
+ else if (!(jp->file[1].iop = sfopenat(pwdfd, NiL, cp, "r")))
{
done(jp);
error(ERROR_system(1),"%s: cannot open",cp);
diff -r -u original/src/lib/libcmd/mkdir.c build_libcmd_at/src/lib/libcmd/mkdir.c
--- src/lib/libcmd/mkdir.c 2012-01-10 19:54:47.000000000 +0100
+++ src/lib/libcmd/mkdir.c 2013-09-01 04:18:41.712569380 +0200
@@ -64,14 +64,15 @@
int
b_mkdir(int argc, char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register char* path;
register int n;
register mode_t mode = DIRMODE;
register mode_t mask = 0;
- register int mflag = 0;
- register int pflag = 0;
- register int vflag = 0;
- int made;
+ bool mflag = false;
+ bool pflag = false;
+ bool vflag = false;
+ bool made;
char* part;
mode_t dmode;
struct stat st;
@@ -82,16 +83,16 @@
switch (optget(argv, usage))
{
case 'm':
- mflag = 1;
+ mflag = true;
mode = strperm(opt_info.arg, &part, mode);
if (*part)
error(ERROR_exit(0), "%s: invalid mode", opt_info.arg);
continue;
case 'p':
- pflag = 1;
+ pflag = true;
continue;
case 'v':
- vflag = 1;
+ vflag = true;
continue;
case ':':
error(2, "%s", opt_info.arg);
@@ -121,11 +122,11 @@
}
while (path = *argv++)
{
- if (!mkdir(path, mode))
+ if (!mkdirat(pwdfd, path, mode))
{
if (vflag)
error(0, "%s: directory created", path);
- made = 1;
+ made = true;
}
else if (!pflag || !(errno == ENOENT || errno == EEXIST || errno == ENOTDIR))
{
@@ -141,7 +142,7 @@
* first eliminate trailing /'s
*/
- made = 0;
+ made = false;
n = strlen(path);
while (n > 0 && path[--n] == '/');
path[n + 1] = 0;
@@ -154,7 +155,7 @@
while ((n = *part) && n != '/')
part++;
*part = 0;
- if (mkdir(path, n ? dmode : mode) < 0 && errno != EEXIST && access(path, F_OK) < 0)
+ if (mkdirat(pwdfd, path, n ? dmode : mode) < 0 && errno != EEXIST && access(path, F_OK) < 0)
{
error(ERROR_system(0), "%s: cannot create intermediate directory", path);
*part = n;
@@ -164,14 +165,14 @@
error(0, "%s: directory created", path);
if (!(*part = n))
{
- made = 1;
+ made = true;
break;
}
}
}
if (made && (mode & (S_ISVTX|S_ISUID|S_ISGID)))
{
- if (stat(path, &st))
+ if (fstatat(pwdfd, path, &st, 0))
{
error(ERROR_system(0), "%s: cannot stat", path);
break;
diff -r -u original/src/lib/libcmd/mkfifo.c build_libcmd_at/src/lib/libcmd/mkfifo.c
--- src/lib/libcmd/mkfifo.c 2012-01-10 19:54:56.000000000 +0100
+++ src/lib/libcmd/mkfifo.c 2013-09-01 03:59:33.506337341 +0200
@@ -52,10 +52,11 @@
int
b_mkfifo(int argc, char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register char* arg;
register mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
register mode_t mask = 0;
- register int mflag = 0;
+ bool mflag = false;
cmdinit(argc, argv, context, ERROR_CATALOG, 0);
for (;;)
@@ -63,7 +64,7 @@
switch (optget(argv, usage))
{
case 'm':
- mflag = 1;
+ mflag = true;
mode = strperm(arg = opt_info.arg, &opt_info.arg, mode);
if (*opt_info.arg)
error(ERROR_exit(0), "%s: invalid mode", arg);
@@ -88,7 +89,7 @@
mask = 0;
}
while (arg = *argv++)
- if (mkfifo(arg, mode) < 0)
+ if (mkfifoat(pwdfd, arg, mode) < 0)
error(ERROR_system(0), "%s:", arg);
if (mask)
umask(mask);
diff -r -u original/src/lib/libcmd/od.c build_libcmd_at/src/lib/libcmd/od.c
--- src/lib/libcmd/od.c 2012-10-19 14:40:51.000000000 +0200
+++ src/lib/libcmd/od.c 2013-09-01 03:57:17.807567179 +0200
@@ -236,6 +236,7 @@
size_t size;
} dup;
unsigned char* eob;
+ int pwdfd;
char* file;
Format_t* form;
Format_t* last;
@@ -949,7 +950,7 @@
state->file = "/dev/stdin";
ip = sfstdin;
}
- else if (!(ip = sfopen(NiL, state->file, "r")))
+ else if (!(ip = sfopenat(state->pwdfd, NiL, state->file, "r")))
{
error(ERROR_system(0), "%s: cannot open", state->file);
error_info.errors = 1;
@@ -1303,6 +1304,7 @@
int
b_od(int argc, char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register int n;
register char* s;
register Format_t* fp;
@@ -1319,7 +1321,8 @@
error(ERROR_SYSTEM|2, "out of space");
return 1;
}
- state.context = context;
+ state.context = context;
+ state.pwdfd = pwdfd;
optinit(&optdisc, optinfo);
per = 0;
state.map = ccmap(CC_ASCII, CC_ASCII);
diff -r -u original/src/lib/libcmd/paste.c build_libcmd_at/src/lib/libcmd/paste.c
--- src/lib/libcmd/paste.c 2012-01-10 19:55:31.000000000 +0100
+++ src/lib/libcmd/paste.c 2013-09-01 03:53:47.602076053 +0200
@@ -174,7 +174,9 @@
int
b_paste(int argc, char** argv, Shbltin_t* context)
{
- register int n, sflag=0;
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
+ register int n;
+ bool sflag = false;
register Sfio_t *fp, **streams;
register char *cp, *delim;
char *ep;
@@ -192,7 +194,7 @@
delim = opt_info.arg;
continue;
case 's':
- sflag++;
+ sflag = true;
continue;
case ':':
error(2, "%s", opt_info.arg);
@@ -261,7 +263,7 @@
{
if(!cp || streq(cp,"-"))
fp = sfstdin;
- else if(!(fp = sfopen(NiL,cp,"r")))
+ else if(!(fp = sfopenat(pwdfd, NiL, cp, "r")))
error(ERROR_system(0),"%s: cannot open",cp);
if(fp && sflag)
{
diff -r -u original/src/lib/libcmd/rev.c build_libcmd_at/src/lib/libcmd/rev.c
--- src/lib/libcmd/rev.c 2012-01-10 19:55:48.000000000 +0100
+++ src/lib/libcmd/rev.c 2013-09-01 03:51:16.046513593 +0200
@@ -116,6 +116,7 @@
int
b_rev(int argc, register char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register Sfio_t *fp;
register char *cp;
register int n, line=0;
@@ -148,7 +149,7 @@
{
if(!cp || streq(cp,"-"))
fp = sfstdin;
- else if(!(fp = sfopen((Sfio_t*)0,cp,"r")))
+ else if(!(fp = sfopenat(pwdfd, (Sfio_t*)0, cp, "r")))
{
error(ERROR_system(0),"%s: cannot open",cp);
n=1;
diff -r -u original/src/lib/libcmd/tail.c build_libcmd_at/src/lib/libcmd/tail.c
--- src/lib/libcmd/tail.c 2012-10-10 06:16:54.000000000 +0200
+++ src/lib/libcmd/tail.c 2013-09-01 03:48:51.297298287 +0200
@@ -141,6 +141,7 @@
long dev;
long ino;
int fifo;
+ int pwdfd;
};
static const char header_fmt[] = "\n==> %s <==\n";
@@ -274,7 +275,7 @@
tp->name = "/dev/stdin";
tp->sp = sfstdin;
}
- else if (!(tp->sp = sfopen(tp->sp, tp->name, "r")))
+ else if (!(tp->sp = sfopenat(tp->pwdfd, tp->sp, tp->name, "r")))
{
error(ERROR_system(0), "%s: cannot open", tp->name);
return -1;
@@ -400,6 +401,7 @@
int
b_tail(int argc, char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register Sfio_t* ip;
register int n;
register int i;
@@ -617,6 +619,7 @@
{
fp->name = s;
fp->sp = 0;
+ fp->pwdfd = pwdfd;
if (!init(fp, number, delim, flags, &format))
{
fp->expire = timeout ? (NOW + timeout + 1) : 0;
@@ -685,7 +688,7 @@
if (flags & LOG)
{
i = 3;
- while (--i && stat(fp->name, &st))
+ while (--i && fstatat(pwdfd, fp->name, &st, 0))
if (sh_checksig(context))
{
error_info.errors++;
@@ -735,7 +738,7 @@
file = "/dev/stdin";
ip = sfstdin;
}
- else if (!(ip = sfopen(NiL, file, "r")))
+ else if (!(ip = sfopenat(pwdfd, NiL, file, "r")))
{
error(ERROR_system(0), "%s: cannot open", file);
continue;
diff -r -u original/src/lib/libcmd/tee.c build_libcmd_at/src/lib/libcmd/tee.c
--- src/lib/libcmd/tee.c 2012-06-19 21:40:02.000000000 +0200
+++ src/lib/libcmd/tee.c 2013-09-01 03:45:40.047766906 +0200
@@ -109,6 +109,7 @@
int
b_tee(int argc, register char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register Tee_t* tp = 0;
register int oflag = O_WRONLY|O_TRUNC|O_CREAT|O_BINARY|O_cloexec;
register int* hp;
@@ -177,7 +178,7 @@
hp = tp->fd;
while (cp = *argv++)
{
- while ((*hp = open(cp, oflag, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0 && errno == EINTR)
+ while ((*hp = openat(pwdfd, cp, oflag, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0 && errno == EINTR)
errno = 0;
if (*hp < 0)
error(ERROR_system(0), "%s: cannot create", cp);
diff -r -u original/src/lib/libcmd/tr.c build_libcmd_at/src/lib/libcmd/tr.c
--- src/lib/libcmd/tr.c 2013-07-18 15:47:52.000000000 +0200
+++ src/lib/libcmd/tr.c 2013-09-01 03:44:31.316342345 +0200
@@ -468,7 +468,7 @@
col[n].chr = set[n];
wcsxfrm(col[n].seq, w, sizeof(col[n].seq));
}
- qsort(col, c, sizeof(col[0]), (Compare_f)wcscmp);
+ qsort_r(col, c, sizeof(col[0]), (Compare_f)wcscmp, NULL);
for (n = 0; n < c; n++)
set[n] = col[n].chr;
}
diff -r -u original/src/lib/libcmd/uniq.c build_libcmd_at/src/lib/libcmd/uniq.c
--- src/lib/libcmd/uniq.c 2012-03-08 02:38:36.000000000 +0100
+++ src/lib/libcmd/uniq.c 2013-09-01 03:41:42.351921772 +0200
@@ -245,6 +245,7 @@
int
b_uniq(int argc, char** argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register int mode=0;
register char *cp;
int fields=0, chars=0, width=-1;
@@ -314,7 +315,7 @@
error(ERROR_usage(2), "%s", optusage(NiL));
if((cp = *argv) && (argv++,!streq(cp,"-")))
{
- if(!(fpin = sfopen(NiL,cp,"r")))
+ if(!(fpin = sfopenat(pwdfd, NiL, cp, "r")))
error(ERROR_system(1),"%s: cannot open",cp);
}
else
@@ -322,7 +323,7 @@
if(cp = *argv)
{
argv++;
- if(!(fpout = sfopen(NiL,cp,"w")))
+ if(!(fpout = sfopenat(pwdfd, NiL, cp, "w")))
error(ERROR_system(1),"%s: cannot create",cp);
}
else
diff -r -u original/src/lib/libcmd/wc.c build_libcmd_at/src/lib/libcmd/wc.c
--- src/lib/libcmd/wc.c 2012-11-21 22:25:48.000000000 +0100
+++ src/lib/libcmd/wc.c 2013-09-01 03:38:54.381552281 +0200
@@ -93,6 +93,7 @@
int
b_wc(int argc,register char **argv, Shbltin_t* context)
{
+ int pwdfd = (context)?context->pwdfd:AT_FDCWD;
register char *cp;
register int mode=0, n;
register Wc_t *wp;
@@ -162,7 +163,7 @@
{
if (!cp || streq(cp,"-"))
fp = sfstdin;
- else if (!(fp = sfopen(NiL,cp,"r")))
+ else if (!(fp = sfopenat(pwdfd, NiL, cp, "r")))
{
error(ERROR_system(0),"%s: cannot open",cp);
continue;