More nixio fixes, initial httpclient
[project/luci.git] / libs / nixio / src / file.c
1 /*
2  * nixio - Linux I/O library for lua
3  *
4  *   Copyright (C) 2009 Steven Barth <steven@midlink.org>
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 #include "nixio.h"
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25
26
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);
30
31         FILE *file = fopen(filename, mode);
32         if (!file) {
33                 return nixio__perror(L);
34         }
35
36         FILE **udata = lua_newuserdata(L, sizeof(FILE**));
37         *udata = file;
38
39         luaL_getmetatable(L, NIXIO_FILE_META);
40         lua_setmetatable(L, -2);
41
42         return 1;
43 }
44
45 static int nixio_pipe(lua_State *L) {
46         int pipefd[2];
47         FILE **udata;
48         if (pipe(pipefd)) {
49                 return nixio__perror(L);
50         }
51
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);
56         }
57         lua_pushvalue(L, -2);
58         lua_setmetatable(L, -2);
59
60
61         udata = lua_newuserdata(L, sizeof(FILE**));
62         if (!(*udata = fdopen(pipefd[1], "w"))) {
63                 return nixio__perror(L);
64         }
65         lua_pushvalue(L, -3);
66         lua_setmetatable(L, -2);
67
68         return 2;
69 }
70
71 static int nixio_file_write(lua_State *L) {
72         FILE *fp = nixio__checkfile(L);
73         size_t len, written;
74         const char *data = luaL_checklstring(L, 2, &len);
75         written = fwrite(data, sizeof(char), len, fp);
76         if (written < 0) {
77                 return nixio__perror(L);
78         } else {
79                 lua_pushnumber(L, written);
80                 return 1;
81         }
82 }
83
84
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");
90
91         if (n == 0) {
92                 if (feof(f)) {
93                         return 0;
94                 } else {
95                         lua_pushliteral(L, "");
96                         return 1;
97                 }
98         }
99
100         size_t rlen;  /* how much to read */
101         size_t nr;  /* number of chars actually read */
102         luaL_Buffer b;
103         luaL_buffinit(L, &b);
104         rlen = LUAL_BUFFERSIZE;  /* try to read that much each time */
105
106         do {
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);
115 }
116
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);
120         int whence;
121         const char *whstr = luaL_optlstring(L, 3, "set", NULL);
122         if (!strcmp(whstr, "set")) {
123                 whence = SEEK_SET;
124         } else if (!strcmp(whstr, "cur")) {
125                 whence = SEEK_CUR;
126         } else if (!strcmp(whstr, "end")) {
127                 whence = SEEK_END;
128         } else {
129                 return luaL_argerror(L, 3, "supported values: set, cur, end");
130         }
131         return nixio__pstatus(L, !fseeko(f, len, whence));
132 }
133
134 static int nixio_file_tell(lua_State *L) {
135         FILE *f = nixio__checkfile(L);
136         off_t pos = ftello(f);
137         if (pos < 0) {
138                 return nixio__perror(L);
139         } else {
140                 lua_pushnumber(L, (lua_Number)pos);
141                 return 1;
142         }
143 }
144
145 static int nixio_file_flush(lua_State *L) {
146         FILE *f = nixio__checkfile(L);
147         return nixio__pstatus(L, !fflush(f));
148 }
149
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);
154         *fpp = NULL;
155         return nixio__pstatus(L, !res);
156 }
157
158 static int nixio_file__gc(lua_State *L) {
159         FILE **fpp = (FILE**)luaL_checkudata(L, 1, NIXIO_FILE_META);
160         if (*fpp) {
161                 fclose(*fpp);
162                 *fpp = NULL;
163         }
164         return 0;
165 }
166
167 /**
168  * string representation
169  */
170 static int nixio_file__tostring(lua_State *L) {
171         lua_pushfstring(L, "nixio file %d", nixio__tofd(L, 1));
172         return 1;
173 }
174
175 /* method table */
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},
185         {NULL,                  NULL}
186 };
187
188 /* module table */
189 static const luaL_reg R[] = {
190         {"open",                nixio_file},
191         {"pipe",                nixio_pipe},
192         {NULL,                  NULL}
193 };
194
195 void nixio_open_file(lua_State *L) {
196         luaL_register(L, NULL, R);
197
198         luaL_newmetatable(L, NIXIO_FILE_META);
199         luaL_register(L, NULL, M);
200         lua_pushvalue(L, -1);
201         lua_setfield(L, -2, "__index");
202         lua_pop(L, 1);
203 }