Main Page | Class List | Directories | File List | Class Members | File Members

fork.c

Go to the documentation of this file.
00001 /*
00002  * fork.c - Provide fork and waitpid functions for gawk.
00003  */
00004 
00005 /*
00006  * Copyright (C) 2001 the Free Software Foundation, Inc.
00007  * 
00008  * This file is part of GAWK, the GNU implementation of the
00009  * AWK Programming Language.
00010  * 
00011  * GAWK is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version.
00015  * 
00016  * GAWK is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  * 
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
00024  */
00025 
00026 #include "awk.h"
00027 
00028 /*  do_fork --- provide dynamically loaded fork() builtin for gawk */
00029 
00030 static NODE *
00031 do_fork(tree)
00032 NODE *tree;
00033 {
00034         int ret = -1;
00035         NODE **aptr;
00036 
00037         if  (do_lint && tree->param_cnt > 0)
00038                 lintwarn("fork: called with too many arguments");
00039 
00040         ret = fork();
00041 
00042         if (ret < 0)
00043                 update_ERRNO();
00044         else if (ret == 0) {
00045                 /* update PROCINFO in the child */
00046 
00047                 aptr = assoc_lookup(PROCINFO_node, tmp_string("pid", 3), FALSE);
00048                 (*aptr)->numbr = (AWKNUM) getpid();
00049 
00050                 aptr = assoc_lookup(PROCINFO_node, tmp_string("ppid", 4), FALSE);
00051                 (*aptr)->numbr = (AWKNUM) getppid();
00052         }
00053 
00054         /* Set the return value */
00055         set_value(tmp_number((AWKNUM) ret));
00056 
00057         /* Just to make the interpreter happy */
00058         return tmp_number((AWKNUM) 0);
00059 }
00060 
00061 
00062 /*  do_waitpid --- provide dynamically loaded waitpid() builtin for gawk */
00063 
00064 static NODE *
00065 do_waitpid(tree)
00066 NODE *tree;
00067 {
00068         NODE *pidnode;
00069         int ret = -1;
00070         double pidval;
00071         pid_t pid;
00072         int options = 0;
00073 
00074         if  (do_lint && tree->param_cnt > 1)
00075                 lintwarn("waitpid: called with too many arguments");
00076 
00077         pidnode = get_argument(tree, 0);
00078         if (pidnode != NULL) {
00079                 pidval = force_number(pidnode);
00080                 pid = (int) pidval;
00081                 options = WNOHANG|WUNTRACED;
00082                 ret = waitpid(pid, NULL, options);
00083                 if (ret < 0)
00084                         update_ERRNO();
00085         } else if (do_lint)
00086                 lintwarn("wait: called with no arguments");
00087 
00088         /* Set the return value */
00089         set_value(tmp_number((AWKNUM) ret));
00090 
00091         /* Just to make the interpreter happy */
00092         return tmp_number((AWKNUM) 0);
00093 }
00094 
00095 /* dlload --- load new builtins in this library */
00096 
00097 NODE *
00098 dlload(tree, dl)
00099 NODE *tree;
00100 void *dl;
00101 {
00102         make_builtin("fork", do_fork, 0);
00103         make_builtin("waitpid", do_waitpid, 1);
00104         return tmp_number((AWKNUM) 0);
00105 }

© sourcejam.com 2005-2008