/* * getpid.c -- * Binary package for UNIX getpid(2) and friends. * * $Id: getpid.c,v 1.4 2001-06-15 17:32:46+09 kabe Exp $ */ #include #include /*getpid()*/ static int Getpid_PpidObjCmd(ClientData cd, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]); /* shorthand of the above */ static Tcl_ObjCmdProc Getpid_fObjCmd; int Getpid_Init(Tcl_Interp *interp) { Tcl_CmdInfo cinfo; /* Don't overwrite existing commands (especially "pid") */ if (!Tcl_GetCommandInfo(interp, "ppid", &cinfo)) Tcl_CreateObjCommand(interp, "ppid", Getpid_PpidObjCmd, NULL, NULL); if (!Tcl_GetCommandInfo(interp, "pid", &cinfo)) Tcl_CreateObjCommand(interp, "pid" , Getpid_fObjCmd, getpid , NULL); if (!Tcl_GetCommandInfo(interp, "pgrp", &cinfo)) Tcl_CreateObjCommand(interp, "pgrp", Getpid_fObjCmd, getpgrp, NULL); if (!Tcl_GetCommandInfo(interp, "pgid", &cinfo)) Tcl_CreateObjCommand(interp, "pgid", Getpid_fObjCmd, getpgid, NULL); /* TODO: rather than defining raw commands, subcommanding like * "process parentpid" "process group" (cf.file,fconfigure) * is more Tcl-ish. * Better is pushing into new namespace. */ Tcl_PkgProvide(interp, "getpid", "1.0"); return TCL_OK; } /* *------------------------------------------------- * Getpid_PpidObjCmd -- * This procedure returns getppid(2) value. *------------------------------------------------- */ static int Getpid_PpidObjCmd(cd, interp, objc, objv) ClientData cd; /* Not used. */ Tcl_Interp *interp; /* Current interpreter. */ int objc; /* # of arguments. */ Tcl_Obj *CONST objv[]; /* Arguments. */ { #ifndef EDUCATIONAL Tcl_SetLongObj(Tcl_GetObjResult(interp), (long)getppid()); #else Tcl_Obj *result; result = Tcl_NewLongObj((long)getppid()); Tcl_SetObjResult(interp, result); #endif return TCL_OK; } /* *------------------------------------------------- * Getpid_fObjCmd -- * This procedure returns whatever returned by pid_t func(void). * (Well, you rarely need these for Tcl..) *------------------------------------------------- */ static int Getpid_fObjCmd(func, interp, objc, objv) ClientData func; /* int func(void) to call. */ Tcl_Interp *interp; /* Current interpreter. */ int objc; /* # of arguments. */ Tcl_Obj *CONST objv[]; /* Arguments. */ { Tcl_SetLongObj(Tcl_GetObjResult(interp), (long)((pid_t (*)(void))func)()); /* -1 return from getpgid() is an error, * but will pass on anyway for now (doesn't throw error). */ /* TODO: Original "pid" command takes an argument for * command pipe. */ return TCL_OK; }