Free space on drive

Free space on drive

Post by Bob Marti » Wed, 05 Sep 2007 16:45:26



I'm porting some programs from OS/2 to Linux.

Is there an easy way to tell how much free space remains on a flash drive
or SD card etc?

On OS/2 I use SysDriveInfo but on Linux I've had to resort to parsing the output
of DF or VDIR.
 
 
 

Free space on drive

Post by Michael Lu » Wed, 05 Sep 2007 19:44:23


rrrr? You seem to be correct:

>>
SysDriveInfo(Windowsonly)
<<

Sorry.

--
Michael Lueck
Lueck Data Systems
http://www.yqcomputer.com/

 
 
 

Free space on drive

Post by ptjm » Thu, 06 Sep 2007 14:44:23

In article <q28Di.48239$ XXXX@XXXXX.COM >,

%
% I'm porting some programs from OS/2 to Linux.
%
% Is there an easy way to tell how much free space remains on a flash drive
% or SD card etc?
%
% On OS/2 I use SysDriveInfo but on Linux I've had to resort to parsing the output
% of DF or VDIR.

There are some portability issues surrounding this functionality, but
my version of rexxutil does support sysdriveinfo on unix systems, altough
the default Linux Makefile doesn't compile it in...

Here's a function which returns the amount of free space on the file
system containing the file named in its argument. It works at least on
fedora core 5.

Save it to a file (call it freespace.c) and compile it with

cc -I /usr/local/include -fpic -shared freespace.c -o libfreespace.so

(replacing /usr/local/include with the directory containing rexxsaa.h).
Then copy libfreespace.so to wherever your rexx .so files are.

You can then use it with
call rxfuncadd 'freespace', 'freespace', 'freespace'
say freespace('/mnt/flashdrive')

Note, for object rexx you might not need the 'lib' prefix on the .so file.

/* c file begins */
#include "rexxsaa.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/statfs.h>

APIRET APIENTRY freespace(PUCHAR fname, ULONG argc, PRXSTRING argv,
PSZ pSomething, PRXSTRING result)
{
struct statfs sfs;
long long freespace;
char * dname;
int rc;

if (argc != 1)
return 22;

dname = alloca(RXSTRLEN(argv[0])+1);
memcpy(dname, RXSTRPTR(argv[0]), RXSTRLEN(argv[0]));
dname[RXSTRLEN(argv[0])] = 0;

if (statfs(dname, &sfs) == -1)
result->strlength = 0;
else {
freespace = sfs.f_bavail;
freespace *= sfs.f_bsize;
result->strlength = sprintf(RXSTRPTR(result[0]), "%lld", freespace);
}

return 0;
}
/* c file ends */
--

Patrick TJ McPhee
North York Canada
XXXX@XXXXX.COM
 
 
 

Free space on drive

Post by Bob Marti » Thu, 06 Sep 2007 16:32:19


<snip>

Thanks, I'll give it a try.
 
 
 

Free space on drive

Post by Michael Lu » Thu, 06 Sep 2007 23:16:08


Great Patrick!

Would you consider contributing that API to ooRexx? Looks to me a prime candidate for SysDriveInfo of sorts... maybe just a bit of tweaking to message the output to the SysDriveInfo standard.

--
Michael Lueck
Lueck Data Systems
http://www.yqcomputer.com/
 
 
 

Free space on drive

Post by ptjm » Fri, 14 Sep 2007 12:22:58

In article <KSyDi.14$ XXXX@XXXXX.COM >,


% > Here's a function which returns the amount of free space on the file
% > system containing the file named in its argument. It works at least on
% > fedora core 5.
%
% Great Patrick!
%
% Would you consider contributing that API to ooRexx? Looks to me a prime
% candidate for SysDriveInfo of sorts... maybe just a bit of tweaking to
% message the output to the SysDriveInfo standard.

Portability will be an issue, but most extent unix systems support something
similar. I have no objection to this code finding its way into oorexx's
rexxutil (or wherever). I might even send in a patch some time.
--

Patrick TJ McPhee
North York Canada
XXXX@XXXXX.COM