*
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*/
-
#include <sys/times.h>
#include <setjmp.h>
# include <assert.h>
# define bb_dev_null "/dev/null"
# define DEFAULT_SHELL "/proc/self/exe"
-# define CONFIG_BUSYBOX_EXEC_PATH "/proc/self/exe"
# define bb_banner "busybox standalone"
# define ENABLE_FEATURE_SH_STANDALONE 0
# define bb_msg_memory_exhausted "memory exhausted"
# define nonblock_safe_read(fd,buf,count) read(fd,buf,count)
# define NOT_LONE_DASH(s) ((s)[0] != '-' || (s)[1])
# define LONE_CHAR(s,c) ((s)[0] == (c) && !(s)[1])
-# define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
+# define NORETURN __attribute__ ((__noreturn__))
static int find_applet_by_name(const char *applet)
{
return -1;
#ifdef MSHDEBUG
static int mshdbg = MSHDEBUG;
-#define DBGPRINTF(x) if (mshdbg>0) printf x
-#define DBGPRINTF0(x) if (mshdbg>0) printf x
-#define DBGPRINTF1(x) if (mshdbg>1) printf x
-#define DBGPRINTF2(x) if (mshdbg>2) printf x
-#define DBGPRINTF3(x) if (mshdbg>3) printf x
-#define DBGPRINTF4(x) if (mshdbg>4) printf x
-#define DBGPRINTF5(x) if (mshdbg>5) printf x
-#define DBGPRINTF6(x) if (mshdbg>6) printf x
-#define DBGPRINTF7(x) if (mshdbg>7) printf x
-#define DBGPRINTF8(x) if (mshdbg>8) printf x
-#define DBGPRINTF9(x) if (mshdbg>9) printf x
+#define DBGPRINTF(x) if (mshdbg > 0) printf x
+#define DBGPRINTF0(x) if (mshdbg > 0) printf x
+#define DBGPRINTF1(x) if (mshdbg > 1) printf x
+#define DBGPRINTF2(x) if (mshdbg > 2) printf x
+#define DBGPRINTF3(x) if (mshdbg > 3) printf x
+#define DBGPRINTF4(x) if (mshdbg > 4) printf x
+#define DBGPRINTF5(x) if (mshdbg > 5) printf x
+#define DBGPRINTF6(x) if (mshdbg > 6) printf x
+#define DBGPRINTF7(x) if (mshdbg > 7) printf x
+#define DBGPRINTF8(x) if (mshdbg > 8) printf x
+#define DBGPRINTF9(x) if (mshdbg > 9) printf x
static int mshdbg_rc = 0;
#define RCPRINTF(x) ((void)0)
-#endif /* MSHDEBUG */
+#endif /* MSHDEBUG */
#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
* shell
*/
-#define LINELIM 2100
-#define NPUSH 8 /* limit to input nesting */
+#define LINELIM 2100
+#define NPUSH 8 /* limit to input nesting */
#undef NOFILE
-#define NOFILE 20 /* Number of open files */
-#define NUFILE 10 /* Number of user-accessible files */
-#define FDBASE 10 /* First file usable by Shell */
+#define NOFILE 20 /* Number of open files */
+#define NUFILE 10 /* Number of user-accessible files */
+#define FDBASE 10 /* First file usable by Shell */
/*
* values returned by wait
/*
* library and system definitions
*/
-typedef void xint; /* base type of jmp_buf, for not broken compilers */
+typedef void xint; /* base type of jmp_buf, for not broken compilers */
/*
* shell components
* redirection
*/
struct ioword {
- short io_unit; /* unit affected */
- short io_flag; /* action (below) */
+ smallint io_flag; /* action (below) */
+ int io_fd; /* fd affected */
char *io_name; /* file name */
};
#define IODUP 32 /* >&digit */
#define IOCLOSE 64 /* >&- */
-#define IODEFAULT (-1) /* token for default IO unit */
+#define IODEFAULT (-1) /* "default" IO fd */
/*
* Might eventually use a union.
*/
struct op {
- smallint type; /* operation type, see Txxxx below */
+ smallint op_type; /* operation type, see Txxxx below */
char **op_words; /* arguments to a command */
struct ioword **ioact; /* IO actions (eg, < > >>) */
struct op *left;
static void globname(char *we, char *pp);
static char *generate(char *start1, char *end1, char *middle, char *end);
static int anyspcl(struct wdblock *wb);
-static int xstrcmp(char *p1, char *p2);
-static void glob0(char *a0, unsigned a1
- /*, int item_sz, int (*f)(char *, char *) */);
static void readhere(char **name, char *s, int ec);
static int xxchar(struct ioarg *ap);
{ NULL , NULL },
};
-static struct op *scantree(struct op *);
-static struct op *dowholefile(int, int);
+static struct op *dowholefile(int /*, int*/);
/* Globals */
static char **dolv;
static int dolc;
-static int exstat;
+static uint8_t exstat;
static smallint gflg; /* (seems to be a parse error indicator) */
static smallint interactive; /* Is this an interactive shell */
static smallint execflg;
return;
}
- DBGPRINTF(("NODE: %p, left %p, right %p\n", head, head->left,
+ DBGPRINTF(("NODE: %p, left %p, right %p\n", head, head->left,
head->right));
if (head->left)
static void prs(const char *s)
{
if (*s)
- write(2, s, strlen(s));
+ xwrite_str(STDERR_FILENO, s);
}
static void prn(unsigned u)
/* fail but return to process next command */
-static void fail(void) ATTRIBUTE_NORETURN;
+static void fail(void) NORETURN;
static void fail(void)
{
longjmp(failpt, 1);
}
/* abort shell (or fail in subshell) */
-static void leave(void) ATTRIBUTE_NORETURN;
+static void leave(void) NORETURN;
static void leave(void)
{
DBGPRINTF(("LEAVE: leave called!\n"));
{
if (*s) {
prs(s);
- exstat = -1;
+ if (!exstat)
+ exstat = 255;
}
prs("\n");
if (FLAG['e'])
f = open(s, O_RDONLY);
if (f < 0) {
prs(s);
- err(": cannot open");
+ err(": can't open");
return 1;
}
}
}
+#ifdef UNUSED
struct op *scantree(struct op *head)
{
struct op *dotnode;
DBGPRINTF5(("SCANTREE: checking node %p\n", head));
- if ((head->type != TDOT) && LONE_CHAR(head->op_words[0], '.')) {
+ if ((head->op_type != TDOT) && LONE_CHAR(head->op_words[0], '.')) {
DBGPRINTF5(("SCANTREE: dot found in node %p\n", head));
return head;
}
return NULL;
}
+#endif
static void onecommand(void)
freehere(areanum);
freearea(areanum);
garbage();
- wdlist = 0;
- iolist = 0;
- global_env.errpt = 0;
+ wdlist = NULL;
+ iolist = NULL;
+ global_env.errpt = NULL;
global_env.linep = line;
yynerrs = 0;
multiline = 0;
PUSHIO(afile, f, filechar);
}
-static void onintr(int s) /* ANSI C requires a parameter */
+static void onintr(int s UNUSED_PARAM) /* ANSI C requires a parameter */
{
signal(SIGINT, onintr);
intr = 1;
* shell: syntax (C version)
*/
-static void yyerror(const char *s) ATTRIBUTE_NORETURN;
+static void yyerror(const char *s) NORETURN;
static void yyerror(const char *s)
{
yynerrs = 1;
fail();
}
-static void zzerr(void) ATTRIBUTE_NORETURN;
+static void zzerr(void) NORETURN;
static void zzerr(void)
{
yyerror("syntax error");
zzerr();
}
- if (t->type != TPAREN && t->type != TCOM) {
+ if (t->op_type != TPAREN && t->op_type != TCOM) {
/* shell statement */
t = block(TPAREN, t, NOBLOCK, NOWORDS);
}
}
t = block(c == LOGAND ? TAND : TOR, t, p, NOWORDS);
- } /* WHILE */
+ }
peeksym = c;
}
case WORD:
if (t == NULL) {
t = newtp();
- t->type = TCOM;
+ t->op_type = TCOM;
}
peeksym = 0;
word(yylval.cp);
if (iolist == NULL)
return NULL;
t = newtp();
- t->type = TCOM;
+ t->op_type = TCOM;
}
break;
case FOR:
t = newtp();
- t->type = TFOR;
+ t->op_type = TFOR;
musthave(WORD, 0);
startl = 1;
t->str = yylval.cp;
case UNTIL:
multiline++;
t = newtp();
- t->type = (c == WHILE ? TWHILE : TUNTIL);
+ t->op_type = (c == WHILE ? TWHILE : TUNTIL);
t->left = c_list();
t->right = dogroup(1);
/* t->op_words = NULL; - newtp() did this */
case CASE:
t = newtp();
- t->type = TCASE;
+ t->op_type = TCASE;
musthave(WORD, 0);
t->str = yylval.cp;
startl++;
case IF:
multiline++;
t = newtp();
- t->type = TIF;
+ t->op_type = TIF;
t->left = c_list();
t->right = thenpart();
musthave(FI, 0);
case DOT:
t = newtp();
- t->type = TDOT;
+ t->op_type = TDOT;
musthave(WORD, 0); /* gets name of file */
DBGPRINTF7(("COMMAND: DOT clause, yylval.cp is %s\n", yylval.cp));
return t;
}
-static struct op *dowholefile(int type, int mark)
+static struct op *dowholefile(int type /*, int mark*/)
{
struct op *t;
- DBGPRINTF(("DOWHOLEFILE: enter, type=%d, mark=%d\n", type, mark));
+ DBGPRINTF(("DOWHOLEFILE: enter, type=%d\n", type /*, mark*/));
multiline++;
t = c_list();
return NULL;
}
t = newtp();
- /*t->type = 0; - newtp() did this */
+ /*t->op_type = 0; - newtp() did this */
t->left = c_list();
if (t->left == NULL)
zzerr();
case ELIF:
t = newtp();
- t->type = TELIF;
+ t->op_type = TELIF;
t->left = c_list();
t->right = thenpart();
return t;
DBGPRINTF7(("CASEPART: enter...\n"));
t = newtp();
- t->type = TPAT;
+ t->op_type = TPAT;
t->op_words = pattern();
musthave(')', 0);
t->left = c_list();
DBGPRINTF7(("BLOCK: enter, type=%d (%s)\n", type, T_CMD_NAMES[type]));
t = newtp();
- t->type = type;
+ t->op_type = type;
t->left = t1;
t->right = t2;
t->op_words = wp;
static struct op *namelist(struct op *t)
{
DBGPRINTF7(("NAMELIST: enter, t=%p, type %s, iolist=%p\n", t,
- T_CMD_NAMES[t->type], iolist));
+ T_CMD_NAMES[t->op_type], iolist));
if (iolist) {
iolist = addword((char *) NULL, iolist);
} else
t->ioact = NULL;
- if (t->type != TCOM) {
- if (t->type != TPAREN && t->ioact != NULL) {
+ if (t->op_type != TCOM) {
+ if (t->op_type != TPAREN && t->ioact != NULL) {
t = block(TPAREN, t, NOBLOCK, NOWORDS);
t->ioact = t->left->ioact;
t->left->ioact = NULL;
char **wd;
wd = getwords(wdlist);
- wdlist = 0;
+ wdlist = NULL;
return wd;
}
struct ioword **iop;
iop = (struct ioword **) getwords(iolist);
- iolist = 0;
+ iolist = NULL;
return iop;
}
struct ioword *iop;
iop = (struct ioword *) tree(sizeof(*iop));
- iop->io_unit = u;
+ iop->io_fd = u;
iop->io_flag = f;
iop->io_name = cp;
iolist = addword((char *) iop, iolist);
}
break;
- case '#': /* Comment, skip to next newline or End-of-string */
+ case '#': /* Comment, skip to next newline or End-of-string */
while ((c = my_getc(0)) != '\0' && c != '\n')
continue;
unget(c);
return NULL;
}
- DBGPRINTF3(("FIND1CASE: enter, t->type=%d (%s)\n", t->type,
- T_CMD_NAMES[t->type]));
+ DBGPRINTF3(("FIND1CASE: enter, t->op_type=%d (%s)\n", t->op_type,
+ T_CMD_NAMES[t->op_type]));
- if (t->type == TLIST) {
+ if (t->op_type == TLIST) {
tp = find1case(t->left, w);
if (tp != NULL) {
DBGPRINTF3(("FIND1CASE: found one to the left, returning tp=%p\n", tp));
return 0;
}
- DBGPRINTF(("EXECUTE: t=%p, t->type=%d (%s), t->op_words is %s\n", t,
- t->type, T_CMD_NAMES[t->type],
+ DBGPRINTF(("EXECUTE: t=%p, t->op_type=%d (%s), t->op_words is %s\n", t,
+ t->op_type, T_CMD_NAMES[t->op_type],
((t->op_words == NULL) ? "NULL" : t->op_words[0])));
rv = 0;
a = areanum++;
wp2 = t->op_words;
wp = (wp2 != NULL)
- ? eval(wp2, t->type == TCOM ? DOALL : DOALL & ~DOKEY)
+ ? eval(wp2, t->op_type == TCOM ? DOALL : DOALL & ~DOKEY)
: NULL;
- switch (t->type) {
+ switch (t->op_type) {
case TDOT:
DBGPRINTF3(("EXECUTE: TDOT\n"));
newfile(evalstr(t->op_words[0], DOALL));
- t->left = dowholefile(TLIST, 0);
+ t->left = dowholefile(TLIST /*, 0*/);
t->right = NULL;
outtree = outtree_save;
case TAND:
rv = execute(t->left, pin, pout, /* no_fork: */ 0);
t1 = t->right;
- if (t1 != NULL && (rv == 0) == (t->type == TAND))
+ if (t1 != NULL && (rv == 0) == (t->op_type == TAND))
rv = execute(t1, pin, pout, /* no_fork: */ 0);
break;
while (setjmp(bc.brkpt))
if (isbreak)
goto broken;
+ /* Restore areanum value. It may be incremented by execute()
+ * below, and then "continue" may jump back to setjmp above */
+ areanum = a + 1;
+ freearea(areanum + 1);
brkset(&bc);
for (t1 = t->left; i-- && *wp != NULL;) {
setval(vp, *wp++);
while (setjmp(bc.brkpt))
if (isbreak)
goto broken;
+ /* Restore areanum value. It may be incremented by execute()
+ * below, and then "continue" may jump back to setjmp above */
+ areanum = a + 1;
+ freearea(areanum + 1);
brkset(&bc);
t1 = t->left;
- while ((execute(t1, pin, pout, /* no_fork: */ 0) == 0) == (t->type == TWHILE))
+ while ((execute(t1, pin, pout, /* no_fork: */ 0) == 0) == (t->op_type == TWHILE))
rv = execute(t->right, pin, pout, /* no_fork: */ 0);
brklist = brklist->nextlev;
break;
((t->op_words == NULL) ? "NULL" : t->op_words[0])));
owp = wp;
resetsig = 0;
- if (t->type == TCOM) {
+ if (t->op_type == TCOM) {
while (*wp++ != NULL)
continue;
cp = *wp;
// longjmps away (at "Run builtin" below), leaving t->op_words clobbered!
// See http://bugs.busybox.net/view.php?id=846.
// Now we do not touch t->op_words, but separately pass wp as param list
- // to builtins
+ // to builtins
DBGPRINTF(("FORKEXEC: bltin %p, no_fork %d, owp %p\n", bltin,
no_fork, owp));
/* Don't fork if it is a lone builtin (not in pipe)
DBGPRINTF3(("FORKEXEC: calling vfork()...\n"));
newpid = vfork();
if (newpid == -1) {
- DBGPRINTF(("FORKEXEC: ERROR, cannot vfork()!\n"));
+ DBGPRINTF(("FORKEXEC: ERROR, can't vfork()!\n"));
return -1;
}
if (iopp) {
if (bltin && bltin != doexec) {
prs(bltin_name);
- err(": cannot redirect shell command");
+ err(": can't redirect shell command");
if (forked)
_exit(-1);
return -1;
/* Builtin in pipe: disallowed */
/* TODO: allow "exec"? */
prs(bltin_name);
- err(": cannot run builtin as part of pipe");
+ err(": can't run builtin as part of pipe");
if (forked)
_exit(-1);
return -1;
signal(SIGQUIT, SIG_DFL);
}
- if (t->type == TPAREN)
+ if (t->op_type == TPAREN)
_exit(execute(t->left, NOPIPE, NOPIPE, /* no_fork: */ 1));
if (wp[0] == NULL)
- _exit(0);
+ _exit(EXIT_SUCCESS);
cp = rexecve(wp[0], wp, makenv(0, NULL));
prs(wp[0]);
DBGPRINTF(("IOSETUP: iop %p, pipein %i, pipeout %i\n", iop,
pipein, pipeout));
- if (iop->io_unit == IODEFAULT) /* take default */
- iop->io_unit = iop->io_flag & (IOREAD | IOHERE) ? 0 : 1;
+ if (iop->io_fd == IODEFAULT) /* take default */
+ iop->io_fd = iop->io_flag & (IOREAD | IOHERE) ? 0 : 1;
- if (pipein && iop->io_unit == 0)
+ if (pipein && iop->io_fd == 0)
return 0;
- if (pipeout && iop->io_unit == 1)
+ if (pipeout && iop->io_fd == 1)
return 0;
msg = iop->io_flag & (IOREAD | IOHERE) ? "open" : "create";
break;
case IODUP:
- u = dup2(*cp - '0', iop->io_unit);
+ u = dup2(*cp - '0', iop->io_fd);
break;
case IOCLOSE:
- close(iop->io_unit);
+ close(iop->io_fd);
return 0;
}
if (u < 0) {
prs(cp);
- prs(": cannot ");
+ prs(": can't ");
warn(msg);
return 1;
}
- if (u != iop->io_unit) {
- dup2(u, iop->io_unit);
- close(u);
- }
+ xmove_fd(u, iop->io_fd);
return 0;
}
prs(" - core dumped");
if (rv >= ARRAY_SIZE(signame) || signame[rv])
prs("\n");
- rv = -1;
+ rv |= 0x80;
} else
rv = WAITVAL(s);
}
if (tp != global_env.linep)
*tp++ = '/';
strcpy(tp, c);
- //for (i = 0; (*tp++ = c[i++]) != '\0';)
- // continue;
DBGPRINTF3(("REXECVE: global_env.linep is %s\n", global_env.linep));
switch (errno) {
case ENOEXEC:
+ /* File is executable but file format isnt recognized */
+ /* Run it as a shell script */
+ /* (execve above didnt do it itself, unlike execvp) */
*v = global_env.linep;
v--;
tp = *v;
- *v = global_env.linep;
+ *v = (char*)DEFAULT_SHELL;
execve(DEFAULT_SHELL, v, envp);
*v = tp;
return "no shell";
return "argument list too long";
}
}
- return errno == ENOENT ? "not found" : "cannot execute";
+ if (errno == ENOENT) {
+ exstat = 127; /* standards require this */
+ return "not found";
+ }
+ exstat = 126; /* mimic bash */
+ return "can't execute";
}
/*
errpt = ev;
if (newenv(setjmp(errpt)) == 0) {
- wdlist = 0;
- iolist = 0;
+ wdlist = NULL;
+ iolist = NULL;
pushio(argp, f);
global_env.iobase = global_env.iop;
yynerrs = 0;
* built-in commands: doX
*/
-static int dohelp(struct op *t, char **args)
+static int dohelp(struct op *t UNUSED_PARAM, char **args UNUSED_PARAM)
{
int col;
const struct builtincmd *x;
- puts("\nBuilt-in commands:\n"
- "-------------------");
+ printf(
+ "Built-in commands:\n"
+ "------------------\n");
col = 0;
x = builtincmds;
return EXIT_SUCCESS;
}
-static int dolabel(struct op *t, char **args)
+static int dolabel(struct op *t UNUSED_PARAM, char **args UNUSED_PARAM)
{
return 0;
}
-static int dochdir(struct op *t, char **args)
+static int dochdir(struct op *t UNUSED_PARAM, char **args)
{
const char *cp, *er;
return 1;
}
-static int doshift(struct op *t, char **args)
+static int doshift(struct op *t UNUSED_PARAM, char **args)
{
int n;
/*
* execute login and newgrp directly
*/
-static int dologin(struct op *t, char **args)
+static int dologin(struct op *t UNUSED_PARAM, char **args)
{
const char *cp;
return 1;
}
-static int doumask(struct op *t, char **args)
+static int doumask(struct op *t UNUSED_PARAM, char **args)
{
int i;
char *cp;
return 1;
}
-static int dodot(struct op *t, char **args)
+static int dodot(struct op *t UNUSED_PARAM, char **args)
{
int i;
const char *sp;
return -1;
}
-static int dowait(struct op *t, char **args)
+static int dowait(struct op *t UNUSED_PARAM, char **args)
{
int i;
char *cp;
return 0;
}
-static int doread(struct op *t, char **args)
+static int doread(struct op *t UNUSED_PARAM, char **args)
{
char *cp, **wp;
int nb = 0;
}
for (wp = args + 1; *wp; wp++) {
for (cp = global_env.linep; !nl && cp < elinep - 1; cp++) {
- nb = nonblock_safe_read(0, cp, sizeof(*cp));
+ nb = nonblock_safe_read(STDIN_FILENO, cp, sizeof(*cp));
if (nb != sizeof(*cp))
break;
nl = (*cp == '\n');
return nb <= 0;
}
-static int doeval(struct op *t, char **args)
+static int doeval(struct op *t UNUSED_PARAM, char **args)
{
return RUN(awordlist, args + 1, wdchar);
}
-static int dotrap(struct op *t, char **args)
+static int dotrap(struct op *t UNUSED_PARAM, char **args)
{
int n, i;
int resetsig;
return n * m;
}
-static int dobreak(struct op *t, char **args)
+static int dobreak(struct op *t UNUSED_PARAM, char **args)
{
return brkcontin(args[1], 1);
}
-static int docontinue(struct op *t, char **args)
+static int docontinue(struct op *t UNUSED_PARAM, char **args)
{
return brkcontin(args[1], 0);
}
/* NOTREACHED */
}
-static int doexit(struct op *t, char **args)
+static int doexit(struct op *t UNUSED_PARAM, char **args)
{
char *cp;
return 0;
}
-static int doexport(struct op *t, char **args)
+static int doexport(struct op *t UNUSED_PARAM, char **args)
{
rdexp(args + 1, export, EXPORT);
return 0;
}
-static int doreadonly(struct op *t, char **args)
+static int doreadonly(struct op *t UNUSED_PARAM, char **args)
{
rdexp(args + 1, ronly, RONLY);
return 0;
err(": bad identifier");
}
-static int doset(struct op *t, char **args)
+static int doset(struct op *t UNUSED_PARAM, char **args)
{
struct var *vp;
char *cp;
cp = args[1];
if (cp == NULL) {
for (vp = vlist; vp; vp = vp->next)
- varput(vp->name, 1);
+ varput(vp->name, STDOUT_FILENO);
return 0;
}
if (*cp == '-') {
static void varput(char *s, int out)
{
if (isalnum(*s) || *s == '_') {
- write(out, s, strlen(s));
- write(out, "\n", 1);
+ xwrite_str(out, s);
+ xwrite(out, "\n", 1);
}
}
#endif
}
-static int dotimes(struct op *t, char **args)
+static int dotimes(struct op *t UNUSED_PARAM, char **args UNUSED_PARAM)
{
struct tms buf;
unsigned clk_tck = sysconf(_SC_CLK_TCK);
switch (c) {
case '=':
if (isdigit(*s)) {
- err("cannot use ${...=...} with $n");
+ err("can't use ${...=...} with $n");
gflg = 1;
break;
}
prs(argument_list[0]);
prs(": ");
err(cp);
- _exit(1);
+ _exit(EXIT_FAILURE);
}
DELETE(cl->w_words[i]);
DELETE(cl);
}
- for (i = 0; i < cl->w_nword; i++)
- unquote(cl->w_words[i]);
- glob0((char *) cl->w_words, cl->w_nword /*, sizeof(char *), xstrcmp*/);
if (cl->w_nword) {
+ for (i = 0; i < cl->w_nword; i++)
+ unquote(cl->w_words[i]);
+ qsort_string_vector(cl->w_words, cl->w_nword);
for (i = 0; i < cl->w_nword; i++)
wb = addword(cl->w_words[i], wb);
DELETE(cl);
for (np = we; np != pp; pp--)
if (pp[-1] == '/')
break;
- for (dp = cp = get_space((int) (pp - np) + 3); np < pp;)
+ dp = cp = get_space((int) (pp - np) + 3);
+ while (np < pp)
*cp++ = *np++;
*cp++ = '.';
*cp = '\0';
- for (gp = cp = get_space(strlen(pp) + 1); *np && *np != '/';)
+ gp = cp = get_space(strlen(pp) + 1);
+ while (*np && *np != '/')
*cp++ = *np++;
*cp = '\0';
dirp = opendir(dp);
dname[NAME_MAX] = '\0';
while ((de = readdir(dirp)) != NULL) {
/* XXX Hmmm... What this could be? (abial) */
- /*
- if (ent[j].d_ino == 0)
- continue;
+ /* if (ent[j].d_ino == 0) continue;
*/
strncpy(dname, de->d_name, NAME_MAX);
if (dname[0] == '.')
return 0;
}
-static int xstrcmp(char *p1, char *p2)
-{
- return strcmp(*(char **) p1, *(char **) p2);
-}
-
/* -------- word.c -------- */
}
nb = sizeof(*wd) * wb->w_nword;
wd = get_space(nb);
- memcpy((char *) wd, (char *) wb->w_words, nb);
- DELETE(wb); /* perhaps should done by caller */
+ memcpy(wd, wb->w_words, nb);
+ DELETE(wb); /* perhaps should done by caller */
return wd;
}
-/*static int (*cmp_func) (char *, char *);*/
-/*static int glob_item_sz;*/
-#define cmp_func xstrcmp
-enum { glob_item_sz = sizeof(char *) };
-
-static void glob3(char *index1, char *index2, char *index3)
-{
- int m = glob_item_sz;
- do {
- char c = *index1;
- *index1++ = *index3;
- *index3++ = *index2;
- *index2++ = c;
- } while (--m);
-}
-
-static void glob2(char *index1, char *index2)
-{
- int m = glob_item_sz;
- do {
- char c = *index1;
- *index1++ = *index2;
- *index2++ = c;
- } while (--m);
-}
-
-static void glob1(char *base, char *lim)
-{
- char *i, *j;
- int v2;
- char *lptr, *hptr;
- int c;
- unsigned n;
-
- v2 = glob_item_sz;
- top:
- n = (int) (lim - base);
- if (n <= v2)
- return;
- n = v2 * (n / (2 * v2));
- hptr = lptr = base + n;
- i = base;
- j = lim - v2;
- for (;;) {
- if (i < lptr) {
- c = cmp_func(i, lptr);
- if (c == 0) {
- lptr -= v2;
- glob2(i, lptr);
- continue;
- }
- if (c < 0) {
- i += v2;
- continue;
- }
- }
- begin:
- if (j > hptr) {
- c = cmp_func(hptr, j);
- if (c == 0) {
- hptr += v2;
- glob2(hptr, j);
- goto begin;
- }
- if (c > 0) {
- if (i == lptr) {
- hptr += v2;
- glob3(i, hptr, j);
- lptr += v2;
- i = lptr;
- goto begin;
- }
- glob2(i, j);
- j -= v2;
- i += v2;
- continue;
- }
- j -= v2;
- goto begin;
- }
-
- if (i == lptr) {
- if (lptr - base >= lim - hptr) {
- glob1(hptr + v2, lim);
- lim = lptr;
- } else {
- glob1(base, lptr);
- base = hptr + v2;
- }
- goto top;
- }
-
- lptr -= v2;
- glob3(j, lptr, i);
- hptr -= v2;
- j = hptr;
- }
-}
-
-static void glob0(char *a0, unsigned a1
- /*, int item_sz, int (*f) (char *, char *) */)
-{
- /*cmp_func = f; - always xstrcmp */
- /*glob_item_sz = item_sz; - always sizeof(char*) */
- glob1(a0, a0 + a1 * /*item_sz:*/ sizeof(char*));
-}
-
/* -------- io.c -------- */
if (global_env.iop == iostack)
ioecho(c);
global_env.iop->prev = c;
- return global_env.iop->prev;
+ return c;
}
if (global_env.iop->task == XIO && global_env.iop->prev != '\n') {
global_env.iop->prev = 0;
if (global_env.iop->task == XIO) {
if (multiline) {
global_env.iop->prev = 0;
- return global_env.iop->prev;
+ return 0;
}
if (interactive && global_env.iop == iostack + 1) {
#if ENABLE_FEATURE_EDITING
static void ioecho(char c)
{
if (FLAG['v'])
- write(2, &c, sizeof c);
+ write(STDERR_FILENO, &c, sizeof c);
}
static void pushio(struct ioarg *argp, int (*fn) (struct ioarg *))
if (fn == filechar || fn == linechar)
global_env.iop->task = XIO;
else if (fn == (int (*)(struct ioarg *)) gravechar
- || fn == (int (*)(struct ioarg *)) qgravechar)
+ || fn == (int (*)(struct ioarg *)) qgravechar)
global_env.iop->task = XGRAVE;
else
global_env.iop->task = XOTHER;
*/
/*
- * Produce the characters of a string, then a newline, then EOF.
+ * Produce the characters of a string, then a newline, then NUL.
*/
static int nlchar(struct ioarg *ap)
{
- int c;
+ char c;
if (ap->aword == NULL)
- return 0;
+ return '\0';
c = *ap->aword++;
- if (c == 0) {
+ if (c == '\0') {
ap->aword = NULL;
return '\n';
}
while (size == 0 || position >= size) {
size = read_line_input(current_prompt, filechar_cmdbuf, BUFSIZ, line_input_state);
if (size < 0) /* Error/EOF */
- exit(0);
+ exit(EXIT_SUCCESS);
position = 0;
/* if Ctrl-C, size == 0 and loop will repeat */
}
}
/*
- * remap fd into Shell's fd space
+ * Remap fd into shell's fd space
*/
static int remap(int fd)
{
DBGPRINTF6(("FREEHERE: enter, area=%d\n", area));
hl = NULL;
- for (h = acthere; h != NULL; h = h->h_next)
+ for (h = acthere; h != NULL; h = h->h_next) {
if (getarea((char *) h) >= area) {
if (h->h_iop->io_name != NULL)
unlink(h->h_iop->io_name);
acthere = h->h_next;
else
hl->h_next = h->h_next;
- } else
+ } else {
hl = h;
+ }
+ }
}
/* Shell is non-interactive, activate printf-based debug */
#ifdef MSHDEBUG
- mshdbg = (int) (((char) (mshdbg_var->value[0])) - '0');
+ mshdbg = mshdbg_var->value[0] - '0';
if (mshdbg < 0)
mshdbg = 0;
#endif
name = *++argv;
if (newfile(name))
- exit(1); /* Exit on error */
+ exit(EXIT_FAILURE); /* Exit on error */
}
}