Re: c question: *printf'ing arrays

2009-07-08 Thread Alexander Best
thx for all the great help guys.

cheers,
alex

Carlos A. M. dos Santos schrieb am 2009-07-02:
> 2009/7/2 Dag-Erling Smørgrav :
> > Alexander Best  writes:
> >>     for (i=0; i < sizeof(hdr->nintendo_logo); i++)
> >>         fprintf(stderr, "%x", hdr->nintendo_logo[i]);

> > What will this print if nintendo_logo is { 0x01, 0x02, 0x03, 0x04
> > }?

> Good catch. It will print "0x1234" but it should print "0x01020304".
> My example has the same error. The conversion specification should be
> "%02x", not just "%x".

___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"


Re: c question: *printf'ing arrays

2009-07-04 Thread Igor Mozolevsky
2009/7/4 Giorgos Keramidas :

[snip]

s/0x%/%#.2hh/g

--
Igor
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"


Re: c question: *printf'ing arrays

2009-07-04 Thread Giorgos Keramidas
On Wed, 01 Jul 2009 00:06:05 +0200 (CEST), Alexander Best 
 wrote:
> thanks for all the help. i decided to take the pill and coded all the
> fprintfs by hand. here's the result. usually i'd stick to a higher
> level languag, but i need C's inline assembly support:
>
> struct Header
> {
> u_int8_t rom_entry[4];
> u_int8_t nintendo_logo[156];

...

> fprintf(stderr, "\nNintendo Logo: 0x");
> for (i=0; i < sizeof(hdr->nintendo_logo); i++)
> fprintf(stderr, "%x", hdr->nintendo_logo[i]);

Note that %x will only print *one* digit for values smaller than 16,
i.e. printf("%x", 10) => "a", so it may be useful to use "%02x" instead.

> fprintf(stderr, "\nFixed Value: 0x");
> fprintf(stderr, "%x", hdr->fixed_val);

For multi-byte fields, it makes sense to print "0x" first and then
iterate.  For single-byte values, it's probably a tiny bit faster to go
only _once_ through for *printf() family formatter, i.e.:

- fprintf(stderr, "\nFixed Value: 0x");
- fprintf(stderr, "%x", hdr->fixed_val);
+ fprintf(stderr, "\nFixed Value: 0x%x", hdr->fixed_val);

Another nit that I noticed is that your last line doesn't end with "\n":

>fprintf(stderr, "\nJoybus Entry Point: 0x");
>for (i=0; i < sizeof(hdr->joybus_entry); i++) fprintf(stderr, "%x",
>hdr->joybus_entry[i]);

Some terminal setups will *not* output the last line if it does not
finish properly with a "\n", so it may be worth editing the code to
avoid the "\nXXX" format idiom, and go for a format style that puts
"\n" at the _end_ of output lines:

fprintf(stderr, "Nintendo Logo: 0x");
for (i = 0; i < sizeof(hdr->nintendo_logo); i++)
 fprintf(stderr, "%02x", hdr->nintendo_logo[i]);
fprintf(stderr, "\n");

fprintf(stderr, "Fixed Value: 0x%02x\n", hdr->fixed_val);

___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"


Re: c question: *printf'ing arrays

2009-07-04 Thread Giorgos Keramidas
On Tue, 30 Jun 2009 20:21:03 +0200 (CEST), Alexander Best 
 wrote:
> thanks. now the output gets redirected using >. i'm quite new to programming
> under unix. sorry for the inconvenience.
>
> so i guess there is no really easy way to output an inhomogeneous struct to
> stdout without using a loop to output each array contained in the struct.

No not really.  You have to do the sizeof() dance.

___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"


Re: c question: *printf'ing arrays

2009-07-02 Thread Carlos A. M. dos Santos
2009/7/2 Dag-Erling Smørgrav :
> Alexander Best  writes:
>>     for (i=0; i < sizeof(hdr->nintendo_logo); i++)
>>         fprintf(stderr, "%x", hdr->nintendo_logo[i]);
>
> What will this print if nintendo_logo is { 0x01, 0x02, 0x03, 0x04 }?

Good catch. It will print "0x1234" but it should print "0x01020304".
My example has the same error. The conversion specification should be
"%02x", not just "%x".

-- 
My preferred quotation of Robert Louis Stevenson is "You cannot
make an omelette without breaking eggs". Not because I like the
omelettes, but because I like the sound of eggs being broken.
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"


Re: c question: *printf'ing arrays

2009-07-02 Thread Dag-Erling Smørgrav
Alexander Best  writes:
> for (i=0; i < sizeof(hdr->nintendo_logo); i++)
> fprintf(stderr, "%x", hdr->nintendo_logo[i]);

What will this print if nintendo_logo is { 0x01, 0x02, 0x03, 0x04 }?

DES
-- 
Dag-Erling Smørgrav - d...@des.no
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"


Re: c question: *printf'ing arrays

2009-07-01 Thread Carlos A. M. dos Santos
On Tue, Jun 30, 2009 at 7:06 PM, Alexander
Best wrote:
> thanks for all the help. i decided to take the pill and coded all the fprintfs
> by hand. here's the result. usually i'd stick to a higher level languag, but i
> need C's inline assembly support:
>
>    struct Header
>    {
>        u_int8_t rom_entry[4];
>        u_int8_t nintendo_logo[156];
>        u_char game_title[12];
>        u_char game_code[4];
>        u_char maker_code[2];
>        u_int8_t fixed_val;
>        u_int8_t unit_code;
>        u_int8_t device_type;
>        u_int8_t reserved_area1[7];
>        u_int8_t software_version;
>        u_int8_t complement_check;
>        u_int8_t reserved_area2;
>        u_int8_t ram_entry[4];
>        u_int8_t boot_mode;
>        u_int8_t slave_id;
>        u_int8_t unused_area[26];
>        u_int8_t joybus_entry[4];
>    };
>
>    struct Header * hdr = rom;
>    int i;
>
>    fprintf(stderr, "ROM Entry: 0x");
>    for (i=0; i < 4; i++) fprintf(stderr, "%x", hdr->rom_entry[i]);
>    fprintf(stderr, "\nNintendo Logo: 0x");
>    for (i=0; i < sizeof(hdr->nintendo_logo); i++) fprintf(stderr, "%x",
>    hdr->nintendo_logo[i]);
>    fprintf(stderr, "\nGame Title: %s",  hdr->game_title);
>    fprintf(stderr, "\nGame Code: %s",  hdr->game_code);
>    fprintf(stderr, "\nMaker Code: %s",  hdr->maker_code);
>    fprintf(stderr, "\nFixed Value: 0x");
>    fprintf(stderr, "%x", hdr->fixed_val);
>    fprintf(stderr, "\nUnit Code: 0x");
>    fprintf(stderr, "%x", hdr->unit_code);
>    fprintf(stderr, "\nDevice Type: 0x");
>    fprintf(stderr, "%x", hdr->device_type);
>    fprintf(stderr, "\nReserved Area: 0x");
>    for (i=0; i < sizeof(hdr->reserved_area1); i++) fprintf(stderr, "%x",
>    hdr->reserved_area1[i]);
>    fprintf(stderr, "\nSoftware Version: 0x");
>    fprintf(stderr, "%x", hdr->software_version);
>    fprintf(stderr, "\nComplement Check: 0x");
>    fprintf(stderr, "%x", hdr->complement_check);
>    fprintf(stderr, "\nReserved Area: 0x");
>    fprintf(stderr, "%x", hdr->reserved_area2);
>    fprintf(stderr, "\nRAM Entry Point: 0x");
>    for (i=0; i < sizeof(hdr->ram_entry); i++) fprintf(stderr, "%x",
>    hdr->ram_entry[i]);
>    fprintf(stderr, "\nBoot Mode: 0x");
>    fprintf(stderr, "%x", hdr->boot_mode);
>    fprintf(stderr, "\nSlave ID: 0x");
>    fprintf(stderr, "%x", hdr->slave_id);
>    fprintf(stderr, "\nUnused Area: 0x");
>    for (i=0; i < sizeof(hdr->unused_area); i++) fprintf(stderr, "%x",
>    hdr->unused_area[i]);
>    fprintf(stderr, "\nJoybus Entry Point: 0x");
>    for (i=0; i < sizeof(hdr->joybus_entry); i++) fprintf(stderr, "%x",
>    hdr->joybus_entry[i]);

The code below is a bit more efficient and easier to read. It is also
safer, since it limits the width of the %s conversions to the sizes of
the corresponding arrays.

fprintf(stderr, "ROM Entry: 0x");
for (i=0; i < 4; i++)
fprintf(stderr, "%x", hdr->rom_entry[i]);
fprintf(stderr, "\nNintendo Logo: 0x");
for (i=0; i < sizeof(hdr->nintendo_logo); i++)
fprintf(stderr, "%x", hdr->nintendo_logo[i]);
fprintf(stderr,
"\nGame Title: %.*s"
"\nGame Code: %.*s"
"\nMaker Code: %.*s"
"\nFixed Value: 0x%x"
"\nUnit Code: 0x%x"
"\nDevice Type: 0x%x"
"\nReserved Area: 0x",
sizeof(hdr->game_title), hdr->game_title,
sizeof(hdr->game_code), hdr->game_code,
sizeof(hdr->maker_code), hdr->maker_code,
hdr->fixed_val,
hdr->unit_code,
hdr->device_type);
for (i=0; i < sizeof(hdr->reserved_area1); i++)
fprintf(stderr, "%x", hdr->reserved_area1[i]);
fprintf(stderr,
"\nSoftware Version: 0x%x"
"\nComplement Check: 0x%x"
"\nReserved Area: 0x%x"
"\nRAM Entry Point: 0x",
hdr->software_version,
hdr->complement_check,
hdr->reserved_area2);
for (i=0; i < sizeof(hdr->ram_entry); i++)
fprintf(stderr, "%x", hdr->ram_entry[i]);
fprintf(stderr,
"\nBoot Mode: 0x""%x"
"\nSlave ID: 0x""%x"
"\nUnused Area: 0x",
hdr->boot_mode,
hdr->slave_id);
for (i=0; i < sizeof(hdr->unused_area); i++)
fprintf(stderr, "%x", hdr->unused_area[i]);
fprintf(stderr, "\nJoybus Entry Point: 0x");
for (i=0; i < sizeof(hdr->joybus_entry); i++)
fprintf(stderr, "%x", hdr->joybus_entry[i]);
fprintf(stderr, "\n");



-- 
My preferred quotation of Robert Louis Stevenson is "You cannot
make an omelette without breaking eggs". Not because I like the
omelettes, but because I like the sound of eggs being broken.
___
freebsd-hackers@freebsd.org mailing list
http://lists.fr

Re: c question: *printf'ing arrays

2009-07-01 Thread Carlos A. M. dos Santos
On Tue, Jun 30, 2009 at 7:54 PM, Alfred Perlstein wrote:
> Hey Alex,
>
> People frown on macros, but this could be a good one:
>
> #define SPRINT(f, fmt) \
>        do {\
>                for (_i = 0; _i < sizeof(f)/sizeof(f[0]); i++) \
>                        printf(fmt, f[i]); \
>        }while(0)
>
> :D
>
> This should allow you to point to any _array_ and print each
> element of it using format "fmt".
>
> Example:
> SPRINT(Header->game_title, "%c");

Yes, it works, but using a loop to print a character array one char at
a time is terribly inefficient.

-- 
My preferred quotation of Robert Louis Stevenson is "You cannot
make an omelette without breaking eggs". Not because I like the
omelettes, but because I like the sound of eggs being broken.
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"


Re: c question: *printf'ing arrays

2009-06-30 Thread Alexander Best
wow. thanks. that's looking really nice. i'll change my sources tomorrow after
a good dose of sleep. ;)

alex

Alfred Perlstein schrieb am 2009-07-01:
> Hey Alex,

> People frown on macros, but this could be a good one:

> #define SPRINT(f, fmt) \
> do {\
> for (_i = 0; _i < sizeof(f)/sizeof(f[0]); i++) \
> printf(fmt, f[i]); \
> }while(0)

> :D

> This should allow you to point to any _array_ and print each
> element of it using format "fmt".

> Example:
> SPRINT(Header->game_title, "%c");



> -Alfred


> * Alexander Best  [090630 15:06]
>   wrote:
> > thanks for all the help. i decided to take the pill and coded all
> > the fprintfs
> > by hand. here's the result. usually i'd stick to a higher level
> > languag, but i
> > need C's inline assembly support:

> > struct Header
> > {
> > u_int8_t rom_entry[4];
> > u_int8_t nintendo_logo[156];
> > u_char game_title[12];
> > u_char game_code[4];
> > u_char maker_code[2];
> > u_int8_t fixed_val;
> > u_int8_t unit_code;
> > u_int8_t device_type;
> > u_int8_t reserved_area1[7];
> > u_int8_t software_version;
> > u_int8_t complement_check;
> > u_int8_t reserved_area2;
> > u_int8_t ram_entry[4];
> > u_int8_t boot_mode;
> > u_int8_t slave_id;
> > u_int8_t unused_area[26];
> > u_int8_t joybus_entry[4];
> > };

> > struct Header * hdr = rom;
> > int i;

> > fprintf(stderr, "ROM Entry: 0x");
> > for (i=0; i < 4; i++) fprintf(stderr, "%x", hdr->rom_entry[i]);
> > fprintf(stderr, "\nNintendo Logo: 0x");
> > for (i=0; i < sizeof(hdr->nintendo_logo); i++) fprintf(stderr,
> > "%x",
> > hdr->nintendo_logo[i]);
> > fprintf(stderr, "\nGame Title: %s",  hdr->game_title);
> > fprintf(stderr, "\nGame Code: %s",  hdr->game_code);
> > fprintf(stderr, "\nMaker Code: %s",  hdr->maker_code);
> > fprintf(stderr, "\nFixed Value: 0x");
> > fprintf(stderr, "%x", hdr->fixed_val);
> > fprintf(stderr, "\nUnit Code: 0x");
> > fprintf(stderr, "%x", hdr->unit_code);
> > fprintf(stderr, "\nDevice Type: 0x");
> > fprintf(stderr, "%x", hdr->device_type);
> > fprintf(stderr, "\nReserved Area: 0x");
> > for (i=0; i < sizeof(hdr->reserved_area1); i++) fprintf(stderr,
> > "%x",
> > hdr->reserved_area1[i]);
> > fprintf(stderr, "\nSoftware Version: 0x");
> > fprintf(stderr, "%x", hdr->software_version);
> > fprintf(stderr, "\nComplement Check: 0x");
> > fprintf(stderr, "%x", hdr->complement_check);
> > fprintf(stderr, "\nReserved Area: 0x");
> > fprintf(stderr, "%x", hdr->reserved_area2);
> > fprintf(stderr, "\nRAM Entry Point: 0x");
> > for (i=0; i < sizeof(hdr->ram_entry); i++) fprintf(stderr,
> > "%x",
> > hdr->ram_entry[i]);
> > fprintf(stderr, "\nBoot Mode: 0x");
> > fprintf(stderr, "%x", hdr->boot_mode);
> > fprintf(stderr, "\nSlave ID: 0x");
> > fprintf(stderr, "%x", hdr->slave_id);
> > fprintf(stderr, "\nUnused Area: 0x");
> > for (i=0; i < sizeof(hdr->unused_area); i++) fprintf(stderr,
> > "%x",
> > hdr->unused_area[i]);
> > fprintf(stderr, "\nJoybus Entry Point: 0x");
> > for (i=0; i < sizeof(hdr->joybus_entry); i++) fprintf(stderr,
> > "%x",
> > hdr->joybus_entry[i]);

> > cheers.

> > Rick C. Petty schrieb am 2009-06-30:
> > > On Tue, Jun 30, 2009 at 08:21:03PM +0200, Alexander Best wrote:
> > > > thanks. now the output gets redirected using >. i'm quite new
> > > > to
> > > > programming
> > > > under unix. sorry for the inconvenience.

> > > No problem; we all had to learn sometime.  But what I suggested
> > > should
> > > work for every platform that adheres to POSIX.  If you were using
> > > fprintf/fwrite, then it would work on anything that's standard C.
> > > As
> > > for
> > > redirection, windows command line allows the same type of
> > > redirection.

> > > > so i guess there is no really easy way to output an
> > > > inhomogeneous
> > > > struct to
> > > > stdout without using a loop to output each array contained in
> > > > the
> > > > struct.

> > > That's not something C would ever provide easily.  You may want
> > > to
> > > use a
> > > different high-level language.  However, I often use macros for
> > > printing
> > > pieces of structures, for example I used this to print out sizes
> > > of
> > > kernel
> > > structures:

> > > #define SIZE(astruct, member) \
> > > printf("%d\t\t.%s\n", sizeof(astruct.member), #member)

> > > #include 
> > > ...
> > > struct ktr_header header;
> > > struct ktr_genio genio;

> > > printf("%d\tktr_header:\n", sizeof(header));
> > > SIZE(header, ktr_len);
> > > SIZE(header, ktr_type);
> > > SIZE(header, ktr_pid);
> > > SIZE(header, ktr_comm);
> > > SIZE(header, ktr_time);
> > > SIZE(header, ktr_t

Re: c question: *printf'ing arrays

2009-06-30 Thread Alfred Perlstein
Hey Alex,

People frown on macros, but this could be a good one:

#define SPRINT(f, fmt) \
do {\
for (_i = 0; _i < sizeof(f)/sizeof(f[0]); i++) \
printf(fmt, f[i]); \
}while(0)

:D

This should allow you to point to any _array_ and print each
element of it using format "fmt".

Example:
SPRINT(Header->game_title, "%c");



-Alfred


* Alexander Best  [090630 15:06] wrote:
> thanks for all the help. i decided to take the pill and coded all the fprintfs
> by hand. here's the result. usually i'd stick to a higher level languag, but i
> need C's inline assembly support:
> 
> struct Header
> {
> u_int8_t rom_entry[4];
> u_int8_t nintendo_logo[156];
> u_char game_title[12];
> u_char game_code[4];
> u_char maker_code[2];
> u_int8_t fixed_val;
> u_int8_t unit_code;
> u_int8_t device_type;
> u_int8_t reserved_area1[7];
> u_int8_t software_version;
> u_int8_t complement_check;
> u_int8_t reserved_area2;
> u_int8_t ram_entry[4];
> u_int8_t boot_mode;
> u_int8_t slave_id;
> u_int8_t unused_area[26];
> u_int8_t joybus_entry[4];
> };
> 
> struct Header * hdr = rom;
> int i;
> 
> fprintf(stderr, "ROM Entry: 0x");
> for (i=0; i < 4; i++) fprintf(stderr, "%x", hdr->rom_entry[i]);
> fprintf(stderr, "\nNintendo Logo: 0x");
> for (i=0; i < sizeof(hdr->nintendo_logo); i++) fprintf(stderr, "%x",
> hdr->nintendo_logo[i]);
> fprintf(stderr, "\nGame Title: %s",  hdr->game_title);
> fprintf(stderr, "\nGame Code: %s",  hdr->game_code);
> fprintf(stderr, "\nMaker Code: %s",  hdr->maker_code);
> fprintf(stderr, "\nFixed Value: 0x");
> fprintf(stderr, "%x", hdr->fixed_val);
> fprintf(stderr, "\nUnit Code: 0x");
> fprintf(stderr, "%x", hdr->unit_code);
> fprintf(stderr, "\nDevice Type: 0x");
> fprintf(stderr, "%x", hdr->device_type);
> fprintf(stderr, "\nReserved Area: 0x");
> for (i=0; i < sizeof(hdr->reserved_area1); i++) fprintf(stderr, "%x",
> hdr->reserved_area1[i]);
> fprintf(stderr, "\nSoftware Version: 0x");
> fprintf(stderr, "%x", hdr->software_version);
> fprintf(stderr, "\nComplement Check: 0x");
> fprintf(stderr, "%x", hdr->complement_check);
> fprintf(stderr, "\nReserved Area: 0x");
> fprintf(stderr, "%x", hdr->reserved_area2);
> fprintf(stderr, "\nRAM Entry Point: 0x");
> for (i=0; i < sizeof(hdr->ram_entry); i++) fprintf(stderr, "%x",
> hdr->ram_entry[i]);
> fprintf(stderr, "\nBoot Mode: 0x");
> fprintf(stderr, "%x", hdr->boot_mode);
> fprintf(stderr, "\nSlave ID: 0x");
> fprintf(stderr, "%x", hdr->slave_id);
> fprintf(stderr, "\nUnused Area: 0x");
> for (i=0; i < sizeof(hdr->unused_area); i++) fprintf(stderr, "%x",
> hdr->unused_area[i]);
> fprintf(stderr, "\nJoybus Entry Point: 0x");
> for (i=0; i < sizeof(hdr->joybus_entry); i++) fprintf(stderr, "%x",
> hdr->joybus_entry[i]);
> 
> cheers.
> 
> Rick C. Petty schrieb am 2009-06-30:
> > On Tue, Jun 30, 2009 at 08:21:03PM +0200, Alexander Best wrote:
> > > thanks. now the output gets redirected using >. i'm quite new to
> > > programming
> > > under unix. sorry for the inconvenience.
> 
> > No problem; we all had to learn sometime.  But what I suggested
> > should
> > work for every platform that adheres to POSIX.  If you were using
> > fprintf/fwrite, then it would work on anything that's standard C.  As
> > for
> > redirection, windows command line allows the same type of
> > redirection.
> 
> > > so i guess there is no really easy way to output an inhomogeneous
> > > struct to
> > > stdout without using a loop to output each array contained in the
> > > struct.
> 
> > That's not something C would ever provide easily.  You may want to
> > use a
> > different high-level language.  However, I often use macros for
> > printing
> > pieces of structures, for example I used this to print out sizes of
> > kernel
> > structures:
> 
> > #define SIZE(astruct, member) \
> > printf("%d\t\t.%s\n", sizeof(astruct.member), #member)
> 
> > #include 
> > ...
> > struct ktr_header header;
> > struct ktr_genio genio;
> 
> > printf("%d\tktr_header:\n", sizeof(header));
> > SIZE(header, ktr_len);
> > SIZE(header, ktr_type);
> > SIZE(header, ktr_pid);
> > SIZE(header, ktr_comm);
> > SIZE(header, ktr_time);
> > SIZE(header, ktr_time.tv_sec);
> > SIZE(header, ktr_time.tv_sec);
> > SIZE(header, ktr_tid);
> 
> > printf("\n%d\tktr_genio:\n", sizeof(genio));
> > SIZE(genio, ktr_fd);
> > SIZE(genio, ktr_rw);
> 
> > In your case, you could make a macro for each type.  Without an
> > example of
> > how you want the output to look, it's hard for us to show you code
> > that
> > will produce such output.
> 
> > -- Rick C. Petty
>

Re: c question: *printf'ing arrays

2009-06-30 Thread Alexander Best
thanks for all the help. i decided to take the pill and coded all the fprintfs
by hand. here's the result. usually i'd stick to a higher level languag, but i
need C's inline assembly support:

struct Header
{
u_int8_t rom_entry[4];
u_int8_t nintendo_logo[156];
u_char game_title[12];
u_char game_code[4];
u_char maker_code[2];
u_int8_t fixed_val;
u_int8_t unit_code;
u_int8_t device_type;
u_int8_t reserved_area1[7];
u_int8_t software_version;
u_int8_t complement_check;
u_int8_t reserved_area2;
u_int8_t ram_entry[4];
u_int8_t boot_mode;
u_int8_t slave_id;
u_int8_t unused_area[26];
u_int8_t joybus_entry[4];
};

struct Header * hdr = rom;
int i;

fprintf(stderr, "ROM Entry: 0x");
for (i=0; i < 4; i++) fprintf(stderr, "%x", hdr->rom_entry[i]);
fprintf(stderr, "\nNintendo Logo: 0x");
for (i=0; i < sizeof(hdr->nintendo_logo); i++) fprintf(stderr, "%x",
hdr->nintendo_logo[i]);
fprintf(stderr, "\nGame Title: %s",  hdr->game_title);
fprintf(stderr, "\nGame Code: %s",  hdr->game_code);
fprintf(stderr, "\nMaker Code: %s",  hdr->maker_code);
fprintf(stderr, "\nFixed Value: 0x");
fprintf(stderr, "%x", hdr->fixed_val);
fprintf(stderr, "\nUnit Code: 0x");
fprintf(stderr, "%x", hdr->unit_code);
fprintf(stderr, "\nDevice Type: 0x");
fprintf(stderr, "%x", hdr->device_type);
fprintf(stderr, "\nReserved Area: 0x");
for (i=0; i < sizeof(hdr->reserved_area1); i++) fprintf(stderr, "%x",
hdr->reserved_area1[i]);
fprintf(stderr, "\nSoftware Version: 0x");
fprintf(stderr, "%x", hdr->software_version);
fprintf(stderr, "\nComplement Check: 0x");
fprintf(stderr, "%x", hdr->complement_check);
fprintf(stderr, "\nReserved Area: 0x");
fprintf(stderr, "%x", hdr->reserved_area2);
fprintf(stderr, "\nRAM Entry Point: 0x");
for (i=0; i < sizeof(hdr->ram_entry); i++) fprintf(stderr, "%x",
hdr->ram_entry[i]);
fprintf(stderr, "\nBoot Mode: 0x");
fprintf(stderr, "%x", hdr->boot_mode);
fprintf(stderr, "\nSlave ID: 0x");
fprintf(stderr, "%x", hdr->slave_id);
fprintf(stderr, "\nUnused Area: 0x");
for (i=0; i < sizeof(hdr->unused_area); i++) fprintf(stderr, "%x",
hdr->unused_area[i]);
fprintf(stderr, "\nJoybus Entry Point: 0x");
for (i=0; i < sizeof(hdr->joybus_entry); i++) fprintf(stderr, "%x",
hdr->joybus_entry[i]);

cheers.

Rick C. Petty schrieb am 2009-06-30:
> On Tue, Jun 30, 2009 at 08:21:03PM +0200, Alexander Best wrote:
> > thanks. now the output gets redirected using >. i'm quite new to
> > programming
> > under unix. sorry for the inconvenience.

> No problem; we all had to learn sometime.  But what I suggested
> should
> work for every platform that adheres to POSIX.  If you were using
> fprintf/fwrite, then it would work on anything that's standard C.  As
> for
> redirection, windows command line allows the same type of
> redirection.

> > so i guess there is no really easy way to output an inhomogeneous
> > struct to
> > stdout without using a loop to output each array contained in the
> > struct.

> That's not something C would ever provide easily.  You may want to
> use a
> different high-level language.  However, I often use macros for
> printing
> pieces of structures, for example I used this to print out sizes of
> kernel
> structures:

> #define SIZE(astruct, member) \
> printf("%d\t\t.%s\n", sizeof(astruct.member), #member)

> #include 
> ...
> struct ktr_header header;
> struct ktr_genio genio;

> printf("%d\tktr_header:\n", sizeof(header));
> SIZE(header, ktr_len);
> SIZE(header, ktr_type);
> SIZE(header, ktr_pid);
> SIZE(header, ktr_comm);
> SIZE(header, ktr_time);
> SIZE(header, ktr_time.tv_sec);
> SIZE(header, ktr_time.tv_sec);
> SIZE(header, ktr_tid);

> printf("\n%d\tktr_genio:\n", sizeof(genio));
> SIZE(genio, ktr_fd);
> SIZE(genio, ktr_rw);

> In your case, you could make a macro for each type.  Without an
> example of
> how you want the output to look, it's hard for us to show you code
> that
> will produce such output.

> -- Rick C. Petty
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"


Re: c question: *printf'ing arrays

2009-06-30 Thread Rick C. Petty
On Tue, Jun 30, 2009 at 08:21:03PM +0200, Alexander Best wrote:
> thanks. now the output gets redirected using >. i'm quite new to programming
> under unix. sorry for the inconvenience.

No problem; we all had to learn sometime.  But what I suggested should
work for every platform that adheres to POSIX.  If you were using
fprintf/fwrite, then it would work on anything that's standard C.  As for
redirection, windows command line allows the same type of redirection.

> so i guess there is no really easy way to output an inhomogeneous struct to
> stdout without using a loop to output each array contained in the struct.

That's not something C would ever provide easily.  You may want to use a
different high-level language.  However, I often use macros for printing
pieces of structures, for example I used this to print out sizes of kernel
structures:

#define SIZE(astruct, member) \
printf("%d\t\t.%s\n", sizeof(astruct.member), #member)

#include 
...
struct ktr_header header;
struct ktr_genio genio;

printf("%d\tktr_header:\n", sizeof(header));
SIZE(header, ktr_len);
SIZE(header, ktr_type);
SIZE(header, ktr_pid);
SIZE(header, ktr_comm);
SIZE(header, ktr_time);
SIZE(header, ktr_time.tv_sec);
SIZE(header, ktr_time.tv_sec);
SIZE(header, ktr_tid);

printf("\n%d\tktr_genio:\n", sizeof(genio));
SIZE(genio, ktr_fd);
SIZE(genio, ktr_rw);

In your case, you could make a macro for each type.  Without an example of
how you want the output to look, it's hard for us to show you code that
will produce such output.

-- Rick C. Petty
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"


Re: c question: *printf'ing arrays

2009-06-30 Thread Stephen Montgomery-Smith

Alexander Best wrote:

thanks. now the output gets redirected using >. i'm quite new to programming
under unix. sorry for the inconvenience.

so i guess there is no really easy way to output an inhomogeneous struct to
stdout without using a loop to output each array contained in the struct.

cheers.


I think you are asking for a print command, that when given a structure, 
can look at the data type of each element of the structure, and produces 
the appropriate print command for each data type.


I think what you are asking for is antithetical to the philosophy of C - 
for example, the printf command demands that you tell the printf command 
what are the data types of the data you give to it.


So I think that the kind of long winded print command you are advocating 
is the correct way to do it.


Stephen
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"


Re: c question: *printf'ing arrays

2009-06-30 Thread Alexander Best
thanks. now the output gets redirected using >. i'm quite new to programming
under unix. sorry for the inconvenience.

so i guess there is no really easy way to output an inhomogeneous struct to
stdout without using a loop to output each array contained in the struct.

cheers.

Rick C. Petty schrieb am 2009-06-30:
> On Tue, Jun 30, 2009 at 08:03:21PM +0200, Alexander Best wrote:
> > should be stdout.


> > struct Header *hdr = rom;

> > int new_fd = open("/dev/stdout", O_RDWR);
> > printf("SIZE: %d\n",sizeof(*hdr));
> > write(new_fd, hdr, sizeof(*hdr));
> > close(new_fd);

> Why are you reopening stdout?  It should already be open, so use
> "fileno(stdout)" or just plain "STDOUT_FILENO" instead of "new_fd".

> -- Rick C. Petty
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"


Re: c question: *printf'ing arrays

2009-06-30 Thread Igor Mozolevsky
2009/6/30 Alexander Best :
> should be stdout.
>
>
> struct Header *hdr = rom;
>
> int new_fd = open("/dev/stdout", O_RDWR);
>
> printf("SIZE: %d\n",sizeof(*hdr));
>
> write(new_fd, hdr, sizeof(*hdr));
>
> close(new_fd);

You should really be checking what open returns, opening /dev/stdout
for reading is a bit weird not sure if that would work, and most
likely it's already open... Just use fileno(...):-

#include 
#include 

int main(void) {
  write(fileno(stdout), "Hello world!\n", 13);
  return 0;
}

Cheers,
--
Igor
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"


Re: c question: *printf'ing arrays

2009-06-30 Thread Rick C. Petty
On Tue, Jun 30, 2009 at 08:03:21PM +0200, Alexander Best wrote:
> should be stdout.
> 
> 
> struct Header *hdr = rom;
> 
> int new_fd = open("/dev/stdout", O_RDWR);
> printf("SIZE: %d\n",sizeof(*hdr));
> write(new_fd, hdr, sizeof(*hdr));
> close(new_fd);

Why are you reopening stdout?  It should already be open, so use
"fileno(stdout)" or just plain "STDOUT_FILENO" instead of "new_fd".

-- Rick C. Petty
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"


Re: c question: *printf'ing arrays

2009-06-30 Thread Alexander Best
should be stdout.


struct Header *hdr = rom;

int new_fd = open("/dev/stdout", O_RDWR);

printf("SIZE: %d\n",sizeof(*hdr));

write(new_fd, hdr, sizeof(*hdr));

close(new_fd);

Igor Mozolevsky schrieb am 2009-06-30:
> 2009/6/30 Alexander Best :
> > that works, but i really want to have a pretty output to stdout. i
> > guess i
> > have to stick with printf and use `for (i=0; i < sizeof(XXX); i++)`
> > for each
> > array in the struct. just thought i could avoid it.

> > btw. `./my-program | hexdump` works, but if i do `./my-program >
> > output`
> > output is being created, but is empty. is this normal?

> Depends if you output to stdout or stderr --- `>' redirects stdout.


> Cheers,
> --
> Igor
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"


Re: c question: *printf'ing arrays

2009-06-30 Thread Igor Mozolevsky
2009/6/30 Alexander Best :
> that works, but i really want to have a pretty output to stdout. i guess i
> have to stick with printf and use `for (i=0; i < sizeof(XXX); i++)` for each
> array in the struct. just thought i could avoid it.
>
> btw. `./my-program | hexdump` works, but if i do `./my-program > output`
> output is being created, but is empty. is this normal?

Depends if you output to stdout or stderr --- `>' redirects stdout.


Cheers,
--
Igor
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"


Re: c question: *printf'ing arrays

2009-06-30 Thread Igor Mozolevsky
2009/6/30 Alexander Best :
> thanks. but that simply dumps the contents of the struct to stdout. but since
> most of the struct's contents aren't ascii the output isn't really of much
> use.

How about ./your-program | hexdump ?

--
Igor
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"


Re: c question: *printf'ing arrays

2009-06-30 Thread Alexander Best
that works, but i really want to have a pretty output to stdout. i guess i
have to stick with printf and use `for (i=0; i < sizeof(XXX); i++)` for each
array in the struct. just thought i could avoid it.

btw. `./my-program | hexdump` works, but if i do `./my-program > output`
output is being created, but is empty. is this normal?

cheers.

Igor Mozolevsky schrieb am 2009-06-30:
> 2009/6/30 Alexander Best :
> > thanks. but that simply dumps the contents of the struct to stdout.
> > but since
> > most of the struct's contents aren't ascii the output isn't really
> > of much
> > use.

> How about ./your-program | hexdump ?

> --
> Igor
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"


Re: c question: *printf'ing arrays

2009-06-30 Thread Alexander Best
thanks. but that simply dumps the contents of the struct to stdout. but since
most of the struct's contents aren't ascii the output isn't really of much
use.

cheers.

Tom Evans schrieb am 2009-06-30:
> On Tue, 2009-06-30 at 18:12 +0200, Alexander Best wrote:
> > hi there,

> > i need to output the header of a file to stdout. the header looks
> > like this:

> > struct Header
> > {
> > u_int8_t rom_entry[4];
> > u_int8_t nintendo_logo[156];
> > u_char game_title[12];
> > u_char game_code[4];
> > u_char maker_code[2];
> > u_int8_t fixed_val;
> > u_int8_t unit_code;
> > u_int8_t device_type;
> > u_int8_t reserved_area1[7];
> > u_int8_t software_version;
> > u_int8_t complement_check;
> > u_int8_t reserved_area2;
> > u_int8_t ram_entry[4];
> > u_int8_t boot_mode;
> > u_int8_t slave_id;
> > u_int8_t unused_area[26];
> > u_int8_t joybus_entry[4];
> > };

> > if there a way to use printf or some other variant of *printf
> > without using
> > sizeof()-loops for all the arrays?

> > cheers.

> None of your arrays are dynamically sized, so surely
> write(fd, &hdr, sizeof(Header));
> would do the trick?

> Cheers

> Tom
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"


Re: c question: *printf'ing arrays

2009-06-30 Thread Tom Evans
On Tue, 2009-06-30 at 18:12 +0200, Alexander Best wrote:
> hi there,
> 
> i need to output the header of a file to stdout. the header looks like this:
> 
> struct Header
> {
> u_int8_t rom_entry[4];
> u_int8_t nintendo_logo[156];
> u_char game_title[12];
> u_char game_code[4];
> u_char maker_code[2];
> u_int8_t fixed_val;
> u_int8_t unit_code;
> u_int8_t device_type;
> u_int8_t reserved_area1[7];
> u_int8_t software_version;
> u_int8_t complement_check;
> u_int8_t reserved_area2;
> u_int8_t ram_entry[4];
> u_int8_t boot_mode;
> u_int8_t slave_id;
> u_int8_t unused_area[26];
> u_int8_t joybus_entry[4];
> };
> 
> if there a way to use printf or some other variant of *printf without using
> sizeof()-loops for all the arrays?
> 
> cheers.

None of your arrays are dynamically sized, so surely 
write(fd, &hdr, sizeof(Header));
would do the trick?

Cheers

Tom

___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"