Merge from vendor branch LESS:
[dragonfly.git] / contrib / bsdinstaller-1.1.6 / ports / devel / lua50-posix / files / patch-lposix.c
1 --- lposix.c.orig       Thu Nov  6 03:23:48 2003
2 +++ lposix.c    Mon Apr  4 17:30:55 2005
3 @@ -9,6 +9,7 @@
4  #include <errno.h>
5  #include <fcntl.h>
6  #include <grp.h>
7 +#include <math.h>
8  #include <pwd.h>
9  #include <signal.h>
10  #include <stdio.h>
11 @@ -24,10 +25,10 @@
12  #include <utime.h>
13  
14  #define MYNAME         "posix"
15 -#define MYVERSION      MYNAME " library for " LUA_VERSION " / Nov 2003"
16 +#define MYVERSION      "2005.0327"
17  
18 -#include "lua.h"
19 -#include "lauxlib.h"
20 +#include "lua50/lua.h"
21 +#include "lua50/lauxlib.h"
22  
23  #ifndef MYBUFSIZ
24  #define MYBUFSIZ 512
25 @@ -112,6 +113,20 @@
26                 return pusherror(L, info);
27  }
28  
29 +/*
30 + * This function is adapted from liolib.c: push a FILE * onto the
31 + * Lua stack as a file object that Lua's file module understands.
32 + */
33 +static void pushfileptr(lua_State *L, FILE *f)
34 +{
35 +       FILE **pf;
36 +
37 +       pf = (FILE **)lua_newuserdata(L, sizeof(FILE *));
38 +       *pf = f;  
39 +       luaL_getmetatable(L, "FILE*");
40 +       lua_setmetatable(L, -2);
41 +}
42 +
43  static void badoption(lua_State *L, int i, const char *what, int option)
44  {
45         luaL_argerror(L, 2,
46 @@ -312,6 +327,33 @@
47  }
48  
49  
50 +static int Pmkstemp(lua_State *L)              /** mkstemp(template) */
51 +{
52 +       char *tpl;
53 +       int fd;
54 +       FILE *f;
55 +
56 +       if ((tpl = strdup(luaL_checkstring(L, 1))) == NULL) {
57 +               lua_pushnil(L);
58 +               lua_pushnumber(L, ENOMEM);
59 +               return(2);
60 +       }
61 +       fd = mkstemp(tpl);
62 +       f = fdopen(fd, "w+");
63 +       if (f == NULL) {
64 +               lua_pushnil(L);
65 +               lua_pushnumber(L, errno);
66 +               free(tpl);
67 +               return(1);
68 +       }
69 +
70 +       pushfileptr(L, f);
71 +       lua_pushstring(L, tpl);
72 +       free(tpl);
73 +       return(2);
74 +}
75 +
76 +
77  static int Pexec(lua_State *L)                 /** exec(path,[args]) */
78  {
79         const char *path = luaL_checkstring(L, 1);
80 @@ -355,6 +397,40 @@
81  }
82  
83  
84 +static int Pnanosleep(lua_State *L)            /** nanosleep(secs,[nanosecs]) */
85 +{
86 +       double sec, nsec;
87 +       struct timespec ts;
88 +
89 +       sec = lua_tonumber(L, 1);
90 +       nsec = lua_tonumber(L, 2);
91 +
92 +       /*
93 +        * Any fractional part of the seconds value should
94 +        * slide over into the nanoseconds value.
95 +        */
96 +       nsec += (sec - floor(sec)) * 1000000000.0;
97 +
98 +       /*
99 +        * Don't allow overflow.
100 +        */
101 +       if (sec > 1000000000.0) {
102 +               sec = 1000000000.0;
103 +       }
104 +       while (nsec > 1000000000.0) {
105 +               nsec -= 1000000000.0;
106 +               sec += 1.0;
107 +       }
108 +
109 +       ts.tv_sec = sec;
110 +       ts.tv_nsec = nsec;
111 +
112 +       lua_pushnumber(L, nanosleep(&ts, NULL));
113 +
114 +       return 1;
115 +}
116 +
117 +
118  static int Pputenv(lua_State *L)               /** putenv(string) */
119  {
120         size_t l;
121 @@ -777,6 +853,8 @@
122         {"link",                Plink},
123         {"mkdir",               Pmkdir},
124         {"mkfifo",              Pmkfifo},
125 +       {"mkstemp",             Pmkstemp},
126 +       {"nanosleep",           Pnanosleep},
127         {"pathconf",            Ppathconf},
128         {"putenv",              Pputenv},
129         {"readlink",            Preadlink},
130 @@ -809,4 +887,9 @@
131         lua_pushliteral(L,MYVERSION);
132         lua_settable(L,-3);
133         return 1;
134 +}
135 +
136 +LUALIB_API int luaopen_lposix (lua_State *L)
137 +{
138 +       return luaopen_posix(L);
139  }