Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strerror description strings

Tags:

c

linux

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.

like image 694
Zombo Avatar asked Nov 23 '12 00:11

Zombo


2 Answers

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:

  • http://emmanuel.varoquaux.free.fr/xos/viewfile.php?file=xos/usr/lib/libc/sys_errlist.c
  • http://cristi.indefero.net/p/uClibc-cristi/source/tree/07439d8df31d7fefe82ff127a1690193a34e9060/libc/string/sys_errlist.c

  • http://fossies.org/dox/glibc-2.16.0/stdio-common_2errlist_8c_source.html

like image 128
0x90 Avatar answered Sep 21 '22 20:09

0x90


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.

like image 45
Fred Foo Avatar answered Sep 20 '22 20:09

Fred Foo