[comp.sources.bugs] shar

kent@ssc.UUCP (Kent) (01/15/88)

This may have been brought up before...

I have found a native int size dependency in Gary Perlman's shar, 
which was posted a while ago. It prints FSIZE, which is of type 
off_t, which is usually a long.  In the printf()'s, the %d's 
corresponding to the FSIZEs must be changed to %ld or it won't work
on a 16 bit machine. 

Somebody elsewhere (in comp.lang.c, maybe) was annoyed about types 
like time_t, which pretend to improve portability via data hiding, but 
actually make a mess, since we need to know the underlying type 
anyway. 

This is an excellent example of the kind of problem these things
have in c.  The only safe way to handle these things (that I can think
of) is to use a flock of conversion macros, in stdjunk.h or whatever.

#define off_ttol(x) ((long)x)
#define ltooff_t(x) ((off_t)x)

etcetera...

gwyn@brl-smoke.ARPA (Doug Gwyn ) (01/15/88)

In article <965@ssc.UUCP> kent@ssc.UUCP (Kent) writes:
-Somebody elsewhere (in comp.lang.c, maybe) was annoyed about types 
-like time_t, which pretend to improve portability via data hiding, but 
-actually make a mess, since we need to know the underlying type 
-anyway. 

For printf purposes, so long as it's known to be an integral type,
simply always cast the argument to long and use %ld format.

peter@sugar.UUCP (01/24/88)

In article <7098@brl-smoke.ARPA>, gwyn@brl-smoke.ARPA (Doug Gwyn ) writes:
> In article <965@ssc.UUCP> kent@ssc.UUCP (Kent) writes:
> >Somebody elsewhere (in comp.lang.c, maybe) was annoyed about types 
> >like time_t, which pretend to improve portability via data hiding, but 
> >actually make a mess, since we need to know the underlying type anyway.

> For printf purposes, so long as it's known to be an integral type,
> simply always cast the argument to long and use %ld format.

Or run something like this sucker to create a macro file:

	int test;

	test = (time_t)1.05;
	if(test == (time_t)1)
		printf("#define CVT_time_t \"%s\"\n", (sizeof(time_t)==sizeof(long)) ? "%ld" : "%d");
	else
		printf("#define CVT_time_t \"%s\"\n", (sizeof(time_t)==sizeof(float)) ? "%f" : "%lf");
-- 
-- Peter da Silva  `-_-'  ...!hoptoad!academ!uhnix1!sugar!peter
-- Disclaimer: These U aren't mere opinions... these are *values*.