This is the mail archive of the
elfutils-devel@sourceware.org
mailing list for the elfutils project.
Re: [PATCH] Check for existence of asprintf and vasprintf
- From: Ulf Hermann <ulf dot hermann at qt dot io>
- To: elfutils-devel at sourceware dot org
- Date: Thu, 23 Feb 2017 11:46:04 +0100
- Subject: Re: [PATCH] Check for existence of asprintf and vasprintf
- Authentication-results: sourceware.org; auth=none
- Authentication-results: spf=none (sender IP is ) smtp.mailfrom=ulf dot hermann at qt dot io;
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qtcompany.onmicrosoft.com; s=selector1-qt-io; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=/cQWqLJqKlYFVREcAo08xAznPQjhbleAzPMBhIAEKb4=; b=TMz0HMUWowWMfn4skWu1yd8NUPBsa4KeNBc1TaKOmqThUkofXH4CTSSjTY5HGa+r5sSxIx5YeAdWkvfsvUk9D7HSxhx4BDkmLLwhMjDu3pvDWAVO5AZNgpDNEPeH0s+yzmIFKMcdny5Q83skjNwG4pa5hxY6c3Wmelf+ifhcaes=
- References: <4efb45a7-3035-67eb-d61e-62e795afd881@qt.io>
- Spamdiagnosticmetadata: NSPM
- Spamdiagnosticoutput: 1:99
Please compare the following code with asprintf.c/vasprintf.c/vasnprintf.c from gnulib. asprintf depends on vasprintf which in turn depends on vasnprintf there. vasnprintf is non-standard and not even available on glibc, while vsnprintf as used by my implementation is standardized by POSIX and widely available (even on windows). Granted, we have two calls to vsnprintf in my code vs one call to vasnprintf in the gnulib code. However, if that becomes an issue, I would rather go for some platform-specific optimization (windows also has a number of non-standard and possibly useful *printf functions) rather than importing such a monster.
> +int
> +asprintf(char **strp, const char *fmt, ...)
> +{
> + va_list ap;
> + va_start(ap, fmt);
> + int result = vasprintf(strp, fmt, ap);
> + va_end(ap);
> + return result;
> +}
> +
> +int
> +vasprintf(char **strp, const char *fmt, va_list ap)
> +{
> + va_list test;
> + va_copy(test, ap);
> + int length = vsnprintf(NULL, 0, fmt, test);
> + va_end(test);
> + *strp = malloc(length + 1);
> + if (*strp == NULL)
> + return -1;
> + return vsnprintf(*strp, length + 1, fmt, ap);
> +}