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
10 +LIBS= -lm -lcrypt $(MYLIBS)
15 lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o \
16 lundump.o lvm.o lzio.o lnum.o
17 LIB_O= lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o \
18 - lstrlib.o loadlib.o linit.o
19 + lstrlib.o loadlib.o linit.o lposix.o
23 Index: lua-5.1.4/src/linit.c
24 ===================================================================
25 --- lua-5.1.4.orig/src/linit.c 2008-09-25 12:19:02.000000000 +0200
26 +++ lua-5.1.4/src/linit.c 2008-09-25 12:19:32.000000000 +0200
28 {LUA_STRLIBNAME, luaopen_string},
29 {LUA_MATHLIBNAME, luaopen_math},
30 {LUA_DBLIBNAME, luaopen_debug},
31 + {LUA_POSIXLIBNAME, luaopen_posix},
35 Index: lua-5.1.4/src/lposix.c
36 ===================================================================
37 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
38 +++ lua-5.1.4/src/lposix.c 2008-09-25 12:16:29.000000000 +0200
42 +* POSIX library for Lua 5.1.
43 +* Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>
44 +* 07 Apr 2006 23:17:49
45 +* Clean up and bug fixes by Leo Razoumov <slonik.az@gmail.com> 2006-10-11 <!LR>
46 +* Based on original by Claudio Terra for Lua 3.x.
47 +* With contributions by Roberto Ierusalimschy.
50 +#include <sys/stat.h>
51 +#include <sys/times.h>
52 +#include <sys/types.h>
53 +#include <sys/utsname.h>
54 +#include <sys/wait.h>
74 +#define MYNAME "posix"
75 +#define MYVERSION MYNAME " library for " LUA_VERSION " / Jan 2008"
77 +#ifndef ENABLE_SYSLOG
78 +#define ENABLE_SYSLOG 1
85 +#include "modemuncher.c"
87 +/* compatibility with Lua 5.0 */
88 +#ifndef LUA_VERSION_NUM
89 +static int luaL_checkoption (lua_State *L, int narg, const char *def,
90 + const char *const lst[]) {
91 + const char *name = (def) ? luaL_optstring(L, narg, def) :
92 + luaL_checkstring(L, narg);
93 + int i = luaL_findstring(name, lst);
95 + luaL_argerror(L, narg, lua_pushfstring(L, "invalid option '%s'", name));
98 +#define lua_pushinteger lua_pushnumber
99 +#define lua_createtable(L,a,r) lua_newtable(L)
100 +#define LUA_FILEHANDLE "FILE*"
102 +#define lua_setfield(l,i,k)
103 +#define lua_getfield(l,i,k)
107 +static const struct { char c; mode_t b; } M[] =
109 + {'r', S_IRUSR}, {'w', S_IWUSR}, {'x', S_IXUSR},
110 + {'r', S_IRGRP}, {'w', S_IWGRP}, {'x', S_IXGRP},
111 + {'r', S_IROTH}, {'w', S_IWOTH}, {'x', S_IXOTH},
115 +static void pushmode(lua_State *L, mode_t mode)
119 + for (i=0; i<9; i++) m[i]= (mode & M[i].b) ? M[i].c : '-';
120 + if (mode & S_ISUID) m[2]= (mode & S_IXUSR) ? 's' : 'S';
121 + if (mode & S_ISGID) m[5]= (mode & S_IXGRP) ? 's' : 'S';
122 + lua_pushlstring(L, m, 9);
125 +typedef void (*Selector)(lua_State *L, int i, const void *data);
127 +static int doselection(lua_State *L, int i, int n,
128 + const char *const S[],
132 + if (lua_isnone(L, i) || lua_istable(L, i))
135 + if (lua_isnone(L, i)) lua_createtable(L,0,n); else lua_settop(L, i);
136 + for (j=0; S[j]!=NULL; j++)
138 + lua_pushstring(L, S[j]);
140 + lua_settable(L, -3);
146 + int k,n=lua_gettop(L);
147 + for (k=i; k<=n; k++)
149 + int j=luaL_checkoption(L, k, NULL, S);
156 +#define doselection(L,i,S,F,d) (doselection)(L,i,sizeof(S)/sizeof(*S)-1,S,F,d)
158 +static int pusherror(lua_State *L, const char *info)
162 + lua_pushstring(L, strerror(errno));
164 + lua_pushfstring(L, "%s: %s", info, strerror(errno));
165 + lua_pushinteger(L, errno);
169 +static int pushresult(lua_State *L, int i, const char *info)
171 + if (i==-1) return pusherror(L, info);
172 + lua_pushinteger(L, i);
176 +static void badoption(lua_State *L, int i, const char *what, int option)
178 + luaL_argerror(L, 2,
179 + lua_pushfstring(L, "unknown %s option '%c'", what, option));
182 +static uid_t mygetuid(lua_State *L, int i)
184 + if (lua_isnone(L, i))
186 + else if (lua_isnumber(L, i))
187 + return (uid_t) lua_tonumber(L, i);
188 + else if (lua_isstring(L, i))
190 + struct passwd *p=getpwnam(lua_tostring(L, i));
191 + return (p==NULL) ? -1 : p->pw_uid;
194 + return luaL_typerror(L, i, "string or number");
197 +static gid_t mygetgid(lua_State *L, int i)
199 + if (lua_isnone(L, i))
201 + else if (lua_isnumber(L, i))
202 + return (gid_t) lua_tonumber(L, i);
203 + else if (lua_isstring(L, i))
205 + struct group *g=getgrnam(lua_tostring(L, i));
206 + return (g==NULL) ? -1 : g->gr_gid;
209 + return luaL_typerror(L, i, "string or number");
213 +static int Perrno(lua_State *L) /** errno([n]) */
215 + int n = luaL_optint(L, 1, errno);
216 + lua_pushstring(L, strerror(n));
217 + lua_pushinteger(L, n);
222 +static int Pbasename(lua_State *L) /** basename(path) */
226 + const char *path = luaL_checklstring(L, 1, &len);
227 + if (len>=sizeof(b)) luaL_argerror(L, 1, "too long");
228 + lua_pushstring(L, basename(strcpy(b,path)));
233 +static int Pdirname(lua_State *L) /** dirname(path) */
237 + const char *path = luaL_checklstring(L, 1, &len);
238 + if (len>=sizeof(b)) luaL_argerror(L, 1, "too long");
239 + lua_pushstring(L, dirname(strcpy(b,path)));
244 +static int Pdir(lua_State *L) /** dir([path]) */
246 + const char *path = luaL_optstring(L, 1, ".");
247 + DIR *d = opendir(path);
249 + return pusherror(L, path);
253 + struct dirent *entry;
255 + for (i=1; (entry = readdir(d)) != NULL; i++)
257 + lua_pushstring(L, entry->d_name);
258 + lua_rawseti(L, -2, i);
261 + lua_pushinteger(L, i-1);
266 +static int Pglob(lua_State *L) /** glob(pattern) */
268 + const char *pattern = luaL_optstring(L, 1, "*");
271 + if (glob(pattern, 0, NULL, &globres))
272 + return pusherror(L, pattern);
277 + for (i=1; i<=globres.gl_pathc; i++) {
278 + lua_pushstring(L, globres.gl_pathv[i-1]);
279 + lua_rawseti(L, -2, i);
281 + globfree(&globres);
286 +static int aux_files(lua_State *L)
288 + DIR **p = (DIR **)lua_touserdata(L, lua_upvalueindex(1));
290 + struct dirent *entry;
291 + if (d == NULL) return 0;
292 + entry = readdir(d);
301 + lua_pushstring(L, entry->d_name);
306 +static int dir_gc (lua_State *L)
308 + DIR *d = *(DIR **)lua_touserdata(L, 1);
309 + if (d!=NULL) closedir(d);
313 +static int Pfiles(lua_State *L) /** files([path]) */
315 + const char *path = luaL_optstring(L, 1, ".");
316 + DIR **d = (DIR **)lua_newuserdata(L, sizeof(DIR *));
317 + if (luaL_newmetatable(L, MYNAME " dir handle"))
319 + lua_pushliteral(L, "__gc");
320 + lua_pushcfunction(L, dir_gc);
321 + lua_settable(L, -3);
323 + lua_setmetatable(L, -2);
324 + *d = opendir(path);
325 + if (*d == NULL) return pusherror(L, path);
326 + lua_pushcclosure(L, aux_files, 1);
331 +static int Pgetcwd(lua_State *L) /** getcwd() */
334 + if (getcwd(b, sizeof(b)) == NULL) return pusherror(L, ".");
335 + lua_pushstring(L, b);
340 +static int Pmkdir(lua_State *L) /** mkdir(path) */
342 + const char *path = luaL_checkstring(L, 1);
343 + return pushresult(L, mkdir(path, 0777), path);
347 +static int Pchdir(lua_State *L) /** chdir(path) */
349 + const char *path = luaL_checkstring(L, 1);
350 + return pushresult(L, chdir(path), path);
353 +static int Prmdir(lua_State *L) /** rmdir(path) */
355 + const char *path = luaL_checkstring(L, 1);
356 + return pushresult(L, rmdir(path), path);
360 +static int Punlink(lua_State *L) /** unlink(path) */
362 + const char *path = luaL_checkstring(L, 1);
363 + return pushresult(L, unlink(path), path);
366 +static int Plink(lua_State *L) /** link(old,new,[symbolic]) */
368 + const char *oldpath = luaL_checkstring(L, 1);
369 + const char *newpath = luaL_checkstring(L, 2);
370 + return pushresult(L,
371 + (lua_toboolean(L,3) ? symlink : link)(oldpath, newpath), NULL);
375 +static int Preadlink(lua_State *L) /** readlink(path) */
378 + const char *path = luaL_checkstring(L, 1);
379 + int n = readlink(path, b, sizeof(b));
380 + if (n==-1) return pusherror(L, path);
381 + lua_pushlstring(L, b, n);
386 +static int Paccess(lua_State *L) /** access(path,[mode]) */
389 + const char *path=luaL_checkstring(L, 1);
391 + for (s=luaL_optstring(L, 2, "f"); *s!=0 ; s++)
395 + case 'r': mode |= R_OK; break;
396 + case 'w': mode |= W_OK; break;
397 + case 'x': mode |= X_OK; break;
398 + case 'f': mode |= F_OK; break;
399 + default: badoption(L, 2, "mode", *s); break;
401 + return pushresult(L, access(path, mode), path);
405 +static int myfclose (lua_State *L) {
406 + FILE **p = (FILE **)lua_touserdata(L, 1);
407 + int rc = fclose(*p);
408 + if (rc == 0) *p = NULL;
409 + return pushresult(L, rc, NULL);
412 +static int pushfile (lua_State *L, int id, const char *mode) {
413 + FILE **f = (FILE **)lua_newuserdata(L, sizeof(FILE *));
415 + luaL_getmetatable(L, LUA_FILEHANDLE);
416 + lua_setmetatable(L, -2);
417 + lua_getfield(L, LUA_REGISTRYINDEX, "POSIX_PIPEFILE");
418 + if (lua_isnil(L, -1)) {
421 + lua_pushvalue(L, -1);
422 + lua_pushcfunction(L, myfclose);
423 + lua_setfield(L, -2, "__close");
424 + lua_setfield(L, LUA_REGISTRYINDEX, "POSIX_PIPEFILE");
426 + lua_setfenv(L, -2);
427 + *f = fdopen(id, mode);
428 + return (*f != NULL);
431 +static int Ppipe(lua_State *L) /** pipe() */
434 + if (pipe(fd)==-1) return pusherror(L, NULL);
435 + if (!pushfile(L, fd[0], "r") || !pushfile(L, fd[1], "w"))
436 + return pusherror(L, "pipe");
441 +static int Pfileno(lua_State *L) /** fileno(filehandle) */
443 + FILE *f = *(FILE**) luaL_checkudata(L, 1, LUA_FILEHANDLE);
444 + return pushresult(L, fileno(f), NULL);
448 +static int Pfdopen(lua_State *L) /** fdopen(fd, mode) */
450 + int fd = luaL_checkint(L, 1);
451 + const char *mode = luaL_checkstring(L, 2);
452 + if (!pushfile(L, fd, mode))
453 + return pusherror(L, "fdpoen");
458 +/* helper func for Pdup */
459 +static const char *filemode(int fd)
462 + int mode = fcntl(fd, F_GETFL);
465 + switch (mode & O_ACCMODE) {
466 + case O_RDONLY: m = "r"; break;
467 + case O_WRONLY: m = "w"; break;
468 + default: m = "rw"; break;
473 +static int Pdup(lua_State *L) /** dup(old,[new]) */
475 + FILE **oldf = (FILE**)luaL_checkudata(L, 1, LUA_FILEHANDLE);
476 + FILE **newf = (FILE **)lua_touserdata(L, 2);
478 + const char *msg = "dup2";
480 + if (newf == NULL) {
481 + fd = dup(fileno(*oldf));
485 + fd = dup2(fileno(*oldf), fileno(*newf));
488 + if ((fd < 0) || !pushfile(L, fd, filemode(fd)))
489 + return pusherror(L, msg);
494 +static int Pmkfifo(lua_State *L) /** mkfifo(path) */
496 + const char *path = luaL_checkstring(L, 1);
497 + return pushresult(L, mkfifo(path, 0777), path);
501 +static int runexec(lua_State *L, int use_shell)
503 + const char *path = luaL_checkstring(L, 1);
504 + int i,n=lua_gettop(L);
505 + char **argv = lua_newuserdata(L,(n+1)*sizeof(char*));
506 + argv[0] = (char*)path;
507 + for (i=1; i<n; i++) argv[i] = (char*)luaL_checkstring(L, i+1);
510 + execvp(path, argv);
514 + return pusherror(L, path);
518 +static int Pexec(lua_State *L) /** exec(path,[args]) */
520 + return runexec(L, 0);
524 +static int Pexecp(lua_State *L) /** execp(path,[args]) */
526 + return runexec(L, 1);
530 +static int Pfork(lua_State *L) /** fork() */
532 + return pushresult(L, fork(), NULL);
535 +/* from http://lua-users.org/lists/lua-l/2007-11/msg00346.html */
536 +static int Ppoll(lua_State *L) /** poll(filehandle, timeout) */
539 + FILE* file = *(FILE**)luaL_checkudata(L,1,LUA_FILEHANDLE);
540 + int timeout = luaL_checkint(L,2);
541 + fds.fd = fileno(file);
542 + fds.events = POLLIN;
543 + return pushresult(L, poll(&fds,1,timeout), NULL);
546 +static int Pwait(lua_State *L) /** wait([pid]) */
549 + pid_t pid = luaL_optint(L, 1, -1);
550 + pid = waitpid(pid, &status, 0);
551 + if (pid == -1) return pusherror(L, NULL);
552 + lua_pushinteger(L, pid);
553 + if (WIFEXITED(status))
555 + lua_pushliteral(L,"exited");
556 + lua_pushinteger(L, WEXITSTATUS(status));
559 + else if (WIFSIGNALED(status))
561 + lua_pushliteral(L,"killed");
562 + lua_pushinteger(L, WTERMSIG(status));
565 + else if (WIFSTOPPED(status))
567 + lua_pushliteral(L,"stopped");
568 + lua_pushinteger(L, WSTOPSIG(status));
575 +static int Pkill(lua_State *L) /** kill(pid,[sig]) */
577 + pid_t pid = luaL_checkint(L, 1);
578 + int sig = luaL_optint(L, 2, SIGTERM);
579 + return pushresult(L, kill(pid, sig), NULL);
582 +static int Psetpid(lua_State *L) /** setpid(option,...) */
584 + const char *what=luaL_checkstring(L, 1);
588 + return pushresult(L, seteuid(mygetuid(L, 2)), NULL);
590 + return pushresult(L, setuid(mygetuid(L, 2)), NULL);
592 + return pushresult(L, setegid(mygetgid(L, 2)), NULL);
594 + return pushresult(L, setgid(mygetgid(L, 2)), NULL);
596 + return pushresult(L, setsid(), NULL);
599 + pid_t pid = luaL_checkint(L, 2);
600 + pid_t pgid = luaL_checkint(L, 3);
601 + return pushresult(L, setpgid(pid,pgid), NULL);
604 + badoption(L, 2, "id", *what);
610 +static int Psleep(lua_State *L) /** sleep(seconds) */
612 + unsigned int seconds = luaL_checkint(L, 1);
613 + lua_pushinteger(L, sleep(seconds));
618 +static int Psetenv(lua_State *L) /** setenv(name,value,[over]) */
620 + const char *name=luaL_checkstring(L, 1);
621 + const char *value=luaL_optstring(L, 2, NULL);
625 + return pushresult(L, 0, NULL);
629 + int overwrite=lua_isnoneornil(L, 3) || lua_toboolean(L, 3);
630 + return pushresult(L, setenv(name,value,overwrite), NULL);
635 +static int Pgetenv(lua_State *L) /** getenv([name]) */
637 + if (lua_isnone(L, 1))
639 + extern char **environ;
642 + for (e=environ; *e!=NULL; e++)
645 + char *eq=strchr(s, '=');
646 + if (eq==NULL) /* will this ever happen? */
648 + lua_pushstring(L,s);
649 + lua_pushboolean(L,1);
653 + lua_pushlstring(L,s,eq-s);
654 + lua_pushstring(L,eq+1);
656 + lua_settable(L,-3);
660 + lua_pushstring(L, getenv(luaL_checkstring(L, 1)));
664 +static int Pumask(lua_State *L) /** umask([mode]) */
665 +{/* <!LR> from old lposix-5.0 version */
668 + umask(mode=umask(0));
670 + if (!lua_isnone(L, 1))
672 + if (mode_munch(&mode, luaL_checkstring(L, 1)))
680 + modechopper(mode, m);
681 + lua_pushstring(L, m);
686 +static int Pchmod(lua_State *L) /** chmod(path,mode) */
690 + const char *path = luaL_checkstring(L, 1);
691 + const char *modestr = luaL_checkstring(L, 2);
692 + if (stat(path, &s)) return pusherror(L, path);
694 + if (mode_munch(&mode, modestr)) luaL_argerror(L, 2, "bad mode");
695 + return pushresult(L, chmod(path, mode), path);
699 +static int Pchown(lua_State *L) /** chown(path,uid,gid) */
701 + const char *path = luaL_checkstring(L, 1);
702 + uid_t uid = mygetuid(L, 2);
703 + gid_t gid = mygetgid(L, 3);
704 + return pushresult(L, chown(path, uid, gid), path);
708 +static int Putime(lua_State *L) /** utime(path,[mtime,atime]) */
710 + struct utimbuf times;
711 + time_t currtime = time(NULL);
712 + const char *path = luaL_checkstring(L, 1);
713 + times.modtime = luaL_optnumber(L, 2, currtime);
714 + times.actime = luaL_optnumber(L, 3, currtime);
715 + return pushresult(L, utime(path, ×), path);
719 +static void FgetID(lua_State *L, int i, const void *data)
723 + case 0: lua_pushinteger(L, getegid()); break;
724 + case 1: lua_pushinteger(L, geteuid()); break;
725 + case 2: lua_pushinteger(L, getgid()); break;
726 + case 3: lua_pushinteger(L, getuid()); break;
727 + case 4: lua_pushinteger(L, getpgrp()); break;
728 + case 5: lua_pushinteger(L, getpid()); break;
729 + case 6: lua_pushinteger(L, getppid()); break;
733 +static const char *const SgetID[] =
735 + "egid", "euid", "gid", "uid", "pgrp", "pid", "ppid", NULL
738 +static int Pgetpid(lua_State *L) /** getpid([options]) */
740 + return doselection(L, 1, SgetID, FgetID, NULL);
744 +static int Phostid(lua_State *L) /** hostid() */
747 + sprintf(b,"%ld",gethostid());
748 + lua_pushstring(L, b);
753 +static int Pttyname(lua_State *L) /** ttyname([fd]) */
755 + int fd=luaL_optint(L, 1, 0);
756 + lua_pushstring(L, ttyname(fd));
761 +static int Pctermid(lua_State *L) /** ctermid() */
764 + lua_pushstring(L, ctermid(b));
769 +static int Pgetlogin(lua_State *L) /** getlogin() */
771 + lua_pushstring(L, getlogin());
776 +static void Fgetpasswd(lua_State *L, int i, const void *data)
778 + const struct passwd *p=data;
781 + case 0: lua_pushstring(L, p->pw_name); break;
782 + case 1: lua_pushinteger(L, p->pw_uid); break;
783 + case 2: lua_pushinteger(L, p->pw_gid); break;
784 + case 3: lua_pushstring(L, p->pw_dir); break;
785 + case 4: lua_pushstring(L, p->pw_shell); break;
786 +/* not strictly POSIX */
787 + case 5: lua_pushstring(L, p->pw_gecos); break;
788 + case 6: lua_pushstring(L, p->pw_passwd); break;
792 +static const char *const Sgetpasswd[] =
794 + "name", "uid", "gid", "dir", "shell", "gecos", "passwd", NULL
798 +static int Pgetpasswd(lua_State *L) /** getpasswd(name|id,[sel]) */
800 + struct passwd *p=NULL;
801 + if (lua_isnoneornil(L, 1))
802 + p = getpwuid(geteuid());
803 + else if (lua_isnumber(L, 1))
804 + p = getpwuid((uid_t)lua_tonumber(L, 1));
805 + else if (lua_isstring(L, 1))
806 + p = getpwnam(lua_tostring(L, 1));
808 + luaL_typerror(L, 1, "string or number");
812 + return doselection(L, 2, Sgetpasswd, Fgetpasswd, p);
817 +static int Pgetgroup(lua_State *L) /** getgroup(name|id) */
819 + struct group *g=NULL;
820 + if (lua_isnumber(L, 1))
821 + g = getgrgid((gid_t)lua_tonumber(L, 1));
822 + else if (lua_isstring(L, 1))
823 + g = getgrnam(lua_tostring(L, 1));
825 + luaL_typerror(L, 1, "string or number");
832 + lua_pushliteral(L, "name");
833 + lua_pushstring(L, g->gr_name);
834 + lua_settable(L, -3);
835 + lua_pushliteral(L, "gid");
836 + lua_pushinteger(L, g->gr_gid);
837 + lua_settable(L, -3);
838 + for (i=0; g->gr_mem[i]!=NULL; i++)
840 + lua_pushstring(L, g->gr_mem[i]);
841 + lua_rawseti(L, -2, i);
854 +/* #define pushtime(L,x) lua_pushnumber(L,((lua_Number)x)/CLOCKS_PER_SEC) */
855 +#define pushtime(L,x) lua_pushnumber(L, ((lua_Number)x)/clk_tck)
857 +static void Ftimes(lua_State *L, int i, const void *data)
859 + static long clk_tck = 0;
860 + const struct mytimes *t=data;
862 + if( !clk_tck){ clk_tck= sysconf(_SC_CLK_TCK);}
865 + case 0: pushtime(L, t->t.tms_utime); break;
866 + case 1: pushtime(L, t->t.tms_stime); break;
867 + case 2: pushtime(L, t->t.tms_cutime); break;
868 + case 3: pushtime(L, t->t.tms_cstime); break;
869 + case 4: pushtime(L, t->elapsed); break;
873 +static const char *const Stimes[] =
875 + "utime", "stime", "cutime", "cstime", "elapsed", NULL
878 +static int Ptimes(lua_State *L) /** times([options]) */
881 + t.elapsed = times(&t.t);
882 + return doselection(L, 1, Stimes, Ftimes, &t);
886 +static const char *filetype(mode_t m)
888 + if (S_ISREG(m)) return "regular";
889 + else if (S_ISLNK(m)) return "link";
890 + else if (S_ISDIR(m)) return "directory";
891 + else if (S_ISCHR(m)) return "character device";
892 + else if (S_ISBLK(m)) return "block device";
893 + else if (S_ISFIFO(m)) return "fifo";
894 + else if (S_ISSOCK(m)) return "socket";
898 +static void Fstat(lua_State *L, int i, const void *data)
900 + const struct stat *s=data;
903 + case 0: pushmode(L, s->st_mode); break;
904 + case 1: lua_pushinteger(L, s->st_ino); break;
905 + case 2: lua_pushinteger(L, s->st_dev); break;
906 + case 3: lua_pushinteger(L, s->st_nlink); break;
907 + case 4: lua_pushinteger(L, s->st_uid); break;
908 + case 5: lua_pushinteger(L, s->st_gid); break;
909 + case 6: lua_pushinteger(L, s->st_size); break;
910 + case 7: lua_pushinteger(L, s->st_atime); break;
911 + case 8: lua_pushinteger(L, s->st_mtime); break;
912 + case 9: lua_pushinteger(L, s->st_ctime); break;
913 + case 10:lua_pushstring(L, filetype(s->st_mode)); break;
917 +static const char *const Sstat[] =
919 + "mode", "ino", "dev", "nlink", "uid", "gid",
920 + "size", "atime", "mtime", "ctime", "type",
924 +static int Pstat(lua_State *L) /** stat(path,[options]) */
927 + const char *path=luaL_checkstring(L, 1);
928 + if (lstat(path,&s)==-1) return pusherror(L, path);
929 + return doselection(L, 2, Sstat, Fstat, &s);
933 +static int Puname(lua_State *L) /** uname([string]) */
938 + if (uname(&u)==-1) return pusherror(L, NULL);
939 + luaL_buffinit(L, &b);
940 + for (s=luaL_optstring(L, 1, "%s %n %r %v %m"); *s; s++)
942 + luaL_putchar(&b, *s);
945 + case '%': luaL_putchar(&b, *s); break;
946 + case 'm': luaL_addstring(&b,u.machine); break;
947 + case 'n': luaL_addstring(&b,u.nodename); break;
948 + case 'r': luaL_addstring(&b,u.release); break;
949 + case 's': luaL_addstring(&b,u.sysname); break;
950 + case 'v': luaL_addstring(&b,u.version); break;
951 + default: badoption(L, 2, "format", *s); break;
953 + luaL_pushresult(&b);
958 +static const int Kpathconf[] =
960 + _PC_LINK_MAX, _PC_MAX_CANON, _PC_MAX_INPUT, _PC_NAME_MAX, _PC_PATH_MAX,
961 + _PC_PIPE_BUF, _PC_CHOWN_RESTRICTED, _PC_NO_TRUNC, _PC_VDISABLE,
965 +static void Fpathconf(lua_State *L, int i, const void *data)
967 + const char *path=data;
968 + lua_pushinteger(L, pathconf(path, Kpathconf[i]));
971 +static const char *const Spathconf[] =
973 + "link_max", "max_canon", "max_input", "name_max", "path_max",
974 + "pipe_buf", "chown_restricted", "no_trunc", "vdisable",
978 +static int Ppathconf(lua_State *L) /** pathconf([path,options]) */
980 + const char *path = luaL_optstring(L, 1, ".");
981 + return doselection(L, 2, Spathconf, Fpathconf, path);
985 +static const int Ksysconf[] =
987 + _SC_ARG_MAX, _SC_CHILD_MAX, _SC_CLK_TCK, _SC_NGROUPS_MAX, _SC_STREAM_MAX,
988 + _SC_TZNAME_MAX, _SC_OPEN_MAX, _SC_JOB_CONTROL, _SC_SAVED_IDS, _SC_VERSION,
992 +static void Fsysconf(lua_State *L, int i, const void *data)
994 + lua_pushinteger(L, sysconf(Ksysconf[i]));
997 +static const char *const Ssysconf[] =
999 + "arg_max", "child_max", "clk_tck", "ngroups_max", "stream_max",
1000 + "tzname_max", "open_max", "job_control", "saved_ids", "version",
1004 +static int Psysconf(lua_State *L) /** sysconf([options]) */
1006 + return doselection(L, 1, Ssysconf, Fsysconf, NULL);
1011 +static int Popenlog(lua_State *L) /** openlog(ident, [option], [facility]) */
1013 + const char *ident = luaL_checkstring(L, 1);
1015 + int facility = luaL_optint(L, 3, LOG_USER);
1016 + const char *s = luaL_optstring(L, 2, "");
1020 + case 'c': option |= LOG_CONS; break;
1021 + case 'n': option |= LOG_NDELAY; break;
1022 + case 'e': option |= LOG_PERROR; break;
1023 + case 'p': option |= LOG_PID; break;
1024 + default: badoption(L, 2, "option", *s); break;
1028 + openlog(ident, option, facility);
1033 +static int Psyslog(lua_State *L) /** syslog(priority, message) */
1035 + int priority = luaL_checkint(L, 1);
1036 + const char *msg = luaL_checkstring(L, 2);
1037 + syslog(priority, "%s", msg);
1042 +static int Pcloselog(lua_State *L) /** closelog() */
1050 + * XXX: GNU and BSD handle the forward declaration of crypt() in different
1051 + * and annoying ways (especially GNU). Declare it here just to make sure
1054 +char *crypt(const char *, const char *);
1056 +static int Pcrypt(lua_State *L)
1058 + const char *str, *salt;
1061 + str = luaL_checkstring(L, 1);
1062 + salt = luaL_checkstring(L, 2);
1063 + if (strlen(salt) < 2)
1064 + luaL_error(L, "not enough salt");
1066 + res = crypt(str, salt);
1067 + lua_pushstring(L, res);
1072 +static const luaL_reg R[] =
1074 + {"access", Paccess},
1075 + {"basename", Pbasename},
1076 + {"chdir", Pchdir},
1077 + {"chmod", Pchmod},
1078 + {"chown", Pchown},
1079 + {"crypt", Pcrypt},
1080 + {"ctermid", Pctermid},
1081 + {"dirname", Pdirname},
1084 + {"errno", Perrno},
1086 + {"execp", Pexecp},
1087 + {"fdopen", Pfdopen},
1088 + {"fileno", Pfileno},
1089 + {"files", Pfiles},
1091 + {"getcwd", Pgetcwd},
1092 + {"getenv", Pgetenv},
1093 + {"getgroup", Pgetgroup},
1094 + {"getlogin", Pgetlogin},
1095 + {"getpasswd", Pgetpasswd},
1096 + {"getpid", Pgetpid},
1098 + {"hostid", Phostid},
1101 + {"mkdir", Pmkdir},
1102 + {"mkfifo", Pmkfifo},
1103 + {"pathconf", Ppathconf},
1105 + {"readlink", Preadlink},
1106 + {"rmdir", Prmdir},
1108 + {"setenv", Psetenv},
1109 + {"setpid", Psetpid},
1110 + {"sleep", Psleep},
1112 + {"sysconf", Psysconf},
1113 + {"times", Ptimes},
1114 + {"ttyname", Pttyname},
1115 + {"unlink", Punlink},
1116 + {"umask", Pumask},
1117 + {"uname", Puname},
1118 + {"utime", Putime},
1122 + {"openlog", Popenlog},
1123 + {"syslog", Psyslog},
1124 + {"closelog", Pcloselog},
1130 +#define set_const(key, value) \
1131 + lua_pushliteral(L, key); \
1132 + lua_pushnumber(L, value); \
1133 + lua_settable(L, -3)
1135 +LUALIB_API int luaopen_posix (lua_State *L)
1137 + luaL_register(L,MYNAME,R);
1138 + lua_pushliteral(L,"version"); /** version */
1139 + lua_pushliteral(L,MYVERSION);
1140 + lua_settable(L,-3);
1143 + set_const("LOG_AUTH", LOG_AUTH);
1144 + set_const("LOG_AUTHPRIV", LOG_AUTHPRIV);
1145 + set_const("LOG_CRON", LOG_CRON);
1146 + set_const("LOG_DAEMON", LOG_DAEMON);
1147 + set_const("LOG_FTP", LOG_FTP);
1148 + set_const("LOG_KERN", LOG_KERN);
1149 + set_const("LOG_LOCAL0", LOG_LOCAL0);
1150 + set_const("LOG_LOCAL1", LOG_LOCAL1);
1151 + set_const("LOG_LOCAL2", LOG_LOCAL2);
1152 + set_const("LOG_LOCAL3", LOG_LOCAL3);
1153 + set_const("LOG_LOCAL4", LOG_LOCAL4);
1154 + set_const("LOG_LOCAL5", LOG_LOCAL5);
1155 + set_const("LOG_LOCAL6", LOG_LOCAL6);
1156 + set_const("LOG_LOCAL7", LOG_LOCAL7);
1157 + set_const("LOG_LPR", LOG_LPR);
1158 + set_const("LOG_MAIL", LOG_MAIL);
1159 + set_const("LOG_NEWS", LOG_NEWS);
1160 + set_const("LOG_SYSLOG", LOG_SYSLOG);
1161 + set_const("LOG_USER", LOG_USER);
1162 + set_const("LOG_UUCP", LOG_UUCP);
1164 + set_const("LOG_EMERG", LOG_EMERG);
1165 + set_const("LOG_ALERT", LOG_ALERT);
1166 + set_const("LOG_CRIT", LOG_CRIT);
1167 + set_const("LOG_ERR", LOG_ERR);
1168 + set_const("LOG_WARNING", LOG_WARNING);
1169 + set_const("LOG_NOTICE", LOG_NOTICE);
1170 + set_const("LOG_INFO", LOG_INFO);
1171 + set_const("LOG_DEBUG", LOG_DEBUG);
1179 Index: lua-5.1.4/src/lualib.h
1180 ===================================================================
1181 --- lua-5.1.4.orig/src/lualib.h 2008-09-25 12:18:14.000000000 +0200
1182 +++ lua-5.1.4/src/lualib.h 2008-09-25 12:18:53.000000000 +0200
1184 #define LUA_LOADLIBNAME "package"
1185 LUALIB_API int (luaopen_package) (lua_State *L);
1187 +#define LUA_POSIXLIBNAME "posix"
1188 +LUALIB_API int (luaopen_posix) (lua_State *L);
1191 /* open all previous libraries */
1192 LUALIB_API void (luaL_openlibs) (lua_State *L);
1193 Index: lua-5.1.4/src/modemuncher.c
1194 ===================================================================
1195 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1196 +++ lua-5.1.4/src/modemuncher.c 2008-09-25 12:16:29.000000000 +0200
1199 + Mode Muncher -- modemuncher.c
1200 + 961110 Claudio Terra
1203 + [ME monchen, perh. influenced by MF mangier to eat --more at MANGER]
1204 + :to chew with a crunching sound: eat with relish
1205 + :to chew food with a crunching sound: eat food with relish
1208 + The NeXT Digital Edition of Webster's Ninth New Collegiate Dictionary
1209 + and Webster's Collegiate Thesaurus
1212 +/* struct for rwx <-> POSIX constant lookup tables */
1219 +typedef struct modeLookup modeLookup;
1221 +static modeLookup modesel[] =
1223 + /* RWX char Posix Constant */
1235 + {0, (mode_t)-1} /* do not delete this line */
1240 +static int rwxrwxrwx(mode_t *mode, const char *p)
1243 + mode_t tmp_mode = *mode;
1245 + tmp_mode &= ~(S_ISUID | S_ISGID); /* turn off suid and sgid flags */
1246 + for (count=0; count<9; count ++)
1248 + if (*p == modesel[count].rwx) tmp_mode |= modesel[count].bits; /* set a bit */
1249 + else if (*p == '-') tmp_mode &= ~modesel[count].bits; /* clear a bit */
1250 + else if (*p=='s') switch(count)
1252 + case 2: /* turn on suid flag */
1253 + tmp_mode |= S_ISUID | S_IXUSR;
1256 + case 5: /* turn on sgid flag */
1257 + tmp_mode |= S_ISGID | S_IXGRP;
1261 + return -4; /* failed! -- bad rwxrwxrwx mode change */
1270 +static void modechopper(mode_t mode, char *p)
1272 + /* requires char p[10] */
1278 + for (count=0; count<9; count ++)
1280 + if (mode & modesel[count].bits) *p = modesel[count].rwx;
1285 + *p=0; /* to finish the string */
1287 + /* dealing with suid and sgid flags */
1288 + if (mode & S_ISUID) pp[2] = (mode & S_IXUSR) ? 's' : 'S';
1289 + if (mode & S_ISGID) pp[5] = (mode & S_IXGRP) ? 's' : 'S';
1293 +static int mode_munch(mode_t *mode, const char* p)
1297 + mode_t affected_bits, ch_mode;
1304 +modechopper(*mode, tmp);
1305 +printf("modemuncher: got base mode = %s\n", tmp);
1310 + /* step 0 -- clear temporary variables */
1314 + /* step 1 -- who's affected? */
1317 +printf("modemuncher step 1\n");
1320 + /* mode string given in rwxrwxrwx format */
1321 + if (*p== 'r' || *p == '-') return rwxrwxrwx(mode, p);
1323 + /* mode string given in ugoa+-=rwx format */
1328 + affected_bits |= 04700;
1332 + affected_bits |= 02070;
1336 + affected_bits |= 01007;
1340 + affected_bits |= 07777;
1343 + /* ignore spaces */
1349 + goto no_more_affected;
1353 + /* If none specified, affect all bits. */
1354 + if (affected_bits == 0) affected_bits = 07777;
1356 + /* step 2 -- how is it changed? */
1359 +printf("modemuncher step 2 (*p='%c')\n", *p);
1370 + /* ignore spaces */
1375 + return -1; /* failed! -- bad operator */
1379 + /* step 3 -- what are the changes? */
1382 +printf("modemuncher step 3\n");
1385 + for (p++ ; *p!=0 ; p++)
1401 + /* Set the setuid/gid bits if `u' or `g' is selected. */
1405 + /* ignore spaces */
1414 + /* step 4 -- apply the changes */
1417 + printf("modemuncher step 4\n");
1419 + if (*p != ',') doneFlag = 1;
1420 + if (*p != 0 && *p != ' ' && *p != ',')
1424 +printf("modemuncher: comma error!\n");
1425 +printf("modemuncher: doneflag = %u\n", doneFlag);
1427 + return -2; /* failed! -- bad mode change */
1431 + /*if (!ch_mode) return -2;*/ /* failed! -- bad mode change */
1432 + if (ch_mode) switch (op)
1435 + *mode = *mode |= ch_mode & affected_bits;
1439 + *mode = *mode &= ~(ch_mode & affected_bits);
1443 + *mode = ch_mode & affected_bits;
1447 + return -3; /* failed! -- unknown error */
1451 +modechopper(*mode, tmp);
1452 +printf("modemuncher: returning mode = %s\n", tmp);
1455 + return 0; /* successful call */