lua-users home
lua-l archive

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


It was thus said that the Great David Given once stated:
> I've just had a report that WordGrinder is failing on a system with a
> Portugese locale. This turns out to be because it's got localised errno
> strings. So, my code, which does this:
> 
>     local fp, e = io.open("somefile")
>     if e and not e:find("No such file or directory") then
>         dosomething()
>     end
> 
> ...gets confused, because the actual string in e is "Arquivo ou
> diretório não encontrado".
> 
> I *could* do something really evil:
> 
>     local _, filenotfound = io.open("/this/file/is/guaranteed/missing")
>     if e != filenotfound then
>         dosomething()
>     end
> 
> ...but I can't really guarantee that the path doesn't exist.
> 
> What I'd really prefer is an errno (and a set of errno constants). But
> Lua doesn't expose those. Any suggestions?

  It's not documented (at least, I didn't see it mentioned in the
description for io.open() for Lua 5.1, 5.2 or 5.3), but Lua does return a
third parameter for io.open():

[spc]lucy:~>lua
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> print(io.open("zzzzz","r"))
nil	zzzzz: No such file or directory	2
> 
[spc]lucy:~>lua-52
Lua 5.2.3  Copyright (C) 1994-2013 Lua.org, PUC-Rio
> print(io.open("zzzzz","r"))
nil	zzzzz: No such file or directory	2
> 
[spc]lucy:~>lua-53
Lua 5.3.0  Copyright (C) 1994-2015 Lua.org, PUC-Rio
> print(io.open("zzzzz","r"))
nil	zzzzz: No such file or directory	2
> 

  If you want meaningful errno constants, I do have a module,
org.conman.errno [1] that creates a table full of constants (it's available
via LuaRocks).  Something like:

	errno = require "org.conman.errno"

	local fp,_,err = io.open("somefile")
	if not fp and err ~= errno.ENOENT then
	  print("somefile",errno[err]) -- errno[number] returns error string
	  dosomething()
	end

  -spc

[1]	https://github.com/spc476/lua-conmanorg/blob/errno-1.0.1/src/errno.c