lseek to EOF always 0 for devices

Jens Schweikhardt schweikh at schweikhardt.net
Sat May 1 04:37:07 PDT 2004


Fellow hackers,

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. The following program prints
correct sizes for plain files and directories, but it consistently
prints 0 for block devices. If this is not supported, how would I find
the number of blocks for a given block device? I don't want to mess with
parsing disklabels and vinum configs...

#include <stdio.h>
#include <fcntl.h>
#include <unistd.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 ((len = lseek (fd, 0, SEEK_END)) == -1) {
        perror ("lseek");
        return 1;
    }

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

    close (fd);
    return 0;
}


Regards,

	Jens
-- 
Jens Schweikhardt http://www.schweikhardt.net/
SIGSIG -- signature too long (core dumped)


More information about the freebsd-hackers mailing list