473,766 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Needed APIs: For copying file and finding Disk Usage

Dear all,
I am programming in Linux , wherein I need to know a couple of
things.

1) Does an API exist that can copy file onto another file ( an API
equivalent of 'cp') ?
2) Is there an API that tells me the disk usage ( equivalent for
cmdline
'du' ) ?
Any pointers would be greatly appreciated.
Thanks n

Dec 5 '06 #1
12 2432
Sankar wrote:
Dear all,
I am programming in Linux , wherein I need to know a couple of
things.

1) Does an API exist that can copy file onto another file ( an API
equivalent of 'cp') ?

2) Is there an API that tells me the disk usage ( equivalent for
cmdline
'du' ) ?

Any pointers would be greatly appreciated.
Please try posting to comp.os.linux.* comp.unix.progr ammer etc.

Also see the link below to try to find what you want:
<http://www.gnu.org/software/libc/manual/html_node/index.html>

Dec 5 '06 #2
On Mon, 04 Dec 2006 18:01:09 -0800, Sankar wrote:
Dear all,
I am programming in Linux , wherein I need to know a couple of
things.

1) Does an API exist that can copy file onto another file ( an API
equivalent of 'cp') ?
The following isn't guaranteed, I believe, by any ISO C standard to work,
per se (though it's difficult to discern exactly what you're asking), but
within the domain of Unix platforms/implementations (including Linux) the
unspecified parts are specified and implemented so:

#include <stdio.h>
/* FILE fopen(3) fread(3) fwrite(3) feof(3) fflush(3) */

#include <stdlib.h>
/* EXIT_FAILURE exit(3) */

int main(void) {

FILE *src, *dst;
unsigned char buf[1024];
size_t n;

if (0 == (src = fopen("/path/to/source/file", "r")))
perror("fopen") , exit(EXIT_FAILU RE);

if (0 == (dst = fopen("/path/to/sink/file", "w")))
perror("fopen") , exit(EXIT_FAILU RE);

while (0 < (n = fread(buf, 1, sizeof buf, src))) {
if (n != fwrite(buf, 1, n, dst))
perror("fwrite" ), exit(EXIT_FAILU RE);
}

if (!feof(src))
perror("fread") , exit(EXIT_FAILU RE);

if (0 != fflush(dst))
perror("fflush" ), exit(EXIT_FAILU RE);

return 0;

}

>
2) Is there an API that tells me the disk usage ( equivalent for cmdline
'du' ) ?
You need to ask in a Linux-specific forum.

- Bill

Dec 5 '06 #3
Sankar wrote:
>
.... snip ...
>
1) Does an API exist that can copy file onto another file ( an API
equivalent of 'cp') ?
#include <stdio.h>
int main(void) {
int ch;
while (EOF != (ch = getchar())) putchar(ch);
return 0;
}

copies stdin to stdout.
2) Is there an API that tells me the disk usage ( equivalent for
cmdline 'du' ) ?
No. Standard C knows nothing about disks or the usage thereof.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>
Dec 5 '06 #4
William Ahern <wi*****@25than dClement.comwri tes:
On Mon, 04 Dec 2006 18:01:09 -0800, Sankar wrote:
>Dear all,
I am programming in Linux , wherein I need to know a couple of
things.

1) Does an API exist that can copy file onto another file ( an API
equivalent of 'cp') ?

The following isn't guaranteed, I believe, by any ISO C standard to work,
per se (though it's difficult to discern exactly what you're asking), but
within the domain of Unix platforms/implementations (including Linux) the
unspecified parts are specified and implemented so:

#include <stdio.h>
/* FILE fopen(3) fread(3) fwrite(3) feof(3) fflush(3) */

#include <stdlib.h>
/* EXIT_FAILURE exit(3) */

int main(void) {

FILE *src, *dst;
unsigned char buf[1024];
Why 1024? It would probably make more sense to use BUFSIZ.
size_t n;

if (0 == (src = fopen("/path/to/source/file", "r")))
perror("fopen") , exit(EXIT_FAILU RE);
This is an awkward use of the comma operator, IMHO. I'd write:

if ((src = fopen("/path/to/source/file", "r")) == NULL) {
perror("fopen") ;
exit(EXIT_FAILU RE);
}

Note that the standard doesn't guarantee that fopen() sets errno.
if (0 == (dst = fopen("/path/to/sink/file", "w")))
perror("fopen") , exit(EXIT_FAILU RE);

while (0 < (n = fread(buf, 1, sizeof buf, src))) {
if (n != fwrite(buf, 1, n, dst))
perror("fwrite" ), exit(EXIT_FAILU RE);
}

if (!feof(src))
perror("fread") , exit(EXIT_FAILU RE);

if (0 != fflush(dst))
perror("fflush" ), exit(EXIT_FAILU RE);

return 0;

}
Assuming that "cp" is an OS command that copies files (yeah, I know it
is, but this is comp.lang.c), it's likely to do a number of
system-specific things to optimize performance. If you don't mind a
Linux- or Unix-specific solution, you might consider invoking the "cp"
command itself via system(). <OT>The cp command does a number of
things that you can't do in portable C, as you can see by reading the
man page.</OT>

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 5 '06 #5
Sankar wrote:
Dear all,
I am programming in Linux , wherein I need to know a couple of
things.
If the operating system is relevant then it's probably not a question
that can be answered within standard C. In particular system calls or
APIs such that you're asking for are not defined by C.
1) Does an API exist that can copy file onto another file ( an API
equivalent of 'cp') ?
Not exactly, but you can write this one in standard C:

int cp(const char *source, const char *dest)
{
FILE *ifp = fopen(source, "rb");
FILE *ofp = fopen(dest, "wb");
int ch;
if(!ifp || !ofp) return -1;
while((ch = getc(ifp)) != EOF) putc(ch, ofp);
if(ferror(ifp) || ferror(ofp)) return -1;
if(fclose(ifp) || fclose(ofp)) return -1;
return 0;
}
2) Is there an API that tells me the disk usage ( equivalent for
cmdline
'du' ) ?
No - this cannot be done in standard C. There are two choices: one is to
find a system-specific function you can call (OT: man statfs). The other
is to use the 'system' function to run your 'du' program through the
command line. If you redirect its output to a file you can then read
that file and parse it.

--
Simon.
Dec 5 '06 #6

Sankar wrote:
Dear all,
I am programming in Linux , wherein I need to know a couple of
things.

1) Does an API exist that can copy file onto another file ( an API
equivalent of 'cp') ?
Sort of. There is a standard C function that will permit you to pass a
string argument to the "command processor to be executed in a manner
which the implementation shall document". This means that you /could/
system("cp fromfile tofile");
and "copy file onto another file" in a manner exactly the same as the
Unix cp(1) command.
2) Is there an API that tells me the disk usage ( equivalent for
cmdline 'du' ) ?
Similarly, the system() function can invoke the du(1) command, but as
system() doesn't return the output of the command, you'll have to do
some trickery.
system("du >some.file");
followed by
FILE *diskusage = fopen("some.fil e", "r");
(with the appropriate error checking everywhere, of course)
This will tell you the disk usage /exactly/ as du(1) would.

HTH
--
Lew

Dec 5 '06 #7
Simon Biber <ne**@ralmin.cc writes:
Sankar wrote:
[...]
>2) Is there an API that tells me the disk usage ( equivalent for
cmdline
'du' ) ?

No - this cannot be done in standard C. There are two choices: one is
to find a system-specific function you can call (OT: man statfs). The
other is to use the 'system' function to run your 'du' program through
the command line. If you redirect its output to a file you can then
read that file and parse it.
And if your system has the "du" command, it probably has a
system-specific function that lets a C program read the output of a
command without writing it to a temporary file. Which is why this is
a question for comp.unix.progr ammer, where the function in question is
topical.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 5 '06 #8
"Keith Thompson" <ks***@mib.orgw rote in message
news:ln******** ****@nuthaus.mi b.org...
William Ahern <wi*****@25than dClement.comwri tes:
>if (0 == (src = fopen("/path/to/source/file", "r")))
perror("fopen" ), exit(EXIT_FAILU RE);

This is an awkward use of the comma operator, IMHO. I'd write:

if ((src = fopen("/path/to/source/file", "r")) == NULL) {
perror("fopen") ;
exit(EXIT_FAILU RE);
}
It's awkward, yes, but still legal. It's also the only use I've found
for the comma operator other than incrementing multiple variables in a
"for" construct.

The problem is that many folks will not notice it's a comma (expecting a
semicolon), or will think it's a typo. That leads to maintenance
problems. It's also not done widely enough to be considered idiomatic,
so I wouldn't use it in an example for a newbie, but I use it in my own
code now and then.

For instance, I once had to change several hundred "close(fd); "
statements to also do "fd=-1;". Since most, but not all of them, were
bare statements underneath an if, the simplest way was a simple
find-and-replace with "close(fd), fd=-1;" Not clean, and I'm not proud
of it, but it worked perfectly and saved me a lot of time.

You'll also see it in macros now and then, as a way to avoid needing the
"do {...} while (0)" trick. It's debatable which construction is worse,
but as long as it's confined to macros most people won't comment.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
--
Posted via a free Usenet account from http://www.teranews.com

Dec 5 '06 #9
"Stephen Sprunk" <st*****@sprunk .orgwrites:
"Keith Thompson" <ks***@mib.orgw rote in message
news:ln******** ****@nuthaus.mi b.org...
>William Ahern <wi*****@25than dClement.comwri tes:
>>if (0 == (src = fopen("/path/to/source/file", "r")))
perror("fopen "), exit(EXIT_FAILU RE);

This is an awkward use of the comma operator, IMHO. I'd write:

if ((src = fopen("/path/to/source/file", "r")) == NULL) {
perror("fopen") ;
exit(EXIT_FAILU RE);
}

It's awkward, yes, but still legal. It's also the only use I've found
for the comma operator other than incrementing multiple variables in a
"for" construct.
Yes, I agree that it's perfectly legal.
The problem is that many folks will not notice it's a comma (expecting
a semicolon), or will think it's a typo. That leads to maintenance
problems. It's also not done widely enough to be considered
idiomatic, so I wouldn't use it in an example for a newbie, but I use
it in my own code now and then.

For instance, I once had to change several hundred "close(fd); "
statements to also do "fd=-1;". Since most, but not all of them, were
bare statements underneath an if, the simplest way was a simple
find-and-replace with "close(fd), fd=-1;" Not clean, and I'm not
proud of it, but it worked perfectly and saved me a lot of time.
Well, I never write bare statements under an if (I always use braces,
even for a single statement), but like all style questions that's
largely a matter of taste.
You'll also see it in macros now and then, as a way to avoid needing
the "do {...} while (0)" trick. It's debatable which construction is
worse, but as long as it's confined to macros most people won't
comment.
The "do {...} while (0)" trick is IMHO ugly, but it's a common idiom
and there's often no other way to achieve the same goal. Using comma
operators to avoid it seems reasonable to me.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 5 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
7432
by: Bryan Rickard | last post by:
I wrote a simple program in VB6 to copy all the files from a directory on a CD-ROM to my hard disk. There are about 10 files, each about 30MB. The program uses Get and Put to get data from the CD into a buffer and then put it into the disk. See code below. It works, but it slows down drastically before it copies all the files. Windows Task Manager shows the CPU usage gradually increasing as the files are copied, until it reaches 100...
13
15256
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the pattern can occur on any byte boundary in the file, so chunking through the code at 16 bytes a frame maybe a problem. The file itself isn't so large, maybe 32 kbytes is all and the need for speed is not so great, but the need for accuracy in the...
12
2873
by: Nobody | last post by:
DB2 500G database, Wintel, heavily loaded OLTP (5M+ transactions a day; all transactions are extremely small, all selects are controlled (no ad-hoc), 99% of all selects are very small (no table scans, index scans are very limited in size) ). Write performance is generally more important than read performance, read performance of heavy queries (the ones with table scans) is not important at all. Question: how to spread data across...
9
2880
by: Jon LaBadie | last post by:
Suppose I'm using stdio calls to write to a disk file. One possible error condition is no space on file system or even (in unix environment) a ulimit of 0 bytes. Which calls would be expected to return error codes for such conditions? Would it happen on the writes (fwrite, fprintf, fputs ...), or as they actually write to io buffers, might the errors not occur until the data is flushed or the file is closed?
3
2644
by: Daz | last post by:
Hi everyone! This is my first time posting in this group, although I have been watching it for the past few months and have to say this is possibly the best group I have seen on Google so far! (That wasn't a butt-kissing comment, I just believe in giving praise when it's due). I would like to copy files using globbing, and I am not sure of the best way to do it. I have tried using system("COPY "), but It's still not quite what I am...
18
4777
by: bjorn.augestad | last post by:
We're planning to migrate our db to new and more disk drives, faster RAID levels and more dedicated disk usage(e.g. placing the translog on dedicated disks). The db server runs on Win2003. Right now we're thinking about what file system to use on the new drives. We opt for performance, but expect reliability as well.(Goes without saying, IMHO ;-)) does some journaling. I'd prefer to have no journaling and sacrifice boot time for...
2
1787
by: jatphat | last post by:
Somebody help me. How do i copy my Access database from one location on my hard disk to another . i tried file.copy(source, distination ) but the file would not move. i always get this error message "Another process is using the destinationFileName" . thanks
8
385
by: Sankar | last post by:
Dear all, I am programming in Linux , wherein I need to know a couple of things. 1) Does an API exist that can copy file onto another file ( an API equivalent of 'cp') ? 2) Is there an API that tells me the disk usage ( equivalent for cmdline
18
2411
by: mike3 | last post by:
Hi. I have an interesting problem. The C program presented below takes around 12 seconds to copy 128 MB of data on my machine. Yet I know the machine can go faster since a copying done at a DOS prompt takes only 4 seconds to do the same 128 MB copy (and a copy is both a read and write.). So why is this thing like 3x
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10008
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8833
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.