Re: lseek to EOF always 0 for devices

From: Dimitry Andric (dimitry_at_andric.com)
Date: 05/01/04

  • Next message: Freddie Cash: "Re: Squid, SquidGuard, FreeBSD"
    Date: Sat, 1 May 2004 16:01:36 +0200
    To: Jens Schweikhardt <schweikh@schweikhardt.net>
    
    
    

    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 message: Freddie Cash: "Re: Squid, SquidGuard, FreeBSD"

    Relevant Pages

    • Re: Survey of sizes of types
      ... has a -sc option to specify sizeof char. ... # printsize(short int); ... Jens Schweikhardt http://www.schweikhardt.net/ ... have an appropriate newsgroups line in your header for your mail to be seen, ...
      (comp.lang.c.moderated)
    • Re: argv Memory Allocation (malloc or PL_malloc)
      ... get this Unhandled Exception error message: ... with a green pointer pointing at this line: ... argv[] should be 2 long and the last argument must be NULL. ... int cycles; ...
      (comp.lang.prolog)
    • Re: input to mex function
      ... either cell arrays with string elements or ... or you can call it like this (string arguments): ... void mexFunction(int nlhs, mxArray *plhs, int nrhs, ... // Your code goes here to use argc and argv. ...
      (comp.soft-sys.matlab)
    • Cocoa Gotcha: NSApplicationMain never returns
      ... int main{ ... return NSApplicationMain(argc, argv); ... autoreleased with no pool in place - just leaking ... just quit the program you'll see that the Quit menu command calls ...
      (comp.sys.mac.programmer.help)
    • Re: writing program from a book
      ... >> I wrote that program from a book that my compile that program ... > int main(argc, argv) ...
      (comp.lang.c)