cgi-bin/tek/os/posix.c
changeset 251 2de5931b723d
parent 250 5107767c6c1e
child 252 8b4bf1c5d718
     1.1 --- a/cgi-bin/tek/os/posix.c	Fri Oct 24 01:19:42 2008 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,453 +0,0 @@
     1.4 -
     1.5 -/*
     1.6 -**	tek.os.posix - tek POSIX library
     1.7 -**	Written by Timm S. Mueller <tmueller at neoscientists.org>
     1.8 -**	See copyright notice in COPYRIGHT
     1.9 -*/
    1.10 -
    1.11 -#include <stdlib.h>
    1.12 -#include <string.h>
    1.13 -#include <sys/types.h>
    1.14 -#include <sys/stat.h>
    1.15 -#include <unistd.h>
    1.16 -#include <dirent.h>
    1.17 -#include <errno.h>
    1.18 -#include <math.h>
    1.19 -#include <time.h>
    1.20 -#include <lua.h>
    1.21 -#include <lualib.h>
    1.22 -#include <lauxlib.h>
    1.23 -
    1.24 -
    1.25 -#define DIRCLASSNAME "dir*"
    1.26 -
    1.27 -
    1.28 -static void
    1.29 -setfield(lua_State *L, const char *attr, const char *key, lua_Integer n)
    1.30 -{
    1.31 -	if (attr == NULL || strcmp(attr, key) == 0)
    1.32 -		lua_pushinteger(L, n);
    1.33 -	if (attr == NULL)
    1.34 -		lua_setfield(L, -2, key);
    1.35 -}
    1.36 -
    1.37 -
    1.38 -static int
    1.39 -setstat(lua_State *L, int result, struct stat *s, const char *attr)
    1.40 -{
    1.41 -	if (result != 0)
    1.42 -	{
    1.43 -		lua_pushnil(L);
    1.44 -		lua_pushstring(L, strerror(errno));
    1.45 -		return 2;
    1.46 -	}
    1.47 -
    1.48 -	if (attr == NULL)
    1.49 -		lua_newtable(L);
    1.50 -
    1.51 -	setfield(L, attr, "dev", s->st_dev);
    1.52 -	setfield(L, attr, "ino", s->st_ino);
    1.53 -
    1.54 -	if (attr == NULL || strcmp(attr, "mode") == 0)
    1.55 -	{
    1.56 -		if (S_ISREG(s->st_mode))
    1.57 -			lua_pushstring(L, "file");
    1.58 -		else if (S_ISDIR(s->st_mode))
    1.59 -			lua_pushstring(L, "directory");
    1.60 -		else if (S_ISLNK(s->st_mode))
    1.61 -			lua_pushstring(L, "link");
    1.62 -		else if (S_ISSOCK(s->st_mode))
    1.63 -			lua_pushstring(L, "socket");
    1.64 -		else if (S_ISFIFO(s->st_mode))
    1.65 -			lua_pushstring(L, "named pipe");
    1.66 -		else if (S_ISCHR(s->st_mode))
    1.67 -			lua_pushstring(L, "char device");
    1.68 -		else if (S_ISBLK(s->st_mode))
    1.69 -			lua_pushstring(L, "block device");
    1.70 -		else
    1.71 -			lua_pushstring(L, "other");
    1.72 -		if (attr == NULL)
    1.73 -			lua_setfield(L, -2, "mode");
    1.74 -	}
    1.75 -
    1.76 -	setfield(L, attr, "nlink", s->st_nlink);
    1.77 -	setfield(L, attr, "uid", s->st_uid);
    1.78 -	setfield(L, attr, "gid", s->st_gid);
    1.79 -	setfield(L, attr, "rdev", s->st_rdev);
    1.80 -	setfield(L, attr, "access", s->st_atime);
    1.81 -	setfield(L, attr, "modifications", s->st_mtime);
    1.82 -	setfield(L, attr, "change", s->st_ctime);
    1.83 -	setfield(L, attr, "size", s->st_size);
    1.84 -	setfield(L, attr, "blocks", s->st_blocks);
    1.85 -	setfield(L, attr, "blksize", s->st_blksize);
    1.86 -
    1.87 -	return 1;
    1.88 -}
    1.89 -
    1.90 -
    1.91 -static int
    1.92 -posix_stat(lua_State *L)
    1.93 -{
    1.94 -	const char *path = luaL_checkstring(L, 1);
    1.95 -	const char *attr = luaL_optstring(L, 2, NULL);
    1.96 -	struct stat s;
    1.97 -	return setstat(L, stat(path, &s), &s, attr);
    1.98 -}
    1.99 -
   1.100 -
   1.101 -static int
   1.102 -posix_lstat(lua_State *L)
   1.103 -{
   1.104 -	const char *path = luaL_checkstring(L, 1);
   1.105 -	const char *attr = luaL_optstring(L, 2, NULL);
   1.106 -	struct stat s;
   1.107 -	return setstat(L, lstat(path, &s), &s, attr);
   1.108 -}
   1.109 -
   1.110 -
   1.111 -static int
   1.112 -posix_opendir(lua_State *L)
   1.113 -{
   1.114 -	const char *path = luaL_checkstring(L, 1);
   1.115 -	DIR **pdir = lua_newuserdata(L, sizeof(void *));
   1.116 -	*pdir = NULL;
   1.117 -	lua_pushvalue(L, lua_upvalueindex(2)); /* class metatable */
   1.118 -	/* attach metatable to the userdata object */
   1.119 -	lua_setmetatable(L, -2); /* s: udata */
   1.120 -	/* create class instance */
   1.121 -	*pdir = opendir(path);
   1.122 -	if (*pdir == NULL)
   1.123 -	{
   1.124 -		lua_pushnil(L);
   1.125 -		lua_pushstring(L, strerror(errno));
   1.126 -		return 2;
   1.127 -	}
   1.128 -	return 1;
   1.129 -}
   1.130 -
   1.131 -
   1.132 -static DIR **
   1.133 -getinstptr(lua_State *L, int narg, const char *classname)
   1.134 -{
   1.135 -	DIR **pinst = luaL_checkudata(L, narg, classname);
   1.136 -	if (*pinst) return pinst;
   1.137 -	luaL_argerror(L, narg, "Closed handle");
   1.138 -	return NULL;
   1.139 -}
   1.140 -
   1.141 -
   1.142 -static int
   1.143 -posix_closedir(lua_State *L)
   1.144 -{
   1.145 -	DIR **pdir = getinstptr(L, 1, DIRCLASSNAME);
   1.146 -	closedir(*pdir);
   1.147 -	*pdir = NULL;
   1.148 -	return 0;
   1.149 -}
   1.150 -
   1.151 -
   1.152 -static int
   1.153 -posix_readdir(lua_State *L)
   1.154 -{
   1.155 -	DIR *dir = *getinstptr(L, 1, DIRCLASSNAME);
   1.156 -	struct dirent *de = readdir(dir);
   1.157 -	if (de)
   1.158 -		lua_pushstring(L, de->d_name);
   1.159 -	else
   1.160 -		lua_pushnil(L);
   1.161 -	return 1;
   1.162 -}
   1.163 -
   1.164 -
   1.165 -static int
   1.166 -posix_readlink(lua_State *L)
   1.167 -{
   1.168 -	const char *path = luaL_checkstring(L, 1);
   1.169 -	char buf[PATH_MAX + 1];
   1.170 -	ssize_t len = readlink(path, buf, sizeof(buf) - 1);
   1.171 -	if (len < 0)
   1.172 -	{
   1.173 -		lua_pushnil(L);
   1.174 -		lua_pushstring(L, strerror(errno));
   1.175 -		return 2;
   1.176 -	}
   1.177 -	buf[len] = 0;
   1.178 -	lua_pushstring(L, buf);
   1.179 -	return 1;
   1.180 -}
   1.181 -
   1.182 -
   1.183 -static int
   1.184 -posix_mkdir(lua_State *L)
   1.185 -{
   1.186 -	const char *path = luaL_checkstring(L, 1);
   1.187 -	if (mkdir(path, 0775) == 0)
   1.188 -	{
   1.189 -		lua_pushboolean(L, 1);
   1.190 -		return 1;
   1.191 -	}
   1.192 -	lua_pushnil(L);
   1.193 -	lua_pushstring(L, strerror(errno));
   1.194 -	return 2;
   1.195 -}
   1.196 -
   1.197 -
   1.198 -static int
   1.199 -posix_symlink(lua_State *L)
   1.200 -{
   1.201 -	const char *oldpath = luaL_checkstring(L, 1);
   1.202 -	const char *newpath = luaL_checkstring(L, 2);
   1.203 -	if (symlink(oldpath, newpath) == 0)
   1.204 -	{
   1.205 -		lua_pushboolean(L, 1);
   1.206 -		return 1;
   1.207 -	}
   1.208 -	lua_pushnil(L);
   1.209 -	lua_pushstring(L, strerror(errno));
   1.210 -	return 2;
   1.211 -}
   1.212 -
   1.213 -
   1.214 -static int
   1.215 -resolvepath(const char *src, char *dest)
   1.216 -{
   1.217 -	int len = strlen(src);
   1.218 -	const char *sp = src + len;
   1.219 -	char *dp = dest;
   1.220 -	int dc = 0, slc = 0, eac = 0, wc = 0;
   1.221 -	int i, c;
   1.222 -
   1.223 -	while (len--)
   1.224 -	{
   1.225 -		c = *(--sp);
   1.226 -		switch (c)
   1.227 -		{
   1.228 -			case '/':
   1.229 -				if (dc == 2)
   1.230 -					eac++;
   1.231 -				dc = 0;
   1.232 -				slc = 1;
   1.233 -				wc = 0;
   1.234 -				break;
   1.235 -
   1.236 -			case '.':
   1.237 -				if (slc)
   1.238 -				{
   1.239 -					dc++;
   1.240 -					break;
   1.241 -				}
   1.242 -				/* fallthru: */
   1.243 -
   1.244 -			default:
   1.245 -				if (wc)
   1.246 -					break;
   1.247 -
   1.248 -				if (slc)
   1.249 -				{
   1.250 -					slc = 0;
   1.251 -
   1.252 -					if (eac > 0)
   1.253 -					{
   1.254 -						/* resolve one eatcount */
   1.255 -						eac--;
   1.256 -						/* now wait for next path part */
   1.257 -						wc = 1;
   1.258 -						break;
   1.259 -					}
   1.260 -
   1.261 -					*dp++ = '/';
   1.262 -				}
   1.263 -
   1.264 -				while (dc == 2 || dc == 1)
   1.265 -				{
   1.266 -					*dp++ = '.';
   1.267 -					dc--;
   1.268 -				}
   1.269 -				dc = 0;
   1.270 -
   1.271 -				*dp++ = c;
   1.272 -				break;
   1.273 -		}
   1.274 -	}
   1.275 -
   1.276 -	/* unresolved eatcount */
   1.277 -	if (eac)
   1.278 -		return 0;
   1.279 -
   1.280 -	/* resolve remaining slash */
   1.281 -	if (slc)
   1.282 -		*dp++ = '/';
   1.283 -
   1.284 -	*dp = 0;
   1.285 -
   1.286 -	len = dp - dest;
   1.287 -	for (i = 0; i < len / 2; ++i)
   1.288 -	{
   1.289 -		char t = dest[i];
   1.290 -		dest[i] = dest[len - i - 1];
   1.291 -		dest[len - i - 1] = t;
   1.292 -	}
   1.293 -
   1.294 -	return 1;
   1.295 -}
   1.296 -
   1.297 -
   1.298 -static int
   1.299 -posix_abspath(lua_State *L)
   1.300 -{
   1.301 -	const char *path = luaL_checkstring(L, 1);
   1.302 -	char *pwd = NULL;
   1.303 -	size_t pwdsize = 16;
   1.304 -	size_t len1, len2;
   1.305 -	char *srcpath, *dstpath;
   1.306 -
   1.307 -	for (;;)
   1.308 -	{
   1.309 -		if (path[0] == '/')
   1.310 -		{
   1.311 -			pwd = malloc(2);
   1.312 -			if (pwd == NULL)
   1.313 -				break;
   1.314 -			pwd[0] = '/';
   1.315 -			pwd[1] = 0;
   1.316 -		}
   1.317 -		else
   1.318 -		{
   1.319 -			char *newpwd = realloc(pwd, pwdsize);
   1.320 -			if (newpwd == NULL)
   1.321 -				break;
   1.322 -			pwd = newpwd;
   1.323 -
   1.324 -			if (!getcwd(pwd, pwdsize))
   1.325 -			{
   1.326 -				if (errno == ERANGE)
   1.327 -				{
   1.328 -					pwdsize <<= 1;
   1.329 -					continue;
   1.330 -				}
   1.331 -				lua_pushnil(L);
   1.332 -				lua_pushstring(L, strerror(errno));
   1.333 -				return 2;
   1.334 -			}
   1.335 -		}
   1.336 -
   1.337 -		len1 = strlen(pwd);
   1.338 -		len2 = strlen(path);
   1.339 -		srcpath = malloc(len1 + 1 + len2 + 1);
   1.340 -		dstpath = malloc(len1 + 1 + len2 + 1);
   1.341 -
   1.342 -		if (srcpath && dstpath)
   1.343 -		{
   1.344 -			int res;
   1.345 -
   1.346 -			strcpy(srcpath, pwd);
   1.347 -			free(pwd);
   1.348 -			srcpath[len1] = '/';
   1.349 -			strcpy(srcpath + len1 + 1, path);
   1.350 -			res = resolvepath(srcpath, dstpath);
   1.351 -			free(srcpath);
   1.352 -
   1.353 -			if (res)
   1.354 -			{
   1.355 -				lua_pushstring(L, dstpath);
   1.356 -				free(dstpath);
   1.357 -				return 1;
   1.358 -			}
   1.359 -
   1.360 -			free(dstpath);
   1.361 -			lua_pushnil(L);
   1.362 -			lua_pushstring(L, "Not a valid path");
   1.363 -			return 2;
   1.364 -		}
   1.365 -
   1.366 -		free(srcpath);
   1.367 -		free(dstpath);
   1.368 -		break;
   1.369 -	}
   1.370 -
   1.371 -	free(pwd);
   1.372 -	luaL_error(L, "Out of memory");
   1.373 -	return 0;
   1.374 -}
   1.375 -
   1.376 -
   1.377 -static int
   1.378 -posix_nanosleep(lua_State *L)
   1.379 -{
   1.380 -	struct timespec req;
   1.381 -	lua_Number d = luaL_checknumber(L, 1);
   1.382 -
   1.383 -	req.tv_sec = (time_t) d;
   1.384 -	req.tv_nsec = (long) ((d - req.tv_sec) * 1000000000);
   1.385 -
   1.386 -	if (nanosleep(&req, NULL) == 0)
   1.387 -	{
   1.388 -		lua_pushboolean(L, 1);
   1.389 -		return 1;
   1.390 -	}
   1.391 -
   1.392 -	lua_pushnil(L);
   1.393 -	lua_pushstring(L, strerror(errno));
   1.394 -	return 2;
   1.395 -}
   1.396 -
   1.397 -
   1.398 -static const luaL_Reg lib[] =
   1.399 -{
   1.400 -	{ "stat", posix_stat },
   1.401 -	{ "lstat", posix_lstat },
   1.402 -	{ "opendir", posix_opendir },
   1.403 -	{ "closedir", posix_closedir },
   1.404 -	{ "readdir", posix_readdir },
   1.405 -	{ "mkdir", posix_mkdir },
   1.406 -	{ "readlink", posix_readlink },
   1.407 -	{ "symlink", posix_symlink },
   1.408 -	{ "abspath", posix_abspath },
   1.409 -	{ "nanosleep", posix_nanosleep },
   1.410 -	{ NULL, NULL }
   1.411 -};
   1.412 -
   1.413 -
   1.414 -static const luaL_Reg methods[] =
   1.415 -{
   1.416 -	{"read", posix_readdir },
   1.417 -	{"close", posix_closedir },
   1.418 -	{"__gc", posix_closedir },
   1.419 -	{NULL, NULL}
   1.420 -};
   1.421 -
   1.422 -
   1.423 -static void
   1.424 -addclass(lua_State *L, const char *libname, const char *classname,
   1.425 -	const luaL_Reg *functions, const luaL_Reg *methods, void *userdata)
   1.426 -{
   1.427 -	luaL_newmetatable(L, classname); /* classtab */
   1.428 -	lua_pushliteral(L, "__index"); /* classtab, "__index" */
   1.429 -
   1.430 -	/* insert self: classtab.__index = classtab */
   1.431 -	lua_pushvalue(L, -2); /* classtab, "__index", classtab */
   1.432 -	lua_rawset(L, -3); /* classtab */
   1.433 -
   1.434 -	/* insert methods. consume 1 userdata. do not create a new tab */
   1.435 -	lua_pushlightuserdata(L, userdata); /* classtab, userdata */
   1.436 -	luaL_openlib(L, NULL, methods, 1); /* classtab */
   1.437 -
   1.438 -	/* first upvalue: userdata */
   1.439 -	lua_pushlightuserdata(L, userdata); /* classtab, userdata */
   1.440 -
   1.441 -	/* duplicate table argument to be used as second upvalue for cclosure */
   1.442 -	lua_pushvalue(L, -2); /* classtab, userdata, classtab */
   1.443 -
   1.444 -	/* insert functions */
   1.445 -	luaL_openlib(L, libname, functions, 2);	/* classtab, libtab */
   1.446 -
   1.447 -	/* adjust stack */
   1.448 -	lua_pop(L, 2);
   1.449 -}
   1.450 -
   1.451 -
   1.452 -int luaopen_tek_os_posix(lua_State *L)
   1.453 -{
   1.454 -	addclass(L, "tek.os.posix", DIRCLASSNAME, lib, methods, NULL);
   1.455 -	return 0;
   1.456 -}