By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,873 Members | 1,040 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,873 IT Pros & Developers. It's quick & easy.

finding how much the file system is full, from a C program?

P: n/a
Hi All,

I am writing a small tool which is supposed to fill the filesystem to a
specified percent.

For, that I need to read how much the file system is full in percent,
like the output given by df -k

lopgod10:~/mycrfile # df -k /mnt/mvdg1/vset
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vx/dsk/mvdg1/vsetdg1
1536000 349854 1112370 24% /mnt/mvdg1/vset

How can I do it from a C program.

************************************************** ************************************************** *********************
OS I M USING
lopgod10:~/mycrfile # cat /etc/SuSE-release
SUSE LINUX Enterprise Server 9 (ppc)
VERSION = 9
PATCHLEVEL = 3
************************************************** ************************************************** *********************

Please help.
Thanks in advance.

Thanks and regards,
Prasad.

Oct 30 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a
pr************@gmail.com wrote:
# Hi All,
#
# I am writing a small tool which is supposed to fill the filesystem to a
# specified percent.
#
# For, that I need to read how much the file system is full in percent,
# like the output given by df -k
#
# lopgod10:~/mycrfile # df -k /mnt/mvdg1/vset
# Filesystem 1K-blocks Used Available Use% Mounted on
# /dev/vx/dsk/mvdg1/vsetdg1
# 1536000 349854 1112370 24% /mnt/mvdg1/vset
#
# How can I do it from a C program.

In ANSI C you can do something like
system("df -k /mnt/mvdg1/vset >/tmp/df-k");
FILE *f = fopen("/tmp/dfk","r");
(read f and parse df output)
fclose(f);

On unices you can do
FILE *f = popen("df -k /mnt/mvdg1/vset >/tmp/df-k");
(read f and parse df output)
pclose(f);

--
SM Ryan http://www.rawbw.com/~wyrmwif/
The whole world's against us.
Oct 30 '06 #2

P: n/a
pr************@gmail.com wrote:
Hi All,

I am writing a small tool which is supposed to fill the filesystem to a
specified percent.

For, that I need to read how much the file system is full in percent,
like the output given by df -k
Ask in e.g. comp.unix.programmer rather ,though,
the statvfs posix function can find some of this information,
Oct 30 '06 #3

P: n/a

Nils O. Selåsdal wrote:
pr************@gmail.com wrote:
Hi All,

I am writing a small tool which is supposed to fill the filesystem to a
specified percent.

For, that I need to read how much the file system is full in percent,
like the output given by df -k
Ask in e.g. comp.unix.programmer rather ,though,
the statvfs posix function can find some of this information,
Thanks Nils / SM Ryan,
For your kind reply.

Oct 30 '06 #4

P: n/a
In article <11*********************@e64g2000cwd.googlegroups. com>,
<pr************@gmail.comwrote:
>I am writing a small tool which is supposed to fill the filesystem to a
specified percent.
On a multiuser system in which there are other active processes using
the same filesystem, there is no way to reliably do this, as the
other processes are going to be changing the freespace. You might
get lucky and manage it for a fraction of a second... but you need
to loop around and recheck because something that was using space
might have released it between your size-check and your space grab.
Or grown it, in which case you are now over the limit you wanted to be
at.

>For, that I need to read how much the file system is full in percent,
like the output given by df -k
>How can I do it from a C program.
Filesystem information is not part of standard C (which is why the
other poster referred you to a unix newsgroup.)
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
Oct 30 '06 #5

P: n/a
On Mon, 30 Oct 2006 07:10:45 UTC, SM Ryan
<wy*****@tango-sierra-oscar-foxtrot-tango.fake.orgwrote:
pr************@gmail.com wrote:
# Hi All,
#
# I am writing a small tool which is supposed to fill the filesystem to a
# specified percent.
#
# For, that I need to read how much the file system is full in percent,
# like the output given by df -k
#
# lopgod10:~/mycrfile # df -k /mnt/mvdg1/vset
# Filesystem 1K-blocks Used Available Use% Mounted on
# /dev/vx/dsk/mvdg1/vsetdg1
# 1536000 349854 1112370 24% /mnt/mvdg1/vset
#
# How can I do it from a C program.

In ANSI C you can do something like
system("df -k /mnt/mvdg1/vset >/tmp/df-k");
That is not ANSI C. The parameter of system is system dependant and
unportable.
On my system is no directory mnt.
FILE *f = fopen("/tmp/dfk","r");
Not ANSI C but system dependant. ANSI C knows nothing about pathes,
directories and so on.
(read f and parse df output)
fclose(f);

On unices you can do
FILE *f = popen("df -k /mnt/mvdg1/vset >/tmp/df-k");
(read f and parse df output)
pclose(f);

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Oct 30 '06 #6

P: n/a
On Mon, 30 Oct 2006 06:46:20 UTC, pr************@gmail.com wrote:
Hi All,

I am writing a small tool which is supposed to fill the filesystem to a
specified percent.

For, that I need to read how much the file system is full in percent,
like the output given by df -k

lopgod10:~/mycrfile # df -k /mnt/mvdg1/vset
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vx/dsk/mvdg1/vsetdg1
1536000 349854 1112370 24% /mnt/mvdg1/vset

How can I do it from a C program.
You can't do that with portable C. You would go to a programmer group
related to your OS and ask there - or much simpler get the
documentatzion of your C compiler or better the C library coming with
your OS and look for a system specific API.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Oct 30 '06 #7

P: n/a
In article <wm***************************@JUPITER1.PC-ROSENAU.DE>,
Herbert Rosenau <os****@pc-rosenau.dewrote:
>On Mon, 30 Oct 2006 07:10:45 UTC, SM Ryan
<wy*****@tango-sierra-oscar-foxtrot-tango.fake.orgwrote:
>In ANSI C you can do something like
system("df -k /mnt/mvdg1/vset >/tmp/df-k");
>That is not ANSI C. The parameter of system is system dependant and
unportable.
On my system is no directory mnt.
Quibble: ANSI C promises that system() will exist, but not that
there will be any command interpreter, and refuses to itself ascribe
any meaning to the string passed to system(). The result is system
dependant, portable at most to other similar systems -- but it *is*
ANSI C.

> FILE *f = fopen("/tmp/dfk","r");
>Not ANSI C but system dependant. ANSI C knows nothing about pathes,
directories and so on.
Similar quibble: ANSI C promises the existance of fopen() but
refuses to itself ascribe any meaning to the filename string. ANSI
C refuses to even define something as simple as the interpretation
of the filename "x" in fopen("x","r") . A filename with a / in
it is, as far as ANSI C is concerned, no more and no less portable
than one without. Your claim thus transforms to "any use of fopen()
is not ANSI C but system dependant"; it is true that it is system
dependant, but fopen() itself is still firmly part of ANSI C.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Oct 30 '06 #8

P: n/a
2006-10-30 <ei**********@canopus.cc.umanitoba.ca>,
Walter Roberson wrote:
Your claim thus transforms to "any use of fopen()
is not ANSI C but system dependant"
except tmpnam().
Oct 30 '06 #9

P: n/a
"Herbert Rosenau" <os****@pc-rosenau.dewrote:

# In ANSI C you can do something like
# system("df -k /mnt/mvdg1/vset >/tmp/df-k");
#
# That is not ANSI C. The parameter of system is system dependant and
# unportable.

Nice try, kid, but the system() function has been in ANSI C
from the beginning.

# On my system is no directory mnt.

And I guess you don't know enough C to use program arguments
or how to construct strings.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
She broke your heart and inadvertently drove men to deviant lifestyles.
Oct 31 '06 #10

P: n/a
On Tue, 31 Oct 2006 07:30:52 UTC, SM Ryan
<wy*****@tango-sierra-oscar-foxtrot-tango.fake.orgwrote:
"Herbert Rosenau" <os****@pc-rosenau.dewrote:

# In ANSI C you can do something like
# system("df -k /mnt/mvdg1/vset >/tmp/df-k");
#
# That is not ANSI C. The parameter of system is system dependant and
# unportable.

Nice try, kid, but the system() function has been in ANSI C
from the beginning.

# On my system is no directory mnt.

And I guess you don't know enough C to use program arguments
or how to construct strings.
I guess you're unable to understund a bit complex sentences an
paragraphs containing more than one simple sentence.

system(argv[1]); is 100% standard compilant - if the parameter
contains an valid (path)filename.
system("myprog z:\\blah\\blubber-foo.bar.boo"); may not even
impletation compilant as it relays on the filesystem the path points
to.

The same is for fopen().

C itself knows nothing about directories, devises, files. All that is
implementation dependant. So you're outside the topic of this group.

Has you ever written a failsave program that handles direcetories
(when supported) and files and had to print to an printer of unknown
type and location but NOT to stdout or stderr who has to run on MVS,
DOS, Windows NT, 2K, XP, Linux, BSD, OS/2 on FAT16, FAT32, NTFS, HPFS,
JFS...? I'm sure the answer is NO.

Else you would know where the limits of standard C are and how to
handle that propper.

Yes it is possible to write programs who are completely standard
compilant - I've done that often enough to have the same source
running on every C compilant environment. But more often you must use
some implementation specific extensions to get your work done. It is
essential to know the limits of standard C.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Oct 31 '06 #11

P: n/a
pr************@gmail.com wrote:
Hi All,

I am writing a small tool which is supposed to fill the filesystem to a
specified percent.

For, that I need to read how much the file system is full in percent,
like the output given by df -k

lopgod10:~/mycrfile # df -k /mnt/mvdg1/vset
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vx/dsk/mvdg1/vsetdg1
1536000 349854 1112370 24% /mnt/mvdg1/vset

How can I do it from a C program.
df is a C program and it's open source. It's part of the
GNU fileutils which is part of the GNU coreutils.

Source code is available here.

http://ftp.gnu.org/pub/gnu/coreutils/

As others have said, this is not portable C. Which means,
you may as well write a shell script which invokes df and,
say, dd to do the job.

Kind regards,

Iwo

Oct 31 '06 #12

P: n/a

pr************@gmail.com wrote:
Hi All,

I am writing a small tool which is supposed to fill the filesystem to a
specified percent.
You have to be careful here.

If the filesystem has disk compression enabled, the numbers reported by
"ls" or "df" are mostly mythical. The actual amount of space available
on a compressed drive is dependent on exactly what gets written to it.

Also under some Unix and Linux systems, the "df" command doesnt tell
the truth.

"df" assumes a file system and the OS gets slow and unreliable when
it's more than 90% full,so it reports that many filesystems are 10% or
so more "full" than they actually are.

Oct 31 '06 #13

P: n/a
I guess you're unable to understund a bit complex sentences an
paragraphs containing more than one simple sentence.

system(argv[1]); is 100% standard compilant - if the parameter
contains an valid (path)filename.
system("myprog z:\\blah\\blubber-foo.bar.boo"); may not even
impletation compilant as it relays on the filesystem the path points
to.
Maybe you should buy a copy of the standard.

7.20.4.6 The system function

Synopsis
1 #include <stdlib.h>
int system(const char *string);

Description

2 If string is a null pointer, the system function determines whether the host
environment has a command processor. If string is not a null pointer, the system
function passes the string pointed to by string to that command processor to be
executed in a manner which the implementation shall document; this might then
cause the program calling system to behave in a non-conforming manner or to
terminate.

Returns
3 If the argument is a null pointer, the system function returns nonzero only if
a command processor is available. If the argument is not a null pointer, and the
system function does return, it returns an implementation-defined value.
Therefore:-
system("I like cabbage on Fridays");

Seems to me to be perfect ansi C
>
The same is for fopen().
So show me below which bit of the standard says that the first argument to fopen
can contain directories, boxes, houses, or squiggles? If its a valid string
fopen will accept it.

7.19.5.3 The fopen function
Synopsis

1 #include <stdio.h>
FILE *fopen(const char * restrict filename, const char * restrict mode);

Description
2 The fopen function opens the file whose name is the string pointed to by
filename, and associates a stream with it.
3 The argument mode points to a string. If the string is one of the following,
the file is open in the indicated mode. Otherwise, the behavior is undefined.228)
r open text file for reading
w truncate to zero length or create text file for writing
a append; open or create text file for writing at end-of-file
rb open binary file for reading
wb truncate to zero length or create binary file for writing
ab append; open or create binary file for writing at end-of-file
r+ open text file for update (reading and writing)
w+ truncate to zero length or create text file for update
a+ append; open or create text file for update, writing at end-of-file
r+b or rb+ open binary file for update (reading and writing)
w+b or wb+ truncate to zero length or create binary file for update
a+b or ab+ append; open or create binary file for update, writing at end-of-file

228) If the string begins with one of the above sequences, the implementation
might choose to ignore the remaining characters, or it might use them to select
different kinds of a file (some of which might not conform to the properties in
7.19.2).

4 Opening a file with read mode ('r' as the first character in the mode
argument) fails if the file does not exist or cannot be read.

5 Opening a file with append mode ('a' as the first character in the mode
argument) causes all subsequent writes to the file to be forced to the then
current end-of-file, regardless of intervening calls to the fseek function. In
some implementations, opening a binary file with append mode ('b' as the second
or third character in the above list of mode argument values) may initially
position the file position indicator for the stream
beyond the last data written, because of null character padding.

6 When a file is opened with update mode ('+' as the second or third character
in the above list of mode argument values), both input and output may be
performed on the associated stream. However, output shall not be directly
followed by input without an intervening call to the fflush function or to a
file positioning function (fseek, fsetpos, or rewind), and input shall not be
directly followed by output without an intervening call to a file positioning
function, unless the input operation encounters endof-file. Opening (or
creating) a text file with update mode may instead open (or create) a
binary stream in some implementations.

7 When opened, a stream is fully buffered if and only if it can be determined
not to refer to an interactive device. The error and end-of-file indicators for
the stream are cleared.

Returns
8 The fopen function returns a pointer to the object controlling the stream. If
the open operation fails, fopen returns a null pointer.
C itself knows nothing about directories, devises, files. All that is
implementation dependant. So you're outside the topic of this group.
And that has what to do with arguments to a function?
>
Has you ever written a failsave program that handles direcetories
(when supported) and files and had to print to an printer of unknown
type and location but NOT to stdout or stderr who has to run on MVS,
DOS, Windows NT, 2K, XP, Linux, BSD, OS/2 on FAT16, FAT32, NTFS, HPFS,
JFS...? I'm sure the answer is NO.
Have you ever eaten 15 jam donuts in 18 seconds? I'm sure the answer is NO.
Else you would know where the limits of standard C are and how to
handle that propper.

Yes it is possible to write programs who are completely standard
compilant - I've done that often enough to have the same source
running on every C compilant environment. But more often you must use
some implementation specific extensions to get your work done. It is
essential to know the limits of standard C.

--

Adrian
Oct 31 '06 #14

P: n/a
<mostly OT>
On Mon, 30 Oct 2006 07:10:45 -0000, SM Ryan
<wy*****@tango-sierra-oscar-foxtrot-tango.fake.orgwrote:
pr************@gmail.com wrote:
<snip>
# For, that I need to read how much the file system is full in percent,
# like the output given by df -k
<snip>
# How can I do it from a C program.

In ANSI C you can do something like
system("df -k /mnt/mvdg1/vset >/tmp/df-k");
FILE *f = fopen("/tmp/dfk","r");
(read f and parse df output)
fclose(f);
1: (only) if df -k works on your platform, which is more or less
implicit in the problem statement
2: but on all(?) such systems, only if the filename in both places is
the same (not dfk versus df-k);
3: and then only if some other user isn't trying to do the same thing,
or something similar, at almost the same time; something like
sprintf ( , "/tmp/free%d", getpid()) or tmpnam ()
is likely to be somewhat safer (although still not against malice).
On unices you can do
FILE *f = popen("df -k /mnt/mvdg1/vset >/tmp/df-k");
(read f and parse df output)
pclose(f);
<oopsonly if you delete the ">outfile" part.

- David.Thompson1 at worldnet.att.net
Nov 20 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.