Accessing the C library on Linux

Using Dyalog under 'nixes

Accessing the C library on Linux

Postby Geoff|Dyalog on Fri Dec 24, 2010 12:41 pm

Writing general code to access libc can be fraught so I thought I would post this fragment which I wrote as part of a qa for v13.0:

      libc←{   ⍝ Linux: extract real name of libc that I am actually using        
pid←⊃⎕SH'echo $PPID'
libs←⎕SH'ldd /proc/',pid,'/exe'

What this does is to bypass the aspect of finding the relevant then extract the text and work out which to actually use. What it actually does is to find the process ID for the APL that I am running. Then look at /proc/PID/exe which is a symbolic link to the actual executable file. ldd then lists the libraries on which that depends. I can then find which actual library is bing used to fulfill the libc requirement.

/proc is an interesting concept. It is not really a file system. It is the kernel information being presented as if it were a file system.

I have used the new ⎕S (this was for a v13.0 qa) but a similar operation could be done with sed or awk or even ⍷. In fact ⍷ might produce cleaner and faster code.

This approach works on any Linux and finds the correct libc because it finds the one the APL itself is actually using.

The first two lines could easily be combined but I am trying to show the approach rather than produce the best possible code.

A similar technique could be used on AIX but would not really be useful because AIX libraries are not generally built to be dynamically loaded.
Posts: 43
Joined: Wed May 13, 2009 12:36 pm

Re: Accessing the C library on Linux

Postby AndyS|Dyalog on Fri Dec 24, 2010 1:54 pm

For those of a nervous disposition ..

Geoff's example of ⎕s appears at first sight to be fairly complex, but that's because he's digging out part of each line of the output of ldd which was not designed with being parsed in mind !

It would have been possible to do the same with multiple calls to ⎕s (or indeed, a combination of ⎕s/⎕r/⍳/∊/⍷). Having used regular expressions for many years (usually in grep or sed) then I can say that they're really useful, and can be extremely powerful - but there's a lot to be said for using multiple simple calls to ⎕s and ⎕r rather than one highly complex one. It's very easy to write regular expressions that make other people's eyes glaze over :-)

I've been playing with ⎕s and ⎕r while parsing HTML pages, and sorting out mis-encoded Unicode characters and I've found that the combination of these with ⍳/∊/⍷ and others is really powerful.
User avatar
Posts: 244
Joined: Tue May 12, 2009 6:06 pm

Return to UNIX and Linux

Who is online

Users browsing this forum: No registered users and 1 guest