a.c

qhwt+dfly, 12/27/2005 12:32 AM

Download (977 Bytes)

 
1
#include <sys/types.h>
2
#include <sys/wait.h>
3
#include <err.h>
4
#include <errno.h>
5
#include <signal.h>
6
#include <stdio.h>
7
#include <stdlib.h>
8
#include <string.h>
9
#include <unistd.h>
10

    
11
int
12
main()
13
{
14
        const char *editor = "/usr/bin/vi";
15
        const char *name;
16
        pid_t pid, caught;
17
        int sig;
18
        int st;
19

    
20
        if (getenv("EDITOR") != NULL)
21
                editor = getenv("EDITOR");
22
        if ((name = strrchr(editor, '/')) != NULL)
23
                ++name;
24
        else
25
                name = editor;
26
        if ((pid = fork()) < 0)
27
                err(1, "fork");
28
        if (pid == 0)
29
                return execlp(editor, name, NULL);
30
#if 0
31
        sleep(1);
32
        killpg(getpgrp(), SIGSTOP);
33
#endif
34
        for (;;) {
35
                caught = waitpid(pid, &st, WUNTRACED);
36
                warnx("waitpid returned %d\n", caught);
37
                errno = WEXITSTATUS(st);
38
                if (caught == -1)
39
                        return 1;
40
                else if (WIFSTOPPED(st)) {
41
                        sig = WSTOPSIG(st);
42
                        warnx("WSTOPSIG(%d) = %d\n", st, sig);
43
                        sig = SIGSTOP;
44
                        raise(sig);
45
                        warnx("after raise(%d)\n", sig);
46
                }
47
                else if (WIFEXITED(st))
48
                        return errno;
49
                else
50
                        return 1;
51
        }
52
        return 0;
53
}
54