*BSD News Article 19840


Return to BSD News archive

Path: sserve!newshost.anu.edu.au!munnari.oz.au!constellation!osuunx.ucc.okstate.edu!moe.ksu.ksu.edu!vixen.cso.uiuc.edu!howland.reston.ans.net!agate!doc.ic.ac.uk!uknet!mcsun!news.eunet.fi!KremlSun!kiae!relcom!newsserv
From: "Andrew A. Chernov, Black Mage" <ache@astral.msk.su>
Newsgroups: comp.os.386bsd.bugs
Subject: Right fix to ctype.h, expr calculated only one time now
Date: Sun, 22 Aug 93 03:47:41 +0400
Distribution: world
Organization: Ha-olahm Yetzirah
Message-ID: <HQTGhTiKG1@astral.msk.su>
Sender: news-service@kiae.su
Reply-To: ache@astral.msk.su
X-Return-Path: kiae.UUCP!newcom.kiae.su!astral!astral.msk.su!ache
Lines: 54

It is well-know bug in toupper/tolower macros: it doesn't
check for valid char, moreover it is ANSI standard violation.

All fixes I seen have another well-know bug,
expr calculated more than one time, produce error in
	*p++ = tolower(*s++)
construction.

Here is right fix, expr calculated only one time:

*** ctype.h.old	Mon Apr  8 18:52:59 1991
--- ctype.h	Fri Aug  6 23:30:37 1993
*************** extern char	_ctype_[];
*** 59,66 ****
  #define	isgraph(c)	((_ctype_ + 1)[c] & (_P|_U|_L|_N))
  #define	iscntrl(c)	((_ctype_ + 1)[c] & _C)
  #define	isascii(c)	((unsigned)(c) <= 0177)
! #define	toupper(c)	((c) - 'a' + 'A')
! #define	tolower(c)	((c) - 'A' + 'a')
  #define	toascii(c)	((c) & 0177)
  
  #endif /* !_CTYPE_H_ */
--- 59,83 ----
  #define	isgraph(c)	((_ctype_ + 1)[c] & (_P|_U|_L|_N))
  #define	iscntrl(c)	((_ctype_ + 1)[c] & _C)
  #define	isascii(c)	((unsigned)(c) <= 0177)
! #define _toupper(c)     ((c) - 'a' + 'A')
! #define _tolower(c)     ((c) - 'A' + 'a')
  #define	toascii(c)	((c) & 0177)
+ 
+ #ifdef __GNUC__
+ #define	tolower(c) \
+ ({ \
+ 	int ___c = (c); \
+ 	isupper(___c) ? _tolower(___c) : ___c; \
+ })
+ 
+ #define	toupper(c) \
+ ({ \
+ 	int ___c = (c); \
+ 	islower(___c) ? _toupper(___c) : ___c; \
+ })
+ #else
+ extern int tolower();
+ extern int toupper();
+ #endif
  
  #endif /* !_CTYPE_H_ */
-- 
In-This-Life:    Andrew A. Chernov      | "Hay mas dicha, mas contento
Internet:        ache@astral.msk.su     | "Que adorar una hermosura
FIDOnet:         2:5020/134.32          | "Brujuleada entre los lejos
RELCOM Development Team, Moscow, Russia | "De lo imposible?!" (Calderon)