getopt (3) problem?

dcm dcm at toysrus.uucp
Fri Sep 14 02:20:48 AEST 1990


In article <3452 at aix.aix.kingston.ibm.com> bchristy at aix.aix.kingston.ibm.com (JB Christy) writes:
>
>I was bitten by something similar, and I believe the problem is that EOF is
>#define'd as -1, which is an int, and I bet you've declared c as char.  When
>the comparison occurs, c is promoted to an int but the sign is lost, so it
>becomes 255 instead of -1.  Try casting EOF to a char, e.g.
>
>	while(( c = getopt( argc, argv, "d:m" )) != (char) EOF ) {
>
>That way -1 gets "demoted" to a char and all is well.


	No no no.  This works, but is not correct.  The solution is
	to note that getopt returns "int", and declare "c" as "int".
	"(char) EOF" is wrong.  I'm not even sure it's portable.

	This is the same problem that K&R #1 noted:  when comparing
	against EOF, *always* use an int var, not a char.

	Back to the basics.  Signed char vs unsigned char.  (found
	the same bug in "more".  Did BSD really write it that way?)
--------
	Craig Miller, contractor @ IBM AWD, Austin  (I don't speak for IBM)
	UUCP: ..!uunet!cs.utexas.edu!ibmaus!auschs!toysrus.austin.ibm.com!dcm

	"Just because it works doesn't means it's right, stupid!"
	"Don't believe .signature files."



More information about the Comp.unix.aix mailing list