2 * Jim - POSIX extension
4 * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE JIM TCL PROJECT ``AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and documentation
31 * are those of the authors and should not be interpreted as representing
32 * official policies, either expressed or implied, of the Jim Tcl Project.
35 #include <sys/types.h>
42 #include "jimautoconf.h"
45 #ifdef HAVE_SYS_SYSINFO_H
46 #include <sys/sysinfo.h>
49 static void Jim_PosixSetError(Jim_Interp
*interp
)
51 Jim_SetResultString(interp
, strerror(errno
), -1);
54 #if defined(HAVE_FORK)
55 static int Jim_PosixForkCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
62 Jim_WrongNumArgs(interp
, 1, argv
, "");
65 if ((pid
= fork()) == -1) {
66 Jim_PosixSetError(interp
);
69 Jim_SetResultInt(interp
, (jim_wide
) pid
);
74 static int Jim_PosixGetidsCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
79 Jim_WrongNumArgs(interp
, 1, argv
, "");
82 objv
[0] = Jim_NewStringObj(interp
, "uid", -1);
83 objv
[1] = Jim_NewIntObj(interp
, getuid());
84 objv
[2] = Jim_NewStringObj(interp
, "euid", -1);
85 objv
[3] = Jim_NewIntObj(interp
, geteuid());
86 objv
[4] = Jim_NewStringObj(interp
, "gid", -1);
87 objv
[5] = Jim_NewIntObj(interp
, getgid());
88 objv
[6] = Jim_NewStringObj(interp
, "egid", -1);
89 objv
[7] = Jim_NewIntObj(interp
, getegid());
90 Jim_SetResult(interp
, Jim_NewListObj(interp
, objv
, 8));
94 #define JIM_HOST_NAME_MAX 1024
95 static int Jim_PosixGethostnameCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
101 Jim_WrongNumArgs(interp
, 1, argv
, "");
104 buf
= Jim_Alloc(JIM_HOST_NAME_MAX
);
105 if (gethostname(buf
, JIM_HOST_NAME_MAX
) == -1) {
106 Jim_PosixSetError(interp
);
111 Jim_SetResult(interp
, Jim_NewStringObjNoAlloc(interp
, buf
, -1));
116 static int Jim_PosixUptimeCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
118 #ifdef HAVE_STRUCT_SYSINFO_UPTIME
122 Jim_WrongNumArgs(interp
, 1, argv
, "");
126 if (sysinfo(&info
) == -1) {
127 Jim_PosixSetError(interp
);
131 Jim_SetResultInt(interp
, info
.uptime
);
133 Jim_SetResultInt(interp
, (long)time(NULL
));
138 int Jim_posixInit(Jim_Interp
*interp
)
140 if (Jim_PackageProvide(interp
, "posix", "1.0", JIM_ERRMSG
))
144 Jim_CreateCommand(interp
, "os.fork", Jim_PosixForkCommand
, NULL
, NULL
);
146 Jim_CreateCommand(interp
, "os.getids", Jim_PosixGetidsCommand
, NULL
, NULL
);
147 Jim_CreateCommand(interp
, "os.gethostname", Jim_PosixGethostnameCommand
, NULL
, NULL
);
148 Jim_CreateCommand(interp
, "os.uptime", Jim_PosixUptimeCommand
, NULL
, NULL
);