[comp.bugs.sys5] Bug in tgetent

bbadger@x102c.harris-atd.com (Badger BA 64810) (08/16/89)

SYMPTOM:
tcsh wouldn't find my personalized termcap entries, even when TERMCAP 
was setenv'd to the correct file path.

PROBLEM: 
tgetent expects the file descriptor returned from 
	tf = open(cp, 0);
to be non-zero.  This isn't true when stdin is closed!  

SOLUTION:
Use (-1) as the default flag value.  This then indicates that either 1) No 
attempt to open was made, or 2) the open failed.

DISCUSSION:
This was probably overlooked in testing because most programs are already 
using file descriptor 0, and hence a successful open would return a 
file descriptor other than zero.  In my case stdin (fd 0) was closed, so 
the successful open returned 0.  This was mistakenly used by the program 
to indicate that an open was not attempted.  

The new code uses the standard
error return value of (-1) which is returned by a failed open.

**** I'm not absolutely certain that this is the correct behavior! ****
What it does is revert to the standard TERMCAP file if the open fails.
This certainly seems to be the right thing to do, but possibly not.

I *think* that the bug would also cause the standard termcap file to be 
ignored in the case that the user provides an inaccessible TERMCAP file path.
In that case, tf is set to -1, and the standard file is not opened.
(My program didn't do this, so I'm giving my own interpretation of this 
possibility.)

ALTERNATIVE:
GNU emacs comes with a version of termcap.c which does not seem to have
this problem.  There are probably other public domain termcap.c 
implementations to choose from.

PATCH:

--------- >8 --- cut here --- 8< ---------
diff -c V3.2.1/usr/src/lib/lxtermlib/termcap.c termcap.c 
*** V3.2.1/usr/src/lib/lxtermlib/termcap.c	Tue May 17 17:50:55 1988
--- termcap.c	Tue Aug 15 09:21:24 1989
***************
*** 101,109 ****
  	char ibuf[BUFSIZ];
  	char *cp2;
  	int tf;
! 
  	tbuf = bp;
! 	tf = 0;
  #ifndef V6
  	cp = getenv("TERMCAP");
  	/*
--- 101,109 ----
  	char ibuf[BUFSIZ];
  	char *cp2;
  	int tf;
! #define OPEN_ERR (-1)
  	tbuf = bp;
! 	tf = OPEN_ERR;
  #ifndef V6
  	cp = getenv("TERMCAP");
  	/*
***************
*** 126,137 ****
  		} else
  			tf = open(cp, 0);
  	}
! 	if (tf==0)
  		tf = open(E_TERMCAP, 0);
  #else
  	tf = open(E_TERMCAP, 0);
  #endif
! 	if (tf < 0)
  		return (-1);
  	for (;;) {
  		cp = bp;
--- 126,137 ----
  		} else
  			tf = open(cp, 0);
  	}
! 	if (tf==OPEN_ERR)
  		tf = open(E_TERMCAP, 0);
  #else
  	tf = open(E_TERMCAP, 0);
  #endif
! 	if (tf == OPEN_ERR)
  		return (-1);
  	for (;;) {
  		cp = bp;


--------- >8 --- cut here --- 8< ---------
Bernard A. Badger Jr.	407/984-6385          |``Use the Source, Luke!''
Secure Computer Products                      |``Get a LIFE!''  -- J.H. Conway
Harris GISD, Melbourne, FL  32902             |Buddy, can you paradigm?
Internet: bbadger%x102c@trantor.harris-atd.com|'s/./&&/g' Tom sed expansively.