Re: Question regarding sscanf() vs. off_t and similar

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thursday 14 September 2006 15:01 Andreas Schwab wrote:
> Philipp Marek <philipp@xxxxxxxxxxxxx> writes:
> > If I'm doing
> > 	ino_t ino;
> > 	char *name;
> > 	sprintf("%Lu %s", ino, name);
> Note that "L" is not a valid length modifier for integer formats.
For gnu libc it is: 
http://www.gnu.org/software/libc/manual/html_node/Integer-Conversions.html#Integer-Conversions

> > that works fine for 64bit systems, but writes trash for sizeof(ino_t) ==
> > 4.
> >
> > Is there an easy way to find the format strings (sprintf, printf, sscanf)
> > for various system types, or something like that?
>
> Assuming you meant %llu instead of %Lu you can always add a cast to
> unsigned long long to match the format.
Yes. But that's a bit unclean, and doing that unnecessarily in a loop for 
~300000 iterations is something I'd like to avoid.

> > Or has somebody some already defined preprocessor magic to get this for
> > some variable?
> > I'm thinking of something along the lines
> > 	#define FORMAT(x)
> > 	  #if sizeof(x) == 4
> > 	    "%l"
> > 	  #else
> > 	    #if sizeof(x) == 8
> > 	      "%L"
> > 	    #else
> > 	      #error "Don't know size"
> > 	    #endif
> > 	  #endif
>
> You can't use sizeof in preprocessor directives. 
Yes, that was meant as some kind of pseudo-code.

> Besides that, the size 
> of an integer says nothing about its type.  For example, a 4 byte integer
> can be either int or long on an ILP32 system, and an 8 byte integer can be
> long or long long on an LP64 system.
But that main problem arises if the printf() format doesn't match the size 
pushed on the stack - then the remaining arguments are read in the wrong 
order.

If I know that some variable is 32bit, and %lu takes a 32bit integer from the 
stack, I'm saved.


Thank you for your answer - I think I'll have to go with casting everything.


Regards,

Phil

-- 
Versioning your /etc, /home or even your whole installation?
             Try fsvs (fsvs.tigris.org)!


_______________________________________________
Autoconf mailing list
Autoconf@xxxxxxx
http://lists.gnu.org/mailman/listinfo/autoconf

[Index of Archives]     [GCC Help]     [Kernel Discussion]     [RPM Discussion]     [Red Hat Development]     [Yosemite News]     [Linux USB]     [Samba]

  Powered by Linux