This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH 1/2] ldd: Make try_trace more robust and portable


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]