• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: ioctl
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ioctl


  • Subject: Re: ioctl
  • From: Mark Day <email@hidden>
  • Date: Wed, 24 Apr 2002 16:04:26 -0700

This following code was built under Mac OS X 10.1.4, but I assume it would work in Darwin. It will print the block size and block count for a disk device (character or block special) that you pass on the command line.

-Mark

#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <dev/disk.h>

static void diskinfo(char *path)
{
int err;
int fd;
int block_size;
int block_count;
u_int64_t block_count64;

struct stat st;

err = stat(path, &st);
if (err)
{
fprintf(stderr, "Can't open %s: %s (%d)\n", path, strerror(errno), errno);
return;
}

if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
{
fprintf(stderr, "Not a special device: %s\n", path);
return;
}

fd = open(path, O_RDONLY, 0);
if (fd < 0)
{
fprintf(stderr, "Can't open %s: %s (%d)\n", path, strerror(errno), errno);
return;
}

err = ioctl(fd, DKIOCGETBLOCKSIZE, &block_size);
if (err)
{
fprintf(stderr, "Can't get block size of %s: %s (%d)\n", path, strerror(errno), errno);
goto EXIT;
}
else
{
printf("%s: block size = %d\n", path, block_size);
}

err = ioctl(fd, DKIOCGETBLOCKCOUNT, &block_count);
if (err)
{
fprintf(stderr, "Can't get block count of %s: %s (%d)\n", path, strerror(errno), errno);
goto EXIT;
}
else
{
printf("%s: block count = %d\n", path, block_count);
}

EXIT:
(void) close(fd);
}

int main(int argc, char *argv[])
{
int i;

if (argc>1)
{
for (i=1; i<argc; ++i)
diskinfo(argv[i]);
}
else
{
printf("DKIOCGETBLOCKSIZE = 0xX\n", DKIOCGETBLOCKSIZE);
printf("DKIOCGETBLOCKCOUNT = 0xX\n", DKIOCGETBLOCKCOUNT);
}
}

On Wednesday, April 24, 2002, at 01:30 PM, devdrvr wrote:

There is pretty standard C ioctl call we need to make to access and get info
back from connected ATA disk drives. After you do an open the call takes a
string of /dev/... for the exact drive you want. We can't seem to get this
call to come back with anything but an error. Is anyone else using it or can
attest to it working? Or is there sample code I can look at that shows how
to properly address devices with the call? We've got the right Darwin
headers etc.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >ioctl (From: devdrvr <email@hidden>)

  • Prev by Date: Re: Application, Home, ... Folders
  • Next by Date: Re: Varying Row Heights
  • Previous by thread: ioctl
  • Next by thread: Re: ioctl
  • Index(es):
    • Date
    • Thread