version 1.22, 2003/07/29 20:10:17 |
version 1.23, 2003/07/30 15:47:54 |
|
|
} |
} |
|
|
/* |
/* |
* Get a response from the user, somehow or other. |
* Get a response from the user via /dev/tty |
*/ |
*/ |
void |
void |
ask(const char *fmt, ...) |
ask(const char *fmt, ...) |
{ |
{ |
va_list ap; |
va_list ap; |
int ttyfd; |
ssize_t nr; |
ssize_t r; |
static int ttyfd = -1; |
bool tty2 = isatty(2); |
|
|
|
va_start(ap, fmt); |
va_start(ap, fmt); |
vsnprintf(buf, sizeof buf, fmt, ap); |
vfprintf(stdout, fmt, ap); |
va_end(ap); |
va_end(ap); |
fflush(stderr); |
fflush(stdout); |
write(2, buf, strlen(buf)); |
if (ttyfd < 0) |
if (tty2) { |
ttyfd = open(_PATH_TTY, O_RDONLY); |
/* might be redirected to a file */ |
if (ttyfd >= 0) { |
r = read(2, buf, sizeof buf); |
if ((nr = read(ttyfd, buf, sizeof(buf))) > 0 && |
} else if (isatty(1)) { /* this may be new file output */ |
buf[nr - 1] == '\n') |
fflush(stdout); |
buf[nr - 1] = '\0'; |
write(1, buf, strlen(buf)); |
|
r = read(1, buf, sizeof buf); |
|
} else if ((ttyfd = open(_PATH_TTY, O_RDWR)) >= 0 && isatty(ttyfd)) { |
|
/* might be deleted or unwriteable */ |
|
write(ttyfd, buf, strlen(buf)); |
|
r = read(ttyfd, buf, sizeof buf); |
|
close(ttyfd); |
|
} else if (isatty(0)) { /* this is probably patch input */ |
|
fflush(stdin); |
|
write(0, buf, strlen(buf)); |
|
r = read(0, buf, sizeof buf); |
|
} else { /* no terminal at all--default it */ |
|
buf[0] = '\n'; |
|
r = 1; |
|
} |
} |
if (r <= 0) |
if (ttyfd < 0 || nr <= 0) { |
buf[0] = 0; |
/* no tty or error reading, pretend user entered 'return' */ |
else |
putchar('\n'); |
buf[r] = '\0'; |
buf[0] = '\0'; |
if (!tty2) |
} |
say(buf); |
|
} |
} |
|
|
/* |
/* |