Roff in C and iscntrl()

hine, butler bph at ut-ngp.UUCP
Thu Jan 3 06:00:10 AEST 1985


[]
>> here's how the manual defines "iscntrl()":
>> 
>> iscntrl             c is a delete character (0177) or ordinary
>>                     control character (less than 040).
>> 
>> BOTH of the stated compilers [DeSmet C88 and CI-C86]
>> failed to interpret "ordinary" in the same way
>> as the PCC routine of the same name -- they return TRUE if the code is less
>> than octal 040.  As written, then, with this interpretation, newlines are
>> never returned, and the text is lost.  PCC, however, excludes newlines,
>> backspace codes, carriage return codes and a few others, presumably because
>> they are not "ordinary."  
>> 
>> This says something fairly awful about "portability."

> This says something fairly awful about available C implementations!
> 
> "ordinary" in the description of iscntrl is not an additional qualifier
> but an explanatory one.  iscntrl( c ) should return non-zero for c in
> { 0, 1, ..., 036, 037, 0177 } and zero for c in
> { 040, 041, ..., 0175, 0176 }.  It is illegal to supply any other value
> of c to the macro/function, although most implementations permit EOF (-1).

If this is indeed correct, then the Portable C Compiler provided with
4.2bsd is guilty of a serious offence.  I wrote a quick program to test it.
Here is the program, and its output under 4.2bsd:
------------------------------------------------
#include <stdio.h>
#include <ctype.h>

main()
{
int c;

for(c = 0; c <= '\040'; c++) {
	if(c == '\040')
		c = '\177';
	printf("%03o    ^%c    %s", c, c+'@', (iscntrl(c)) ? "Yes" : " No");
	(c&1) ? putchar('\n') : putchar('\t');
	}
}
-------------------------------------------------

000    ^@    Yes	001    ^A    Yes
002    ^B    Yes	003    ^C    Yes
004    ^D    Yes	005    ^E    Yes
006    ^F    Yes	007    ^G    Yes
010    ^H    Yes	011    ^I     No
012    ^J     No	013    ^K     No
014    ^L     No	015    ^M     No
016    ^N    Yes	017    ^O    Yes
020    ^P    Yes	021    ^Q    Yes
022    ^R    Yes	023    ^S    Yes
024    ^T    Yes	025    ^U    Yes
026    ^V    Yes	027    ^W    Yes
030    ^X    Yes	031    ^Y    Yes
032    ^Z    Yes	033    ^[    Yes
034    ^\    Yes	035    ^]    Yes
036    ^^    Yes	037    ^_    Yes
177    ^?    Yes

> "PCC" has nothing to do with the ctype macros; they are defined in
> /usr/include/ctype.h (or equivalent on non-UNIX) and usually use a table
> loaded from the standard C library.

It seems to me the manual page ought to tell you exactly what a subroutine
returns.  Clearly whoever made up the table decided codes 011-013 were NOT
ordinary control codes. Other compilers (or their libraries) take the
description of "iscntrl" above at face value, returning "Yes" for ALL of
the codes listed above.

You might try the above program on YOUR C compiler and see what happens.
 



More information about the Comp.lang.c mailing list