Don't try to write() the start/stop string if the fd is a pipe, because this will never work

This commit is contained in:
Paul LeoNerd Evans 2012-01-20 20:01:11 +00:00
parent 62b8773108
commit 2ed8bae4f6
1 changed files with 18 additions and 0 deletions

View File

@ -18,6 +18,8 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
/* To be efficient at lookups, we store the byte sequence => keyinfo mapping /* To be efficient at lookups, we store the byte sequence => keyinfo mapping
* in a trie. This avoids a slow linear search through a flat list of * in a trie. This avoids a slow linear search through a flat list of
@ -292,6 +294,7 @@ abort_free_ti:
static int start_driver(TermKey *tk, void *info) static int start_driver(TermKey *tk, void *info)
{ {
TermKeyTI *ti = info; TermKeyTI *ti = info;
struct stat statbuf;
char *start_string = ti->start_string; char *start_string = ti->start_string;
size_t len; size_t len;
@ -302,6 +305,13 @@ static int start_driver(TermKey *tk, void *info)
* We may need to enable that mode * We may need to enable that mode
*/ */
/* There's no point trying to write() to a pipe */
if(fstat(tk->fd, &statbuf) == -1)
return 0;
if(S_ISFIFO(statbuf.st_mode))
return 1;
// Can't call putp or tputs because they suck and don't give us fd control // Can't call putp or tputs because they suck and don't give us fd control
len = strlen(start_string); len = strlen(start_string);
while(len) { while(len) {
@ -317,12 +327,20 @@ static int start_driver(TermKey *tk, void *info)
static int stop_driver(TermKey *tk, void *info) static int stop_driver(TermKey *tk, void *info)
{ {
TermKeyTI *ti = info; TermKeyTI *ti = info;
struct stat statbuf;
char *stop_string = ti->stop_string; char *stop_string = ti->stop_string;
size_t len; size_t len;
if(tk->fd == -1 || !stop_string) if(tk->fd == -1 || !stop_string)
return 1; return 1;
/* There's no point trying to write() to a pipe */
if(fstat(tk->fd, &statbuf) == -1)
return 0;
if(S_ISFIFO(statbuf.st_mode))
return 1;
/* The terminfo database will contain keys in application cursor key mode. /* The terminfo database will contain keys in application cursor key mode.
* We may need to enable that mode * We may need to enable that mode
*/ */