forked from solidco2/node-dup2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dup2.cc
32 lines (28 loc) · 772 Bytes
/
dup2.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
#include <v8.h>
#include <node.h>
#include <fcntl.h>
#ifdef WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
using namespace v8;
Handle<Value> InvokeMethod(const Arguments& args) {
HandleScope scope;
int oldfd = 0, newfd = 0;
int ret = 0;
if(!(args[0]->IsNumber() && args[1]->IsNumber())){
ThrowException(Exception::TypeError(String::New("FileDescriptor must be number")));
}else{
oldfd = args[0]->Uint32Value();
newfd = args[1]->Uint32Value();
}
close(newfd);
ret = fcntl(oldfd, F_DUPFD, newfd);
return scope.Close(Number::New(ret));
}
void init(Handle<Object> exports) {
exports->Set(String::NewSymbol("invoke"),
FunctionTemplate::New(InvokeMethod)->GetFunction());
}
NODE_MODULE(dup2, init)