lua-users home
lua-l archive

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


Hi David

On 23 February 2016 at 21:11, David Given <dg@cowlark.com> wrote:
> 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

This is fragile and is going to bite you sooner or later; you're right
to want something more robust.

>
> ...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?
>

`io.open` returns `errno` as its 3rd return value

    local fp, err, errno = io.open("somefile")
    if not fp then
       print(err, "ERRNO:", errno)
    end

You don't mention the version of Lua you're using, so here are the
relevant sources:

    Lua5.1: http://www.lua.org/source/5.1/liolib.c.html#pushresult
    Lua5.2 http://www.lua.org/source/5.2/lauxlib.c.html#luaL_fileresult
    Lua5.3 http://www.lua.org/source/5.3/lauxlib.c.html#luaL_fileresult
    LuaJIT https://github.com/LuaJIT/LuaJIT/blob/2e85af8836931f10aaaaae8c10f9b394219187a5/src/lib_aux.c#L31

Hope that helps

Best

Luke