Thanks for your quick reply.
Well, I wrote df.
Cool! That means the buck stops here, since df works just fine on my
system.
Okay, I’ve posted my code in its entirety at the bottom, along with output
on our system. We are using a compact flash card for a disk-- I don’t know
much about how it is mounted, but it seems to be via NFS, since the
mounted-by call returns /net/…/dev/hd0t79.
A friend of mine with much better *NIX programming abilities suggested that
statvfs64() might return correctly, and that it should work with any file
except symbolic links, for example “.”. Anyway, what am I doing wrong here?
The code you posted works for me. What exactly is it you are trying to
do;
from previous posts I take it to be - given a partition name, get the free
space on the filesystem it is mounted as? Answer - DCMD_FSYS_MOUNTED_BY
(as in that compatability routine) and statvfs().
Simply put I am trying to find the free space on the disk I’m writing to
before I write to it.
Thanks,
Dave McMordie
Ambulatory Robotics Lab / Centre for Intelligent Machines / McGill
University
mcmordie@cim.mcgill.ca
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/statvfs.h>
#include <devctl.h>
#include <fcntl.h>
#include <sys/dcmd_blk.h>
#include <fcntl.h>
#include <share.h>
#include <string.h>
#include <sys/dcmd_blk.h>
#include <sys/iomsg.h>
// From nto migration kit:
int fsys_get_mount_pt(const char *device, char *directory)
{
union {
struct {
struct _io_devctl devctl;
} i;
struct {
struct _io_devctl_reply devctl;
char path[256];
} o;
} msg;
msg.i.devctl.type = _IO_DEVCTL;
msg.i.devctl.combine_len = sizeof(msg.i);
msg.i.devctl.dcmd = DCMD_FSYS_MOUNTED_BY;
msg.i.devctl.nbytes = DCMD_FSYS_MOUNTED_BY >> 16;
msg.i.devctl.zero = 0;
if (_connect_combine(device, 0, O_ACCMODE, SH_DENYNO, 0, _FTYPE_ANY,
sizeof(msg.i), &msg.i, sizeof(msg.o), &msg.o) == -1)
return(-1);
strcpy(directory, msg.o.path);
return(0);
}
int main(int argc, char *argv[]){
int r;
FILE f;
float used;
f = popen(“df -Pk | grep " / " | awk '{print
sprintf(”%f",100$3/$2)}’", “r”);
fscanf(f, “%f”, &used);
printf(“df returns: disk is %f %% used\n”, used);
int i;
struct statvfs fs;
int return_val;
int fd;
char mount_point[256];
size_t bytes;
int crap;
if (argc<2){
printf(“usage getFree /dev/hd0t79 \n”);
return 0;
}
for (i=1;i<argc;i++){
// Find out where the partition is mounted from
if (fd = open(argv[1], O_RDONLY)==NULL){
perror(“open”);
return -1;
}
fsys_get_mount_pt(argv[1], mount_point);
printf(“return str: %s\n”, mount_point);
fsys_get_mount_pt(mount_point,mount_point );
printf(“return str: %s\n”, mount_point);
fsys_get_mount_pt( mount_point, mount_point);
printf(“return str: %s\n”, mount_point);
fsys_get_mount_pt(mount_point, mount_point);
if ( statvfs(mount_point, &fs) == -1){
perror(“statvfs”);
return -1;
}
printf("%15s\t", mount_point);
printf(“total blocks %d\t”, fs.f_blocks);
printf(“free blocks %d\t”, fs.f_bfree);
printf(“avail blocks %d\n”, fs.f_bavail);
}
return 0;
}
And the output (with IP numbers masked):
$ getFree /dev/hd0t79
df returns: disk is 78.033730 % used
return str: /net/x.x.x.x/
return str: /net/x.x.x.x/
return str: /net/x.x.x.x/
/net/x.x.x.x/ total blocks 1 free blocks 0 avail blocks 0