static int Paccess(lua_State *L) /** access(path,[mode]) */ { int mode=F_OK; const char *path=luaL_checkstring(L, 1); const char *s; for (s=luaL_optstring(L, 2, "f"); *s!=0 ; s++) switch (*s) { case ' ': break; case 'r': mode |= R_OK; break; case 'w': mode |= W_OK; break; case 'x': mode |= X_OK; break; case 'f': mode |= F_OK; break; default: badoption(L, 2, "mode", *s); break; } return pushresult(L, access(path, mode), path); }
/* syslog funcs */ static int Popenlog(lua_State *L) /** openlog(ident, [option], [facility]) */ { const char *ident = luaL_checkstring(L, 1); int option = 0; int facility = luaL_optint(L, 3, LOG_USER); const char *s = luaL_optstring(L, 2, ""); while (*s) { switch (*s) { case ' ': break; case 'c': option |= LOG_CONS; break; case 'n': option |= LOG_NDELAY; break; #ifdef LOG_PERROR case 'e': option |= LOG_PERROR; break; #endif case 'p': option |= LOG_PID; break; default: badoption(L, 2, "option", *s); break; } s++; } openlog(ident, option, facility); return 0; }
static int Puname(lua_State *L) /** uname([string]) */ { struct utsname u; luaL_Buffer b; const char *s; if (uname(&u) == -1) return pusherror(L, NULL); luaL_buffinit(L, &b); for (s=luaL_optstring(L, 1, "%s %n %r %v %m"); *s; s++) if (*s!='%') luaL_putchar(&b, *s); else switch (*++s) { case '%': luaL_putchar(&b, *s); break; case 'm': luaL_addstring(&b,u.machine); break; case 'n': luaL_addstring(&b,u.nodename); break; case 'r': luaL_addstring(&b,u.release); break; case 's': luaL_addstring(&b,u.sysname); break; case 'v': luaL_addstring(&b,u.version); break; default: badoption(L, 2, "format", *s); break; } luaL_pushresult(&b); return 1; }