Zombo
Zombo

Reputation: 1

strerror description strings

The C function strerror returns an error description string, as detailed here. Example string

No such file or directory

The question is where are these strings defined? I looked through my header files and did not see anything.

Upvotes: 12

Views: 6692

Answers (4)

0x90
0x90

Reputation: 40982

The header file that contains the error messages is named errmsg.h

00012 const char *const sys_errlist[] = {
00013         "Operation succeeded",        /* 0 */
00014         "Invalid argument",           /* EINVAL */
00015         "Bad memory address",         /* EFAULT */
00016         "String too long",            /* ENAMETOOLONG */
00017         "Out of memory",              /* ENOMEM */
00018         "Input/output error",         /* EIO */
00019         "No such file or directory",  /* ENOENT */
00020         "Not a directory",            /* ENOTDIR */
00021         "Is a directory",             /* EISDIR */
00022         "File or object exists",      /* EEXIST */
00023         "Cross-device link",          /* EXDEV */
00024         "Try again later",            /* EAGAIN */
00025         "Illegal seek",               /* ESPIPE */
00026         "Unimplemented feature",      /* EUNIMP */
00027         "Device not available",       /* ENXIO */
00028         "No such device",             /* ENODEV */
00029         "Device or resource busy",    /* EBUSY */
00030         "Invalid/inappropriate ioctl",/* EIOCTL (ENOTTY in Unix) */
00031         "Directory not empty",        /* ENOTEMPTY */
00032         "Result too large",           /* ERANGE */
00033         "No space left on device",    /* ENOSPC */
00034         "Too many open files",        /* EMFILE */
00035         "Too many open files in system",/* ENFILE */
00036         "No such system call",        /* ENOSYS */
00037         "File is not executable",     /* ENOEXEC */
00038         "Argument list too long",     /* E2BIG */
00039         "Bad file number",            /* EBADF */
00040 };

As you can see it depends on the libc implementation. but the general idea is the same: some array contains a mapping from a error number to a string max length of 1024 bytes.

other implementations:

Upvotes: 8

Fred Foo
Fred Foo

Reputation: 363487

They're defined somewhere in the C library, traditionally in a global array of char* called sys_errlist of length sys_nerr, at least on Unix systems.

Because legacy programs, written before strerror was standardized, may access this array directly, it's still available even on modern GNU/Linux and Mac OS X for backward compatibility (though you really shouldn't access it except through perror or strerror).

E.g, here's the Mac OS X 10.8.2 definition of sys_errlist.

Upvotes: 8

Jerry Coffin
Jerry Coffin

Reputation: 490048

At least in a typical library, they'll be in an object file that gets linked in -- typically strerror.o (or .obj, etc.). A little grepping through the source code to the library should turn them up, if you care enough to look.

Upvotes: 2

nos
nos

Reputation: 229058

They are usually likely defined and embedded in your C runtime library, e.g. libc on most unix like systems.

Upvotes: 2

Related Questions