From: session@seq.uncwil.edu (Zack C. Sessions)
Subject: Re: Need code example for determining free disk space
Organization: Univ. of North Carolina @ Wilmington
Date: Thu, 8 Oct 1992 00:00:11 GMT

magnuson@skyler.mavd.honeywell.com writes:

>I am writing a program which needs to find out how much free space is remaining
>on a disk (similar to the 'free' program).  Does anyone have an example of how
>to do this (preferably in C)?  Thanks.

Here is a few functions which will aid the Level 2 C programmer to compute
the amount of free space on a disk volume.

/*
**	this function is used by csec()
**
**	this is a pretty neat way of counting the ON bits in a byte.
*/

int cbit(v)
 char *v;
{
    char j, k;
    int nbit = 0;
    k = *v;
    while(k != 0) {
        j = k - 1;
        k = k & j;
        ++nbit;
    }
    return(nbit);
}

/*
**	this function is used by freeb()
*/

csec(p,num,flag)
 char *p;
 int num;
 int flag;
{
    int i;
    char c;
    for(i = 0; i<num; ++i) {
        c = *p;
        if(flag)
            if(i == (num-1))
                c = c & work3;
        count += (long) (cbit(&c));
        ++p;
    }
}

/*
**	determine number of bytes available on a device and return that value.
**
**	path = path to output device, usually will be 1 for stdout.
**
**	devname = pointer to a character string containing the name of the
**	          device to check, must be NULL terminated.
**
**	the function dialog() is an error message handler, you'll have to
**	supply the code for this yourself. Yours may not need all of the
**	parameters mine uses. If an error occurs, a value of -1 is returned
**	by freeb().
**
**	this may or may not be the most elegant solution to this problem, but
**	this solution has the characteristic that it works.
*/

long freeb(path,devname)
 int path;
 char *devname;
{
    unsigned altsz, ddbit, i, work4;
    int j, k, devpath, finished = FALSE;
    long sectors;
    char dname[65], nsect[3];
    char sect[256];
    count = 0l;
    strcpy(dname,devname);
    strcat(dname,"@");
    if((devpath = open(dname,1)) == -1) {
        dialog(path,1,"Cannot open device!",FALSE,2);
        return(-1l);
    }
    if(lseek(devpath,0l,0) == -1) {
        dialog(path,1,"Drive Seek Error!",FALSE,2);
        return(-1l);
    }
    read(devpath,nsect,3);
    l3tol(&sectors,nsect,1);
    work1 = sectors / 8l;
    work2 = sectors - work1 * 8l;
    if(work2 != 0l) {
        work3 = (char) (8l - work2);
        for(k = 0, j = 1; k < work3; ++k)
            j *= 2;
        --j;
        work3 = ~((char) (j));
    }
    else
        work3 = 0xff;
    if(lseek(devpath,1l,1) == -1) {
        dialog(path,1,"Drive Seek Error!",FALSE,2);
        return(-1l);
    }
    read(devpath,&altsz,2);
    work4 = altsz;
    read(devpath,&ddbit,2);
    if(lseek(devpath,256l,0) == -1) {
        dialog(path,1,"Drive Seek Error!",FALSE,2);
        return(-1l);
    }
    while(!finished) {
        if(work4 >= 256) {
            read(devpath,sect,256);
            csec(sect,256,FALSE);
            work4 -= 256;
        }
        else {
            if(work4 > 0) {
                read(devpath,sect,work4);
                csec(sect,work4,TRUE);
            }
            finished = TRUE;
        }
    }
    close(devpath);
    count *= (long) ddbit;
    return(sectors-count);
}
-- 
Zack Sessions
sessions@seq.uncwil.edu
University of North Carolina at Wilmington
"Good health is merely the slowest form of dying."
Newsgroups: comp.os.os9