473,411 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

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

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
14 2766
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
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

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
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
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
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
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
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
"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
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
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

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
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
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Hal Vaughan | last post by:
I want to have a config file for my program, which means I need to know where the config file is. If I type: java myclass and it runs myclass.class, is there any way to obtain the location of...
11
by: Fuzzyman | last post by:
What's the best, cross platform, way of finding out the directory a script is run from ? I've googled a bit, but can't get a clear answer. On sys.argv the docs say : argv is the script name...
15
by: Twan Kennis | last post by:
Hi, I have a DB2 database on the IBM iSeries platform, on which I created several Stored Procedures with the SQLCODE as a return-parameter. These Stored Procedures are called from a Windows...
11
by: VJ | last post by:
I have these functions in my application. FileInfo.CopyTo DirectoryInfo.Create File.Copy Directory.Create
6
by: Tor Inge Rislaa | last post by:
Finding current Procedure I want to write errors to a log-file, where the error log contain the description of where the error occurred and what kind of error it was. Private Sub...
5
by: Jay | last post by:
Hello, I am writing a c++ program that stores some values in a config located in it's directory. is there a way in c++ to get the full path of the running program so I can open the file not...
20
by: Joel Hedlund | last post by:
Hi all! I use python for writing terminal applications and I have been bothered by how hard it seems to be to determine the terminal size. What is the best way of doing this? At the end I've...
4
by: sathyashrayan | last post by:
(This is not a home work question) Dear group, I want a program to find one number between a set of natural number.A program to guess a number in between a Natural number set.This should be a...
2
by: Extremest | last post by:
Here is the code I have so far. It connects to a db and grabs headers. It then sorts them into groups and then puts all the complete ones into another table. Problem I am having is that for some...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.