• access(2) is broken in at least kFreeBSD 9.0 as “modern” OS, so bring

back the wrapper code as well as refactor most other code calling it
• apparently, names can’t end in ‘_’ or contain ‘__’ anywhere…
This commit is contained in:
tg
2011-09-07 15:24:22 +00:00
parent 577c918beb
commit 9782f6b4d1
16 changed files with 326 additions and 332 deletions

91
funcs.c
View File

@ -38,7 +38,7 @@
#endif
#endif
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.196 2011/08/27 18:06:44 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.197 2011/09/07 15:24:15 tg Exp $");
#if HAVE_KILLPG
/*
@ -87,14 +87,14 @@ const struct builtin mkshbuiltins[] = {
{"*=:", c_true},
{"[", c_test},
{"*=break", c_brkcont},
{T_gbuiltin, c_builtin},
{Tgbuiltin, c_builtin},
{"*=continue", c_brkcont},
{"*=eval", c_eval},
{"*=exec", c_exec},
{"*=exit", c_exitreturn},
{"+false", c_false},
{"*=return", c_exitreturn},
{T_sgset, c_set},
{Tsgset, c_set},
{"*=shift", c_shift},
{"=times", c_times},
{"*=trap", c_trap},
@ -106,7 +106,7 @@ const struct builtin mkshbuiltins[] = {
{"+umask", c_umask},
{"*=unset", c_unset},
/* no =: AT&T manual wrong */
{T_palias, c_alias},
{Tpalias, c_alias},
{"+cd", c_cd},
/* dash compatibility hack */
{"chdir", c_cd},
@ -125,8 +125,8 @@ const struct builtin mkshbuiltins[] = {
#endif
{"pwd", c_pwd},
{"*=readonly", c_typeset},
{T__typeset, c_typeset},
{T_punalias, c_unalias},
{T_typeset, c_typeset},
{Tpunalias, c_unalias},
{"whence", c_whence},
#ifndef MKSH_UNEMPLOYED
{"+bg", c_fgbg},
@ -205,7 +205,6 @@ static const struct t_op b_ops[] = {
{"", TO_NONOP }
};
static int test_eaccess(const char *, int);
static int test_oexpr(Test_env *, bool);
static int test_aexpr(Test_env *, bool);
static int test_nexpr(Test_env *, bool);
@ -244,6 +243,7 @@ c_pwd(const char **wp)
}
p = current_wd[0] ? (physical ? allocd = do_realpath(current_wd) :
current_wd) : NULL;
/* LINTED use of access */
if (p && access(p, R_OK) < 0)
p = NULL;
if (!p && !(p = allocd = ksh_get_wd())) {
@ -544,9 +544,9 @@ c_whence(const char **wp)
if (vflag)
shprintf("n %s%s for ",
(tp->flag & EXPORT) ? "exported " : null,
T_alias);
Talias);
if (!iam_whence && !vflag)
shprintf("%s %s=", T_alias, id);
shprintf("%s %s=", Talias, id);
print_value_quoted(tp->val.s);
break;
case CFUNC:
@ -561,14 +561,14 @@ c_whence(const char **wp)
shprintf(" (autoload from %s)",
tp->u.fpath);
}
shf_puts(T__function, shl_stdout);
shf_puts(T_function, shl_stdout);
}
break;
case CSHELL:
if (vflag)
shprintf("%s %s %s",
(tp->flag & SPEC_BI) ? " special" : null,
"shell", T_builtin);
"shell", Tbuiltin);
break;
case CTALIAS:
case CEXEC:
@ -579,7 +579,7 @@ c_whence(const char **wp)
shprintf("a tracked %s%s for ",
(tp->flag & EXPORT) ?
"exported " : null,
T_alias);
Talias);
}
shf_puts(tp->val.s, shl_stdout);
} else {
@ -1031,12 +1031,12 @@ c_alias(const char **wp)
/* "hash -r" means reset all the tracked aliases.. */
if (rflag) {
static const char *args[] = {
T_unalias, "-ta", NULL
Tunalias, "-ta", NULL
};
if (!tflag || *wp) {
shprintf("%s: -r flag can only be used with -t"
" and without arguments\n", T_alias);
" and without arguments\n", Talias);
return (1);
}
ksh_getopt_reset(&builtin_opt, GF_ERROR);
@ -1049,7 +1049,7 @@ c_alias(const char **wp)
for (p = ktsort(t); (ap = *p++) != NULL; )
if ((ap->flag & (ISSET|xflag)) == (ISSET|xflag)) {
if (pflag)
shprintf("%s ", T_alias);
shprintf("%s ", Talias);
shf_puts(ap->name, shl_stdout);
if (prefix != '+') {
shf_putc('=', shl_stdout);
@ -1074,7 +1074,7 @@ c_alias(const char **wp)
ap = ktsearch(t, alias, h);
if (ap != NULL && (ap->flag&ISSET)) {
if (pflag)
shprintf("%s ", T_alias);
shprintf("%s ", Talias);
shf_puts(ap->name, shl_stdout);
if (prefix != '+') {
shf_putc('=', shl_stdout);
@ -1082,7 +1082,7 @@ c_alias(const char **wp)
}
shf_putc('\n', shl_stdout);
} else {
shprintf("%s %s %s\n", alias, T_alias,
shprintf("%s %s %s\n", alias, Talias,
"not found");
rv = 1;
}
@ -1097,7 +1097,9 @@ c_alias(const char **wp)
afree(ap->val.s, APERM);
}
/* ignore values for -t (AT&T ksh does this) */
newval = tflag ? search(alias, path, X_OK, NULL) : val;
newval = tflag ?
search_path(alias, path, X_OK, NULL) :
val;
if (newval) {
strdupx(ap->val.s, newval, APERM);
ap->flag |= ALLOC|ISSET;
@ -1711,9 +1713,8 @@ c_dot(const char **wp)
bi_errorf("missing argument");
return (1);
}
if ((file = search(cp, path, R_OK, &errcode)) == NULL) {
bi_errorf("%s: %s", cp,
errcode ? strerror(errcode) : "not found");
if ((file = search_path(cp, path, R_OK, &errcode)) == NULL) {
bi_errorf("%s: %s", cp, strerror(errcode));
return (1);
}
@ -1821,7 +1822,7 @@ c_read(const char **wp)
#if HAVE_SELECT
case 't':
if (parse_usec(builtin_opt.optarg, &tv)) {
bi_errorf("%s: %s '%s'", T_synerr, strerror(errno),
bi_errorf("%s: %s '%s'", Tsynerr, strerror(errno),
builtin_opt.optarg);
return (2);
}
@ -1903,7 +1904,7 @@ c_read(const char **wp)
rv = 1;
goto c_read_out;
default:
bi_errorf("%s: %s", T_select, strerror(errno));
bi_errorf("%s: %s", Tselect, strerror(errno));
rv = 2;
goto c_read_out;
}
@ -2361,7 +2362,7 @@ c_set(const char **wp)
const char **owp;
if (wp[1] == NULL) {
static const char *args[] = { T_set, "-", NULL };
static const char *args[] = { Tset, "-", NULL };
return (c_typeset(args));
}
@ -2862,15 +2863,17 @@ test_eval(Test_env *te, Test_op op, const char *opnd1, const char *opnd2,
/* -r */
case TO_FILRD:
return (test_eaccess(opnd1, R_OK) == 0);
/* LINTED use of access */
return (access(opnd1, R_OK) == 0);
/* -w */
case TO_FILWR:
return (test_eaccess(opnd1, W_OK) == 0);
/* LINTED use of access */
return (access(opnd1, W_OK) == 0);
/* -x */
case TO_FILEX:
return (test_eaccess(opnd1, X_OK) == 0);
return (ksh_access(opnd1, X_OK) == 0);
/* -a */
case TO_FILAXST:
@ -3067,26 +3070,6 @@ test_eval(Test_env *te, Test_op op, const char *opnd1, const char *opnd2,
return (1);
}
/* On most/all unixen, access() says everything is executable for root... */
static int
test_eaccess(const char *pathl, int mode)
{
int rv;
if ((rv = access(pathl, mode)) == 0 && ksheuid == 0 && (mode & X_OK)) {
struct stat statb;
if (stat(pathl, &statb) < 0)
rv = -1;
else if (S_ISDIR(statb.st_mode))
rv = 0;
else
rv = (statb.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) ?
0 : -1;
}
return (rv);
}
int
test_parse(Test_env *te)
{
@ -3516,7 +3499,7 @@ c_rename(const char **wp)
if (wp[0] == NULL /* first argument */ ||
wp[1] == NULL /* second argument */ ||
wp[2] != NULL /* no further args please */)
bi_errorf(T_synerr);
bi_errorf(Tsynerr);
else if ((rv = rename(wp[0], wp[1])) != 0) {
rv = errno;
bi_errorf("%s: %s", "failed", strerror(rv));
@ -3539,7 +3522,7 @@ c_realpath(const char **wp)
/* check for exactly one argument */
if (wp[0] == NULL || wp[1] != NULL)
bi_errorf(T_synerr);
bi_errorf(Tsynerr);
else if ((buf = do_realpath(wp[0])) == NULL) {
rv = errno;
bi_errorf("%s: %s", wp[0], strerror(rv));
@ -3564,7 +3547,7 @@ c_cat(const char **wp)
#define MKSH_CAT_BUFSIZ 4096
if ((buf = malloc_osfunc(MKSH_CAT_BUFSIZ)) == NULL) {
bi_errorf(T_oomem, (unsigned long)MKSH_CAT_BUFSIZ);
bi_errorf(Toomem, (unsigned long)MKSH_CAT_BUFSIZ);
return (1);
}
@ -3575,7 +3558,7 @@ c_cat(const char **wp)
/* we already operate unbuffered */
break;
default:
bi_errorf(T_synerr);
bi_errorf(Tsynerr);
return (1);
}
}
@ -3653,9 +3636,9 @@ c_sleep(const char **wp)
++wp;
if (!wp[0] || wp[1])
bi_errorf(T_synerr);
bi_errorf(Tsynerr);
else if (parse_usec(wp[0], &tv))
bi_errorf("%s: %s '%s'", T_synerr, strerror(errno), wp[0]);
bi_errorf("%s: %s '%s'", Tsynerr, strerror(errno), wp[0]);
else {
#ifndef MKSH_NOPROSPECTOFWORK
sigset_t omask;
@ -3670,7 +3653,7 @@ c_sleep(const char **wp)
*/
rv = 0;
else
bi_errorf("%s: %s", T_select, strerror(errno));
bi_errorf("%s: %s", Tselect, strerror(errno));
#ifndef MKSH_NOPROSPECTOFWORK
sigprocmask(SIG_SETMASK, &omask, NULL);
#endif