File: llfslibext.c

package info (click to toggle)
texlive-bin 2018.20181218.49446-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 186,920 kB
  • sloc: ansic: 873,264; cpp: 311,278; perl: 82,918; sh: 23,243; makefile: 8,590; lex: 4,939; python: 4,462; pascal: 3,813; java: 3,569; yacc: 2,901; tcl: 2,379; exp: 2,031; xml: 844; ruby: 678; lisp: 398; sed: 331; asm: 140; csh: 46; awk: 30
file content (171 lines) | stat: -rw-r--r-- 4,060 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* llfslibext.c
   
   Copyright 2010-2011 Taco Hoekwater <taco@luatex.org>

   This file is part of LuaTeX.

   LuaTeX is free software; you can redistribute it and/or modify it under
   the terms of the GNU General Public License as published by the Free
   Software Foundation; either version 2 of the License, or (at your
   option) any later version.

   LuaTeX is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
   License for more details.

   You should have received a copy of the GNU General Public License along
   with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */

#include "ptexlib.h"
#include "lua/luatex-api.h"

#include <kpathsea/c-stat.h>
#include <kpathsea/c-dir.h>
#include <time.h>


#ifdef _WIN32
#  include <windows.h>
#else
#endif

#ifdef _WIN32

static int get_short_name(lua_State * L)
{
    long length = 0;
    TCHAR *buffer = NULL;
    const char *lpszPath = luaL_checkstring(L, 1);
    length = GetShortPathName(lpszPath, NULL, 0);
    if (length == 0) {
        lua_pushnil(L);
        lua_pushfstring(L, "operating system error: %d", (int) GetLastError());
        return 2;
    }
    buffer = (TCHAR *) xmalloc(length * sizeof(TCHAR));
    length = GetShortPathName(lpszPath, buffer, length);
    if (length == 0) {
        lua_pushnil(L);
        lua_pushfstring(L, "operating system error: %d", (int) GetLastError());
        return 2;
    }
    lua_pushlstring(L, (const char *) buffer, (size_t) length);
    return 1;
}

static int read_link(lua_State * L)
{
    lua_pushboolean(L, 0);
    lua_pushliteral(L, "readlink not supported on this platform");
    return 2;
}

#else

static int pusherror(lua_State * L, const char *info)
{
    lua_pushnil(L);
    if (info == NULL)
        lua_pushstring(L, strerror(errno));
    else
        lua_pushfstring(L, "%s: %s", info, strerror(errno));
    lua_pushinteger(L, errno);
    return 3;
}

static int Preadlink(lua_State * L)
{
/** readlink(path) */
    const char *path = luaL_checkstring(L, 1);
    char *b = NULL;
    int allocated = 128;
    int n;
    while (1) {
        b = malloc(allocated);
        if (!b)
            return pusherror(L, path);
        n = readlink(path, b, allocated);
        if (n == -1) {
            free(b);
            return pusherror(L, path);
        }
        if (n < allocated)
            break;
        /* Not enough room, try bigger */
        allocated *= 2;
        free(b);
    }
    lua_pushlstring(L, b, n);
    free(b);
    return 1;
}


static int read_link(lua_State * L)
{
    return Preadlink(L);
}

static int get_short_name(lua_State * L __attribute__ ((unused)))
{
    /* simply do nothing */
    return 1;
}
#endif


/*
** Get file information
*/
static int file_is_directory(lua_State * L)
{
    struct stat info;
    const char *file = luaL_checkstring(L, 1);

    if (stat(file, &info)) {
        lua_pushnil(L);
        lua_pushfstring(L, "cannot obtain information from file `%s'", file);
        return 2;
    }
    if (S_ISDIR(info.st_mode))
        lua_pushboolean(L, 1);
    else
        lua_pushboolean(L, 0);

    return 1;
}

static int file_is_file(lua_State * L)
{
    struct stat info;
    const char *file = luaL_checkstring(L, 1);

    if (stat(file, &info)) {
        lua_pushnil(L);
        lua_pushfstring(L, "cannot obtain information from file `%s'", file);
        return 2;
    }
    if (S_ISREG(info.st_mode))
        lua_pushboolean(L, 1);
    else
        lua_pushboolean(L, 0);

    return 1;
}


void open_lfslibext(lua_State * L)
{

    lua_getglobal(L, "lfs");
    lua_pushcfunction(L, file_is_directory);
    lua_setfield(L, -2, "isdir");
    lua_pushcfunction(L, file_is_file);
    lua_setfield(L, -2, "isfile");
    lua_pushcfunction(L, read_link);
    lua_setfield(L, -2, "readlink");
    lua_pushcfunction(L, get_short_name);
    lua_setfield(L, -2, "shortname");
    lua_pop(L, 1);              /* pop the table */
}