diff --git a/edit.c b/edit.c index 877da48..e21bdec 100644 --- a/edit.c +++ b/edit.c @@ -251,7 +251,7 @@ x_print_expansions(int nwords, char * const *words, bool is_command) break; /* All in same directory? */ if (i == nwords) { - while (prefix_len > 0 && !IS_DIR_SEP(words[0][prefix_len - 1])) + while (prefix_len > 0 && !mksh_dirsep(words[0][prefix_len - 1])) prefix_len--; use_copy = true; XPinit(l, nwords + 1); @@ -327,7 +327,7 @@ x_glob_hlp_tilde_and_rem_qchar(char *s, bool magic_flag) * and if so, discern "~foo/bar" and "~/baz" from "~blah"; * if we have a directory part (the former), try to expand */ - if (*s == '~' && (cp = ksh_strchr_dirsep(s)) != NULL) { + if (*s == '~' && (cp = mksh_strchr_dirsep(s)) != NULL) { /* ok, so split into "~foo"/"bar" or "~"/"baz" */ *cp++ = 0; /* try to expand the tilde */ @@ -577,7 +577,7 @@ x_locate_word(const char *buf, int buflen, int pos, int *startp, * like file globbing. */ for (p = start; p < end; p++) - if (IS_DIR_SEP(buf[p])) + if (mksh_dirsep(buf[p])) break; iscmd = p == end; } @@ -641,7 +641,7 @@ x_cf_glob(int *flagsp, const char *buf, int buflen, int pos, int *startp, } } - if (*toglob == '~' && !ksh_vstrchr_dirsep(toglob)) { + if (*toglob == '~' && !mksh_vstrchr_dirsep(toglob)) { /* neither for '~foo' (but '~foo/bar') */ *flagsp |= XCF_IS_NOSPACE; goto dont_add_glob; @@ -731,11 +731,11 @@ x_basename(const char *s, const char *se) return (0); /* Skip trailing slashes */ - for (p = se - 1; p > s && IS_DIR_SEP(*p); p--) + for (p = se - 1; p > s && mksh_dirsep(*p); p--) ; - for (; p > s && !IS_DIR_SEP(*p); p--) + for (; p > s && !mksh_dirsep(*p); p--) ; - if (IS_DIR_SEP(*p) && p + 1 < se) + if (mksh_dirsep(*p) && p + 1 < se) p++; return (p - s); @@ -2800,7 +2800,7 @@ do_complete( * append a space if this is a single non-directory match * and not a parameter or homedir substitution */ - if (nwords == 1 && !IS_DIR_SEP(words[0][nlen - 1]) && + if (nwords == 1 && !mksh_dirsep(words[0][nlen - 1]) && !(flags & XCF_IS_NOSPACE)) { x_ins(" "); } @@ -5443,7 +5443,7 @@ complete_word(int cmd, int count) * append a space if this is a non-directory match * and not a parameter or homedir substitution */ - if (match_len > 0 && !IS_DIR_SEP(match[match_len - 1]) && + if (match_len > 0 && !mksh_dirsep(match[match_len - 1]) && !(flags & XCF_IS_NOSPACE)) rval = putbuf(" ", 1, false); } diff --git a/eval.c b/eval.c index 950bfe9..66f8a43 100644 --- a/eval.c +++ b/eval.c @@ -1535,7 +1535,7 @@ globit(XString *xs, /* dest string */ * SunOS 4.1.3 does this... */ if ((check & GF_EXCHECK) && xp > Xstring(*xs, xp) && - IS_DIR_SEP(xp[-1]) && !S_ISDIR(lstatb.st_mode) && + mksh_dirsep(xp[-1]) && !S_ISDIR(lstatb.st_mode) && (!S_ISLNK(lstatb.st_mode) || stat_check() < 0 || !S_ISDIR(statb.st_mode))) return; @@ -1545,7 +1545,7 @@ globit(XString *xs, /* dest string */ * directory */ if (((check & GF_MARKDIR) && (check & GF_GLOBBED)) && - xp > Xstring(*xs, xp) && !IS_DIR_SEP(xp[-1]) && + xp > Xstring(*xs, xp) && !mksh_dirsep(xp[-1]) && (S_ISDIR(lstatb.st_mode) || (S_ISLNK(lstatb.st_mode) && stat_check() > 0 && S_ISDIR(statb.st_mode)))) { @@ -1560,11 +1560,11 @@ globit(XString *xs, /* dest string */ if (xp > Xstring(*xs, xp)) *xp++ = '/'; - while (IS_DIR_SEP(*sp)) { + while (mksh_dirsep(*sp)) { Xcheck(*xs, xp); *xp++ = *sp++; } - np = ksh_strchr_dirsep(sp); + np = mksh_strchr_dirsep(sp); if (np != NULL) { se = np; /* don't assume '/', can be multiple kinds */ @@ -1672,7 +1672,7 @@ maybe_expand_tilde(const char *p, XString *dsp, char **dpp, bool isassign) Xinit(ts, tp, 16, ATEMP); /* : only for DOASNTILDE form */ - while (p[0] == CHAR && !IS_DIR_SEP(p[1]) && (!isassign || p[1] != ':')) + while (p[0] == CHAR && !mksh_dirsep(p[1]) && (!isassign || p[1] != ':')) { Xcheck(ts, tp); *tp++ = p[1]; diff --git a/exec.c b/exec.c index ab4ffb5..8992a69 100644 --- a/exec.c +++ b/exec.c @@ -703,7 +703,7 @@ comexec(struct op *t, struct tbl * volatile tp, const char **ap, rv = subst_exstat; goto Leave; } else if (!tp) { - if (Flag(FRESTRICTED) && ksh_vstrchr_dirsep(cp)) { + if (Flag(FRESTRICTED) && mksh_vstrchr_dirsep(cp)) { warningf(true, "%s: %s", cp, "restricted"); rv = 1; goto Leave; @@ -993,7 +993,7 @@ scriptexec(struct op *tp, const char **ap) * Search shell/interpreter name without directory in PATH * if specified path does not exist */ - if (ksh_vstrchr_dirsep(sh) && !search_path(sh, path, X_OK, NULL)) { + if (mksh_vstrchr_dirsep(sh) && !search_path(sh, path, X_OK, NULL)) { cp = search_path(_getname(sh), path, X_OK, NULL); if (cp) sh = cp; @@ -1183,7 +1183,7 @@ findcom(const char *name, int flags) char *fpath; union mksh_cchack npath; - if (ksh_vstrchr_dirsep(name)) { + if (mksh_vstrchr_dirsep(name)) { insert = 0; /* prevent FPATH search below */ flags &= ~FC_FUNC; @@ -1330,7 +1330,7 @@ search_path(const char *name, const char *lpath, size_t namelen; int ec = 0, ev; - if (ksh_vstrchr_dirsep(name)) { + if (mksh_vstrchr_dirsep(name)) { if ((ec = search_access(name, mode)) == 0) { search_path_ok: if (errnop) diff --git a/main.c b/main.c index 9bf667d..b5f3d08 100644 --- a/main.c +++ b/main.c @@ -225,7 +225,7 @@ main_init(int argc, const char *argv[], Source **sp, struct block **lp) ccp = kshname; goto begin_parse_kshname; while ((i = ccp[argi++])) { - if (IS_DIR_SEP(i)) { + if (mksh_dirsep(i)) { ccp += argi; begin_parse_kshname: argi = 0; diff --git a/misc.c b/misc.c index da20bb2..99e169f 100644 --- a/misc.c +++ b/misc.c @@ -387,7 +387,7 @@ parse_args(const char **argv, */ if (*p != '-') for (q = p; *q; ) - if (IS_DIR_SEP(*q++)) + if (mksh_dirsep(*q++)) p = q; Flag(FLOGIN) = (*p == '-'); opts = cmd_opts; @@ -1440,14 +1440,14 @@ do_realpath(const char *upath) while (*ip) { /* skip slashes in input */ - while (IS_DIR_SEP(*ip)) + while (mksh_dirsep(*ip)) ++ip; if (!*ip) break; /* get next pathname component from input */ tp = ip; - while (*ip && !IS_DIR_SEP(*ip)) + while (*ip && !mksh_dirsep(*ip)) ++ip; len = ip - tp; @@ -1459,7 +1459,7 @@ do_realpath(const char *upath) else if (len == 2 && tp[1] == '.') { /* strip off last pathname component */ while (xp > Xstring(xs, xp)) - if (IS_DIR_SEP(*--xp)) + if (mksh_dirsep(*--xp)) break; /* then continue with the next one */ continue; @@ -1482,7 +1482,7 @@ do_realpath(const char *upath) /* lstat failed */ if (errno == ENOENT) { /* because the pathname does not exist */ - while (IS_DIR_SEP(*ip)) + while (mksh_dirsep(*ip)) /* skip any trailing slashes */ ++ip; /* no more components left? */ @@ -1542,7 +1542,7 @@ do_realpath(const char *upath) /* assert: xp == xs.beg => start of path */ /* exactly two leading slashes? (SUSv4 3.266) */ - if (IS_DIR_SEP(ip[1]) && !IS_DIR_SEP(ip[2])) { + if (mksh_dirsep(ip[1]) && !mksh_dirsep(ip[2])) { /* keep them, e.g. for UNC pathnames */ Xput(xs, xp, '/'); } @@ -1567,7 +1567,7 @@ do_realpath(const char *upath) * if source path had a trailing slash, check if target path * is not a non-directory existing file */ - if (ip > ipath && IS_DIR_SEP(ip[-1])) { + if (ip > ipath && mksh_dirsep(ip[-1])) { if (stat(Xstring(xs, xp), &sb)) { if (errno != ENOENT) goto notfound; @@ -1638,7 +1638,7 @@ make_path(const char *cwd, const char *file, if (c == '.') c = file[2]; - if (IS_DIR_SEP(c) || c == '\0') + if (mksh_dirsep(c) || c == '\0') use_cdpath = false; } @@ -1660,7 +1660,7 @@ make_path(const char *cwd, const char *file, XcheckN(*xsp, xp, len); memcpy(xp, cwd, len); xp += len; - if (!IS_DIR_SEP(cwd[len - 1])) + if (!mksh_dirsep(cwd[len - 1])) Xput(*xsp, xp, '/'); } *phys_pathp = Xlength(*xsp, xp); @@ -1668,7 +1668,7 @@ make_path(const char *cwd, const char *file, XcheckN(*xsp, xp, plen); memcpy(xp, plist, plen); xp += plen; - if (!IS_DIR_SEP(plist[plen - 1])) + if (!mksh_dirsep(plist[plen - 1])) Xput(*xsp, xp, '/'); rval = 1; } @@ -1713,7 +1713,7 @@ simplify_path(char *p) case '\\': #endif /* exactly two leading slashes? (SUSv4 3.266) */ - if (IS_DIR_SEP(p[1]) && !IS_DIR_SEP(p[2])) + if (mksh_dirsep(p[1]) && !mksh_dirsep(p[2])) /* keep them, e.g. for UNC pathnames */ ++p; needslash = true; @@ -1725,14 +1725,14 @@ simplify_path(char *p) while (*ip) { /* skip slashes in input */ - while (IS_DIR_SEP(*ip)) + while (mksh_dirsep(*ip)) ++ip; if (!*ip) break; /* get next pathname component from input */ tp = ip; - while (*ip && !IS_DIR_SEP(*ip)) + while (*ip && !mksh_dirsep(*ip)) ++ip; len = ip - tp; @@ -1752,7 +1752,7 @@ simplify_path(char *p) strip_last_component: /* strip off last pathname component */ while (dp > sp) - if (IS_DIR_SEP(*--dp)) + if (mksh_dirsep(*--dp)) break; } else { /* relative path, at its beginning */ diff --git a/sh.h b/sh.h index dbc3658..48d9943 100644 --- a/sh.h +++ b/sh.h @@ -302,42 +302,6 @@ struct rusage { } while (/* CONSTCOND */ 0) #endif -#ifdef __OS2__ -#define UNIXROOT "/@unixroot" -#define PATH_SEP ';' -#define PATH_SEP_STR ";" -#define IS_DIR_SEP(c) ({ \ - char _ch_ = (c); \ - (_ch_ == '/' || _ch_ == '\\'); \ -}) -#define IS_ABS_PATH(x) ({ \ - const char *_p_ = (x); \ - ((ksh_isalphx(_p_[0]) && _p_[1] == ':') || IS_DIR_SEP(_p_[0])); \ -}) -#define ksh_strchr_dirsep(s) ({ \ - const char *_p_ = (s); \ - const char *_p1_ = strchr(_p_, '/'); \ - const char *_p2_ = strchr(_p_, '\\'); \ - const char *_p3_ = ((ksh_isalphx(_p_[0]) && _p_[1] == ':') \ - ? (_p_ + 1) : NULL ); \ - _p1_ = _p1_ > _p2_ ? _p1_ : _p2_; \ - ((char *)(_p1_ > _p3_ ? _p1_ : _p3_)); \ -}) -#define ksh_vstrchr_dirsep(s) ({ \ - const char *_p_ = (s); \ - (vstrchr((_p_), '/') || vstrchr((_p_), '\\') || \ - (ksh_isalphx(_p_[0]) && _p_[1] == ':')); \ -}) -#else -#define UNIXROOT "" -#define PATH_SEP ':' -#define PATH_SEP_STR ":" -#define IS_DIR_SEP(c) ((c) == '/') -#define IS_ABS_PATH(x) (IS_DIR_SEP((x)[0])) -#define ksh_strchr_dirsep(s) (strchr((s), '/')) -#define ksh_vstrchr_dirsep(s) (vstrchr((s), '/')) -#endif - #ifdef MKSH__NO_PATH_MAX #undef PATH_MAX #else @@ -1109,7 +1073,7 @@ EXTERN mksh_ari_t x_lins E_INIT(24); /* tty lines */ #if defined(ANDROID) #define MKSH_DEFAULT_PROFILEDIR "/system/etc" #else -#define MKSH_DEFAULT_PROFILEDIR UNIXROOT "/etc" +#define MKSH_DEFAULT_PROFILEDIR MKSH_UNIXROOT "/etc" #endif #endif @@ -2156,13 +2120,41 @@ extern int tty_init_fd(void); /* initialise tty_fd, tty_devtty */ }) #define mksh_abspath(s) __extension__({ \ const char *mksh_abspath_s = (s); \ - (mksh_abspath_s[0] == '/' || (ksh_isalphx(mksh_abspath_s[0]) && \ + (mksh_dirsep(mksh_abspath_s[0]) || (ksh_isalphx(mksh_abspath_s[0]) && \ mksh_abspath_s[1] == ':')); \ }) +#define mksh_dirsep(c) __extension__({ \ + char mksh_dirsep_c = (c); \ + (mksh_dirsep_c == '/' || mksh_dirsep_c == '\\'); \ +}) +#define mksh_strchr_dirsep(s) __extension__({ \ + const char *mksh_strchr_dirsep_p = (s); \ + const char *mksh_strchr_dirsep_p1 = strchr(mksh_strchr_dirsep_p, '/'); \ + const char *mksh_strchr_dirsep_p2 = strchr(mksh_strchr_dirsep_p, '\\'); \ + const char *mksh_strchr_dirsep_p3 = \ + ((ksh_isalphx(mksh_strchr_dirsep_p[0]) && \ + mksh_strchr_dirsep_p[1] == ':') \ + ? (mksh_strchr_dirsep_p + 1) : NULL ); \ + mksh_strchr_dirsep_p1 = mksh_strchr_dirsep_p1 > mksh_strchr_dirsep_p2 ? \ + mksh_strchr_dirsep_p1 : mksh_strchr_dirsep_p2; \ + ((char *)(mksh_strchr_dirsep_p1 > mksh_strchr_dirsep_p3 ? \ + mksh_strchr_dirsep_p1 : mksh_strchr_dirsep_p3)); \ +}) +#define mksh_vstrchr_dirsep(s) __extension__({ \ + const char *mksh_vstrchr_dirsep_p = (s); \ + (vstrchr((mksh_vstrchr_dirsep_p), '/') || \ + vstrchr((mksh_vstrchr_dirsep_p), '\\') || \ + (ksh_isalphx(mksh_vstrchr_dirsep_p[0]) && \ + mksh_vstrchr_dirsep_p[1] == ':')); \ +}) + #else #define binopen2(path,flags) open((path), (flags) | O_BINARY) #define binopen3(path,flags,mode) open((path), (flags) | O_BINARY, (mode)) #define mksh_abspath(s) ((s)[0] == '/') +#define mksh_dirsep(c) ((c) == '/') +#define mksh_strchr_dirsep(s) (strchr((s), '/')) +#define mksh_vstrchr_dirsep(s) (vstrchr((s), '/')) #endif /* be sure not to interfere with anyone else's idea about EXTERN */