jim.c: Jim_Length/Jim_String internal checks
[jimtcl.git] / jim-posix.c
bloba4ba61e478ed0da6100486954c58908e47c2689d
1 /*
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
8 * are met:
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>
36 #include <sys/time.h>
37 #include <sys/wait.h>
38 #include <unistd.h>
39 #include <string.h>
40 #include <errno.h>
42 #include "jimautoconf.h"
43 #include <jim.h>
45 #ifdef HAVE_SYS_SYSINFO_H
46 #include <sys/sysinfo.h>
47 #endif
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)
57 pid_t pid;
59 JIM_NOTUSED(argv);
61 if (argc != 1) {
62 Jim_WrongNumArgs(interp, 1, argv, "");
63 return JIM_ERR;
65 if ((pid = fork()) == -1) {
66 Jim_PosixSetError(interp);
67 return JIM_ERR;
69 Jim_SetResultInt(interp, (jim_wide) pid);
70 return JIM_OK;
72 #endif
74 static int Jim_PosixGetidsCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
76 Jim_Obj *objv[8];
78 if (argc != 1) {
79 Jim_WrongNumArgs(interp, 1, argv, "");
80 return JIM_ERR;
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));
91 return JIM_OK;
94 #define JIM_HOST_NAME_MAX 1024
95 static int Jim_PosixGethostnameCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
97 char *buf;
98 int rc = JIM_OK;
100 if (argc != 1) {
101 Jim_WrongNumArgs(interp, 1, argv, "");
102 return JIM_ERR;
104 buf = Jim_Alloc(JIM_HOST_NAME_MAX);
105 if (gethostname(buf, JIM_HOST_NAME_MAX) == -1) {
106 Jim_PosixSetError(interp);
107 Jim_Free(buf);
108 rc = JIM_ERR;
110 else {
111 Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, buf, -1));
113 return rc;
116 static int Jim_PosixUptimeCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
118 #ifdef HAVE_STRUCT_SYSINFO_UPTIME
119 struct sysinfo info;
121 if (argc != 1) {
122 Jim_WrongNumArgs(interp, 1, argv, "");
123 return JIM_ERR;
126 if (sysinfo(&info) == -1) {
127 Jim_PosixSetError(interp);
128 return JIM_ERR;
131 Jim_SetResultInt(interp, info.uptime);
132 #else
133 Jim_SetResultInt(interp, (long)time(NULL));
134 #endif
135 return JIM_OK;
138 int Jim_posixInit(Jim_Interp *interp)
140 if (Jim_PackageProvide(interp, "posix", "1.0", JIM_ERRMSG))
141 return JIM_ERR;
143 #ifdef HAVE_FORK
144 Jim_CreateCommand(interp, "os.fork", Jim_PosixForkCommand, NULL, NULL);
145 #endif
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);
149 return JIM_OK;