Project

General

Profile

Bug #24 ยป a.c

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

 
#include <sys/types.h>
#include <sys/wait.h>
#include <err.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int
main()
{
const char *editor = "/usr/bin/vi";
const char *name;
pid_t pid, caught;
int sig;
int st;

if (getenv("EDITOR") != NULL)
editor = getenv("EDITOR");
if ((name = strrchr(editor, '/')) != NULL)
++name;
else
name = editor;
if ((pid = fork()) < 0)
err(1, "fork");
if (pid == 0)
return execlp(editor, name, NULL);
#if 0
sleep(1);
killpg(getpgrp(), SIGSTOP);
#endif
for (;;) {
caught = waitpid(pid, &st, WUNTRACED);
warnx("waitpid returned %d\n", caught);
errno = WEXITSTATUS(st);
if (caught == -1)
return 1;
else if (WIFSTOPPED(st)) {
sig = WSTOPSIG(st);
warnx("WSTOPSIG(%d) = %d\n", st, sig);
sig = SIGSTOP;
raise(sig);
warnx("after raise(%d)\n", sig);
}
else if (WIFEXITED(st))
return errno;
else
return 1;
}
return 0;
}

    (1-1/1)