obtain information about the size of a disk
#include <sys/types.h> #include <sys/disk.h> int disk_space( int fildes, long *free_blocks, long *tot_blocks );
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.
#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 ); }
QNX
Safety: | |
---|---|
Interrupt handler | No |
Signal handler | Yes, but modifies errno |
Thread | Yes |
disk_get_entry(), errno, open()