Skip to content

Instantly share code, notes, and snippets.

@xef6
Created September 4, 2013 02:36
Show Gist options
  • Save xef6/6432180 to your computer and use it in GitHub Desktop.
Save xef6/6432180 to your computer and use it in GitHub Desktop.
lua c lib thingy
#include <stdlib.h>
#include <sys/utsname.h>
#include <sys/time.h>
#include <string.h>
#define lnetlib_c /* Define the library */
/* Include the Lua API header files */
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
/* The 'uname' function.
** It accepts a Lua State (Don't worry about this),
** and doesn't return anything.
*/
static int net_uname (lua_State *L)
{
const char *option = luaL_checkstring(L, 1); /* Reads the string parameter */
int z;
struct utsname u_name; /* Creates an instance of the utsname structure */
/* Uses the C function 'uname' and stores the
** results in the structure we just created.
** You could add some error checking, by testing the value of z -
** But I cut this out, though you should add it.
*/
z = uname(&u_name);
/* Detects which option was passed to the function,
** and prints the correspnding uname response.
** Again, you should add error checking.
*/
if ((strcmp(option, "sysname")) == 0)
{
printf("sysname : %s", u_name.sysname);
}
else if ((strcmp(option, "nodename")) == 0)
{
printf("nodename : %s", u_name.nodename);
}
else if ((strcmp(option, "release")) == 0)
{
printf("release : %s", u_name.release);
}
else if ((strcmp(option, "version")) == 0)
{
printf("version : %s", u_name.version);
}
else if ((strcmp(option, "machine")) == 0)
{
printf("machine : %s", u_name.machine);
}
else if ((strcmp(option, "all")) == 0)
{
printf("sysname : %s\n", u_name.sysname);
printf("nodename : %s\n", u_name.nodename);
printf("release : %s\n", u_name.release);
printf("version : %s\n", u_name.version);
printf("machine : %s\n", u_name.machine);
}
return 0;
}
/* Get time of day as unix seconds and microseconds */
static int net_gettimeofday (lua_State *L)
{
int err;
struct timeval t_now;
err = gettimeofday( &t_now, NULL );
if( err == 0 ){
lua_pushnumber(L, t_now.tv_sec);
lua_pushnumber(L, t_now.tv_usec);
return 2;
} else {
return 0;
}
}
/* Get time of day as unix seconds + microseconds float */
static int net_time (lua_State *L)
{
int err;
struct timeval t_now;
err = gettimeofday( &t_now, NULL );
if( err == 0 ){
lua_pushnumber(L, t_now.tv_sec + t_now.tv_usec * 0.000001);
return 1;
} else {
return 0;
}
}
/* Now, this defines the functions you have created, into the net library group.
** The right hand name ('net_uname'), is the name of the function in the C code.
** The left hand name, is the name you want it to be called from Lua.
** Take note of the names of variables, they correspond to your 'net' library.
** If you changed the name of the library, you would have to change these
** variables and names.
*/
static const luaL_Reg netlib[] = {
{"uname", net_uname},
{"timeofday", net_gettimeofday},
{"time", net_time},
{NULL, NULL}
};
/* This defines a function that opens up your library. */
LUALIB_API int luaopen_net (lua_State *L) {
luaL_openlib(L, LUA_NETLIBNAME, netlib, 0);
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment