tags:

views:

1539

answers:

9

When calling execl(...) I get an errno=2. What does it mean ? How can I know the meaning of this errno ?

A: 

File or Directory not found.

Otávio Décio
+4  A: 

Here are the docs. that should tell you what it means and what to do with them. You should avoid using the numeric value and use the constants listed there as well, as the number may change between different systems.

dsm
A: 
Johannes Weiß
+11  A: 

Error code 2 means "File/Directory not found". In general, you could use the perror function to print a human readable string.

schnaader
+5  A: 

Instead of running perror on any error code you get, you can retrieve a complete listing of errno values on your system with the following one-liner:

cpp -dM /usr/include/errno.h | grep 'define E' | sort -n -k 3

Josh Kelley
A: 

When you use strace (on Linux) to run your binary it will output the returns from system calls and what the error number means. This may sometimes be useful to you.

sjbotha
+12  A: 

You can use strerror() to get a human-readable string for the error number. This is the same string printed by perror() but it's useful if you're formatting the error message for something other than standard error output.

For example:

#include <errno.h>
#include <string.h>

/* ... */

if(read(fd, buf, 1)==-1) {
    printf("Oh dear, something went wrong with read()! %s\n", strerror(errno));
}

Linux also supports the explicitly-threadsafe variant strerror_r().

Commodore Jaeger
I'd recommend avoiding strerror_r because: 1) strerror is guaranteed reentrant (thread-safe) in POSIX anyway 2) POSIX and GNU's version of strerror_r are different 3) an implementation would have to be brain damaged to have a strerror which wrote to its own static buffer.
Chris Young
+2  A: 

There's a few useful functions for dealing with errnos. (Just to make it clear, these are built-in to libc -- I'm just providing sample implementations because some people find reading code clearer than reading English.)

#include <string.h>
char *strerror(int errnum);

/* you can think of it as being implemented like this: */
static char strerror_buf[1024];
const char *sys_errlist[] = {
    [EPERM]  = "Operation not permitted",
    [ENOENT] = "No such file or directory",
    [ESRCH]  = "No such process",
    [EINTR]  = "Interrupted system call",
    [EIO]    = "I/O error",
    [ENXIO]  = "No such device or address",
    [E2BIG]  = "Argument list too long",
    /* etc. */
};
int sys_nerr = sizeof(sys_errlist) / sizeof(char *);
char *strerror(int errnum) {
    if (0 <= errnum && errnum < sys_nerr && sys_errlist[errnum])
        strcpy(strerror_buf, sys_errlist[errnum]);
    else
        sprintf(strerror_buf, "Unknown error %d", errnum);
    return strerror_buf;
}

strerror returns a string describing the error number you've passed to it. Caution, this is not thread- or interrupt-safe; it is free to rewrite the string and return the same pointer on the next invocation. Use strerror_r if you need to worry about that.

#include <stdio.h>
void perror(const char *s);

/* you can think of it as being implemented like this: */
void perror(const char *s) {
    fprintf(stderr, "%s: %s\n", s, strerror(errno));
}

perror prints out the message you give it, plus a string describing the current errno, to standard error.

ephemient
A: 

I use the following script:

#!/usr/bin/python

import errno
import os
import sys

toname = dict((str(getattr(errno, x)), x) 
              for x in dir(errno) 
              if x.startswith("E"))
tocode = dict((x, getattr(errno, x)) 
              for x in dir(errno) 
              if x.startswith("E"))

for arg in sys.argv[1:]:
    if arg in tocode:
        print arg, tocode[arg], os.strerror(tocode[arg])
    elif arg in toname:
        print toname[arg], arg, os.strerror(int(arg))
    else:
        print "Unknown:", arg
Lars Wirzenius