diff options
| author | apb <apb@NetBSD.org> | 2007-10-13 16:16:41 +0000 |
|---|---|---|
| committer | apb <apb@NetBSD.org> | 2007-10-13 16:16:41 +0000 |
| commit | 67bb3243121aedf160df9336bead3ef558d3a1de (patch) | |
| tree | 3cbd5ad7327e8ffa2aa3f3fc821e8753e1fa9f14 /usr.bin/make | |
| parent | ed29af22c3fc8ea82ef9ca79b3bf617d4207b338 (diff) | |
* Convert all uses of strdup() to estrdup();
* Use estrndup() in a few cases where it simplifies the code;
* Provide compatibility definitions of strndup and estrndup;
Diffstat (limited to 'usr.bin/make')
| -rw-r--r-- | usr.bin/make/main.c | 22 | ||||
| -rw-r--r-- | usr.bin/make/nonints.h | 3 | ||||
| -rw-r--r-- | usr.bin/make/util.c | 33 | ||||
| -rw-r--r-- | usr.bin/make/var.c | 28 |
4 files changed, 62 insertions, 24 deletions
diff --git a/usr.bin/make/main.c b/usr.bin/make/main.c index 50169ba819d..69c2c08dee6 100644 --- a/usr.bin/make/main.c +++ b/usr.bin/make/main.c @@ -1,4 +1,4 @@ -/* $NetBSD: main.c,v 1.143 2007/10/05 15:27:45 sjg Exp $ */ +/* $NetBSD: main.c,v 1.144 2007/10/13 16:16:41 apb Exp $ */ /* * Copyright (c) 1988, 1989, 1990, 1993 @@ -69,7 +69,7 @@ */ #ifndef MAKE_NATIVE -static char rcsid[] = "$NetBSD: main.c,v 1.143 2007/10/05 15:27:45 sjg Exp $"; +static char rcsid[] = "$NetBSD: main.c,v 1.144 2007/10/13 16:16:41 apb Exp $"; #else #include <sys/cdefs.h> #ifndef lint @@ -81,7 +81,7 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993\n\ #if 0 static char sccsid[] = "@(#)main.c 8.3 (Berkeley) 3/19/94"; #else -__RCSID("$NetBSD: main.c,v 1.143 2007/10/05 15:27:45 sjg Exp $"); +__RCSID("$NetBSD: main.c,v 1.144 2007/10/13 16:16:41 apb Exp $"); #endif #endif /* not lint */ #endif @@ -939,7 +939,7 @@ main(int argc, char **argv) if (syspath == NULL || *syspath == '\0') syspath = defsyspath; else - syspath = strdup(syspath); + syspath = estrdup(syspath); for (start = syspath; *start != '\0'; start = cp) { for (cp = start; *cp != '\0' && *cp != ':'; cp++) @@ -1689,6 +1689,20 @@ estrdup(const char *str) } /* + * estrndup -- + * strndup, but die on error. + */ +char * +estrndup(const char *str, size_t len) +{ + char *p; + + if ((p = strndup(str, len)) == NULL) + enomem(); + return(p); +} + +/* * erealloc -- * realloc, but die on error. */ diff --git a/usr.bin/make/nonints.h b/usr.bin/make/nonints.h index e199e4af879..5fa5d86b90e 100644 --- a/usr.bin/make/nonints.h +++ b/usr.bin/make/nonints.h @@ -1,4 +1,4 @@ -/* $NetBSD: nonints.h,v 1.44 2007/10/05 15:27:45 sjg Exp $ */ +/* $NetBSD: nonints.h,v 1.45 2007/10/13 16:16:41 apb Exp $ */ /*- * Copyright (c) 1988, 1989, 1990, 1993 @@ -118,6 +118,7 @@ int PrintAddr(ClientData, ClientData); void Finish(int); #ifndef HAVE_EMALLOC char *estrdup(const char *); +char *estrndup(const char *, size_t); void *emalloc(size_t); void *erealloc(void *, size_t); void enomem(void); diff --git a/usr.bin/make/util.c b/usr.bin/make/util.c index 37c3dc81cce..bdd10f159d1 100644 --- a/usr.bin/make/util.c +++ b/usr.bin/make/util.c @@ -1,15 +1,15 @@ -/* $NetBSD: util.c,v 1.40 2007/01/17 00:21:44 hubertf Exp $ */ +/* $NetBSD: util.c,v 1.41 2007/10/13 16:16:41 apb Exp $ */ /* * Missing stuff from OS's */ #ifndef MAKE_NATIVE -static char rcsid[] = "$NetBSD: util.c,v 1.40 2007/01/17 00:21:44 hubertf Exp $"; +static char rcsid[] = "$NetBSD: util.c,v 1.41 2007/10/13 16:16:41 apb Exp $"; #else #include <sys/cdefs.h> #ifndef lint -__RCSID("$NetBSD: util.c,v 1.40 2007/01/17 00:21:44 hubertf Exp $"); +__RCSID("$NetBSD: util.c,v 1.41 2007/10/13 16:16:41 apb Exp $"); #endif #endif @@ -61,6 +61,33 @@ strdup(const char *str) } #endif +#if !defined(MAKE_NATIVE) && !defined(HAVE_STRNDUP) +#include <string.h> + +/* strndup + * + * Make a duplicate of a string, up to a maximum length. + * For systems which lack this function. + */ +char * +strndup(const char *str, size_t maxlen) +{ + size_t len; + char *p; + + if (str == NULL) + return NULL; + len = strlen(str); + if (len > maxlen) + len = maxlen; + p = emalloc(len + 1); + + memcpy(p, str, len); + p[len] = '\0'; + return p; +} +#endif + #if !defined(MAKE_NATIVE) && !defined(HAVE_SETENV) int setenv(const char *name, const char *value, int dum) diff --git a/usr.bin/make/var.c b/usr.bin/make/var.c index 616eff4a810..066eea97c8c 100644 --- a/usr.bin/make/var.c +++ b/usr.bin/make/var.c @@ -1,4 +1,4 @@ -/* $NetBSD: var.c,v 1.121 2007/10/13 14:32:18 apb Exp $ */ +/* $NetBSD: var.c,v 1.122 2007/10/13 16:16:41 apb Exp $ */ /* * Copyright (c) 1988, 1989, 1990, 1993 @@ -69,14 +69,14 @@ */ #ifndef MAKE_NATIVE -static char rcsid[] = "$NetBSD: var.c,v 1.121 2007/10/13 14:32:18 apb Exp $"; +static char rcsid[] = "$NetBSD: var.c,v 1.122 2007/10/13 16:16:41 apb Exp $"; #else #include <sys/cdefs.h> #ifndef lint #if 0 static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 3/19/94"; #else -__RCSID("$NetBSD: var.c,v 1.121 2007/10/13 14:32:18 apb Exp $"); +__RCSID("$NetBSD: var.c,v 1.122 2007/10/13 16:16:41 apb Exp $"); #endif #endif /* not lint */ #endif @@ -1643,7 +1643,7 @@ VarSelectWords(GNode *ctx __unused, Var_Parse_State *vpstate, /* fake what brk_string() would do if there were only one word */ ac = 1; av = emalloc((ac + 1) * sizeof(char *)); - as = strdup(str); + as = estrdup(str); av[0] = as; av[1] = NULL; } else { @@ -1736,7 +1736,7 @@ VarModify(GNode *ctx, Var_Parse_State *vpstate, /* fake what brk_string() would do if there were only one word */ ac = 1; av = emalloc((ac + 1) * sizeof(char *)); - as = strdup(str); + as = estrdup(str); av[0] = as; av[1] = NULL; } else { @@ -2259,11 +2259,11 @@ ApplyModifiers(char *nstr, const char *tstr, ++tstr; if (v->flags & VAR_JUNK) { /* - * We need to strdup() it incase + * We need to estrdup() it incase * VarGetPattern() recurses. */ sv_name = v->name; - v->name = strdup(v->name); + v->name = estrdup(v->name); } else if (ctxt != VAR_GLOBAL) { Var *gv = VarFind(v->name, ctxt, 0); if (gv == (Var *)NIL) @@ -2423,7 +2423,7 @@ ApplyModifiers(char *nstr, const char *tstr, { if ((v->flags & VAR_JUNK) != 0) v->flags |= VAR_KEEP; - newStr = strdup(v->name); + newStr = estrdup(v->name); cp = ++tstr; termc = *tstr; break; @@ -2438,12 +2438,12 @@ ApplyModifiers(char *nstr, const char *tstr, if (gn == NILGNODE || gn->type & OP_NOPATH) { newStr = NULL; } else if (gn->path) { - newStr = strdup(gn->path); + newStr = estrdup(gn->path); } else { newStr = Dir_FindFile(v->name, Suff_FindPath(gn)); } if (!newStr) { - newStr = strdup(v->name); + newStr = estrdup(v->name); } cp = ++tstr; termc = *tstr; @@ -3436,9 +3436,7 @@ Var_Parse(const char *str, GNode *ctxt, Boolean errnum, int *lengthPtr, *lengthPtr = tstr - start + 1; *WR(tstr) = endc; if (dynamic) { - char *pstr = emalloc(*lengthPtr + 1); - strncpy(pstr, start, *lengthPtr); - pstr[*lengthPtr] = '\0'; + char *pstr = estrndup(start, *lengthPtr); *freePtr = pstr; Buf_Destroy(buf, TRUE); return(pstr); @@ -3530,9 +3528,7 @@ Var_Parse(const char *str, GNode *ctxt, Boolean errnum, int *lengthPtr, *freePtr = NULL; } if (dynamic) { - nstr = emalloc(*lengthPtr + 1); - strncpy(nstr, start, *lengthPtr); - nstr[*lengthPtr] = '\0'; + nstr = estrndup(start, *lengthPtr); *freePtr = nstr; } else { nstr = var_Error; |
