1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-04 09:09:56 +00:00

Invent a Buf_Append function to append a NUL-terminated string

and use it thoughout the code.

Submitted by:	Max Okumoto <okumoto@ucsd.edu>
This commit is contained in:
Hartmut Brandt 2005-02-07 07:49:16 +00:00
parent b348abd6cd
commit d01d6678ad
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=141436
7 changed files with 44 additions and 40 deletions

View File

@ -193,6 +193,16 @@ Buf_ReplaceLastByte(Buffer *bp, Byte byte)
}
}
/**
* Append characters in str to Buffer object
*/
void
Buf_Append(Buffer *bp, const char str[])
{
Buf_AddBytes(bp, strlen(str), str);
}
/**
* Clear the contents of the buffer.
*/

View File

@ -83,4 +83,6 @@ Buffer *Buf_Init(size_t);
void Buf_Destroy(Buffer *, Boolean);
void Buf_ReplaceLastByte(Buffer *, Byte);
void Buf_Append(Buffer *, const char []);
#endif /* buf_h_a61a6812 */

View File

@ -239,7 +239,7 @@ CondGetArg(char **linePtr, char **argPtr, const char *func, Boolean parens)
cp2 = Var_Parse(cp, VAR_CMD, TRUE, &len, &doFree);
Buf_AddBytes(buf, strlen(cp2), (Byte *)cp2);
Buf_Append(buf, cp2);
if (doFree) {
free(cp2);
}
@ -540,12 +540,10 @@ CondToken(Boolean doEval)
if (!isspace((unsigned char)*condExpr) &&
strchr("!=><", *condExpr) == NULL) {
Buffer *buf;
char *cp;
buf = Buf_Init(0);
for (cp = lhs; *cp; cp++)
Buf_AddByte(buf, (Byte)*cp);
Buf_Append(buf, lhs);
if (doFree)
free(lhs);
@ -637,7 +635,7 @@ CondToken(Boolean doEval)
cp2 = Var_Parse(cp, VAR_CMD, doEval, &len, &freeIt);
if (cp2 != var_Error) {
Buf_AddBytes(buf, strlen(cp2), (Byte *)cp2);
Buf_Append(buf, cp2);
if (freeIt) {
free(cp2);
}

View File

@ -226,7 +226,7 @@ For_Eval(char *line)
}
if (forLevel != 0) {
Buf_AddBytes(forBuf, strlen(line), (Byte *)line);
Buf_Append(forBuf, line);
Buf_AddByte(forBuf, (Byte)'\n');
return (1);
}

View File

@ -524,5 +524,5 @@ Str_SYSVSubst(Buffer *buf, const char *pat, const char *src, int len)
Buf_AddBytes(buf, len, (const Byte *)src);
/* append the rest */
Buf_AddBytes(buf, strlen(pat), (const Byte *)pat);
Buf_Append(buf, pat);
}

View File

@ -286,17 +286,13 @@ VarFind(const char *name, GNode *ctxt, int flags)
char *env;
if ((env = getenv(name)) != NULL) {
int len;
v = emalloc(sizeof(Var));
v->name = estrdup(name);
len = strlen(env);
v->val = Buf_Init(len);
Buf_AddBytes(v->val, len, (Byte *)env);
v->val = Buf_Init(0);
v->flags = VAR_FROM_ENV;
Buf_Append(v->val, env);
return (v);
} else if ((checkEnvFirst || localCheckEnvFirst) &&
(flags & FIND_GLOBAL) && (ctxt != VAR_GLOBAL))
@ -335,18 +331,16 @@ static void
VarAdd(const char *name, const char *val, GNode *ctxt)
{
Var *v;
int len;
v = emalloc(sizeof(Var));
v->name = estrdup(name);
len = val ? strlen(val) : 0;
v->val = Buf_Init(len+1);
Buf_AddBytes(v->val, len, (const Byte *)val);
v->val = Buf_Init(0);
v->flags = 0;
if (val != NULL) {
Buf_Append(v->val, val);
}
Lst_AtFront(&ctxt->context, v);
DEBUGF(VAR, ("%s:%s = %s\n", ctxt->name, name, val));
}
@ -437,7 +431,7 @@ Var_Set(const char *name, const char *val, GNode *ctxt)
VarAdd(n, val, ctxt);
} else {
Buf_Clear(v->val);
Buf_AddBytes(v->val, strlen(val), (const Byte *)val);
Buf_Append(v->val, val);
DEBUGF(VAR, ("%s:%s = %s\n", ctxt->name, n, val));
}
@ -486,7 +480,7 @@ Var_Append(const char *name, const char *val, GNode *ctxt)
VarAdd(n, val, ctxt);
} else {
Buf_AddByte(v->val, (Byte)' ');
Buf_AddBytes(v->val, strlen(val), (const Byte *)val);
Buf_Append(v->val, val);
DEBUGF(VAR, ("%s:%s = %s\n", ctxt->name, n,
(char *)Buf_GetAll(v->val, (size_t *)NULL)));
@ -642,7 +636,7 @@ VarSortWords(char *str, int (*cmp)(const void *, const void *))
av = brk_string(str, &ac, FALSE);
qsort(av + 1, ac - 1, sizeof(char *), cmp);
for (i = 1; i < ac; i++) {
Buf_AddBytes(buf, strlen(av[i]), (Byte *)av[i]);
Buf_Append(buf, av[i]);
Buf_AddByte(buf, (Byte)((i < ac - 1) ? ' ' : '\0'));
}
str = (char *)Buf_GetAll(buf, (size_t *)NULL);
@ -726,7 +720,7 @@ VarGetPattern(GNode *ctxt, int err, char **tstr, int delim, int *flags,
* substitution and recurse.
*/
cp2 = Var_Parse(cp, ctxt, err, &len, &freeIt);
Buf_AddBytes(buf, strlen(cp2), (Byte *)cp2);
Buf_Append(buf, cp2);
if (freeIt)
free(cp2);
cp += len - 1;
@ -958,7 +952,7 @@ Var_Parse(char *str, GNode *ctxt, Boolean err, size_t *lengthPtr,
if (rval == var_Error) {
Fatal("Error expanding embedded variable.");
} else if (rval != NULL) {
Buf_AddBytes(buf, strlen(rval), (Byte *)rval);
Buf_Append(buf, rval);
if (rfree)
free(rval);
}
@ -1266,7 +1260,7 @@ Var_Parse(char *str, GNode *ctxt, Boolean err, size_t *lengthPtr,
Boolean freeIt;
cp2 = Var_Parse(cp, ctxt, err, &len, &freeIt);
Buf_AddBytes(buf, strlen(cp2), (Byte *)cp2);
Buf_Append(buf, cp2);
if (freeIt) {
free(cp2);
}
@ -1330,7 +1324,7 @@ Var_Parse(char *str, GNode *ctxt, Boolean err, size_t *lengthPtr,
Boolean freeIt;
cp2 = Var_Parse(cp, ctxt, err, &len, &freeIt);
Buf_AddBytes(buf, strlen(cp2), (Byte *)cp2);
Buf_Append(buf, cp2);
cp += len - 1;
if (freeIt) {
free(cp2);
@ -1862,7 +1856,7 @@ Var_Subst(const char *var, char *str, GNode *ctxt, Boolean undefErr)
* Copy all the characters from the variable value straight
* into the new string.
*/
Buf_AddBytes(buf, strlen(val), (Byte *)val);
Buf_Append(buf, val);
if (doFree) {
free(val);
}

View File

@ -83,7 +83,7 @@ VarHead(const char *word, Boolean addSpace, Buffer *buf, void *dummy __unused)
* If no directory part, give . (q.v. the POSIX standard)
*/
if (addSpace) {
Buf_AddBytes(buf, 2, (const Byte *)" .");
Buf_Append(buf, " .");
} else {
Buf_AddByte(buf, (Byte)'.');
}
@ -118,9 +118,9 @@ VarTail(const char *word, Boolean addSpace, Buffer *buf, void *dummy __unused)
slash = strrchr(word, '/');
if (slash != NULL) {
slash++;
Buf_AddBytes(buf, strlen(slash), (const Byte *)slash);
Buf_Append(buf, slash);
} else {
Buf_AddBytes(buf, strlen(word), (const Byte *)word);
Buf_Append(buf, word);
}
return (TRUE);
}
@ -150,7 +150,7 @@ VarSuffix(const char *word, Boolean addSpace, Buffer *buf, void *dummy __unused)
Buf_AddByte(buf, (Byte)' ');
}
dot++;
Buf_AddBytes(buf, strlen(dot), (const Byte *)dot);
Buf_Append(buf, dot);
addSpace = TRUE;
}
return (addSpace);
@ -184,7 +184,7 @@ VarRoot(const char *word, Boolean addSpace, Buffer *buf, void *dummy __unused)
if (dot != NULL) {
Buf_AddBytes(buf, dot - word, (const Byte *)word);
} else {
Buf_AddBytes(buf, strlen(word), (const Byte *)word);
Buf_Append(buf, word);
}
return (TRUE);
}
@ -215,7 +215,7 @@ VarMatch(const char *word, Boolean addSpace, Buffer *buf, void *pattern)
Buf_AddByte(buf, (Byte)' ');
}
addSpace = TRUE;
Buf_AddBytes(buf, strlen(word), word);
Buf_Append(buf, word);
}
return (addSpace);
}
@ -252,7 +252,7 @@ VarSYSVMatch(const char *word, Boolean addSpace, Buffer *buf, void *patp)
if ((ptr = Str_SYSVMatch(word, pat->lhs, &len)) != NULL)
Str_SYSVSubst(buf, pat->rhs, ptr, len);
else
Buf_AddBytes(buf, strlen(word), (const Byte *)word);
Buf_Append(buf, word);
return (addSpace);
}
@ -284,7 +284,7 @@ VarNoMatch(const char *word, Boolean addSpace, Buffer *buf, void *pattern)
Buf_AddByte(buf, (Byte)' ');
}
addSpace = TRUE;
Buf_AddBytes(buf, strlen(word), (const Byte *)word);
Buf_Append(buf, word);
}
return (addSpace);
}
@ -564,7 +564,7 @@ VarRESubstitute(const char *word, Boolean addSpace, Buffer *buf, void *patternp)
}
if (*wp) {
MAYBE_ADD_SPACE();
Buf_AddBytes(buf, strlen(wp), (const Byte *)wp);
Buf_Append(buf, wp);
}
break;
default:
@ -573,7 +573,7 @@ VarRESubstitute(const char *word, Boolean addSpace, Buffer *buf, void *patternp)
case REG_NOMATCH:
if (*wp) {
MAYBE_ADD_SPACE();
Buf_AddBytes(buf, strlen(wp), (const Byte *)wp);
Buf_Append(buf, wp);
}
break;
}