/
grp.c
181 lines (155 loc) · 3.22 KB
/
grp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* POSIX library for Lua 5.1, 5.2 & 5.3.
* Copyright (C) 2013-2018 Gary V. Vaughan
* Copyright (C) 2010-2013 Reuben Thomas <rrt@sc3d.org>
* Copyright (C) 2008-2010 Natanael Copa <natanael.copa@gmail.com>
* Clean up and bug fixes by Leo Razoumov <slonik.az@gmail.com> 2006-10-11
* Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> 07 Apr 2006 23:17:49
* Based on original by Claudio Terra for Lua 3.x.
* With contributions by Roberto Ierusalimschy.
* With documentation from Steve Donovan 2012
*/
/***
Group Database Operations.
Query the system group database.
@see posix.pwd
@module posix.grp
*/
#include <grp.h>
#include "_helpers.c"
/***
Group record.
@table PosixGroup
@string gr_name name of group
@int gr_gid unique group id
@tfield list gr_mem a list of group members
*/
static int
pushgroup(lua_State *L, struct group *g)
{
if (!g)
return lua_pushnil(L), 1;
lua_createtable(L, 0, 3);
setintegerfield(g, gr_gid);
setstringfield(g, gr_name);
if (g->gr_mem)
{
int i;
lua_newtable(L);
for (i = 0; g->gr_mem[i] != NULL; i++)
{
lua_pushstring(L, g->gr_mem[i]);
lua_rawseti(L, -2, i + 1);
}
lua_setfield(L, -2, "gr_mem");
}
settypemetatable("PosixGroup");
return 1;
}
/***
Release group database resources.
@function endgrent
@see getgrent
*/
static int
Pendgrent(lua_State *L)
{
checknargs(L, 0);
endgrent();
return 0;
}
/***
Fetch next group.
@function getgrent
@treturn PosixGroup next group record
@see endgrent
@usage
local grp = require "posix.grp"
t = grp.getgrent ()
while t ~= nil do
process (t)
t = grp.getgrent ()
end
grp.endgrent ()
*/
static int
Pgetgrent(lua_State *L)
{
struct group *g;
checknargs(L, 0);
g = getgrent();
if (!g && errno == 0)
endgrent();
return pushgroup(L, g);
}
/***
Fetch group with given group id.
@function getgrgid
@int gid group id
@treturn PosixGroup group record for *gid*
@usage
local grp = require "posix.grp"
t = grp.getgrgid (0)
*/
static int
Pgetgrgid(lua_State *L)
{
gid_t gid = (gid_t) checkint(L, 1);
struct group *g;
checknargs(L, 1);
errno = 0; /* so we can recognise a successful empty result */
g = getgrgid(gid);
if (!g && errno != 0)
return pusherror(L, "getgrgid");
return pushgroup(L, g);
}
/***
Fetch named group.
@function getgrnam
@string name group name
@treturn PosixGroup group record for *name*
@usage
local grp = require "posix.grp"
t = grp.getgrnam "wheel"
*/
static int
Pgetgrnam(lua_State *L)
{
const char *name = luaL_checkstring(L, 1);
struct group *g;
checknargs(L, 1);
errno = 0; /* so we can recognise a successful empty result */
g = getgrnam (name);
if (!g && errno != 0)
return pusherror(L, "getgrnam");
return pushgroup(L, g);
}
/***
Rewind next @{getgrent} back to start of database.
@function setgrent
@see getgrent
*/
static int
Psetgrent(lua_State *L)
{
checknargs(L, 0);
setgrent();
return 0;
}
static const luaL_Reg posix_grp_fns[] =
{
LPOSIX_FUNC( Pendgrent ),
LPOSIX_FUNC( Pgetgrent ),
LPOSIX_FUNC( Pgetgrgid ),
LPOSIX_FUNC( Pgetgrnam ),
LPOSIX_FUNC( Psetgrent ),
{NULL, NULL}
};
LUALIB_API int
luaopen_posix_grp(lua_State *L)
{
luaL_register(L, "posix.grp", posix_grp_fns);
lua_pushstring(L, LPOSIX_VERSION_STRING("grp"));
lua_setfield(L, -2, "version");
return 1;
}