diff options
author | Oleg Safiullin <form@cvs.openbsd.org> | 2000-04-24 03:33:28 +0000 |
---|---|---|
committer | Oleg Safiullin <form@cvs.openbsd.org> | 2000-04-24 03:33:28 +0000 |
commit | 48b49e3dbecf303d0aa748ddd58e2b05c7062ce5 (patch) | |
tree | 49386f86a690e410bc8543edaf0a5ac8215254a3 | |
parent | 4652f5890765d50db31ba39442dd315754d49698 (diff) |
Add xstrdup() - like strdup but get fatal error if memory is exhausted.
Avoid duplicates in search path.
ok espie@
-rw-r--r-- | gnu/usr.bin/ld/etc.c | 17 | ||||
-rw-r--r-- | gnu/usr.bin/ld/ld.h | 3 | ||||
-rw-r--r-- | gnu/usr.bin/ld/shlib.c | 16 |
3 files changed, 32 insertions, 4 deletions
diff --git a/gnu/usr.bin/ld/etc.c b/gnu/usr.bin/ld/etc.c index d3716f03ece..1b6aec578c4 100644 --- a/gnu/usr.bin/ld/etc.c +++ b/gnu/usr.bin/ld/etc.c @@ -1,4 +1,4 @@ -/* * $OpenBSD: etc.c,v 1.2 1998/03/26 19:46:18 niklas Exp $*/ +/* * $OpenBSD: etc.c,v 1.3 2000/04/24 03:33:27 form Exp $*/ /* */ @@ -7,6 +7,21 @@ #include <string.h> /* + * Like strdup but get fatal error if memory is exhausted. + */ +char * +xstrdup(s) + char *s; +{ + char *result = strdup(s); + + if (!result) + errx(1, "virtual memory exhausted"); + + return result; +} + +/* * Like malloc but get fatal error if memory is exhausted. */ void * diff --git a/gnu/usr.bin/ld/ld.h b/gnu/usr.bin/ld/ld.h index d5e6efabd1d..9eb86a3f806 100644 --- a/gnu/usr.bin/ld/ld.h +++ b/gnu/usr.bin/ld/ld.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ld.h,v 1.4 1999/05/10 16:18:33 espie Exp $ */ +/* $OpenBSD: ld.h,v 1.5 2000/04/24 03:33:27 form Exp $ */ /*- * This code is derived from software copyrighted by the Free Software @@ -640,6 +640,7 @@ void prline_file_name __P((struct file_entry *, FILE *)); int do_warnings __P((FILE *)); /* In etc.c: */ +char *xstrdup __P((char *)); void *xmalloc __P((size_t)); void *xrealloc __P((void *, size_t)); char *concat __P((const char *, const char *, const char *)); diff --git a/gnu/usr.bin/ld/shlib.c b/gnu/usr.bin/ld/shlib.c index 01343faf557..9c564e631d7 100644 --- a/gnu/usr.bin/ld/shlib.c +++ b/gnu/usr.bin/ld/shlib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: shlib.c,v 1.8 2000/01/16 14:31:26 espie Exp $ */ +/* $OpenBSD: shlib.c,v 1.9 2000/04/24 03:33:27 form Exp $ */ /* $NetBSD: shlib.c,v 1.13 1998/04/04 01:00:29 fvdl Exp $ */ /* @@ -77,10 +77,22 @@ void add_search_dir(name) char *name; { + int i, len; + + len = strlen(name); + + while (len > 1 && name[len - 1] == '/') + --len; + + for (i = 0; i < n_search_dirs; i++) + if (strlen(search_dirs[i]) == len && + !strncmp(search_dirs[i], name, len)) + return; n_search_dirs++; search_dirs = (char **) xrealloc(search_dirs, n_search_dirs * sizeof search_dirs[0]); - search_dirs[n_search_dirs - 1] = strdup(name); + search_dirs[n_search_dirs - 1] = xmalloc(++len); + (void)strlcpy(search_dirs[n_search_dirs - 1], name, len); } void |