source: trunk/essentials/sys-apps/gawk/extension/fork.c

Last change on this file was 3076, checked in by bird, 18 years ago

gawk 3.1.5

File size: 2.6 KB
Line 
1/*
2 * fork.c - Provide fork and waitpid functions for gawk.
3 *
4 * Revised 6/2004
5 */
6
7/*
8 * Copyright (C) 2001, 2004 the Free Software Foundation, Inc.
9 *
10 * This file is part of GAWK, the GNU implementation of the
11 * AWK Programming Language.
12 *
13 * GAWK is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * GAWK is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26 */
27
28#include "awk.h"
29
30/* do_fork --- provide dynamically loaded fork() builtin for gawk */
31
32static NODE *
33do_fork(tree)
34NODE *tree;
35{
36 int ret = -1;
37 NODE **aptr;
38
39 if (do_lint && get_curfunc_arg_count() > 0)
40 lintwarn("fork: called with too many arguments");
41
42 ret = fork();
43
44 if (ret < 0)
45 update_ERRNO();
46 else if (ret == 0) {
47 /* update PROCINFO in the child */
48
49 aptr = assoc_lookup(PROCINFO_node, tmp_string("pid", 3), FALSE);
50 (*aptr)->numbr = (AWKNUM) getpid();
51
52 aptr = assoc_lookup(PROCINFO_node, tmp_string("ppid", 4), FALSE);
53 (*aptr)->numbr = (AWKNUM) getppid();
54 }
55
56 /* Set the return value */
57 set_value(tmp_number((AWKNUM) ret));
58
59 /* Just to make the interpreter happy */
60 return tmp_number((AWKNUM) 0);
61}
62
63
64/* do_waitpid --- provide dynamically loaded waitpid() builtin for gawk */
65
66static NODE *
67do_waitpid(tree)
68NODE *tree;
69{
70 NODE *pidnode;
71 int ret = -1;
72 double pidval;
73 pid_t pid;
74 int options = 0;
75
76 if (do_lint && get_curfunc_arg_count() > 1)
77 lintwarn("waitpid: called with too many arguments");
78
79 pidnode = get_argument(tree, 0);
80 if (pidnode != NULL) {
81 pidval = force_number(pidnode);
82 pid = (int) pidval;
83 options = WNOHANG|WUNTRACED;
84 ret = waitpid(pid, NULL, options);
85 if (ret < 0)
86 update_ERRNO();
87 } else if (do_lint)
88 lintwarn("wait: called with no arguments");
89
90 /* Set the return value */
91 set_value(tmp_number((AWKNUM) ret));
92
93 /* Just to make the interpreter happy */
94 return tmp_number((AWKNUM) 0);
95}
96
97/* dlload --- load new builtins in this library */
98
99NODE *
100dlload(tree, dl)
101NODE *tree;
102void *dl;
103{
104 make_builtin("fork", do_fork, 0);
105 make_builtin("waitpid", do_waitpid, 1);
106 return tmp_number((AWKNUM) 0);
107}
Note: See TracBrowser for help on using the repository browser.