Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

readlink returning strange stuff

by pileofrogs (Priest)
on Aug 05, 2010 at 00:03 UTC ( [id://852992]=perlquestion: print w/replies, xml ) Need Help??

pileofrogs has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, ye Monks of Perl.

I'm writing something that does a lot of readlink calls, and sometimes it returns some pretty strange stuff. I'm guessing the strangeness is actually coming from my operating system, but I wanted to share and see what you all thought.

Sometimes the returned string contains some non-printing characters followed by a bunch of pretty random looking stuff. Here's an example:

/proc/32433/exe -> /usr/bin/perl.pid]o_ff.jpg (deleted)

If I pipe that through less, I get to see markers showing me where the non-printing characters are:

/proc/32433/exe -> /usr/bin/perl^@.pid^@]^@^@o_ff.jpg (deleted)

An ordinary ls -l /proc/32433/exe says /proc/32433/exe -> /usr/bin/perl. I get the same by running the readlink command (/usr/bin/readlink).

So it looks like the correct answer followed by a bunch of other stuff.

If you're on linux, you can try this...

find /proc -type l -name 'exe' | \ perl -n -e 'chomp($_); print "$_ -> ".readlink($_)."\n";'
... and you might see what I'm talking about.

Anybody know what's going on here?

I'm on CentOS 5.5 running perl 5.8.8 from the perl-5.8.8-32.el5_5.1 rpm package.

Thanks!

--Pileofrogs

Replies are listed 'Best First'.
Re: readlink returning strange stuff
by ikegami (Patriarch) on Aug 05, 2010 at 03:12 UTC
    NUL (^@) marks the end of a C string, so it looks like the system is placing extra "hidden" information in the symlink. s/\0.*// should clear that up for you.
Re: readlink returning strange stuff
by aquarium (Curate) on Aug 05, 2010 at 01:42 UTC
    There are proper system calls and perl modules to deal with proc filesystem. Generally not a good idea to be playing in there with directory commands. Files in there have some magick attributes and appear and dissappear suddenly.
    The inner workings of /proc is different between systems.
    the hardest line to type correctly is: stty erase ^H

      Could you tell me a little about those system calls and modules?

Re: readlink returning strange stuff
by oko1 (Deacon) on Aug 05, 2010 at 00:41 UTC

    Must be specific to your machine, distro, or perhaps your $LANG setting (wild guess here); running the above on my Ubuntu 9.10/Perl 5.10.0/$LANG=en_US.UTF-8 setup produces not a single "weird character" in 489 entries, either via 'less' or just on the console.

    Is there any sort of a common factor to the entries that give you the "weird" output?


    --
    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://852992]
Approved by zwon
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-28 15:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found