This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: PATCH: Re: jartool.c:539: undefined reference to `strdup'
- To: aoliva at redhat dot com (Alexandre Oliva)
- Subject: Re: PATCH: Re: jartool.c:539: undefined reference to `strdup'
- From: "John David Anglin" <dave at hiauly1 dot hia dot nrc dot ca>
- Date: Thu, 3 May 2001 14:26:09 -0400 (EDT)
- Cc: gcc-bugs at gcc dot gnu dot org, gcc-patches at gcc dot gnu dot org
> I don't think so. strdup() is sometimes defined as a macro, and
> sometimes it's declared to take a const char *. Better check for it
> and define it only if it's not present, or go for libiberty, which I
> think would be a better idea.
Here is a revised patch. Guaranteed to build and run under vax-dec-ultrix4.3.
As I see it, using strdup from libiberty is something to be avoided because
there is no declaration for it in libiberty.h. Gcc doesn't use it. It
uses xstrdup. However, it has a somewhat different behavior if malloc
fails (admittedly unlikely in this case). If you want to link against
libiberty, probably jartool.c should include libiberty.h and use xstrdup.
Dave
--
J. David Anglin dave.anglin@nrc.ca
National Research Council of Canada (613) 990-0752 (FAX: 952-6605)
2001-05-03 John David Anglin <dave@hiauly1.hia.nrc.ca>
* jartool.c (jt_strdup): New function.
(get_next_arg): Use jt_strdup instead of strdup.
--- jartool.c.orig Thu Dec 28 16:47:37 2000
+++ jartool.c Thu May 3 13:41:22 2001
@@ -223,6 +223,7 @@
int make_manifest(int, const char*);
static void init_args(char **, int);
static char *get_next_arg (void);
+static char *jt_strdup (char*);
/* global variables */
ub1 file_header[30];
@@ -536,7 +537,7 @@
if (pos)
{
s [pos] = '\0';
- return strdup (s);
+ return jt_strdup (s);
}
else
return NULL;
@@ -1825,4 +1826,15 @@
");
exit(1);
+}
+
+static char *
+jt_strdup(s)
+ char *s;
+{
+ char *result = (char*)malloc(strlen(s) + 1);
+ if (result == (char*)0)
+ return (char*)0;
+ strcpy(result, s);
+ return result;
}