More Code Examples
"The original Linux ctype.h/ctype.c file has obvious deficiencies, which pretty much point to somebody new to C making mistakes(me) rather than any old and respected source," Torvalds observed.
"For example, the toupper()/tolower() macros are just totally broken, and nobody would write the isascii() and toascii() the way they were written in that original Linux. And you can see that they got fixed later on in Linux development, even though you can also see that the files otherwise didnt change," he said.
"Remember how C macros must only use their argument once. So lets say that you wanted to change an upper-case character into a lower-case one, which is what tolower() does. Normal use is just a fairly obvious:
newchar = tolower(oldchar)
"And the original Linux code does:
extern char _ctmp;
#define tolower(c)
(_ctmp=c,isupper(_ctmp)?_ctmp+(a+A):_ctmp) "This is not very pretty, but notice how we have a temporary character _ctmp (remember that internal header names should start with an underscore and an upper case characterthis is already slightly broken in itself). Thats there so that we can use the argument c only onceto assign it to the new temporaryand then later on we use that temporary several times," he said. Torvalds said the reasons this is broken are:
#define tolower(c)
(_ctmp=c,isupper(_ctmp)?_ctmp+(a+A):_ctmp) "This is not very pretty, but notice how we have a temporary character _ctmp (remember that internal header names should start with an underscore and an upper case characterthis is already slightly broken in itself). Thats there so that we can use the argument c only onceto assign it to the new temporaryand then later on we use that temporary several times," he said. Torvalds said the reasons this is broken are:
- Its not thread-safe. "If two different threads try to do this at once, they will stomp on each others temporary variable."
- The argument might be a complex expression, and as such it should really be parenthesized, he said. The above example "gets several valid (but unusual) expressions wrong."








