Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

List standard Unix error codes

by jmcnamara (Monsignor)
on Jul 16, 2003 at 16:12 UTC ( [id://274896]=CUFP: print w/replies, xml ) Need Help??

Print a list of Unix error codes and descriptions such as those defined in errno.h.
perl -le 'print $!+0, "\t", $!++ for 0..127' Prints: 0 1 Operation not permitted 2 No such file or directory 3 No such process 4 Interrupted system call 5 Input/output error ...

Note that it isn't guaranteed that any given program will use these return values. On other OSes, $^E may give more relevant output than $!

Replies are listed 'Best First'.
Re: List standard Unix error codes (less output, more code)
by tye (Sage) on Jul 16, 2003 at 18:01 UTC

    I don't like to see "Unknown error" dozens of times. So I prefer the longer:

    # Win32: perl -e "print grep !/unknown error/i, map $_.qq'\t'.($!=$_).$/, 0..12 +7" # Other: perl -e 'print grep !/unknown error/i, map $_."\t".($!=$_).$/, 0..127'
    Or you can get fancy like:
    # Win32: perl -MErrno -e "my %e= map { Errno->$_()=>$_ } keys(%!); print grep ! +/unknown error/i, map sprintf('%4d %-12s %s'.$/,$_,$e{$_},$!=$_), 0.. +127" # Other: perl -MErrno -e 'my %e= map { Errno->$_()=>$_ } keys(%!); print grep ! +/unknown error/i, map sprintf("%4d %-12s %s".$/,$_,$e{$_},$!=$_), 0.. +127'
    which produces output like:
    0 1 EPERM Operation not permitted 2 ENOENT No such file or directory 3 ESRCH No such process 4 EINTR Interrupted function call
    Enjoy. (:

    Update: Change the /unknown error/i bit to match your platform. I expected Perl to just use the standard strerror() but it appears that this code is split up much more than that and unknown error codes are handled rather differently on different platforms (the "Unknown error" is in Win32-specific source code for Perl).

                    - tye
      Heheh, solaris nicely forgoes the Unkown Error en lieu of Error xxx.

      --
      I'm not belgian but I play one on TV.

      If you want to look up a specific error code, you can't beat:
      perl -lpe '$_=$!=$_'

      Sufficiently advanced Perl is indistinguishable from garbage.
Re: List standard Unix error codes
by Abigail-II (Bishop) on Jul 16, 2003 at 20:53 UTC
    Here's a way to get the symbolic names as well:
    #!/usr/bin/perl use strict; use warnings; use Errno; foreach my $err (keys (%!)) { $! = eval "Errno::$err"; printf "%3d %20s %s\n", $! + 0, $err, $! } __END__

    Note that the names are somewhat portable (it improves if you stick to the POSIX names), but the numbers and descriptions are not portable. Not even from one Unix to the other.

    Abigail

      Now if it was only sorted somehow ;)
        I only had 2 minutes to write, test and post the program, because there was a television program I wanted to see. But feel free to modify the code, and post the results.

        Abigail

Re: List standard Unix error codes
by belg4mit (Prior) on Jul 16, 2003 at 20:12 UTC
    Here's a slightly different version that's tcsh friendly.
    perl -e 'printf "%i\t%s\n", (${!}=$_)x2 for 0..127'

    --
    I'm not belgian but I play one on TV.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-03-19 03:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found