Connecting Tech Pros Worldwide Forums | Help | Site Map

Needed APIs: For copying file and finding Disk Usage

Sankar
Guest
 
Posts: n/a
#1: Dec 5 '06
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


santosh
Guest
 
Posts: n/a
#2: Dec 5 '06

re: Needed APIs: For copying file and finding Disk Usage


Sankar wrote:
Quote:
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.programmer etc.

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

William Ahern
Guest
 
Posts: n/a
#3: Dec 5 '06

re: Needed APIs: For copying file and finding Disk Usage


On Mon, 04 Dec 2006 18:01:09 -0800, Sankar wrote:
Quote:
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_FAILURE);

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

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

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

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

return 0;

}

Quote:
>
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

CBFalconer
Guest
 
Posts: n/a
#4: Dec 5 '06

re: Needed APIs: For copying file and finding Disk Usage


Sankar wrote:
Quote:
>
.... snip ...
Quote:
>
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.
Quote:
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>


Keith Thompson
Guest
 
Posts: n/a
#5: Dec 5 '06

re: Needed APIs: For copying file and finding Disk Usage


William Ahern <william@25thandClement.comwrites:
Quote:
On Mon, 04 Dec 2006 18:01:09 -0800, Sankar wrote:
>
Quote:
>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.
Quote:
size_t n;
>
if (0 == (src = fopen("/path/to/source/file", "r")))
perror("fopen"), exit(EXIT_FAILURE);
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_FAILURE);
}

Note that the standard doesn't guarantee that fopen() sets errno.
Quote:
if (0 == (dst = fopen("/path/to/sink/file", "w")))
perror("fopen"), exit(EXIT_FAILURE);
>
while (0 < (n = fread(buf, 1, sizeof buf, src))) {
if (n != fwrite(buf, 1, n, dst))
perror("fwrite"), exit(EXIT_FAILURE);
}
>
if (!feof(src))
perror("fread"), exit(EXIT_FAILURE);
>
if (0 != fflush(dst))
perror("fflush"), exit(EXIT_FAILURE);
>
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_Keith) kst-u@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.
Simon Biber
Guest
 
Posts: n/a
#6: Dec 5 '06

re: Needed APIs: For copying file and finding Disk Usage


Sankar wrote:
Quote:
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.
Quote:
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;
}
Quote:
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.
Lew Pitcher
Guest
 
Posts: n/a
#7: Dec 5 '06

re: Needed APIs: For copying file and finding Disk Usage



Sankar wrote:
Quote:
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.
Quote:
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.file", "r");
(with the appropriate error checking everywhere, of course)
This will tell you the disk usage /exactly/ as du(1) would.

HTH
--
Lew

Keith Thompson
Guest
 
Posts: n/a
#8: Dec 5 '06

re: Needed APIs: For copying file and finding Disk Usage


Simon Biber <news@ralmin.ccwrites:
Quote:
Sankar wrote:
[...]
Quote:
Quote:
>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.programmer, where the function in question is
topical.

--
Keith Thompson (The_Other_Keith) kst-u@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.
Stephen Sprunk
Guest
 
Posts: n/a
#9: Dec 5 '06

re: Needed APIs: For copying file and finding Disk Usage


"Keith Thompson" <kst-u@mib.orgwrote in message
news:lnu00avqe9.fsf@nuthaus.mib.org...
Quote:
William Ahern <william@25thandClement.comwrites:
Quote:
>if (0 == (src = fopen("/path/to/source/file", "r")))
>perror("fopen"), exit(EXIT_FAILURE);
>
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_FAILURE);
}
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

Keith Thompson
Guest
 
Posts: n/a
#10: Dec 5 '06

re: Needed APIs: For copying file and finding Disk Usage


"Stephen Sprunk" <stephen@sprunk.orgwrites:
Quote:
"Keith Thompson" <kst-u@mib.orgwrote in message
news:lnu00avqe9.fsf@nuthaus.mib.org...
Quote:
>William Ahern <william@25thandClement.comwrites:
Quote:
>>if (0 == (src = fopen("/path/to/source/file", "r")))
>>perror("fopen"), exit(EXIT_FAILURE);
>>
>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_FAILURE);
> }
>
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.
Quote:
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.
Quote:
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_Keith) kst-u@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.
Ben Pfaff
Guest
 
Posts: n/a
#11: Dec 5 '06

re: Needed APIs: For copying file and finding Disk Usage


"Stephen Sprunk" <stephen@sprunk.orgwrites:
Quote:
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.
C has a construct called "functions" that can be usefully applied
to reduce duplication. For example:

void
my_close (int *fd)
{
close (*fd);
*fd = 1;
}

Then, later, when you want to check whether the fd is -1 before
closing it, you only have to change one place.
--
"Welcome to the wonderful world of undefined behavior, where the demons
are nasal and the DeathStation users are nervous." --Daniel Fox
pete
Guest
 
Posts: n/a
#12: Dec 6 '06

re: Needed APIs: For copying file and finding Disk Usage


Stephen Sprunk wrote:
Quote:
>
"Keith Thompson" <kst-u@mib.orgwrote in message
news:lnu00avqe9.fsf@nuthaus.mib.org...
Quote:
William Ahern <william@25thandClement.comwrites:
Quote:
if (0 == (src = fopen("/path/to/source/file", "r")))
perror("fopen"), exit(EXIT_FAILURE);
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_FAILURE);
}
>
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.
I always use a compound statement in my if statements.
Quote:
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.
That's most of the reason why
I always use a compound statement with an if, or an else, or a loop.
Quote:
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.
I use comma style macros whenever I can.

#define SWAP(A, B, T) \
((void)(*(T) = *(A), *(A) = *(B), *(B) = *(T)))

If the macro needs to contain statements
which are not expression statements, then I can't.

--
pete
William Ahern
Guest
 
Posts: n/a
#13: Dec 6 '06

re: Needed APIs: For copying file and finding Disk Usage


On Tue, 05 Dec 2006 05:54:38 +0000, Keith Thompson wrote:
<snip>
Quote:
Quote:
>FILE *src, *dst;
>unsigned char buf[1024];
>
Why 1024? It would probably make more sense to use BUFSIZ.
Yes. However, I didn't have my copy of the standard at-hand, and figured
I'd rather take the lashing for not using it, rather than to use it but
document it's source incorrectly ;)
Quote:
Quote:
>size_t n;
>>
>if (0 == (src = fopen("/path/to/source/file", "r")))
> perror("fopen"), exit(EXIT_FAILURE);
>
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_FAILURE);
}
I often use the comma operator like so

if (some_arg == 0)
return (errno = EINVAL), -1;

because I'm trying to express that I'm returning a particular state, not
simply doing a series of actions and then returning a value.

I carried it over to the example, but I agree that the example code is a
awkward. Neither the former (perror()) nor latter (exit()) action
in the expression strictly follows the pattern I use when employing the
comma operator in that situation. I suppose I was simply trying to be
terse


Closed Thread