This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH 1/2] ldd: Make try_trace more robust and portable
- From: "Patrick 'P. J.' McDermott" <pjm at nac dot net>
- To: libc-alpha at sourceware dot org
- Date: Sat, 29 Jun 2013 20:45:28 -0400
- Subject: Re: [PATCH 1/2] ldd: Make try_trace more robust and portable
- References: <50AEF056 dot 20302 at nac dot net> <20121127002140 dot GA26454 at altlinux dot org>
- Reply-to: "P. J. McDermott" <pjm at nac dot net>, libc-alpha at sourceware dot org
On 2012-11-26 19:21, Dmitry V. Levin wrote:
> On Thu, Nov 22, 2012 at 10:41:10PM -0500, P. J. McDermott wrote:
> [...]
>> +try_trace() {
>> + output=$(eval $add_env '"$@"' && printf 'x') && printf '%s' "${output%x}"
>
> If the trace command would fail somewhere in the middle, its output will
> be lost.
Ah, yeah.
> What about this variant?
>
> try_trace() {
> local output rc
> output=$(eval $add_env '"$@"'; rc=$?; printf 'x'; exit $rc)
> rc=$?
> printf '%s' "${output%x}"
> return $rc
> }
Yes, that's much better. I'll repost the patch with this function.
I'm not sure I'd use local, though. While most Bourne-like shells
support it (as an extension to POSIX.1 XCU), there are still a couple
that don't (e.g. ksh).
Using the global "rc" parameter would have no ill effects in the calling
code, though using a different name might be safer for future changes.
Alternatively, the function could be defined as a subshell compound
list, i.e.:
try_trace() (
output=$(eval $add_env '"$@"'; rc=$?; printf 'x'; exit $rc)
rc=$?
printf '%s' "${output%x}"
return $rc
)
This has the effect of giving "local scope" to every parameter that is
set in the function. It's specified by POSIX.1, and I'm not aware of a
Bourne-like shell that doesn't support it (i.e. only supports non-
subshell function definitions).
Any preference between these alternatives to local (if any)?
--
Patrick "P. J." McDermott
http://www.pehjota.net/
http://www.pehjota.net/contact.html