-
- setbuf(stdout, obuf);
- if (!(no_tty = gtty(1, &otty))) {
- if ((term = getenv("TERM")) && tgetent(buf, term) <= 0) {
+ int tgrp;
+ struct winsize win;
+
+retry:
+ if (!(no_tty = gtty(fileno(stdout), &otty))) {
+ if (ioctl(fileno(stdout), TIOCLGET, &lmode) < 0) {
+ perror("TIOCLGET");
+ exit(1);
+ }
+ docrterase = ((lmode & LCRTERA) != 0);
+ docrtkill = ((lmode & LCRTKIL) != 0);
+ /*
+ * Wait until we're in the foreground before we save the
+ * the terminal modes.
+ */
+ if (ioctl(fileno(stdout), TIOCGPGRP, &tgrp) < 0) {
+ perror("TIOCGPGRP");
+ exit(1);
+ }
+ if (tgrp != getpgrp(0)) {
+ kill(0, SIGTTOU);
+ goto retry;
+ }
+ if ((term = getenv("TERM")) == 0 || tgetent(buf, term) <= 0) {