Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

/dev/disk and /dev/rdisk behavior



The following code snippet( when run with superuser privileges ) reads 8 bytes from the /dev/rdisk0. I would expect that since rdisk is a character device I can do this. But I get an invalid argument error from the read() call. If I increase the number of bytes I read to >=512 (disk sector size?) then the read() call will succeed.
If I read() from /dev/disk0, I can make an 8 byte read() fine, but being a block oriented device this seems counter-intuitive to me.
Is this the intended behavior of /dev/rdisk devices, or is this a bug?

Thanks,
Matt Darland
email@hidden

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int
main( void )
{
int fd;

if( (fd = open( "/dev/rdisk0", O_RDONLY )) < 0 )
{
fprintf( stderr, "open() error %d: %s\n", errno, strerror( errno ) );
}
else
{
int readval;
char buf[ 8 ];

/* Only works if sizeof( buf ) >= 512, or we opened /dev/disk0. */
if( (readval = read( fd, buf, sizeof( buf ) )) < 0 )
{
fprintf(
stderr, "read() error %d: %s\n", errno, strerror( errno ) );
}
else
{
int i;

fprintf( stderr, "Read %d bytes:\n", readval );
for( i = 0; i < (int)(sizeof( buf ) / sizeof( buf[ 0 ] )); ++i )
{
fprintf( stderr, "%02x ", (int)buf[ i ] );
}

fprintf( stderr, "\n" );
}
}

return( 0 );
}
_______________________________________________
darwin-development mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/darwin-development
Do not post admin requests to the list. They will be ignored.



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.