Any problem with this code?

  • Follow


Hi,

The following code is as similar as the fragments showed in section 
2.15, AUP. In the code, the version should be 1 but the output is 0. 
What's the problem?

#include <sys/stat.h>
#include <fcntl.h>
#include <sys/uio.h>
#include <stdio.h>
#include <string.h>

#define PERM_FILE ( S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH )

int main( )
{
	struct header {
		int h_version;
		int h_num_items;
	} hdr, *hp;

	struct data {
		enum {TYPE_STRING, TYPE_FLOAT} d_type;
		union {
			float d_val;
			char d_str[ 20 ];
		} d_data;
	} d1, d2, *dp;

	struct iovec v[ 3 ];

	hdr.h_version = 1;
	hdr.h_num_items = 2;
	d1.d_type = TYPE_STRING;
	strcpy( d1.d_data.d_str, "Hello!" );
	d2.d_type = TYPE_FLOAT;
	d2.d_data.d_val = 123.456;

	v[ 0 ].iov_base = ( char* )&hdr;
	v[ 0 ].iov_len = sizeof( hdr );
	v[ 1 ].iov_base = ( char* )&d1;
	v[ 1 ].iov_len = sizeof( d1 );
	v[ 2 ].iov_base = ( char* )&d2;
	v[ 2 ].iov_len = sizeof( d2 );

	int fd = open( "/home/tony/Program/temp/aaa", O_WRONLY | O_TRUNC, 
PERM_FILE );
	
	int n = writev( fd, v, sizeof( v ) / sizeof( v[ 0 ] ) );

	printf( "%d\n", n );

	char *buf;
	buf = malloc( n );
	lseek( fd, 0, SEEK_SET );
	read( fd, buf, n );
	hp = buf;
	dp = ( struct data* )( hp + 1 );

	printf( "Version= %d\n", hp->h_version );

	return 0;
}
0
Reply zyzhang (10) 3/5/2008 12:25:26 PM

Tony, Zhang wrote:
> Hi,
> 
> The following code is as similar as the fragments showed in section
> 2.15, AUP. In the code, the version should be 1 but the output is 0.
> What's the problem?
> 
> #include <sys/stat.h>
> #include <fcntl.h>
> #include <sys/uio.h>
> #include <stdio.h>
> #include <string.h>
> 
> #define PERM_FILE ( S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH )
> 
> int main( )
> {
>     struct header {
>         int h_version;
>         int h_num_items;
>     } hdr, *hp;
> 
>     struct data {
>         enum {TYPE_STRING, TYPE_FLOAT} d_type;
>         union {
>             float d_val;
>             char d_str[ 20 ];
>         } d_data;
>     } d1, d2, *dp;
> 
>     struct iovec v[ 3 ];
> 
>     hdr.h_version = 1;
>     hdr.h_num_items = 2;
>     d1.d_type = TYPE_STRING;
>     strcpy( d1.d_data.d_str, "Hello!" );
>     d2.d_type = TYPE_FLOAT;
>     d2.d_data.d_val = 123.456;
> 
>     v[ 0 ].iov_base = ( char* )&hdr;
>     v[ 0 ].iov_len = sizeof( hdr );
>     v[ 1 ].iov_base = ( char* )&d1;
>     v[ 1 ].iov_len = sizeof( d1 );
>     v[ 2 ].iov_base = ( char* )&d2;
>     v[ 2 ].iov_len = sizeof( d2 );
> 
>     int fd = open( "/home/tony/Program/temp/aaa", O_WRONLY | O_TRUNC,
> PERM_FILE );
That means open only for writing
>     
>     int n = writev( fd, v, sizeof( v ) / sizeof( v[ 0 ] ) );
> 
>     printf( "%d\n", n );
> 
>     char *buf;
>     buf = malloc( n );
>     lseek( fd, 0, SEEK_SET );
>     read( fd, buf, n );
This call should fail. What does it return?

Robert

>     hp = buf;
>     dp = ( struct data* )( hp + 1 );
> 
>     printf( "Version= %d\n", hp->h_version );
> 
>     return 0;
> }
0
Reply robert.f.harris (386) 3/5/2008 3:54:57 PM


"Tony, Zhang" <zyzhang@cuhk.edu.hk> writes:

> In the code, the version should be 1 but the output is
> 0. What's the problem?

Several. You should check return values from malloc(), lseek(),
read(), etc. If you did, you'd find out that your read() fails
(hint: what do you think O_WRONLY means?).

Also, on Linux your program crashes, unless 'aaa' file already
exists (hint: perhaps you want to add O_CREAT?).

Cheers,
-- 
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
0
Reply ppluzhnikov-nsp2 (108) 3/5/2008 5:15:57 PM

I see.

I have made a foolish mistake!

Thanks a lot! Thanks for your guys very much!

Best,
Tony

Paul Pluzhnikov wrote:
> "Tony, Zhang" <zyzhang@cuhk.edu.hk> writes:
> 
>> In the code, the version should be 1 but the output is
>> 0. What's the problem?
> 
> Several. You should check return values from malloc(), lseek(),
> read(), etc. If you did, you'd find out that your read() fails
> (hint: what do you think O_WRONLY means?).
> 
> Also, on Linux your program crashes, unless 'aaa' file already
> exists (hint: perhaps you want to add O_CREAT?).
> 
> Cheers,
0
Reply zyzhang (10) 3/6/2008 2:46:26 AM

3 Replies
34 Views

(page loaded in 0.08 seconds)


Reply: