-
Notifications
You must be signed in to change notification settings - Fork 0
/
grp.cc
71 lines (53 loc) · 1.87 KB
/
grp.cc
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
// grp.cc
#include <node.h>
#include <sys/types.h>
#include <grp.h>
namespace grp {
using namespace v8;
Local<Object> mkgroup(Isolate* isolate, const struct group* grp) {
Local<Object> obj = Object::New(isolate);
obj->Set(String::NewFromUtf8(isolate, "gr_name"), String::NewFromUtf8(isolate, grp->gr_name));
//obj->Set(String::NewFromUtf8(isolate, "gr_passwd"), String::NewFromUtf8(isolate, grp->gr_passwd));
obj->Set(String::NewFromUtf8(isolate, "gr_gid"), Number::New(isolate, grp->gr_gid));
// TODO return member
// grp->gr_mem
return obj;
}
void Getgrgid(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
if (args.Length() < 1) {
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Wrong number of arguments")));
return;
}
if (!args[0]->IsNumber()) {
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Argument must be a number")));
return;
}
gid_t gid = args[0]->NumberValue();
struct group *grp = getgrgid(gid);
if (grp != NULL) {
args.GetReturnValue().Set(mkgroup(isolate, grp));
}
}
void Getgrnam(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
if (args.Length() < 1) {
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Wrong number of arguments")));
return;
}
if (!args[0]->IsString()) {
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Argument must be a string")));
return;
}
String::Utf8Value name(args[0]);
struct group *grp = getgrnam(*name);
if (grp != NULL) {
args.GetReturnValue().Set(mkgroup(isolate, grp));
}
}
void Init(Local<Object> exports, Local<Object> module) {
NODE_SET_METHOD(exports, "getgrgid", Getgrgid);
NODE_SET_METHOD(exports, "getgrnam", Getgrnam);
}
NODE_MODULE(addon, Init)
} // namespace grp