lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Wed, Nov 06, 2013 at 11:58:05AM -0500, Sean Conner wrote:
> It was thus said that the Great Patrick Donnelly once stated:
<snip>
> > [It would be nice if the io/os library provided fields in the io/os
> > table for each of these (ANSI only) errors. Of course, if you're doing
> > something complicated you might as well use a POSIX-y library.]
> 
>   C89 (which is what Lua is written to) only defines the following errors:
> 
> 	EDOM
> 	ERANGE
> 
>   C99 adds the following:
> 
> 	EILSEQ
> 
>   ENOENT is POSIX.  
> 
>   -spc (Obligatory plug: https://github.com/spc476/lua-conmanorg/blob/master/src/errno.c)

Almost any syscall can return an errno not defined by POSIX.

Fortunately it's trivial to autogenerate a file which includes _all_ the
errnos on the system. The following script should work on Linux, OS X,
FreeBSD, NetBSD, OpenBSD, and Solaris, including GCC, clang, and SunPro
compilers.

 #!/bin/sh

 filter() {
  awk '$1~/^#define/ && $2~/^E/{print $2 }'
 }

 if [ "$(uname -s)" = "SunOS" ]; then
  TMPFILE="${TMPDIR:-/tmp}/$0.$(od -An -N8 -tx1 < /dev/urandom | tr -d ' 	').c"
  printf "#include <errno.h>\n" >| ${TMPFILE}
  cc -xM ${TMPFILE} | awk '/\.h$/{ print $2 }' | sort -u | xargs cat | filter
  rm ${TMPFILE}
 else
  printf "#include <errno.h>\n" | cc -dM -E - | filter
 fi
 
You can union these with the POSIX set of errnos, so that you never do worse
than what POSIX specifies but in practice get the entire system-specific set
of macros.