On 09/14/18 16:30, Michael Mikonos wrote:
> On Sat, Sep 08, 2018 at 10:13:35AM +0200, Martijn van Duren wrote:
>> On 09/08/18 04:57, Michael Mikonos wrote:
>>> Hello,
>>>
>>> The function strsave() in csh(1) is practically strdup(3).
>>> The only difference is memory allocation failure results in
>>> calling the stderror() error handler, which will later exit.
>>> This patch makes the code (IMO) clearer by removing two loops.
>>> xmalloc() behaves the same as xreallocarray() in terms of
>>> calling stderror(). Does this look OK?
>>>
>>> - Michael
>>>
>> Why not use strdup(3) altogether then? This way it's even more
>> clear what's intended. Maybe we should even rename the function
>> to xstrdup?
>>
>> martijn@
>
> Your patch was better. Here is a version with the function renamed
> and const added to the param list to match strdup(3).
OK martijn@
While here, should we also remove any in favour of strchr? Only
difference seems to be the return type (bool vs pointer).
Index: dol.c
===================================================================
RCS file: /cvs/src/bin/csh/dol.c,v
retrieving revision 1.21
diff -u -p -r1.21 dol.c
--- dol.c 16 Dec 2017 10:27:21 -0000 1.21
+++ dol.c 15 Sep 2018 10:41:24 -0000
@@ -541,7 +541,7 @@ Dgetdol(void)
for (i = 0; Isdigit(*np); i = i * 10 + *np++ - '0')
continue;
- if ((i < 0 || i > upb) && !any("-*", *np)) {
+ if ((i < 0 || i > upb) && !strchr("-*", *np)) {
dolerror(vp->v_name);
return;
}
@@ -642,7 +642,7 @@ fixDolMod(void)
dolmod[dolnmod++] = delim;
if (!delim || letter(delim)
- || Isdigit(delim) || any(" \t\n", delim)) {
+ || Isdigit(delim) || strchr(" \t\n", delim)) {
seterror(ERR_BADSUBST);
break;
}
@@ -657,7 +657,7 @@ fixDolMod(void)
}
continue;
}
- if (!any("htrqxes", c))
+ if (!strchr("htrqxes", c))
stderror(ERR_BADMOD, c);
dolmod[dolnmod++] = c;
if (c == 'q')
@@ -691,7 +691,7 @@ setDolp(Char *cp)
delim = dolmod[++i];
if (!delim || letter(delim)
- || Isdigit(delim) || any(" \t\n", delim)) {
+ || Isdigit(delim) || strchr(" \t\n", delim)) {
seterror(ERR_BADSUBST);
break;
}
@@ -901,7 +901,7 @@ heredoc(Char *term)
/* \ quotes \ $ ` here */
if (c == '\\') {
c = DgetC(0);
- if (!any("$\\`", c))
+ if (!strchr("$\\`", c))
unDgetC(c | QUOTE), c = '\\';
else
c |= QUOTE;
@@ -918,7 +918,7 @@ heredoc(Char *term)
* If any ` in line do command substitution
*/
mbp = mbuf;
- if (any(short2str(mbp), '`')) {
+ if (strchr(short2str(mbp), '`')) {
/*
* 1 arg to dobackp causes substitution to be literal. Words are
* broken only at newlines so that all blanks and tabs are
Index: exec.c
===================================================================
RCS file: /cvs/src/bin/csh/exec.c,v
retrieving revision 1.19
diff -u -p -r1.19 exec.c
--- exec.c 26 Dec 2015 13:48:38 -0000 1.19
+++ exec.c 15 Sep 2018 10:41:24 -0000
@@ -137,7 +137,7 @@ doexec(Char **v, struct command *t)
blkfree(pv);
pexerr();
}
- slash = any(short2str(expath), '/');
+ slash = (bool) strchr(short2str(expath), '/');
/*
* Glob the argument list, if necessary. Otherwise trim off the quote bits.
@@ -492,7 +492,7 @@ iscommand(Char *name)
Char **pv;
Char *sav;
struct varent *v;
- bool slash = any(short2str(name), '/');
+ bool slash = (bool) strchr(short2str(name), '/');
int hashval = 0, hashval1, i;
v = adrof(STRpath);
@@ -680,7 +680,7 @@ tellmewhat(struct wordent *lexp, Char *s
if ((i = iscommand(sp->word)) != 0) {
Char **pv;
struct varent *v;
- bool slash = any(short2str(sp->word), '/');
+ bool slash = (bool) strchr(short2str(sp->word), '/');
v = adrof(STRpath);
if (v == 0 || v->vec[0] == 0 || slash)
Index: exp.c
===================================================================
RCS file: /cvs/src/bin/csh/exp.c,v
retrieving revision 1.16
diff -u -p -r1.16 exp.c
--- exp.c 26 Dec 2015 13:48:38 -0000 1.16
+++ exp.c 15 Sep 2018 10:41:24 -0000
@@ -36,6 +36,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
+#include <string.h>
#include "csh.h"
#include "extern.h"
@@ -238,7 +239,7 @@ exp3a(Char ***vp, bool ignore)
p1 = exp4(vp, ignore);
op = **vp;
- if (op && any("<>", op[0]) && op[0] == op[1]) {
+ if (op && strchr("<>", op[0]) && op[0] == op[1]) {
(*vp)++;
p2 = exp3a(vp, ignore);
if (op[0] == '<')
@@ -392,7 +393,7 @@ exp6(Char ***vp, bool ignore)
if (isa(**vp, ANYOP))
return (Strsave(STRNULL));
cp = *(*vp)++;
- if (*cp == '-' && any("erwxfdzopls", cp[1])) {
+ if (*cp == '-' && strchr("erwxfdzopls", cp[1])) {
struct stat stb;
if (cp[2] != '\0')
Index: extern.h
===================================================================
RCS file: /cvs/src/bin/csh/extern.h,v
retrieving revision 1.26
diff -u -p -r1.26 extern.h
--- extern.h 22 Jul 2017 09:37:21 -0000 1.26
+++ extern.h 15 Sep 2018 10:41:24 -0000
@@ -181,7 +181,6 @@ void unreadc(int);
/*
* misc.c
*/
-int any(char *, int);
Char **blkcat(Char **, Char **);
Char **blkcpy(Char **, Char **);
Char **blkend(Char **);
Index: lex.c
===================================================================
RCS file: /cvs/src/bin/csh/lex.c,v
retrieving revision 1.25
diff -u -p -r1.25 lex.c
--- lex.c 30 Aug 2017 07:54:54 -0000 1.25
+++ lex.c 15 Sep 2018 10:41:24 -0000
@@ -406,7 +406,7 @@ getdol(void)
np = name, *np++ = '$';
c = sc = getC(DOEXCL);
- if (any("\t \n", c)) {
+ if (strchr("\t \n", c)) {
ungetD(c);
ungetC('$' | QUOTE);
return;
@@ -550,7 +550,7 @@ getdol(void)
*np++ = delim;
if (!delim || letter(delim)
- || Isdigit(delim) || any(" \t\n", delim)) {
+ || Isdigit(delim) || strchr(" \t\n", delim)) {
seterror(ERR_BADSUBST);
break;
}
@@ -565,7 +565,7 @@ getdol(void)
}
c = 's';
}
- if (!any("htrqxes", c)) {
+ if (!strchr("htrqxes", c)) {
if ((amodflag || gmodflag) && c == '\n')
stderror(ERR_VARSYN); /* strike */
seterror(ERR_VARMOD, c);
@@ -651,7 +651,7 @@ getexcl(int sc)
goto subst;
}
c = getC(0);
- if (!any(":^$*-%", c))
+ if (!strchr(":^$*-%", c))
goto subst;
left = right = -1;
if (c == ':') {
@@ -744,7 +744,7 @@ getsub(struct wordent *en)
case 's':
delim = getC(0);
- if (letter(delim) || Isdigit(delim) || any(" \t\n", delim)) {
+ if (letter(delim) || Isdigit(delim) || strchr(" \t\n", delim)) {
unreadc(delim);
lhsb[0] = 0;
seterror(ERR_BADSUBST);
@@ -957,7 +957,7 @@ domod(Char *cp, int type)
case 'h':
case 't':
- if (!any(short2str(cp), '/'))
+ if (!strchr(short2str(cp), '/'))
return (type == 't' ? Strsave(cp) : 0);
wp = Strend(cp);
while (*--wp != '/')
@@ -1067,7 +1067,7 @@ getsel(int *al, int *ar, int dol)
if (first) {
c = getC(0);
unreadc(c);
- if (any("-$*", c))
+ if (strchr("-$*", c))
return (1);
}
if (*al > *ar || *ar > dol) {
@@ -1122,14 +1122,14 @@ gethent(int sc)
/* FALLSTHROUGH */
default:
- if (any("(=~", c)) {
+ if (strchr("(=~", c)) {
unreadc(c);
ungetC(HIST);
return (0);
}
np = lhsb;
event = 0;
- while (!cmap(c, _ESC | _META | _QF | _QB) && !any("${}:", c)) {
+ while (!cmap(c, _ESC | _META | _QF | _QB) && !strchr("${}:", c)) {
if (event != -1 && Isdigit(c))
event = event * 10 + c - '0';
else
Index: misc.c
===================================================================
RCS file: /cvs/src/bin/csh/misc.c,v
retrieving revision 1.20
diff -u -p -r1.20 misc.c
--- misc.c 20 Jun 2017 16:44:06 -0000 1.20
+++ misc.c 15 Sep 2018 10:41:24 -0000
@@ -41,17 +41,6 @@
static int fdcmp(int);
static int renum(int, int);
-int
-any(char *s, int c)
-{
- if (!s)
- return (0); /* Check for nil pointer */
- while (*s)
- if (*s++ == c)
- return (1);
- return (0);
-}
-
char *
strsave(char *s)
{
Index: parse.c
===================================================================
RCS file: /cvs/src/bin/csh/parse.c,v
retrieving revision 1.12
diff -u -p -r1.12 parse.c
--- parse.c 26 Dec 2015 13:48:38 -0000 1.12
+++ parse.c 15 Sep 2018 10:41:24 -0000
@@ -89,7 +89,7 @@ static void
asyntax(struct wordent *p1, struct wordent *p2)
{
while (p1 != p2)
- if (any(";&\n", p1->word[0]))
+ if (strchr(";&\n", p1->word[0]))
p1 = p1->next;
else {
asyn0(p1, p2);
@@ -214,7 +214,7 @@ syntax(struct wordent *p1, struct worden
{
while (p1 != p2)
- if (any(";&\n", p1->word[0]))
+ if (strchr(";&\n", p1->word[0]))
p1 = p1->next;
else
return (syn0(p1, p2, flags));
@@ -516,7 +516,7 @@ again:
}
if (p->next == p2)
continue;
- if (any(RELPAR, p->next->word[0]))
+ if (strchr(RELPAR, p->next->word[0]))
continue;
n--;
continue;
@@ -576,7 +576,7 @@ again:
continue;
}
p = p->next;
- if (any(RELPAR, p->word[0])) {
+ if (strchr(RELPAR, p->word[0])) {
seterror(ERR_MISRED);
continue;
}
@@ -596,7 +596,7 @@ again:
continue;
}
p = p->next;
- if (any(RELPAR, p->word[0])) {
+ if (strchr(RELPAR, p->word[0])) {
seterror(ERR_MISRED);
continue;
}
Index: set.c
===================================================================
RCS file: /cvs/src/bin/csh/set.c,v
retrieving revision 1.19
diff -u -p -r1.19 set.c
--- set.c 26 Dec 2015 13:48:38 -0000 1.19
+++ set.c 15 Sep 2018 10:41:24 -0000
@@ -33,6 +33,7 @@
#include <sys/types.h>
#include <stdlib.h>
#include <stdarg.h>
+#include <string.h>
#include "csh.h"
#include "extern.h"
@@ -243,13 +244,13 @@ dolet(Char **v, struct command *t)
}
else {
c = *p++;
- if (any("+-", c)) {
+ if (strchr("+-", c)) {
if (c != op || *p)
stderror(ERR_NAME | ERR_UNKNOWNOP);
p = Strsave(STR1);
}
else {
- if (any("<>", op)) {
+ if (strchr("<>", op)) {
if (c != op)
stderror(ERR_NAME | ERR_UNKNOWNOP);
c = *p++;
Index: time.c
===================================================================
RCS file: /cvs/src/bin/csh/time.c,v
retrieving revision 1.15
diff -u -p -r1.15 time.c
--- time.c 22 Jul 2017 09:37:21 -0000 1.15
+++ time.c 15 Sep 2018 10:41:24 -0000
@@ -32,6 +32,7 @@
#include <sys/types.h>
#include <stdarg.h>
+#include <string.h>
#include "csh.h"
#include "extern.h"
@@ -83,7 +84,7 @@ donice(Char **v, struct command *t)
v++, cp = *v++;
if (cp == 0)
nval = 4;
- else if (*v == 0 && any("+-", cp[0]))
+ else if (*v == 0 && strchr("+-", cp[0]))
nval = getn(cp);
(void) setpriority(PRIO_PROCESS, 0, nval);
}