lseek to EOF always 0 for devices

Dimitry Andric dimitry at
Sat May 1 07:03:17 PDT 2004

On 2004-05-01 at 13:37:04 Jens Schweikhardt wrote:

> I'm not sure if I'm trying something impossible here. I want to find the
> size in bytes of a disk special device, eg /dev/da0, /dev/da0s1,
> /dev/da0s1a, /dev/vinum/usr and so on.

I have no idea why lseek'ing in the devices doesn't work, but try
using ioctl(2) with DIOCGMEDIASIZE (from <sys/disk.h>), this works
fine for me:

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/disk.h>

int main (int argc, char **argv)
    int fd;
    off_t len;

    if (argc != 2)
        return 1;

    if ((fd = open (argv[1], O_RDONLY)) < 0) {
        perror (argv[1]);
        return 1;

    if (ioctl (fd, DIOCGMEDIASIZE, &len) == -1) {
        perror ("DIOCGMEDIASIZE");
        return 1;

    printf ("%lld\n", (long long)len);

    close (fd);
    return 0;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 183 bytes
Desc: not available
Url :

More information about the freebsd-hackers mailing list