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
);
84 /* Some code borrowed from Lua 5.1.4 liolib.c */
85 static int nixio_file_read(lua_State
*L
) {
86 FILE *f
= nixio__checkfile(L
);
87 size_t n
= (size_t)luaL_checkinteger(L
, 2);
88 luaL_argcheck(L
, 2, n
>= 0, "invalid length");
94 lua_pushliteral(L
, "");
99 size_t rlen
; /* how much to read */
100 size_t nr
; /* number of chars actually read */
102 luaL_buffinit(L
, &b
);
103 rlen
= LUAL_BUFFERSIZE
; /* try to read that much each time */
106 char *p
= luaL_prepbuffer(&b
);
107 if (rlen
> n
) rlen
= n
; /* cannot read more than asked */
108 nr
= fread(p
, sizeof(char), rlen
, f
);
109 luaL_addsize(&b
, nr
);
110 n
-= nr
; /* still have to read `n' chars */
111 } while (n
> 0 && nr
== rlen
); /* until end of count or eof */
112 luaL_pushresult(&b
); /* close buffer */
113 return (n
== 0 || lua_objlen(L
, -1) > 0);
116 static int nixio_file_seek(lua_State
*L
) {
117 FILE *f
= nixio__checkfile(L
);
118 off_t len
= (off_t
)luaL_checknumber(L
, 2);
120 const char *whstr
= luaL_optlstring(L
, 3, "set", NULL
);
121 if (!strcmp(whstr
, "set")) {
123 } else if (!strcmp(whstr
, "cur")) {
125 } else if (!strcmp(whstr
, "end")) {
128 return luaL_argerror(L
, 3, "supported values: set, cur, end");
130 return nixio__pstatus(L
, !fseeko(f
, len
, whence
));
133 static int nixio_file_tell(lua_State
*L
) {
134 FILE *f
= nixio__checkfile(L
);
135 off_t pos
= ftello(f
);
137 return nixio__perror(L
);
139 lua_pushnumber(L
, (lua_Number
)pos
);
144 static int nixio_file_flush(lua_State
*L
) {
145 FILE *f
= nixio__checkfile(L
);
146 return nixio__pstatus(L
, !fflush(f
));
149 static int nixio_file_close(lua_State
*L
) {
150 FILE **fpp
= (FILE**)luaL_checkudata(L
, 1, NIXIO_FILE_META
);
151 luaL_argcheck(L
, *fpp
, 1, "invalid file object");
152 int res
= fclose(*fpp
);
154 return nixio__pstatus(L
, !res
);
157 static int nixio_file__gc(lua_State
*L
) {
158 FILE **fpp
= (FILE**)luaL_checkudata(L
, 1, NIXIO_FILE_META
);
167 * string representation
169 static int nixio_file__tostring(lua_State
*L
) {
170 lua_pushfstring(L
, "nixio file %d", nixio__tofd(L
, 1));
175 static const luaL_reg M
[] = {
176 {"write", nixio_file_write
},
177 {"read", nixio_file_read
},
178 {"tell", nixio_file_tell
},
179 {"seek", nixio_file_seek
},
180 {"flush", nixio_file_flush
},
181 {"close", nixio_file_close
},
182 {"__gc", nixio_file__gc
},
183 {"__tostring", nixio_file__tostring
},
188 static const luaL_reg R
[] = {
189 {"open", nixio_file
},
190 {"pipe", nixio_pipe
},
194 void nixio_open_file(lua_State
*L
) {
195 luaL_register(L
, NULL
, R
);
197 luaL_newmetatable(L
, NIXIO_FILE_META
);
198 luaL_register(L
, NULL
, M
);
199 lua_pushvalue(L
, -1);
200 lua_setfield(L
, -2, "__index");