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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
/*
* nixio - Linux I/O library for lua
*
* Copyright (C) 2009 Steven Barth <steven@midlink.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "nixio.h"
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/file.h>
static int nixio_file(lua_State *L) {
const char *filename = luaL_checklstring(L, 1, NULL);
const char *mode = luaL_optlstring(L, 2, "r", NULL);
FILE *file = fopen(filename, mode);
if (!file) {
return nixio__perror(L);
}
FILE **udata = lua_newuserdata(L, sizeof(FILE*));
if (!udata) {
return luaL_error(L, "out of memory");
}
*udata = file;
luaL_getmetatable(L, NIXIO_FILE_META);
lua_setmetatable(L, -2);
return 1;
}
static int nixio_pipe(lua_State *L) {
int pipefd[2];
FILE **udata;
if (pipe(pipefd)) {
return nixio__perror(L);
}
luaL_getmetatable(L, NIXIO_FILE_META);
udata = lua_newuserdata(L, sizeof(FILE*));
if (!udata) {
return luaL_error(L, "out of memory");
}
if (!(*udata = fdopen(pipefd[0], "r"))) {
return nixio__perror(L);
}
lua_pushvalue(L, -2);
lua_setmetatable(L, -2);
udata = lua_newuserdata(L, sizeof(FILE**));
if (!(*udata = fdopen(pipefd[1], "w"))) {
return nixio__perror(L);
}
lua_pushvalue(L, -3);
lua_setmetatable(L, -2);
return 2;
}
static int nixio_file_write(lua_State *L) {
int fd = nixio__checkfd(L, 1);
size_t len;
ssize_t sent;
const char *data = luaL_checklstring(L, 2, &len);
do {
sent = write(fd, data, len);
} while(sent == -1 && errno == EINTR);
if (sent >= 0) {
lua_pushinteger(L, sent);
return 1;
} else {
return nixio__perror(L);
}
}
static int nixio_file_read(lua_State *L) {
int fd = nixio__checkfd(L, 1);
char buffer[NIXIO_BUFFERSIZE];
int req = luaL_checkinteger(L, 2);
int readc;
/* We limit the readsize to NIXIO_BUFFERSIZE */
req = (req > NIXIO_BUFFERSIZE) ? NIXIO_BUFFERSIZE : req;
do {
readc = read(fd, buffer, req);
} while (readc == -1 && errno == EINTR);
if (readc < 0) {
return nixio__perror(L);
} else {
lua_pushlstring(L, buffer, readc);
return 1;
}
}
static int nixio_file_seek(lua_State *L) {
FILE *f = nixio__checkfile(L);
off_t len = (off_t)luaL_checknumber(L, 2);
int whence;
const char *whstr = luaL_optlstring(L, 3, "set", NULL);
if (!strcmp(whstr, "set")) {
whence = SEEK_SET;
} else if (!strcmp(whstr, "cur")) {
whence = SEEK_CUR;
} else if (!strcmp(whstr, "end")) {
whence = SEEK_END;
} else {
return luaL_argerror(L, 3, "supported values: set, cur, end");
}
return nixio__pstatus(L, !fseeko(f, len, whence));
}
static int nixio_file_tell(lua_State *L) {
FILE *f = nixio__checkfile(L);
off_t pos = ftello(f);
if (pos < 0) {
return nixio__perror(L);
} else {
lua_pushnumber(L, (lua_Number)pos);
return 1;
}
}
static int nixio_file_flush(lua_State *L) {
FILE *f = nixio__checkfile(L);
return nixio__pstatus(L, !fflush(f));
}
static int nixio_file_lock(lua_State *L) {
int fd = fileno(nixio__checkfile(L));
const int j = lua_gettop(L);
int flags = 0;
for (int i=2; i<=j; i++) {
const char *flag = luaL_checkstring(L, i);
if (!strcmp(flag, "sh")) {
flags |= LOCK_SH;
} else if (!strcmp(flag, "ex")) {
flags |= LOCK_EX;
} else if (!strcmp(flag, "un")) {
flags |= LOCK_UN;
} else if (!strcmp(flag, "nb")) {
flags |= LOCK_NB;
} else {
return luaL_argerror(L, i, "supported values: sh, ex, un, nb");
}
}
return nixio__pstatus(L, !flock(fd, flags));
}
static int nixio_file_close(lua_State *L) {
FILE **fpp = (FILE**)luaL_checkudata(L, 1, NIXIO_FILE_META);
luaL_argcheck(L, *fpp, 1, "invalid file object");
int res = fclose(*fpp);
*fpp = NULL;
return nixio__pstatus(L, !res);
}
static int nixio_file__gc(lua_State *L) {
FILE **fpp = (FILE**)luaL_checkudata(L, 1, NIXIO_FILE_META);
if (*fpp) {
fclose(*fpp);
*fpp = NULL;
}
return 0;
}
/**
* string representation
*/
static int nixio_file__tostring(lua_State *L) {
lua_pushfstring(L, "nixio file %d", nixio__tofd(L, 1));
return 1;
}
/* method table */
static const luaL_reg M[] = {
{"write", nixio_file_write},
{"read", nixio_file_read},
{"tell", nixio_file_tell},
{"seek", nixio_file_seek},
{"flush", nixio_file_flush},
{"lock", nixio_file_lock},
{"close", nixio_file_close},
{"__gc", nixio_file__gc},
{"__tostring", nixio_file__tostring},
{NULL, NULL}
};
/* module table */
static const luaL_reg R[] = {
{"open", nixio_file},
{"pipe", nixio_pipe},
{NULL, NULL}
};
void nixio_open_file(lua_State *L) {
luaL_register(L, NULL, R);
luaL_newmetatable(L, NIXIO_FILE_META);
luaL_register(L, NULL, M);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
lua_setfield(L, -2, "meta_file");
}
|