Subject: Re: Bug in regex library?
To: None <current-users@NetBSD.ORG>
From: der Mouse <mouse@Holo.Rodents.Montreal.QC.CA>
List: current-users
Date: 10/29/1996 15:23:11
>>   printf ( "%ld %ld\n" , (long int)pmatch[0].rm_so , (long int)pmatch[0].rm_eo ) ;

> <sermon>

:-)

> Usually, I cast system-defined types to the type *printf() expects,
> as der Mouse does above, to prevent exactly this problem.  If you
> want your code to be portable, casting arguments to varargs/stdargs
> functions is a necessary precaution.

Unfortunately, there is another problem: there is no way to tell what
format to use.  Back before gcc invented "long long int" [%], you could
just use %ld or %lu or %lx or %lwhatever.  Now, there is no way to tell
what format to use to print a (say) off_t or uid_t, nor indeed whether
there even _is_ any format string that won't lose information.  In
fully portable code, the most you can do is cast it to long, or
unsigned long, and hope that doesn't lose information; if you already
have per-system porting definitions, you can do something like

(in the NetBSD section)
	typedef quad_t signed_big;
	typedef u_quad_t unsigned_big;
	#define BIG_D "%qd"
	#define BIG_U "%qu"
	#define BIG_X "%qx"

(and in the generic section)
	typedef long int signed_big;
	typedef unsigned long int unsigned_big;
	#defnie BIG_D "%ld"
	#defnie BIG_U "%lu"
	#defnie BIG_X "%lx"

and then write stuff like

	signed_big totsiz;
[...]
	printf("Total size = " BIG_U "\n",totsiz);

or

	printf(BIG_D " " BIG_D "\n", (signed_big)pm[0].rm_so, (signed_big)pm[0].rm_eo);

This is somewhat ugly, but given the current state of C (and nearly-C)
compilers, it's about the best I can see.

[%] I think gcc was the first to do a size bigger than long; I
    certainly haven't heard of anyone doing one earlier.  (Though I'm
    sure I'll get plenty of corrections if I'm wrong on this....)

Of course, if all you care about is preserving information, then just
print the sizeof(object) unsigned chars that make up the object, and be
done with it.  But that's not too useful for things like user
interfaces.

					der Mouse

			       mouse@rodents.montreal.qc.ca
		     01 EE 31 F6 BB 0C 34 36  00 F3 7C 5A C1 A0 67 1D