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

Re: C programming question



On Tuesday 13 April 2010 17:16:03 Stephen Powell wrote:
> What I need to do is to have two structures overlay each other; so that
>  they occupy the same storage.  To be specific, here is a structure which
>  describes the volume label for an OS-formatted disk:
> 
> struct __attribute__ ((packed)) volume_label {
>         char volkey[4];         /* volume key = volume label */
[...]
> };
> 
> And here is a structure which describes the volume label for a
>  CMS-formatted disk:
> 
> struct __attribute__ ((packed)) cms_label {
>         char label_id[4];       /* Label identifier */
[...]
> };
> 

union any_label {
	struct volume_label vl;
	struct cms_label    cl;
};

> Note that both structures have as their first member a character variable
> of length 4.  In the case of the "volume_label" structure it is "volkey"
> and in the case of the "cms_label" structure it is "label_id".  If the
> value of this variable is "VOL1" (in EBCDIC) then it is the first structure
> which maps the storage.  If the value of this variable is "CMS1" (in
>  EBCDIC) then it is the second structure which maps the storage.  The
>  volume_label structure is apparently a based structure, as references to
>  the volkey variable look something like this:

union any_label *label = /* Initialize somehow */;
struct volume_label *maybe_vl = &label->vl;
struct cms_label *maybe_cl = &label->cl;

if (strncmp(maybe_vl->volkey, "VOL1", 4) == 0) {
    maybe_cl = NULL;
    /* Use maybe_vl all you want, e.g. */
    maybe_vl->security = 0xFF;
} else if (strncmp(maybe_cl->label_id, "CMS1", 4) == 0) {
    maybe_vl = NULL;
    /* Use maybe_cl all you want, e.g. */
    printf("%lu\n", (unsigned long) maybe_cl->disk_offset);
} else {
    assert(("Unrecognized disk!", 0));
}

Hope that helps.
-- 
Boyd Stephen Smith Jr.           	 ,= ,-_-. =.
bss@iguanasuicide.net            	((_/)o o(\_))
ICQ: 514984 YM/AIM: DaTwinkDaddy 	 `-'(. .)`-'
http://iguanasuicide.net/        	     \_/

Attachment: signature.asc
Description: This is a digitally signed message part.


Reply to: