de43ee487d561511161e75fddea6ed97361361cc
2 * nixio - Linux I/O library for lua
4 * Copyright (C) 2009 Steven Barth <steven@midlink.org>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
28 static int nixio_file(lua_State
*L
) {
29 const char *filename
= luaL_checklstring(L
, 1, NULL
);
30 const char *mode
= luaL_optlstring(L
, 2, "r", NULL
);
32 FILE *file
= fopen(filename
, mode
);
34 return nixio__perror(L
);
37 FILE **udata
= lua_newuserdata(L
, sizeof(FILE*));
39 return luaL_error(L
, "out of memory");
44 luaL_getmetatable(L
, NIXIO_FILE_META
);
45 lua_setmetatable(L
, -2);
50 static int nixio_pipe(lua_State
*L
) {
54 return nixio__perror(L
);
57 luaL_getmetatable(L
, NIXIO_FILE_META
);
58 udata
= lua_newuserdata(L
, sizeof(FILE*));
60 return luaL_error(L
, "out of memory");
63 if (!(*udata
= fdopen(pipefd
[0], "r"))) {
64 return nixio__perror(L
);
67 lua_setmetatable(L
, -2);
70 udata
= lua_newuserdata(L
, sizeof(FILE**));
71 if (!(*udata
= fdopen(pipefd
[1], "w"))) {
72 return nixio__perror(L
);
75 lua_setmetatable(L
, -2);
80 static int nixio_file_write(lua_State
*L
) {
81 int fd
= nixio__checkfd(L
, 1);
84 const char *data
= luaL_checklstring(L
, 2, &len
);
87 sent
= write(fd
, data
, len
);
88 } while(sent
== -1 && errno
== EINTR
);
90 lua_pushinteger(L
, sent
);
93 return nixio__perror(L
);
97 static int nixio_file_read(lua_State
*L
) {
98 int fd
= nixio__checkfd(L
, 1);
99 char buffer
[NIXIO_BUFFERSIZE
];
100 int req
= luaL_checkinteger(L
, 2);
103 /* We limit the readsize to NIXIO_BUFFERSIZE */
104 req
= (req
> NIXIO_BUFFERSIZE
) ? NIXIO_BUFFERSIZE
: req
;
107 readc
= read(fd
, buffer
, req
);
108 } while (readc
== -1 && errno
== EINTR
);
111 return nixio__perror(L
);
113 lua_pushlstring(L
, buffer
, readc
);
119 static int nixio_file_seek(lua_State
*L
) {
120 FILE *f
= nixio__checkfile(L
);
121 off_t len
= (off_t
)luaL_checknumber(L
, 2);
123 const char *whstr
= luaL_optlstring(L
, 3, "set", NULL
);
124 if (!strcmp(whstr
, "set")) {
126 } else if (!strcmp(whstr
, "cur")) {
128 } else if (!strcmp(whstr
, "end")) {
131 return luaL_argerror(L
, 3, "supported values: set, cur, end");
133 return nixio__pstatus(L
, !fseeko(f
, len
, whence
));
136 static int nixio_file_tell(lua_State
*L
) {
137 FILE *f
= nixio__checkfile(L
);
138 off_t pos
= ftello(f
);
140 return nixio__perror(L
);
142 lua_pushnumber(L
, (lua_Number
)pos
);
147 static int nixio_file_flush(lua_State
*L
) {
148 FILE *f
= nixio__checkfile(L
);
149 return nixio__pstatus(L
, !fflush(f
));
152 static int nixio_file_lock(lua_State
*L
) {
153 int fd
= fileno(nixio__checkfile(L
));
155 const int j
= lua_gettop(L
);
157 for (int i
=2; i
<=j
; i
++) {
158 const char *flag
= luaL_checkstring(L
, i
);
159 if (!strcmp(flag
, "sh")) {
161 } else if (!strcmp(flag
, "ex")) {
163 } else if (!strcmp(flag
, "un")) {
165 } else if (!strcmp(flag
, "nb")) {
168 return luaL_argerror(L
, i
, "supported values: sh, ex, un, nb");
172 return nixio__pstatus(L
, !flock(fd
, flags
));
175 static int nixio_file_close(lua_State
*L
) {
176 FILE **fpp
= (FILE**)luaL_checkudata(L
, 1, NIXIO_FILE_META
);
177 luaL_argcheck(L
, *fpp
, 1, "invalid file object");
178 int res
= fclose(*fpp
);
180 return nixio__pstatus(L
, !res
);
183 static int nixio_file__gc(lua_State
*L
) {
184 FILE **fpp
= (FILE**)luaL_checkudata(L
, 1, NIXIO_FILE_META
);
193 * string representation
195 static int nixio_file__tostring(lua_State
*L
) {
196 lua_pushfstring(L
, "nixio file %d", nixio__tofd(L
, 1));
201 static const luaL_reg M
[] = {
202 {"write", nixio_file_write
},
203 {"read", nixio_file_read
},
204 {"tell", nixio_file_tell
},
205 {"seek", nixio_file_seek
},
206 {"flush", nixio_file_flush
},
207 {"lock", nixio_file_lock
},
208 {"close", nixio_file_close
},
209 {"__gc", nixio_file__gc
},
210 {"__tostring", nixio_file__tostring
},
215 static const luaL_reg R
[] = {
216 {"open", nixio_file
},
217 {"pipe", nixio_pipe
},
221 void nixio_open_file(lua_State
*L
) {
222 luaL_register(L
, NULL
, R
);
224 luaL_newmetatable(L
, NIXIO_FILE_META
);
225 luaL_register(L
, NULL
, M
);
226 lua_pushvalue(L
, -1);
227 lua_setfield(L
, -2, "__index");
228 lua_setfield(L
, -2, "meta_file");