Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

List standard Unix error codes

by jmcnamara (Monsignor)
on Jul 16, 2003 at 16:12 UTC ( #274896=snippet: print w/replies, xml ) Need Help??
Description: Print a list of Unix error codes and descriptions such as those defined in errno.h.

    perl -le 'print $!+0, "\t", $!++ for 0..127'


    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.


      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.


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?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://274896]
[shmem]: moin Corion, karlgoethebier. I stumbled over <<>> occasionally on the p5p mailing list
[Discipulus]: not noticed
[karlgoethebier]: sometimes rereading manages is helpful ;-) at least for me...
[Eily]: hello people \o/
[karlgoethebier]: manpages..
[Eily]: and non-people and NodeReaper

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2017-07-21 07:59 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (319 votes). Check out past polls.