400-luaposix_5.1.4-embedded.patch 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  1. Index: lua-5.1.4/src/Makefile
  2. ===================================================================
  3. --- lua-5.1.4.orig/src/Makefile 2008-09-25 12:19:44.000000000 +0200
  4. +++ lua-5.1.4/src/Makefile 2008-09-25 12:20:03.000000000 +0200
  5. @@ -12,7 +12,7 @@
  6. AR= ar rcu
  7. RANLIB= ranlib
  8. RM= rm -f
  9. -LIBS= -lm $(MYLIBS)
  10. +LIBS= -lm -lcrypt $(MYLIBS)
  11. MYCFLAGS=
  12. MYLDFLAGS=
  13. @@ -29,7 +29,7 @@
  14. lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o \
  15. lundump.o lvm.o lzio.o lnum.o
  16. LIB_O= lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o \
  17. - lstrlib.o loadlib.o linit.o
  18. + lstrlib.o loadlib.o linit.o lposix.o
  19. LUA_T= lua
  20. LUA_O= lua.o
  21. Index: lua-5.1.4/src/linit.c
  22. ===================================================================
  23. --- lua-5.1.4.orig/src/linit.c 2008-09-25 12:19:02.000000000 +0200
  24. +++ lua-5.1.4/src/linit.c 2008-09-25 12:19:32.000000000 +0200
  25. @@ -23,6 +23,7 @@
  26. {LUA_STRLIBNAME, luaopen_string},
  27. {LUA_MATHLIBNAME, luaopen_math},
  28. {LUA_DBLIBNAME, luaopen_debug},
  29. + {LUA_POSIXLIBNAME, luaopen_posix},
  30. {NULL, NULL}
  31. };
  32. Index: lua-5.1.4/src/lposix.c
  33. ===================================================================
  34. --- /dev/null 1970-01-01 00:00:00.000000000 +0000
  35. +++ lua-5.1.4/src/lposix.c 2008-09-25 12:16:29.000000000 +0200
  36. @@ -0,0 +1,1139 @@
  37. +/*
  38. +* lposix.c
  39. +* POSIX library for Lua 5.1.
  40. +* Luiz Henrique de Figueiredo <[email protected]>
  41. +* 07 Apr 2006 23:17:49
  42. +* Clean up and bug fixes by Leo Razoumov <[email protected]> 2006-10-11 <!LR>
  43. +* Based on original by Claudio Terra for Lua 3.x.
  44. +* With contributions by Roberto Ierusalimschy.
  45. +*/
  46. +
  47. +#include <sys/stat.h>
  48. +#include <sys/times.h>
  49. +#include <sys/types.h>
  50. +#include <sys/utsname.h>
  51. +#include <sys/wait.h>
  52. +
  53. +#include <dirent.h>
  54. +#include <errno.h>
  55. +#include <fcntl.h>
  56. +#include <glob.h>
  57. +#include <grp.h>
  58. +#include <libgen.h>
  59. +#include <limits.h>
  60. +#include <poll.h>
  61. +#include <pwd.h>
  62. +#include <signal.h>
  63. +#include <stdio.h>
  64. +#include <stdlib.h>
  65. +#include <string.h>
  66. +#include <syslog.h>
  67. +#include <time.h>
  68. +#include <unistd.h>
  69. +#include <utime.h>
  70. +
  71. +#define MYNAME "posix"
  72. +#define MYVERSION MYNAME " library for " LUA_VERSION " / Jan 2008"
  73. +
  74. +#ifndef ENABLE_SYSLOG
  75. +#define ENABLE_SYSLOG 1
  76. +#endif
  77. +
  78. +#include "lua.h"
  79. +#include "lualib.h"
  80. +#include "lauxlib.h"
  81. +
  82. +#include "modemuncher.c"
  83. +
  84. +/* compatibility with Lua 5.0 */
  85. +#ifndef LUA_VERSION_NUM
  86. +static int luaL_checkoption (lua_State *L, int narg, const char *def,
  87. + const char *const lst[]) {
  88. + const char *name = (def) ? luaL_optstring(L, narg, def) :
  89. + luaL_checkstring(L, narg);
  90. + int i = luaL_findstring(name, lst);
  91. + if (i == -1)
  92. + luaL_argerror(L, narg, lua_pushfstring(L, "invalid option '%s'", name));
  93. + return i;
  94. +}
  95. +#define lua_pushinteger lua_pushnumber
  96. +#define lua_createtable(L,a,r) lua_newtable(L)
  97. +#define LUA_FILEHANDLE "FILE*"
  98. +
  99. +#define lua_setfield(l,i,k)
  100. +#define lua_getfield(l,i,k)
  101. +
  102. +#endif
  103. +
  104. +static const struct { char c; mode_t b; } M[] =
  105. +{
  106. + {'r', S_IRUSR}, {'w', S_IWUSR}, {'x', S_IXUSR},
  107. + {'r', S_IRGRP}, {'w', S_IWGRP}, {'x', S_IXGRP},
  108. + {'r', S_IROTH}, {'w', S_IWOTH}, {'x', S_IXOTH},
  109. +};
  110. +
  111. +
  112. +static void pushmode(lua_State *L, mode_t mode)
  113. +{
  114. + char m[9];
  115. + int i;
  116. + for (i=0; i<9; i++) m[i]= (mode & M[i].b) ? M[i].c : '-';
  117. + if (mode & S_ISUID) m[2]= (mode & S_IXUSR) ? 's' : 'S';
  118. + if (mode & S_ISGID) m[5]= (mode & S_IXGRP) ? 's' : 'S';
  119. + lua_pushlstring(L, m, 9);
  120. +}
  121. +
  122. +typedef void (*Selector)(lua_State *L, int i, const void *data);
  123. +
  124. +static int doselection(lua_State *L, int i, int n,
  125. + const char *const S[],
  126. + Selector F,
  127. + const void *data)
  128. +{
  129. + if (lua_isnone(L, i) || lua_istable(L, i))
  130. + {
  131. + int j;
  132. + if (lua_isnone(L, i)) lua_createtable(L,0,n); else lua_settop(L, i);
  133. + for (j=0; S[j]!=NULL; j++)
  134. + {
  135. + lua_pushstring(L, S[j]);
  136. + F(L, j, data);
  137. + lua_settable(L, -3);
  138. + }
  139. + return 1;
  140. + }
  141. + else
  142. + {
  143. + int k,n=lua_gettop(L);
  144. + for (k=i; k<=n; k++)
  145. + {
  146. + int j=luaL_checkoption(L, k, NULL, S);
  147. + F(L, j, data);
  148. + lua_replace(L, k);
  149. + }
  150. + return n-i+1;
  151. + }
  152. +}
  153. +#define doselection(L,i,S,F,d) (doselection)(L,i,sizeof(S)/sizeof(*S)-1,S,F,d)
  154. +
  155. +static int pusherror(lua_State *L, const char *info)
  156. +{
  157. + lua_pushnil(L);
  158. + if (info==NULL)
  159. + lua_pushstring(L, strerror(errno));
  160. + else
  161. + lua_pushfstring(L, "%s: %s", info, strerror(errno));
  162. + lua_pushinteger(L, errno);
  163. + return 3;
  164. +}
  165. +
  166. +static int pushresult(lua_State *L, int i, const char *info)
  167. +{
  168. + if (i==-1) return pusherror(L, info);
  169. + lua_pushinteger(L, i);
  170. + return 1;
  171. +}
  172. +
  173. +static void badoption(lua_State *L, int i, const char *what, int option)
  174. +{
  175. + luaL_argerror(L, 2,
  176. + lua_pushfstring(L, "unknown %s option '%c'", what, option));
  177. +}
  178. +
  179. +static uid_t mygetuid(lua_State *L, int i)
  180. +{
  181. + if (lua_isnone(L, i))
  182. + return -1;
  183. + else if (lua_isnumber(L, i))
  184. + return (uid_t) lua_tonumber(L, i);
  185. + else if (lua_isstring(L, i))
  186. + {
  187. + struct passwd *p=getpwnam(lua_tostring(L, i));
  188. + return (p==NULL) ? -1 : p->pw_uid;
  189. + }
  190. + else
  191. + return luaL_typerror(L, i, "string or number");
  192. +}
  193. +
  194. +static gid_t mygetgid(lua_State *L, int i)
  195. +{
  196. + if (lua_isnone(L, i))
  197. + return -1;
  198. + else if (lua_isnumber(L, i))
  199. + return (gid_t) lua_tonumber(L, i);
  200. + else if (lua_isstring(L, i))
  201. + {
  202. + struct group *g=getgrnam(lua_tostring(L, i));
  203. + return (g==NULL) ? -1 : g->gr_gid;
  204. + }
  205. + else
  206. + return luaL_typerror(L, i, "string or number");
  207. +}
  208. +
  209. +
  210. +static int Perrno(lua_State *L) /** errno([n]) */
  211. +{
  212. + int n = luaL_optint(L, 1, errno);
  213. + lua_pushstring(L, strerror(n));
  214. + lua_pushinteger(L, n);
  215. + return 2;
  216. +}
  217. +
  218. +
  219. +static int Pbasename(lua_State *L) /** basename(path) */
  220. +{
  221. + char b[PATH_MAX];
  222. + size_t len;
  223. + const char *path = luaL_checklstring(L, 1, &len);
  224. + if (len>=sizeof(b)) luaL_argerror(L, 1, "too long");
  225. + lua_pushstring(L, basename(strcpy(b,path)));
  226. + return 1;
  227. +}
  228. +
  229. +
  230. +static int Pdirname(lua_State *L) /** dirname(path) */
  231. +{
  232. + char b[PATH_MAX];
  233. + size_t len;
  234. + const char *path = luaL_checklstring(L, 1, &len);
  235. + if (len>=sizeof(b)) luaL_argerror(L, 1, "too long");
  236. + lua_pushstring(L, dirname(strcpy(b,path)));
  237. + return 1;
  238. +}
  239. +
  240. +
  241. +static int Pdir(lua_State *L) /** dir([path]) */
  242. +{
  243. + const char *path = luaL_optstring(L, 1, ".");
  244. + DIR *d = opendir(path);
  245. + if (d == NULL)
  246. + return pusherror(L, path);
  247. + else
  248. + {
  249. + int i;
  250. + struct dirent *entry;
  251. + lua_newtable(L);
  252. + for (i=1; (entry = readdir(d)) != NULL; i++)
  253. + {
  254. + lua_pushstring(L, entry->d_name);
  255. + lua_rawseti(L, -2, i);
  256. + }
  257. + closedir(d);
  258. + lua_pushinteger(L, i-1);
  259. + return 2;
  260. + }
  261. +}
  262. +
  263. +static int Pglob(lua_State *L) /** glob(pattern) */
  264. +{
  265. + const char *pattern = luaL_optstring(L, 1, "*");
  266. + glob_t globres;
  267. +
  268. + if (glob(pattern, 0, NULL, &globres))
  269. + return pusherror(L, pattern);
  270. + else
  271. + {
  272. + int i;
  273. + lua_newtable(L);
  274. + for (i=1; i<=globres.gl_pathc; i++) {
  275. + lua_pushstring(L, globres.gl_pathv[i-1]);
  276. + lua_rawseti(L, -2, i);
  277. + }
  278. + globfree(&globres);
  279. + return 1;
  280. + }
  281. +}
  282. +
  283. +static int aux_files(lua_State *L)
  284. +{
  285. + DIR **p = (DIR **)lua_touserdata(L, lua_upvalueindex(1));
  286. + DIR *d = *p;
  287. + struct dirent *entry;
  288. + if (d == NULL) return 0;
  289. + entry = readdir(d);
  290. + if (entry == NULL)
  291. + {
  292. + closedir(d);
  293. + *p=NULL;
  294. + return 0;
  295. + }
  296. + else
  297. + {
  298. + lua_pushstring(L, entry->d_name);
  299. + return 1;
  300. + }
  301. +}
  302. +
  303. +static int dir_gc (lua_State *L)
  304. +{
  305. + DIR *d = *(DIR **)lua_touserdata(L, 1);
  306. + if (d!=NULL) closedir(d);
  307. + return 0;
  308. +}
  309. +
  310. +static int Pfiles(lua_State *L) /** files([path]) */
  311. +{
  312. + const char *path = luaL_optstring(L, 1, ".");
  313. + DIR **d = (DIR **)lua_newuserdata(L, sizeof(DIR *));
  314. + if (luaL_newmetatable(L, MYNAME " dir handle"))
  315. + {
  316. + lua_pushliteral(L, "__gc");
  317. + lua_pushcfunction(L, dir_gc);
  318. + lua_settable(L, -3);
  319. + }
  320. + lua_setmetatable(L, -2);
  321. + *d = opendir(path);
  322. + if (*d == NULL) return pusherror(L, path);
  323. + lua_pushcclosure(L, aux_files, 1);
  324. + return 1;
  325. +}
  326. +
  327. +
  328. +static int Pgetcwd(lua_State *L) /** getcwd() */
  329. +{
  330. + char b[PATH_MAX];
  331. + if (getcwd(b, sizeof(b)) == NULL) return pusherror(L, ".");
  332. + lua_pushstring(L, b);
  333. + return 1;
  334. +}
  335. +
  336. +
  337. +static int Pmkdir(lua_State *L) /** mkdir(path) */
  338. +{
  339. + const char *path = luaL_checkstring(L, 1);
  340. + return pushresult(L, mkdir(path, 0777), path);
  341. +}
  342. +
  343. +
  344. +static int Pchdir(lua_State *L) /** chdir(path) */
  345. +{
  346. + const char *path = luaL_checkstring(L, 1);
  347. + return pushresult(L, chdir(path), path);
  348. +}
  349. +
  350. +static int Prmdir(lua_State *L) /** rmdir(path) */
  351. +{
  352. + const char *path = luaL_checkstring(L, 1);
  353. + return pushresult(L, rmdir(path), path);
  354. +}
  355. +
  356. +
  357. +static int Punlink(lua_State *L) /** unlink(path) */
  358. +{
  359. + const char *path = luaL_checkstring(L, 1);
  360. + return pushresult(L, unlink(path), path);
  361. +}
  362. +
  363. +static int Plink(lua_State *L) /** link(old,new,[symbolic]) */
  364. +{
  365. + const char *oldpath = luaL_checkstring(L, 1);
  366. + const char *newpath = luaL_checkstring(L, 2);
  367. + return pushresult(L,
  368. + (lua_toboolean(L,3) ? symlink : link)(oldpath, newpath), NULL);
  369. +}
  370. +
  371. +
  372. +static int Preadlink(lua_State *L) /** readlink(path) */
  373. +{
  374. + char b[PATH_MAX];
  375. + const char *path = luaL_checkstring(L, 1);
  376. + int n = readlink(path, b, sizeof(b));
  377. + if (n==-1) return pusherror(L, path);
  378. + lua_pushlstring(L, b, n);
  379. + return 1;
  380. +}
  381. +
  382. +
  383. +static int Paccess(lua_State *L) /** access(path,[mode]) */
  384. +{
  385. + int mode=F_OK;
  386. + const char *path=luaL_checkstring(L, 1);
  387. + const char *s;
  388. + for (s=luaL_optstring(L, 2, "f"); *s!=0 ; s++)
  389. + switch (*s)
  390. + {
  391. + case ' ': break;
  392. + case 'r': mode |= R_OK; break;
  393. + case 'w': mode |= W_OK; break;
  394. + case 'x': mode |= X_OK; break;
  395. + case 'f': mode |= F_OK; break;
  396. + default: badoption(L, 2, "mode", *s); break;
  397. + }
  398. + return pushresult(L, access(path, mode), path);
  399. +}
  400. +
  401. +
  402. +static int myfclose (lua_State *L) {
  403. + FILE **p = (FILE **)lua_touserdata(L, 1);
  404. + int rc = fclose(*p);
  405. + if (rc == 0) *p = NULL;
  406. + return pushresult(L, rc, NULL);
  407. +}
  408. +
  409. +static int pushfile (lua_State *L, int id, const char *mode) {
  410. + FILE **f = (FILE **)lua_newuserdata(L, sizeof(FILE *));
  411. + *f = NULL;
  412. + luaL_getmetatable(L, LUA_FILEHANDLE);
  413. + lua_setmetatable(L, -2);
  414. + lua_getfield(L, LUA_REGISTRYINDEX, "POSIX_PIPEFILE");
  415. + if (lua_isnil(L, -1)) {
  416. + lua_pop(L, 1);
  417. + lua_newtable(L);
  418. + lua_pushvalue(L, -1);
  419. + lua_pushcfunction(L, myfclose);
  420. + lua_setfield(L, -2, "__close");
  421. + lua_setfield(L, LUA_REGISTRYINDEX, "POSIX_PIPEFILE");
  422. + }
  423. + lua_setfenv(L, -2);
  424. + *f = fdopen(id, mode);
  425. + return (*f != NULL);
  426. +}
  427. +
  428. +static int Ppipe(lua_State *L) /** pipe() */
  429. +{
  430. + int fd[2];
  431. + if (pipe(fd)==-1) return pusherror(L, NULL);
  432. + if (!pushfile(L, fd[0], "r") || !pushfile(L, fd[1], "w"))
  433. + return pusherror(L, "pipe");
  434. + return 2;
  435. +}
  436. +
  437. +
  438. +static int Pfileno(lua_State *L) /** fileno(filehandle) */
  439. +{
  440. + FILE *f = *(FILE**) luaL_checkudata(L, 1, LUA_FILEHANDLE);
  441. + return pushresult(L, fileno(f), NULL);
  442. +}
  443. +
  444. +
  445. +static int Pfdopen(lua_State *L) /** fdopen(fd, mode) */
  446. +{
  447. + int fd = luaL_checkint(L, 1);
  448. + const char *mode = luaL_checkstring(L, 2);
  449. + if (!pushfile(L, fd, mode))
  450. + return pusherror(L, "fdpoen");
  451. + return 1;
  452. +}
  453. +
  454. +
  455. +/* helper func for Pdup */
  456. +static const char *filemode(int fd)
  457. +{
  458. + const char *m;
  459. + int mode = fcntl(fd, F_GETFL);
  460. + if (mode < 0)
  461. + return NULL;
  462. + switch (mode & O_ACCMODE) {
  463. + case O_RDONLY: m = "r"; break;
  464. + case O_WRONLY: m = "w"; break;
  465. + default: m = "rw"; break;
  466. + }
  467. + return m;
  468. +}
  469. +
  470. +static int Pdup(lua_State *L) /** dup(old,[new]) */
  471. +{
  472. + FILE **oldf = (FILE**)luaL_checkudata(L, 1, LUA_FILEHANDLE);
  473. + FILE **newf = (FILE **)lua_touserdata(L, 2);
  474. + int fd;
  475. + const char *msg = "dup2";
  476. + fflush(*newf);
  477. + if (newf == NULL) {
  478. + fd = dup(fileno(*oldf));
  479. + msg = "dup";
  480. + } else {
  481. + fflush(*newf);
  482. + fd = dup2(fileno(*oldf), fileno(*newf));
  483. + }
  484. +
  485. + if ((fd < 0) || !pushfile(L, fd, filemode(fd)))
  486. + return pusherror(L, msg);
  487. + return 1;
  488. +}
  489. +
  490. +
  491. +static int Pmkfifo(lua_State *L) /** mkfifo(path) */
  492. +{
  493. + const char *path = luaL_checkstring(L, 1);
  494. + return pushresult(L, mkfifo(path, 0777), path);
  495. +}
  496. +
  497. +
  498. +static int runexec(lua_State *L, int use_shell)
  499. +{
  500. + const char *path = luaL_checkstring(L, 1);
  501. + int i,n=lua_gettop(L);
  502. + char **argv = lua_newuserdata(L,(n+1)*sizeof(char*));
  503. + argv[0] = (char*)path;
  504. + for (i=1; i<n; i++) argv[i] = (char*)luaL_checkstring(L, i+1);
  505. + argv[n] = NULL;
  506. + if (use_shell) {
  507. + execvp(path, argv);
  508. + } else {
  509. + execv(path, argv);
  510. + }
  511. + return pusherror(L, path);
  512. +}
  513. +
  514. +
  515. +static int Pexec(lua_State *L) /** exec(path,[args]) */
  516. +{
  517. + return runexec(L, 0);
  518. +}
  519. +
  520. +
  521. +static int Pexecp(lua_State *L) /** execp(path,[args]) */
  522. +{
  523. + return runexec(L, 1);
  524. +}
  525. +
  526. +
  527. +static int Pfork(lua_State *L) /** fork() */
  528. +{
  529. + return pushresult(L, fork(), NULL);
  530. +}
  531. +
  532. +/* from http://lua-users.org/lists/lua-l/2007-11/msg00346.html */
  533. +static int Ppoll(lua_State *L) /** poll(filehandle, timeout) */
  534. +{
  535. + struct pollfd fds;
  536. + FILE* file = *(FILE**)luaL_checkudata(L,1,LUA_FILEHANDLE);
  537. + int timeout = luaL_checkint(L,2);
  538. + fds.fd = fileno(file);
  539. + fds.events = POLLIN;
  540. + return pushresult(L, poll(&fds,1,timeout), NULL);
  541. +}
  542. +
  543. +static int Pwait(lua_State *L) /** wait([pid]) */
  544. +{
  545. + int status;
  546. + pid_t pid = luaL_optint(L, 1, -1);
  547. + pid = waitpid(pid, &status, 0);
  548. + if (pid == -1) return pusherror(L, NULL);
  549. + lua_pushinteger(L, pid);
  550. + if (WIFEXITED(status))
  551. + {
  552. + lua_pushliteral(L,"exited");
  553. + lua_pushinteger(L, WEXITSTATUS(status));
  554. + return 3;
  555. + }
  556. + else if (WIFSIGNALED(status))
  557. + {
  558. + lua_pushliteral(L,"killed");
  559. + lua_pushinteger(L, WTERMSIG(status));
  560. + return 3;
  561. + }
  562. + else if (WIFSTOPPED(status))
  563. + {
  564. + lua_pushliteral(L,"stopped");
  565. + lua_pushinteger(L, WSTOPSIG(status));
  566. + return 3;
  567. + }
  568. + return 1;
  569. +}
  570. +
  571. +
  572. +static int Pkill(lua_State *L) /** kill(pid,[sig]) */
  573. +{
  574. + pid_t pid = luaL_checkint(L, 1);
  575. + int sig = luaL_optint(L, 2, SIGTERM);
  576. + return pushresult(L, kill(pid, sig), NULL);
  577. +}
  578. +
  579. +static int Psetpid(lua_State *L) /** setpid(option,...) */
  580. +{
  581. + const char *what=luaL_checkstring(L, 1);
  582. + switch (*what)
  583. + {
  584. + case 'U':
  585. + return pushresult(L, seteuid(mygetuid(L, 2)), NULL);
  586. + case 'u':
  587. + return pushresult(L, setuid(mygetuid(L, 2)), NULL);
  588. + case 'G':
  589. + return pushresult(L, setegid(mygetgid(L, 2)), NULL);
  590. + case 'g':
  591. + return pushresult(L, setgid(mygetgid(L, 2)), NULL);
  592. + case 's':
  593. + return pushresult(L, setsid(), NULL);
  594. + case 'p':
  595. + {
  596. + pid_t pid = luaL_checkint(L, 2);
  597. + pid_t pgid = luaL_checkint(L, 3);
  598. + return pushresult(L, setpgid(pid,pgid), NULL);
  599. + }
  600. + default:
  601. + badoption(L, 2, "id", *what);
  602. + return 0;
  603. + }
  604. +}
  605. +
  606. +
  607. +static int Psleep(lua_State *L) /** sleep(seconds) */
  608. +{
  609. + unsigned int seconds = luaL_checkint(L, 1);
  610. + lua_pushinteger(L, sleep(seconds));
  611. + return 1;
  612. +}
  613. +
  614. +
  615. +static int Psetenv(lua_State *L) /** setenv(name,value,[over]) */
  616. +{
  617. + const char *name=luaL_checkstring(L, 1);
  618. + const char *value=luaL_optstring(L, 2, NULL);
  619. + if (value==NULL)
  620. + {
  621. + unsetenv(name);
  622. + return pushresult(L, 0, NULL);
  623. + }
  624. + else
  625. + {
  626. + int overwrite=lua_isnoneornil(L, 3) || lua_toboolean(L, 3);
  627. + return pushresult(L, setenv(name,value,overwrite), NULL);
  628. + }
  629. +}
  630. +
  631. +
  632. +static int Pgetenv(lua_State *L) /** getenv([name]) */
  633. +{
  634. + if (lua_isnone(L, 1))
  635. + {
  636. + extern char **environ;
  637. + char **e;
  638. + lua_newtable(L);
  639. + for (e=environ; *e!=NULL; e++)
  640. + {
  641. + char *s=*e;
  642. + char *eq=strchr(s, '=');
  643. + if (eq==NULL) /* will this ever happen? */
  644. + {
  645. + lua_pushstring(L,s);
  646. + lua_pushboolean(L,1);
  647. + }
  648. + else
  649. + {
  650. + lua_pushlstring(L,s,eq-s);
  651. + lua_pushstring(L,eq+1);
  652. + }
  653. + lua_settable(L,-3);
  654. + }
  655. + }
  656. + else
  657. + lua_pushstring(L, getenv(luaL_checkstring(L, 1)));
  658. + return 1;
  659. +}
  660. +
  661. +static int Pumask(lua_State *L) /** umask([mode]) */
  662. +{/* <!LR> from old lposix-5.0 version */
  663. + char m[10];
  664. + mode_t mode;
  665. + umask(mode=umask(0));
  666. + mode=(~mode)&0777;
  667. + if (!lua_isnone(L, 1))
  668. + {
  669. + if (mode_munch(&mode, luaL_checkstring(L, 1)))
  670. + {
  671. + lua_pushnil(L);
  672. + return 1;
  673. + }
  674. + mode&=0777;
  675. + umask(~mode);
  676. + }
  677. + modechopper(mode, m);
  678. + lua_pushstring(L, m);
  679. + return 1;
  680. +}
  681. +
  682. +
  683. +static int Pchmod(lua_State *L) /** chmod(path,mode) */
  684. +{
  685. + mode_t mode;
  686. + struct stat s;
  687. + const char *path = luaL_checkstring(L, 1);
  688. + const char *modestr = luaL_checkstring(L, 2);
  689. + if (stat(path, &s)) return pusherror(L, path);
  690. + mode = s.st_mode;
  691. + if (mode_munch(&mode, modestr)) luaL_argerror(L, 2, "bad mode");
  692. + return pushresult(L, chmod(path, mode), path);
  693. +}
  694. +
  695. +
  696. +static int Pchown(lua_State *L) /** chown(path,uid,gid) */
  697. +{
  698. + const char *path = luaL_checkstring(L, 1);
  699. + uid_t uid = mygetuid(L, 2);
  700. + gid_t gid = mygetgid(L, 3);
  701. + return pushresult(L, chown(path, uid, gid), path);
  702. +}
  703. +
  704. +
  705. +static int Putime(lua_State *L) /** utime(path,[mtime,atime]) */
  706. +{
  707. + struct utimbuf times;
  708. + time_t currtime = time(NULL);
  709. + const char *path = luaL_checkstring(L, 1);
  710. + times.modtime = luaL_optnumber(L, 2, currtime);
  711. + times.actime = luaL_optnumber(L, 3, currtime);
  712. + return pushresult(L, utime(path, &times), path);
  713. +}
  714. +
  715. +
  716. +static void FgetID(lua_State *L, int i, const void *data)
  717. +{
  718. + switch (i)
  719. + {
  720. + case 0: lua_pushinteger(L, getegid()); break;
  721. + case 1: lua_pushinteger(L, geteuid()); break;
  722. + case 2: lua_pushinteger(L, getgid()); break;
  723. + case 3: lua_pushinteger(L, getuid()); break;
  724. + case 4: lua_pushinteger(L, getpgrp()); break;
  725. + case 5: lua_pushinteger(L, getpid()); break;
  726. + case 6: lua_pushinteger(L, getppid()); break;
  727. + }
  728. +}
  729. +
  730. +static const char *const SgetID[] =
  731. +{
  732. + "egid", "euid", "gid", "uid", "pgrp", "pid", "ppid", NULL
  733. +};
  734. +
  735. +static int Pgetpid(lua_State *L) /** getpid([options]) */
  736. +{
  737. + return doselection(L, 1, SgetID, FgetID, NULL);
  738. +}
  739. +
  740. +
  741. +static int Phostid(lua_State *L) /** hostid() */
  742. +{
  743. + char b[32];
  744. + sprintf(b,"%ld",gethostid());
  745. + lua_pushstring(L, b);
  746. + return 1;
  747. +}
  748. +
  749. +
  750. +static int Pttyname(lua_State *L) /** ttyname([fd]) */
  751. +{
  752. + int fd=luaL_optint(L, 1, 0);
  753. + lua_pushstring(L, ttyname(fd));
  754. + return 1;
  755. +}
  756. +
  757. +
  758. +static int Pctermid(lua_State *L) /** ctermid() */
  759. +{
  760. + char b[L_ctermid];
  761. + lua_pushstring(L, ctermid(b));
  762. + return 1;
  763. +}
  764. +
  765. +
  766. +static int Pgetlogin(lua_State *L) /** getlogin() */
  767. +{
  768. + lua_pushstring(L, getlogin());
  769. + return 1;
  770. +}
  771. +
  772. +
  773. +static void Fgetpasswd(lua_State *L, int i, const void *data)
  774. +{
  775. + const struct passwd *p=data;
  776. + switch (i)
  777. + {
  778. + case 0: lua_pushstring(L, p->pw_name); break;
  779. + case 1: lua_pushinteger(L, p->pw_uid); break;
  780. + case 2: lua_pushinteger(L, p->pw_gid); break;
  781. + case 3: lua_pushstring(L, p->pw_dir); break;
  782. + case 4: lua_pushstring(L, p->pw_shell); break;
  783. +/* not strictly POSIX */
  784. + case 5: lua_pushstring(L, p->pw_gecos); break;
  785. + case 6: lua_pushstring(L, p->pw_passwd); break;
  786. + }
  787. +}
  788. +
  789. +static const char *const Sgetpasswd[] =
  790. +{
  791. + "name", "uid", "gid", "dir", "shell", "gecos", "passwd", NULL
  792. +};
  793. +
  794. +
  795. +static int Pgetpasswd(lua_State *L) /** getpasswd(name|id,[sel]) */
  796. +{
  797. + struct passwd *p=NULL;
  798. + if (lua_isnoneornil(L, 1))
  799. + p = getpwuid(geteuid());
  800. + else if (lua_isnumber(L, 1))
  801. + p = getpwuid((uid_t)lua_tonumber(L, 1));
  802. + else if (lua_isstring(L, 1))
  803. + p = getpwnam(lua_tostring(L, 1));
  804. + else
  805. + luaL_typerror(L, 1, "string or number");
  806. + if (p==NULL)
  807. + lua_pushnil(L);
  808. + else
  809. + return doselection(L, 2, Sgetpasswd, Fgetpasswd, p);
  810. + return 1;
  811. +}
  812. +
  813. +
  814. +static int Pgetgroup(lua_State *L) /** getgroup(name|id) */
  815. +{
  816. + struct group *g=NULL;
  817. + if (lua_isnumber(L, 1))
  818. + g = getgrgid((gid_t)lua_tonumber(L, 1));
  819. + else if (lua_isstring(L, 1))
  820. + g = getgrnam(lua_tostring(L, 1));
  821. + else
  822. + luaL_typerror(L, 1, "string or number");
  823. + if (g==NULL)
  824. + lua_pushnil(L);
  825. + else
  826. + {
  827. + int i;
  828. + lua_newtable(L);
  829. + lua_pushliteral(L, "name");
  830. + lua_pushstring(L, g->gr_name);
  831. + lua_settable(L, -3);
  832. + lua_pushliteral(L, "gid");
  833. + lua_pushinteger(L, g->gr_gid);
  834. + lua_settable(L, -3);
  835. + for (i=0; g->gr_mem[i]!=NULL; i++)
  836. + {
  837. + lua_pushstring(L, g->gr_mem[i]);
  838. + lua_rawseti(L, -2, i);
  839. + }
  840. + }
  841. + return 1;
  842. +}
  843. +
  844. +
  845. +struct mytimes
  846. +{
  847. + struct tms t;
  848. + clock_t elapsed;
  849. +};
  850. +
  851. +/* #define pushtime(L,x) lua_pushnumber(L,((lua_Number)x)/CLOCKS_PER_SEC) */
  852. +#define pushtime(L,x) lua_pushnumber(L, ((lua_Number)x)/clk_tck)
  853. +
  854. +static void Ftimes(lua_State *L, int i, const void *data)
  855. +{
  856. + static long clk_tck = 0;
  857. + const struct mytimes *t=data;
  858. +
  859. + if( !clk_tck){ clk_tck= sysconf(_SC_CLK_TCK);}
  860. + switch (i)
  861. + {
  862. + case 0: pushtime(L, t->t.tms_utime); break;
  863. + case 1: pushtime(L, t->t.tms_stime); break;
  864. + case 2: pushtime(L, t->t.tms_cutime); break;
  865. + case 3: pushtime(L, t->t.tms_cstime); break;
  866. + case 4: pushtime(L, t->elapsed); break;
  867. + }
  868. +}
  869. +
  870. +static const char *const Stimes[] =
  871. +{
  872. + "utime", "stime", "cutime", "cstime", "elapsed", NULL
  873. +};
  874. +
  875. +static int Ptimes(lua_State *L) /** times([options]) */
  876. +{
  877. + struct mytimes t;
  878. + t.elapsed = times(&t.t);
  879. + return doselection(L, 1, Stimes, Ftimes, &t);
  880. +}
  881. +
  882. +
  883. +static const char *filetype(mode_t m)
  884. +{
  885. + if (S_ISREG(m)) return "regular";
  886. + else if (S_ISLNK(m)) return "link";
  887. + else if (S_ISDIR(m)) return "directory";
  888. + else if (S_ISCHR(m)) return "character device";
  889. + else if (S_ISBLK(m)) return "block device";
  890. + else if (S_ISFIFO(m)) return "fifo";
  891. + else if (S_ISSOCK(m)) return "socket";
  892. + else return "?";
  893. +}
  894. +
  895. +static void Fstat(lua_State *L, int i, const void *data)
  896. +{
  897. + const struct stat *s=data;
  898. + switch (i)
  899. + {
  900. + case 0: pushmode(L, s->st_mode); break;
  901. + case 1: lua_pushinteger(L, s->st_ino); break;
  902. + case 2: lua_pushinteger(L, s->st_dev); break;
  903. + case 3: lua_pushinteger(L, s->st_nlink); break;
  904. + case 4: lua_pushinteger(L, s->st_uid); break;
  905. + case 5: lua_pushinteger(L, s->st_gid); break;
  906. + case 6: lua_pushinteger(L, s->st_size); break;
  907. + case 7: lua_pushinteger(L, s->st_atime); break;
  908. + case 8: lua_pushinteger(L, s->st_mtime); break;
  909. + case 9: lua_pushinteger(L, s->st_ctime); break;
  910. + case 10:lua_pushstring(L, filetype(s->st_mode)); break;
  911. + }
  912. +}
  913. +
  914. +static const char *const Sstat[] =
  915. +{
  916. + "mode", "ino", "dev", "nlink", "uid", "gid",
  917. + "size", "atime", "mtime", "ctime", "type",
  918. + NULL
  919. +};
  920. +
  921. +static int Pstat(lua_State *L) /** stat(path,[options]) */
  922. +{
  923. + struct stat s;
  924. + const char *path=luaL_checkstring(L, 1);
  925. + if (lstat(path,&s)==-1) return pusherror(L, path);
  926. + return doselection(L, 2, Sstat, Fstat, &s);
  927. +}
  928. +
  929. +
  930. +static int Puname(lua_State *L) /** uname([string]) */
  931. +{
  932. + struct utsname u;
  933. + luaL_Buffer b;
  934. + const char *s;
  935. + if (uname(&u)==-1) return pusherror(L, NULL);
  936. + luaL_buffinit(L, &b);
  937. + for (s=luaL_optstring(L, 1, "%s %n %r %v %m"); *s; s++)
  938. + if (*s!='%')
  939. + luaL_putchar(&b, *s);
  940. + else switch (*++s)
  941. + {
  942. + case '%': luaL_putchar(&b, *s); break;
  943. + case 'm': luaL_addstring(&b,u.machine); break;
  944. + case 'n': luaL_addstring(&b,u.nodename); break;
  945. + case 'r': luaL_addstring(&b,u.release); break;
  946. + case 's': luaL_addstring(&b,u.sysname); break;
  947. + case 'v': luaL_addstring(&b,u.version); break;
  948. + default: badoption(L, 2, "format", *s); break;
  949. + }
  950. + luaL_pushresult(&b);
  951. + return 1;
  952. +}
  953. +
  954. +
  955. +static const int Kpathconf[] =
  956. +{
  957. + _PC_LINK_MAX, _PC_MAX_CANON, _PC_MAX_INPUT, _PC_NAME_MAX, _PC_PATH_MAX,
  958. + _PC_PIPE_BUF, _PC_CHOWN_RESTRICTED, _PC_NO_TRUNC, _PC_VDISABLE,
  959. + -1
  960. +};
  961. +
  962. +static void Fpathconf(lua_State *L, int i, const void *data)
  963. +{
  964. + const char *path=data;
  965. + lua_pushinteger(L, pathconf(path, Kpathconf[i]));
  966. +}
  967. +
  968. +static const char *const Spathconf[] =
  969. +{
  970. + "link_max", "max_canon", "max_input", "name_max", "path_max",
  971. + "pipe_buf", "chown_restricted", "no_trunc", "vdisable",
  972. + NULL
  973. +};
  974. +
  975. +static int Ppathconf(lua_State *L) /** pathconf([path,options]) */
  976. +{
  977. + const char *path = luaL_optstring(L, 1, ".");
  978. + return doselection(L, 2, Spathconf, Fpathconf, path);
  979. +}
  980. +
  981. +
  982. +static const int Ksysconf[] =
  983. +{
  984. + _SC_ARG_MAX, _SC_CHILD_MAX, _SC_CLK_TCK, _SC_NGROUPS_MAX, _SC_STREAM_MAX,
  985. + _SC_TZNAME_MAX, _SC_OPEN_MAX, _SC_JOB_CONTROL, _SC_SAVED_IDS, _SC_VERSION,
  986. + -1
  987. +};
  988. +
  989. +static void Fsysconf(lua_State *L, int i, const void *data)
  990. +{
  991. + lua_pushinteger(L, sysconf(Ksysconf[i]));
  992. +}
  993. +
  994. +static const char *const Ssysconf[] =
  995. +{
  996. + "arg_max", "child_max", "clk_tck", "ngroups_max", "stream_max",
  997. + "tzname_max", "open_max", "job_control", "saved_ids", "version",
  998. + NULL
  999. +};
  1000. +
  1001. +static int Psysconf(lua_State *L) /** sysconf([options]) */
  1002. +{
  1003. + return doselection(L, 1, Ssysconf, Fsysconf, NULL);
  1004. +}
  1005. +
  1006. +#if ENABLE_SYSLOG
  1007. +/* syslog funcs */
  1008. +static int Popenlog(lua_State *L) /** openlog(ident, [option], [facility]) */
  1009. +{
  1010. + const char *ident = luaL_checkstring(L, 1);
  1011. + int option = 0;
  1012. + int facility = luaL_optint(L, 3, LOG_USER);
  1013. + const char *s = luaL_optstring(L, 2, "");
  1014. + while (*s) {
  1015. + switch (*s) {
  1016. + case ' ': break;
  1017. + case 'c': option |= LOG_CONS; break;
  1018. + case 'n': option |= LOG_NDELAY; break;
  1019. + case 'e': option |= LOG_PERROR; break;
  1020. + case 'p': option |= LOG_PID; break;
  1021. + default: badoption(L, 2, "option", *s); break;
  1022. + }
  1023. + s++;
  1024. + }
  1025. + openlog(ident, option, facility);
  1026. + return 0;
  1027. +}
  1028. +
  1029. +
  1030. +static int Psyslog(lua_State *L) /** syslog(priority, message) */
  1031. +{
  1032. + int priority = luaL_checkint(L, 1);
  1033. + const char *msg = luaL_checkstring(L, 2);
  1034. + syslog(priority, "%s", msg);
  1035. + return 0;
  1036. +}
  1037. +
  1038. +
  1039. +static int Pcloselog(lua_State *L) /** closelog() */
  1040. +{
  1041. + closelog();
  1042. + return 0;
  1043. +}
  1044. +#endif
  1045. +
  1046. +/*
  1047. + * XXX: GNU and BSD handle the forward declaration of crypt() in different
  1048. + * and annoying ways (especially GNU). Declare it here just to make sure
  1049. + * that it's there
  1050. + */
  1051. +char *crypt(const char *, const char *);
  1052. +
  1053. +static int Pcrypt(lua_State *L)
  1054. +{
  1055. + const char *str, *salt;
  1056. + char *res;
  1057. +
  1058. + str = luaL_checkstring(L, 1);
  1059. + salt = luaL_checkstring(L, 2);
  1060. + if (strlen(salt) < 2)
  1061. + luaL_error(L, "not enough salt");
  1062. +
  1063. + res = crypt(str, salt);
  1064. + lua_pushstring(L, res);
  1065. +
  1066. + return 1;
  1067. +}
  1068. +
  1069. +static const luaL_reg R[] =
  1070. +{
  1071. + {"access", Paccess},
  1072. + {"basename", Pbasename},
  1073. + {"chdir", Pchdir},
  1074. + {"chmod", Pchmod},
  1075. + {"chown", Pchown},
  1076. + {"crypt", Pcrypt},
  1077. + {"ctermid", Pctermid},
  1078. + {"dirname", Pdirname},
  1079. + {"dir", Pdir},
  1080. + {"dup", Pdup},
  1081. + {"errno", Perrno},
  1082. + {"exec", Pexec},
  1083. + {"execp", Pexecp},
  1084. + {"fdopen", Pfdopen},
  1085. + {"fileno", Pfileno},
  1086. + {"files", Pfiles},
  1087. + {"fork", Pfork},
  1088. + {"getcwd", Pgetcwd},
  1089. + {"getenv", Pgetenv},
  1090. + {"getgroup", Pgetgroup},
  1091. + {"getlogin", Pgetlogin},
  1092. + {"getpasswd", Pgetpasswd},
  1093. + {"getpid", Pgetpid},
  1094. + {"glob", Pglob},
  1095. + {"hostid", Phostid},
  1096. + {"kill", Pkill},
  1097. + {"link", Plink},
  1098. + {"mkdir", Pmkdir},
  1099. + {"mkfifo", Pmkfifo},
  1100. + {"pathconf", Ppathconf},
  1101. + {"pipe", Ppipe},
  1102. + {"readlink", Preadlink},
  1103. + {"rmdir", Prmdir},
  1104. + {"rpoll", Ppoll},
  1105. + {"setenv", Psetenv},
  1106. + {"setpid", Psetpid},
  1107. + {"sleep", Psleep},
  1108. + {"stat", Pstat},
  1109. + {"sysconf", Psysconf},
  1110. + {"times", Ptimes},
  1111. + {"ttyname", Pttyname},
  1112. + {"unlink", Punlink},
  1113. + {"umask", Pumask},
  1114. + {"uname", Puname},
  1115. + {"utime", Putime},
  1116. + {"wait", Pwait},
  1117. +
  1118. +#if ENABLE_SYSLOG
  1119. + {"openlog", Popenlog},
  1120. + {"syslog", Psyslog},
  1121. + {"closelog", Pcloselog},
  1122. +#endif
  1123. +
  1124. + {NULL, NULL}
  1125. +};
  1126. +
  1127. +#define set_const(key, value) \
  1128. + lua_pushliteral(L, key); \
  1129. + lua_pushnumber(L, value); \
  1130. + lua_settable(L, -3)
  1131. +
  1132. +LUALIB_API int luaopen_posix (lua_State *L)
  1133. +{
  1134. + luaL_register(L,MYNAME,R);
  1135. + lua_pushliteral(L,"version"); /** version */
  1136. + lua_pushliteral(L,MYVERSION);
  1137. + lua_settable(L,-3);
  1138. +
  1139. +#if ENABLE_SYSLOG
  1140. + set_const("LOG_AUTH", LOG_AUTH);
  1141. + set_const("LOG_AUTHPRIV", LOG_AUTHPRIV);
  1142. + set_const("LOG_CRON", LOG_CRON);
  1143. + set_const("LOG_DAEMON", LOG_DAEMON);
  1144. + set_const("LOG_FTP", LOG_FTP);
  1145. + set_const("LOG_KERN", LOG_KERN);
  1146. + set_const("LOG_LOCAL0", LOG_LOCAL0);
  1147. + set_const("LOG_LOCAL1", LOG_LOCAL1);
  1148. + set_const("LOG_LOCAL2", LOG_LOCAL2);
  1149. + set_const("LOG_LOCAL3", LOG_LOCAL3);
  1150. + set_const("LOG_LOCAL4", LOG_LOCAL4);
  1151. + set_const("LOG_LOCAL5", LOG_LOCAL5);
  1152. + set_const("LOG_LOCAL6", LOG_LOCAL6);
  1153. + set_const("LOG_LOCAL7", LOG_LOCAL7);
  1154. + set_const("LOG_LPR", LOG_LPR);
  1155. + set_const("LOG_MAIL", LOG_MAIL);
  1156. + set_const("LOG_NEWS", LOG_NEWS);
  1157. + set_const("LOG_SYSLOG", LOG_SYSLOG);
  1158. + set_const("LOG_USER", LOG_USER);
  1159. + set_const("LOG_UUCP", LOG_UUCP);
  1160. +
  1161. + set_const("LOG_EMERG", LOG_EMERG);
  1162. + set_const("LOG_ALERT", LOG_ALERT);
  1163. + set_const("LOG_CRIT", LOG_CRIT);
  1164. + set_const("LOG_ERR", LOG_ERR);
  1165. + set_const("LOG_WARNING", LOG_WARNING);
  1166. + set_const("LOG_NOTICE", LOG_NOTICE);
  1167. + set_const("LOG_INFO", LOG_INFO);
  1168. + set_const("LOG_DEBUG", LOG_DEBUG);
  1169. +#endif
  1170. +
  1171. +
  1172. + return 1;
  1173. +}
  1174. +
  1175. +/*EOF*/
  1176. Index: lua-5.1.4/src/lualib.h
  1177. ===================================================================
  1178. --- lua-5.1.4.orig/src/lualib.h 2008-09-25 12:18:14.000000000 +0200
  1179. +++ lua-5.1.4/src/lualib.h 2008-09-25 12:18:53.000000000 +0200
  1180. @@ -39,6 +39,9 @@
  1181. #define LUA_LOADLIBNAME "package"
  1182. LUALIB_API int (luaopen_package) (lua_State *L);
  1183. +#define LUA_POSIXLIBNAME "posix"
  1184. +LUALIB_API int (luaopen_posix) (lua_State *L);
  1185. +
  1186. /* open all previous libraries */
  1187. LUALIB_API void (luaL_openlibs) (lua_State *L);
  1188. Index: lua-5.1.4/src/modemuncher.c
  1189. ===================================================================
  1190. --- /dev/null 1970-01-01 00:00:00.000000000 +0000
  1191. +++ lua-5.1.4/src/modemuncher.c 2008-09-25 12:16:29.000000000 +0200
  1192. @@ -0,0 +1,261 @@
  1193. +/*
  1194. + Mode Muncher -- modemuncher.c
  1195. + 961110 Claudio Terra
  1196. +
  1197. + munch vb
  1198. + [ME monchen, perh. influenced by MF mangier to eat --more at MANGER]
  1199. + :to chew with a crunching sound: eat with relish
  1200. + :to chew food with a crunching sound: eat food with relish
  1201. + --munch-er n
  1202. +
  1203. + The NeXT Digital Edition of Webster's Ninth New Collegiate Dictionary
  1204. + and Webster's Collegiate Thesaurus
  1205. +*/
  1206. +
  1207. +/* struct for rwx <-> POSIX constant lookup tables */
  1208. +struct modeLookup
  1209. +{
  1210. + char rwx;
  1211. + mode_t bits;
  1212. +};
  1213. +
  1214. +typedef struct modeLookup modeLookup;
  1215. +
  1216. +static modeLookup modesel[] =
  1217. +{
  1218. + /* RWX char Posix Constant */
  1219. + {'r', S_IRUSR},
  1220. + {'w', S_IWUSR},
  1221. + {'x', S_IXUSR},
  1222. +
  1223. + {'r', S_IRGRP},
  1224. + {'w', S_IWGRP},
  1225. + {'x', S_IXGRP},
  1226. +
  1227. + {'r', S_IROTH},
  1228. + {'w', S_IWOTH},
  1229. + {'x', S_IXOTH},
  1230. + {0, (mode_t)-1} /* do not delete this line */
  1231. +};
  1232. +
  1233. +
  1234. +
  1235. +static int rwxrwxrwx(mode_t *mode, const char *p)
  1236. +{
  1237. + int count;
  1238. + mode_t tmp_mode = *mode;
  1239. +
  1240. + tmp_mode &= ~(S_ISUID | S_ISGID); /* turn off suid and sgid flags */
  1241. + for (count=0; count<9; count ++)
  1242. + {
  1243. + if (*p == modesel[count].rwx) tmp_mode |= modesel[count].bits; /* set a bit */
  1244. + else if (*p == '-') tmp_mode &= ~modesel[count].bits; /* clear a bit */
  1245. + else if (*p=='s') switch(count)
  1246. + {
  1247. + case 2: /* turn on suid flag */
  1248. + tmp_mode |= S_ISUID | S_IXUSR;
  1249. + break;
  1250. +
  1251. + case 5: /* turn on sgid flag */
  1252. + tmp_mode |= S_ISGID | S_IXGRP;
  1253. + break;
  1254. +
  1255. + default:
  1256. + return -4; /* failed! -- bad rwxrwxrwx mode change */
  1257. + break;
  1258. + }
  1259. + p++;
  1260. + }
  1261. + *mode = tmp_mode;
  1262. + return 0;
  1263. +}
  1264. +
  1265. +static void modechopper(mode_t mode, char *p)
  1266. +{
  1267. + /* requires char p[10] */
  1268. + int count;
  1269. + char *pp;
  1270. +
  1271. + pp=p;
  1272. +
  1273. + for (count=0; count<9; count ++)
  1274. + {
  1275. + if (mode & modesel[count].bits) *p = modesel[count].rwx;
  1276. + else *p='-';
  1277. +
  1278. + p++;
  1279. + }
  1280. + *p=0; /* to finish the string */
  1281. +
  1282. + /* dealing with suid and sgid flags */
  1283. + if (mode & S_ISUID) pp[2] = (mode & S_IXUSR) ? 's' : 'S';
  1284. + if (mode & S_ISGID) pp[5] = (mode & S_IXGRP) ? 's' : 'S';
  1285. +
  1286. +}
  1287. +
  1288. +static int mode_munch(mode_t *mode, const char* p)
  1289. +{
  1290. +
  1291. + char op=0;
  1292. + mode_t affected_bits, ch_mode;
  1293. + int doneFlag = 0;
  1294. +#ifdef DEBUG
  1295. +char tmp[10];
  1296. +#endif
  1297. +
  1298. +#ifdef DEBUG
  1299. +modechopper(*mode, tmp);
  1300. +printf("modemuncher: got base mode = %s\n", tmp);
  1301. +#endif
  1302. +
  1303. + while (!doneFlag)
  1304. + {
  1305. + /* step 0 -- clear temporary variables */
  1306. + affected_bits=0;
  1307. + ch_mode=0;
  1308. +
  1309. + /* step 1 -- who's affected? */
  1310. +
  1311. +#ifdef DEBUG
  1312. +printf("modemuncher step 1\n");
  1313. +#endif
  1314. +
  1315. + /* mode string given in rwxrwxrwx format */
  1316. + if (*p== 'r' || *p == '-') return rwxrwxrwx(mode, p);
  1317. +
  1318. + /* mode string given in ugoa+-=rwx format */
  1319. + for ( ; ; p++)
  1320. + switch (*p)
  1321. + {
  1322. + case 'u':
  1323. + affected_bits |= 04700;
  1324. + break;
  1325. +
  1326. + case 'g':
  1327. + affected_bits |= 02070;
  1328. + break;
  1329. +
  1330. + case 'o':
  1331. + affected_bits |= 01007;
  1332. + break;
  1333. +
  1334. + case 'a':
  1335. + affected_bits |= 07777;
  1336. + break;
  1337. +
  1338. + /* ignore spaces */
  1339. + case ' ':
  1340. + break;
  1341. +
  1342. +
  1343. + default:
  1344. + goto no_more_affected;
  1345. + }
  1346. +
  1347. + no_more_affected:
  1348. + /* If none specified, affect all bits. */
  1349. + if (affected_bits == 0) affected_bits = 07777;
  1350. +
  1351. + /* step 2 -- how is it changed? */
  1352. +
  1353. +#ifdef DEBUG
  1354. +printf("modemuncher step 2 (*p='%c')\n", *p);
  1355. +#endif
  1356. +
  1357. + switch (*p)
  1358. + {
  1359. + case '+':
  1360. + case '-':
  1361. + case '=':
  1362. + op = *p;
  1363. + break;
  1364. +
  1365. + /* ignore spaces */
  1366. + case ' ':
  1367. + break;
  1368. +
  1369. + default:
  1370. + return -1; /* failed! -- bad operator */
  1371. + }
  1372. +
  1373. +
  1374. + /* step 3 -- what are the changes? */
  1375. +
  1376. +#ifdef DEBUG
  1377. +printf("modemuncher step 3\n");
  1378. +#endif
  1379. +
  1380. + for (p++ ; *p!=0 ; p++)
  1381. + switch (*p)
  1382. + {
  1383. + case 'r':
  1384. + ch_mode |= 00444;
  1385. + break;
  1386. +
  1387. + case 'w':
  1388. + ch_mode |= 00222;
  1389. + break;
  1390. +
  1391. + case 'x':
  1392. + ch_mode |= 00111;
  1393. + break;
  1394. +
  1395. + case 's':
  1396. + /* Set the setuid/gid bits if `u' or `g' is selected. */
  1397. + ch_mode |= 06000;
  1398. + break;
  1399. +
  1400. + /* ignore spaces */
  1401. + case ' ':
  1402. + break;
  1403. +
  1404. + default:
  1405. + goto specs_done;
  1406. + }
  1407. +
  1408. + specs_done:
  1409. + /* step 4 -- apply the changes */
  1410. +
  1411. +#ifdef DEBUG
  1412. + printf("modemuncher step 4\n");
  1413. +#endif
  1414. + if (*p != ',') doneFlag = 1;
  1415. + if (*p != 0 && *p != ' ' && *p != ',')
  1416. + {
  1417. +
  1418. +#ifdef DEBUG
  1419. +printf("modemuncher: comma error!\n");
  1420. +printf("modemuncher: doneflag = %u\n", doneFlag);
  1421. +#endif
  1422. + return -2; /* failed! -- bad mode change */
  1423. +
  1424. + }
  1425. + p++;
  1426. + /*if (!ch_mode) return -2;*/ /* failed! -- bad mode change */
  1427. + if (ch_mode) switch (op)
  1428. + {
  1429. + case '+':
  1430. + *mode = *mode |= ch_mode & affected_bits;
  1431. + break;
  1432. +
  1433. + case '-':
  1434. + *mode = *mode &= ~(ch_mode & affected_bits);
  1435. + break;
  1436. +
  1437. + case '=':
  1438. + *mode = ch_mode & affected_bits;
  1439. + break;
  1440. +
  1441. + default:
  1442. + return -3; /* failed! -- unknown error */
  1443. + }
  1444. + }
  1445. +#ifdef DEBUG
  1446. +modechopper(*mode, tmp);
  1447. +printf("modemuncher: returning mode = %s\n", tmp);
  1448. +#endif
  1449. +
  1450. + return 0; /* successful call */
  1451. +}
  1452. +
  1453. +