On Thu, Sep 14, 2006 at 04:51:44AM +0400, Alexey Tourbin wrote: > Implement the following behaviour: > > 1) execute a program also when a key is pressed; > 2) but if that key is 'q' then exit instead. > > This makes watch look more like an interactive program. > > diff --git a/procps/watch.c b/procps/watch.c > index 18ded5e..4abad74 100644 > --- a/procps/watch.c > +++ b/procps/watch.c > @@ -20,6 +20,7 @@ #include > #include > #include > #include > +#include > #include > #include > #include > @@ -227,6 +228,7 @@ main(int argc, char *argv[]) > nonl(); > noecho(); > cbreak(); > + nodelay(stdscr, TRUE); > > for (;;) { > time_t t = time(NULL); > @@ -236,6 +238,10 @@ main(int argc, char *argv[]) > FILE *p; > int x, y; > int oldeolseen = 1; > + int key; > + > + if ((key = getch()) == 'q') > + do_exit(0); > > if (screen_size_changed) { > get_terminal_size(); > @@ -314,7 +320,16 @@ main(int argc, char *argv[]) > > first_screen = 0; > refresh(); > - usleep(interval * 1000000); > + > + /* wait until tiemout or until a key press */ > + { > + fd_set set; > + int fd = fileno(stdin); > + struct timeval timeout = { interval, 0 }; > + FD_ZERO(&set); > + FD_SET(fd, &set); > + select(fd + 1, &set, NULL, NULL, &timeout); > + } > } > > endwin(); > $ В общем затея оказалась небеспроблемная -- select может отвалить или вернуть раньше времени по самым разным причинам, а входить в безудержный цикл последнее дело. Можно конечно проверять на isatty(0), но мне эта идея почему-то не очень нравится. Короче окончательный вариант в git.alt:/people/at/packages/procps.git несколько лучше, чем этот и несколько последующих.