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 FILE *fp = nixio__checkfile(L);
83 const char *data = luaL_checklstring(L, 2, &len);
84 written = fwrite(data, sizeof(char), len, fp);
86 return nixio__perror(L);
88 lua_pushnumber(L, written);
94 /* Some code borrowed from Lua 5.1.4 liolib.c */
95 static int nixio_file_read(lua_State *L) {
96 FILE *f = nixio__checkfile(L);
97 size_t n = (size_t)luaL_checkinteger(L, 2);
98 luaL_argcheck(L, 2, n >= 0, "invalid length");
104 lua_pushliteral(L, "");
109 size_t rlen; /* how much to read */
110 size_t nr; /* number of chars actually read */
112 luaL_buffinit(L, &b);
113 rlen = LUAL_BUFFERSIZE; /* try to read that much each time */
116 char *p = luaL_prepbuffer(&b);
117 if (rlen > n) rlen = n; /* cannot read more than asked */
118 nr = fread(p, sizeof(char), rlen, f);
119 luaL_addsize(&b, nr);
120 n -= nr; /* still have to read `n' chars */
121 } while (n > 0 && nr == rlen); /* until end of count or eof */
122 luaL_pushresult(&b); /* close buffer */
123 return (n == 0 || lua_objlen(L, -1) > 0);
126 static int nixio_file_seek(lua_State *L) {
127 FILE *f = nixio__checkfile(L);
128 off_t len = (off_t)luaL_checknumber(L, 2);
130 const char *whstr = luaL_optlstring(L, 3, "set", NULL);
131 if (!strcmp(whstr, "set")) {
133 } else if (!strcmp(whstr, "cur")) {
135 } else if (!strcmp(whstr, "end")) {
138 return luaL_argerror(L, 3, "supported values: set, cur, end");
140 return nixio__pstatus(L, !fseeko(f, len, whence));
143 static int nixio_file_tell(lua_State *L) {
144 FILE *f = nixio__checkfile(L);
145 off_t pos = ftello(f);
147 return nixio__perror(L);
149 lua_pushnumber(L, (lua_Number)pos);
154 static int nixio_file_flush(lua_State *L) {
155 FILE *f = nixio__checkfile(L);
156 return nixio__pstatus(L, !fflush(f));
159 static int nixio_file_lock(lua_State *L) {
160 int fd = fileno(nixio__checkfile(L));
162 const int j = lua_gettop(L);
164 for (int i=2; i<=j; i++) {
165 const char *flag = luaL_checkstring(L, i);
166 if (!strcmp(flag, "sh")) {
168 } else if (!strcmp(flag, "ex")) {
170 } else if (!strcmp(flag, "un")) {
172 } else if (!strcmp(flag, "nb")) {
175 return luaL_argerror(L, i, "supported values: sh, ex, un, nb");
179 return nixio__pstatus(L, flock(fd, flags));
182 static int nixio_file_close(lua_State *L) {
183 FILE **fpp = (FILE**)luaL_checkudata(L, 1, NIXIO_FILE_META);
184 luaL_argcheck(L, *fpp, 1, "invalid file object");
185 int res = fclose(*fpp);
187 return nixio__pstatus(L, !res);
190 static int nixio_file__gc(lua_State *L) {
191 FILE **fpp = (FILE**)luaL_checkudata(L, 1, NIXIO_FILE_META);
200 * string representation
202 static int nixio_file__tostring(lua_State *L) {
203 lua_pushfstring(L, "nixio file %d", nixio__tofd(L, 1));
208 static const luaL_reg M[] = {
209 {"write", nixio_file_write},
210 {"read", nixio_file_read},
211 {"tell", nixio_file_tell},
212 {"seek", nixio_file_seek},
213 {"flush", nixio_file_flush},
214 {"lock", nixio_file_lock},
215 {"close", nixio_file_close},
216 {"__gc", nixio_file__gc},
217 {"__tostring", nixio_file__tostring},
222 static const luaL_reg R[] = {
223 {"open", nixio_file},
224 {"pipe", nixio_pipe},
228 void nixio_open_file(lua_State *L) {
229 luaL_register(L, NULL, R);
231 luaL_newmetatable(L, NIXIO_FILE_META);
232 luaL_register(L, NULL, M);
233 lua_pushvalue(L, -1);
234 lua_setfield(L, -2, "__index");