cgi-bin/tek/os/posix.c
changeset 198 87a4de7c7457
parent 193 80a1a4eb0bb7
child 241 c6c81629f54e
     1.1 --- a/cgi-bin/tek/os/posix.c	Sun May 20 22:39:45 2007 +0200
     1.2 +++ b/cgi-bin/tek/os/posix.c	Fri Oct 05 01:41:59 2007 +0200
     1.3 @@ -44,10 +44,10 @@
     1.4  
     1.5  	if (attr == NULL)
     1.6  		lua_newtable(L);
     1.7 -		
     1.8 +
     1.9  	setfield(L, attr, "dev", s->st_dev);
    1.10  	setfield(L, attr, "ino", s->st_ino);
    1.11 -	
    1.12 +
    1.13  	if (attr == NULL || strcmp(attr, "mode") == 0)
    1.14  	{
    1.15  		if (S_ISREG(s->st_mode))
    1.16 @@ -69,7 +69,7 @@
    1.17  		if (attr == NULL)
    1.18  			lua_setfield(L, -2, "mode");
    1.19  	}
    1.20 -	
    1.21 +
    1.22  	setfield(L, attr, "nlink", s->st_nlink);
    1.23  	setfield(L, attr, "uid", s->st_uid);
    1.24  	setfield(L, attr, "gid", s->st_gid);
    1.25 @@ -80,22 +80,22 @@
    1.26  	setfield(L, attr, "size", s->st_size);
    1.27  	setfield(L, attr, "blocks", s->st_blocks);
    1.28  	setfield(L, attr, "blksize", s->st_blksize);
    1.29 -	
    1.30 +
    1.31  	return 1;
    1.32  }
    1.33  
    1.34  
    1.35 -static int 
    1.36 +static int
    1.37  posix_stat(lua_State *L)
    1.38  {
    1.39  	const char *path = luaL_checkstring(L, 1);
    1.40  	const char *attr = luaL_optstring(L, 2, NULL);
    1.41  	struct stat s;
    1.42  	return setstat(L, stat(path, &s), &s, attr);
    1.43 -}	
    1.44 +}
    1.45  
    1.46  
    1.47 -static int 
    1.48 +static int
    1.49  posix_lstat(lua_State *L)
    1.50  {
    1.51  	const char *path = luaL_checkstring(L, 1);
    1.52 @@ -105,7 +105,7 @@
    1.53  }
    1.54  
    1.55  
    1.56 -static int 
    1.57 +static int
    1.58  posix_opendir(lua_State *L)
    1.59  {
    1.60  	const char *path = luaL_checkstring(L, 1);
    1.61 @@ -136,7 +136,7 @@
    1.62  }
    1.63  
    1.64  
    1.65 -static int 
    1.66 +static int
    1.67  posix_closedir(lua_State *L)
    1.68  {
    1.69  	DIR **pdir = getinstptr(L, 1, DIRCLASSNAME);
    1.70 @@ -146,7 +146,7 @@
    1.71  }
    1.72  
    1.73  
    1.74 -static int 
    1.75 +static int
    1.76  posix_readdir(lua_State *L)
    1.77  {
    1.78  	DIR *dir = *getinstptr(L, 1, DIRCLASSNAME);
    1.79 @@ -159,7 +159,7 @@
    1.80  }
    1.81  
    1.82  
    1.83 -static int 
    1.84 +static int
    1.85  posix_readlink(lua_State *L)
    1.86  {
    1.87  	const char *path = luaL_checkstring(L, 1);
    1.88 @@ -177,7 +177,7 @@
    1.89  }
    1.90  
    1.91  
    1.92 -static int 
    1.93 +static int
    1.94  posix_mkdir(lua_State *L)
    1.95  {
    1.96  	const char *path = luaL_checkstring(L, 1);
    1.97 @@ -192,7 +192,7 @@
    1.98  }
    1.99  
   1.100  
   1.101 -static int 
   1.102 +static int
   1.103  posix_symlink(lua_State *L)
   1.104  {
   1.105  	const char *oldpath = luaL_checkstring(L, 1);
   1.106 @@ -216,8 +216,8 @@
   1.107  	char *dp = dest;
   1.108  	int dc = 0, slc = 0, eac = 0, wc = 0;
   1.109  	int i, c;
   1.110 -	
   1.111 -	while (len--)	
   1.112 +
   1.113 +	while (len--)
   1.114  	{
   1.115  		c = *(--sp);
   1.116  		switch (c)
   1.117 @@ -241,11 +241,11 @@
   1.118  			default:
   1.119  				if (wc)
   1.120  					break;
   1.121 -			
   1.122 +
   1.123  				if (slc)
   1.124  				{
   1.125  					slc = 0;
   1.126 -					
   1.127 +
   1.128  					if (eac > 0)
   1.129  					{
   1.130  						/* resolve one eatcount */
   1.131 @@ -254,37 +254,37 @@
   1.132  						wc = 1;
   1.133  						break;
   1.134  					}
   1.135 -					
   1.136 +
   1.137  					*dp++ = '/';
   1.138  				}
   1.139 -				
   1.140 +
   1.141  				while (dc == 2 || dc == 1)
   1.142  				{
   1.143  					*dp++ = '.';
   1.144  					dc--;
   1.145  				}
   1.146  				dc = 0;
   1.147 -				
   1.148 +
   1.149  				*dp++ = c;
   1.150  				break;
   1.151  		}
   1.152  	}
   1.153 -	
   1.154 +
   1.155  	/* unresolved eatcount */
   1.156  	if (eac)
   1.157  		return 0;
   1.158 -	
   1.159 +
   1.160  	/* resolve remaining slash */
   1.161  	if (slc)
   1.162  		*dp++ = '/';
   1.163 -	
   1.164 +
   1.165  	*dp = 0;
   1.166 -	
   1.167 +
   1.168  	len = dp - dest;
   1.169  	for (i = 0; i < len / 2; ++i)
   1.170  	{
   1.171  		char t = dest[i];
   1.172 -		dest[i] = dest[len - i - 1]; 
   1.173 +		dest[i] = dest[len - i - 1];
   1.174  		dest[len - i - 1] = t;
   1.175  	}
   1.176  
   1.177 @@ -292,87 +292,100 @@
   1.178  }
   1.179  
   1.180  
   1.181 -static int 
   1.182 +static int
   1.183  posix_abspath(lua_State *L)
   1.184  {
   1.185  	const char *path = luaL_checkstring(L, 1);
   1.186  	char *pwd = NULL;
   1.187  	size_t pwdsize = 16;
   1.188 -	
   1.189 +	size_t len1, len2;
   1.190 +	char *srcpath, *dstpath;
   1.191 +
   1.192  	for (;;)
   1.193  	{
   1.194 -		char *newpwd = realloc(pwd, pwdsize);
   1.195 -		if (newpwd == NULL)
   1.196 -			break;
   1.197 -		
   1.198 -		pwd = newpwd;
   1.199 -		if (getcwd(pwd, pwdsize))
   1.200 +		if (path[0] == '/')
   1.201  		{
   1.202 -			size_t len1 = strlen(pwd);
   1.203 -			size_t len2 = strlen(path);
   1.204 -			char *srcpath = malloc(len1 + 1 + len2 + 1);
   1.205 -			char *dstpath = malloc(len1 + 1 + len2 + 1);
   1.206 -			if (srcpath && dstpath)
   1.207 +			pwd = malloc(2);
   1.208 +			if (pwd == NULL)
   1.209 +				break;
   1.210 +			pwd[0] = '/';
   1.211 +			pwd[1] = 0;
   1.212 +		}
   1.213 +		else
   1.214 +		{
   1.215 +			char *newpwd = realloc(pwd, pwdsize);
   1.216 +			if (newpwd == NULL)
   1.217 +				break;
   1.218 +			pwd = newpwd;
   1.219 +
   1.220 +			if (!getcwd(pwd, pwdsize))
   1.221  			{
   1.222 -				int res;
   1.223 -				
   1.224 -				strcpy(srcpath, pwd);
   1.225 -				free(pwd);
   1.226 -				srcpath[len1] = '/';
   1.227 -				strcpy(srcpath + len1 + 1, path);
   1.228 -				res = resolvepath(srcpath, dstpath);
   1.229 -				free(srcpath);
   1.230 -				
   1.231 -				if (res)
   1.232 +				if (errno == ERANGE)
   1.233  				{
   1.234 -					lua_pushstring(L, dstpath);
   1.235 -					free(dstpath);
   1.236 -					return 1;			
   1.237 +					pwdsize <<= 1;
   1.238 +					continue;
   1.239  				}
   1.240 -				
   1.241 -				free(dstpath);
   1.242  				lua_pushnil(L);
   1.243 -				lua_pushstring(L, "Not a valid path");
   1.244 +				lua_pushstring(L, strerror(errno));
   1.245  				return 2;
   1.246  			}
   1.247 -			
   1.248 +		}
   1.249 +
   1.250 +		len1 = strlen(pwd);
   1.251 +		len2 = strlen(path);
   1.252 +		srcpath = malloc(len1 + 1 + len2 + 1);
   1.253 +		dstpath = malloc(len1 + 1 + len2 + 1);
   1.254 +
   1.255 +		if (srcpath && dstpath)
   1.256 +		{
   1.257 +			int res;
   1.258 +
   1.259 +			strcpy(srcpath, pwd);
   1.260 +			free(pwd);
   1.261 +			srcpath[len1] = '/';
   1.262 +			strcpy(srcpath + len1 + 1, path);
   1.263 +			res = resolvepath(srcpath, dstpath);
   1.264  			free(srcpath);
   1.265 +
   1.266 +			if (res)
   1.267 +			{
   1.268 +				lua_pushstring(L, dstpath);
   1.269 +				free(dstpath);
   1.270 +				return 1;
   1.271 +			}
   1.272 +
   1.273  			free(dstpath);
   1.274 -			break;
   1.275 +			lua_pushnil(L);
   1.276 +			lua_pushstring(L, "Not a valid path");
   1.277 +			return 2;
   1.278  		}
   1.279 -		
   1.280 -		if (errno == ERANGE)
   1.281 -		{
   1.282 -			pwdsize <<= 1;
   1.283 -			continue;
   1.284 -		}
   1.285 -		
   1.286 -		lua_pushnil(L);
   1.287 -		lua_pushstring(L, strerror(errno));
   1.288 -		return 2;
   1.289 +
   1.290 +		free(srcpath);
   1.291 +		free(dstpath);
   1.292 +		break;
   1.293  	}
   1.294 -	
   1.295 +
   1.296  	free(pwd);
   1.297  	luaL_error(L, "Out of memory");
   1.298  	return 0;
   1.299  }
   1.300  
   1.301  
   1.302 -static int 
   1.303 +static int
   1.304  posix_nanosleep(lua_State *L)
   1.305  {
   1.306  	struct timespec req;
   1.307  	lua_Number d = luaL_checknumber(L, 1);
   1.308 -	
   1.309 +
   1.310  	req.tv_sec = (time_t) d;
   1.311  	req.tv_nsec = (long) ((d - req.tv_sec) * 1000000000);
   1.312 -	
   1.313 +
   1.314  	if (nanosleep(&req, NULL) == 0)
   1.315  	{
   1.316  		lua_pushboolean(L, 1);
   1.317  		return 1;
   1.318  	}
   1.319 -	
   1.320 +
   1.321  	lua_pushnil(L);
   1.322  	lua_pushstring(L, strerror(errno));
   1.323  	return 2;
   1.324 @@ -429,7 +442,7 @@
   1.325  	luaL_openlib(L, libname, functions, 2);	/* classtab, libtab */
   1.326  
   1.327  	/* adjust stack */
   1.328 -	lua_pop(L, 2);	
   1.329 +	lua_pop(L, 2);
   1.330  }
   1.331  
   1.332