Perl unpack/tacct file

Art Neilson art at pilikia.pegasus.com
Wed May 8 03:48:09 AEST 1991


In article <1991May06.235754.29292 at pinhead.pegasus.com> todd at pinhead.pegasus.com (Todd Ogasawara) writes:
>I posted a query last week. No responses so far, but I found a
>workaround. My question now is, "Why does my workaround work?"
>
>Here is my original posting.
>
>In article <1991May04.185409.24228 at pinhead.pegasus.com> todd at pinhead.pegasus.com (Todd Ogasawara) writes:
>The following Perl program reads tacct type files correctly. My only
>problem is I don't understand why it works. I had to place two skips of x2
>in the unpack template (see $stbuf_def) to get the program to work. I'm
>pretty sure my problem is not understanding exactly how C structures are
>built. I found that a C sizeof(tacctstruct) told me the structure is 52
>bytes large. However, when I added the size of each individual structure
>member it added up to 48 bytes. I had to do a hex dump to find that there
>were four bytes in two places (two bytes in each place) that never seemed
>to be used. Is there some easy rule I can follow when using Perl to unpack
>files created using C structures?

Your problem most likely is caused by structure padding.

The following quote from "C A Reference Manual" Second Edition by
Harbison and Steele is quite clear on the subject.

"The size of an object of a structure type is the amount of storage
necessary to represent all components of that type, including any
unused padding space between or after the components.  The rule is
that the structure will be padded out to the size the type would
occupy as an element of an array of such types.  (For any type T,
including structures, the size of an n-element array of T is the
same as the size of T times n.)  Another way of saying this is that
the structure must terminate on the same alignment boundary on which it
started; that is, if the structure must begin on an even byte boundary,
it must also end on an even byte boundary." 

The text in the book goes on for several paragraphs more, providing
examples and such.  See page 107, section 5.6.7 titled
"Sizes of Structures" for the complete story.
-- 
Arthur W. Neilson III		| INET: art at pilikia.pegasus.com
Bank of Hawaii Tech Support	| UUCP: uunet!ucsd!nosc!pilikia!art



More information about the Comp.unix.sysv386 mailing list