forked from andrewcmyers/civs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgrp.c
47 lines (44 loc) · 1005 Bytes
/
pgrp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
extern int setpgrp(int pid, int pgrp);
extern int getpid();
extern int getdtablesize();
extern int getopt(int argc, char **argv, char *opts);
extern int optind;
#if 0
extern int open(char *, int, ...);
#endif
extern int dup2(int, int);
#include <stdio.h>
#include <assert.h>
#include <fcntl.h>
void usage(char **argv)
{
fprintf(stderr, "Usage: %s [-n] <command>\n", argv[0]);
exit(-1);
}
int main(int argc, char **argv)
{
int devnull = 0;
int c, pid;
while (-1 != (c = getopt(argc, argv, "n"))) {
switch(c) {
case 'n': devnull = 1; break;
default: usage(argv);
}
}
if (optind == argc) usage(argv);
pid = fork();
if (pid) {
if (0 > setpgrp(pid, pid)) { perror("setpgrp"); }
} else {
int i;
int maxfd = getdtablesize();
for (i=0; i< maxfd; i++) close(i);
if (devnull) {
int fd = open("/dev/null", O_RDWR);
assert(fd == 0);
dup2(fd, 1);
dup2(fd, 2);
}
if (0 > execvp(argv[optind], argv + optind)) { perror("exec"); }
}
}