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.
27 static int nixio_file(lua_State
*L
) {
28 const char *filename
= luaL_checklstring(L
, 1, NULL
);
29 const char *mode
= luaL_optlstring(L
, 2, "r", NULL
);
31 FILE *file
= fopen(filename
, mode
);
33 return nixio__perror(L
);
36 FILE **udata
= lua_newuserdata(L
, sizeof(FILE**));
39 luaL_getmetatable(L
, NIXIO_FILE_META
);
40 lua_setmetatable(L
, -2);
45 static int nixio_pipe(lua_State
*L
) {
49 return nixio__perror(L
);
52 luaL_getmetatable(L
, NIXIO_FILE_META
);
53 udata
= lua_newuserdata(L
, sizeof(FILE**));
54 if (!(*udata
= fdopen(pipefd
[0], "r"))) {
55 return nixio__perror(L
);
58 lua_setmetatable(L
, -2);
61 udata
= lua_newuserdata(L
, sizeof(FILE**));
62 if (!(*udata
= fdopen(pipefd
[1], "w"))) {
63 return nixio__perror(L
);
66 lua_setmetatable(L
, -2);
71 static int nixio_file_write(lua_State
*L
) {
72 FILE *fp
= nixio__checkfile(L
);
74 const char *data
= luaL_checklstring(L
, 2, &len
);
75 written
= fwrite(data
, sizeof(char), len
, fp
);
77 return nixio__perror(L
);
79 lua_pushnumber(L
, written
);
85 /* Some code borrowed from Lua 5.1.4 liolib.c */
86 static int nixio_file_read(lua_State
*L
) {
87 FILE *f
= nixio__checkfile(L
);
88 size_t n
= (size_t)luaL_checkinteger(L
, 2);
89 luaL_argcheck(L
, 2, n
>= 0, "invalid length");
95 lua_pushliteral(L
, "");
100 size_t rlen
; /* how much to read */
101 size_t nr
; /* number of chars actually read */
103 luaL_buffinit(L
, &b
);
104 rlen
= LUAL_BUFFERSIZE
; /* try to read that much each time */
107 char *p
= luaL_prepbuffer(&b
);
108 if (rlen
> n
) rlen
= n
; /* cannot read more than asked */
109 nr
= fread(p
, sizeof(char), rlen
, f
);
110 luaL_addsize(&b
, nr
);
111 n
-= nr
; /* still have to read `n' chars */
112 } while (n
> 0 && nr
== rlen
); /* until end of count or eof */
113 luaL_pushresult(&b
); /* close buffer */
114 return (n
== 0 || lua_objlen(L
, -1) > 0);
117 static int nixio_file_seek(lua_State
*L
) {
118 FILE *f
= nixio__checkfile(L
);
119 off_t len
= (off_t
)luaL_checknumber(L
, 2);
121 const char *whstr
= luaL_optlstring(L
, 3, "set", NULL
);
122 if (!strcmp(whstr
, "set")) {
124 } else if (!strcmp(whstr
, "cur")) {
126 } else if (!strcmp(whstr
, "end")) {
129 return luaL_argerror(L
, 3, "supported values: set, cur, end");
131 return nixio__pstatus(L
, !fseeko(f
, len
, whence
));
134 static int nixio_file_tell(lua_State
*L
) {
135 FILE *f
= nixio__checkfile(L
);
136 off_t pos
= ftello(f
);
138 return nixio__perror(L
);
140 lua_pushnumber(L
, (lua_Number
)pos
);
145 static int nixio_file_flush(lua_State
*L
) {
146 FILE *f
= nixio__checkfile(L
);
147 return nixio__pstatus(L
, !fflush(f
));
150 static int nixio_file_close(lua_State
*L
) {
151 FILE **fpp
= (FILE**)luaL_checkudata(L
, 1, NIXIO_FILE_META
);
152 luaL_argcheck(L
, *fpp
, 1, "invalid file object");
153 int res
= fclose(*fpp
);
155 return nixio__pstatus(L
, !res
);
158 static int nixio_file__gc(lua_State
*L
) {
159 FILE **fpp
= (FILE**)luaL_checkudata(L
, 1, NIXIO_FILE_META
);
168 * string representation
170 static int nixio_file__tostring(lua_State
*L
) {
171 lua_pushfstring(L
, "nixio file %d", nixio__tofd(L
, 1));
176 static const luaL_reg M
[] = {
177 {"write", nixio_file_write
},
178 {"read", nixio_file_read
},
179 {"tell", nixio_file_tell
},
180 {"seek", nixio_file_seek
},
181 {"flush", nixio_file_flush
},
182 {"close", nixio_file_close
},
183 {"__gc", nixio_file__gc
},
184 {"__tostring", nixio_file__tostring
},
189 static const luaL_reg R
[] = {
190 {"open", nixio_file
},
191 {"pipe", nixio_pipe
},
195 void nixio_open_file(lua_State
*L
) {
196 luaL_register(L
, NULL
, R
);
198 luaL_newmetatable(L
, NIXIO_FILE_META
);
199 luaL_register(L
, NULL
, M
);
200 lua_pushvalue(L
, -1);
201 lua_setfield(L
, -2, "__index");