[Previous]
[Contents]
[Next]

disk_space()

obtain information about the size of a disk

Synopsis:

#include <sys/types.h>
#include <sys/disk.h>
int disk_space( int fildes,
                long *free_blocks,
                long *tot_blocks );

Description:

The disk_space() function obtains information about the size of the disk associated with the file descriptor fildes. Both the total disk size and free space are returned. The numbers are expressed as multiples of 512-byte blocks. The file descriptor fildes may have been obtained by a successful open() of any file or directory on the disk.

Returns:

0
Success
-1
An error occurred. errno is set to indicate the error.

Errors:

EBADF
The fildes argument is not a valid open file descriptor.
EIO
A physical I/O error occurred accessing the device while acquiring information.
ENOSYS
An attempt was made to get disk information using a file descriptor that is not associated with block-oriented device (for example, a file descriptor associated with the device manager).

Examples:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

char *disk_size( char *buf, long size )
  {
    if( size >= 2000 )
      sprintf( buf, "%ld.%1ldM",
        ( size+100 ) / 2000,
        ( ( size+100 ) % 2000 ) / 200 );
    else    /* disk less than 1M display in K */
      sprintf( buf, "%ldK", size / 2 );
    return( buf );
  }

int main( int argc, char **argv )
  {
    long     free, used, total;
    int      fd;
    char    *dsk, str1[30], str2[30];

    dsk = argv[1];
    if( ( fd = open( dsk, O_RDONLY ) ) == ( -1 ) ) {
      perror( "example" );
      exit( 1 );
    }

    if( disk_space( fd, &free, &total) == ( -1 ) ) {
      perror( "example" );
      exit( 1 );
    }

    used = total - free;
    printf( "%s free, %s used, %ld%% full",
        disk_size( str1, free ),
        disk_size( str2, used ),
        ( used * 100L / total ) );
    printf( " ( %ld free, %ld used of %ld blocks )\n",
        free, used, total );
    return( EXIT_SUCCESS );
  }

Classification:

QNX

Safety:
Interrupt handler No
Signal handler Yes, but modifies errno
Thread Yes

See also:

disk_get_entry(), errno, open()


[Previous]
[Contents]
[Next]