system calls

S

Sheldon

Hi,

I'd like to make a system call from C to zip the output fie before
exiting.
This zipping process will take a little more time of course.

I tried the following:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/syscall.h>

char cmd[500];

strcpy(cmd," bzip2 -v9 /path/filename /path/filename.bz2");

status = system(cmd);

But the system returned a -1 and exited without any more error
message.

Does anyone know a better way of doing this?

/M
 
K

Keith Thompson

Sheldon said:
Does anyone know a better way of doing this?

We don't use anything other than system(NULL); around here.

Try pumping your **** into that, and if you have any further
questions, let us know.

Heh, heh, just kidding. I'll give you a serious answer
soon.
 
G

Guest

We don't use anything other than system(NULL); around here.

<snip>

unfortuatly, Sheldon, you fell foul of the local troll
who is forging Keith Thompson's posts (Keith is actually
a well respected regular poster in comp.lang.c).

I can't see anything blindingly obviously wrong
with your code so I suggest posting to comp.unix.programmer
where you will encounter more people with Unix knowledge.

BTW. the strcpy is a waste of time.

You *could* try typing your line
bzip2 -v9 /path/filename /path/filename.bz2

into your shell and see what happens.
 
K

Keith Thompson

(e-mail address removed) writes
unfortuatly, Sheldon, you fell foul of the local troll
who is forging Keith Thompson's posts (Keith is actually
a well respected regular poster in comp.lang.c)

Sheldon, Nick is right. We have a troll on this newsgroup named Richard
Heathfield who thinks it's funny forging posts. I'm sorry you had to
see his obscenities.

Nick's advice looks fine to me. Note that the troll is forging
Nick's posts as well, though, so you may want to take any further
comments from Nick with a grain of salt - they could be from someone
else.
 
G

Guest

Sheldon, Nick is right. We have a troll on this newsgroup named Richard
Heathfield who thinks it's funny forging posts. I'm sorry you had to
see his obscenities.

*nods*

its getting relaly childish the way hes actin aruound these
days
 
P

Paul

Sheldon said:
Hi,

I'd like to make a system call from C to zip the output fie before
exiting.
This zipping process will take a little more time of course.

I tried the following:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/syscall.h>

char cmd[500];

strcpy(cmd," bzip2 -v9 /path/filename /path/filename.bz2");

status = system(cmd);

But the system returned a -1 and exited without any more error
message.

Does anyone know a better way of doing this?

/M

"bzip2" versus "/usr/local/bin/bzip2" ?

Is that executable in a PATH somewhere ? For a first
try, I might hardcode the path to the executable.

Paul
 
A

Antoninus Twink

I'd like to make a system call from C to zip the output fie before
exiting.

Why do you want to rely on an external program instead of doing it
programmatically using the libbz2 library?
#include <unistd.h>
#include <sys/syscall.h>

Only stdlib.h is needed for system(3).
char cmd[500];
strcpy(cmd," bzip2 -v9 /path/filename /path/filename.bz2");

Usual warning about fixed-size buffers (assuming /path/filename might
actually be something determined at runtime).
status = system(cmd);

But the system returned a -1 and exited without any more error
message.

Does anyone know a better way of doing this?

Use fork and execlp instead of system (or if it really is the very last
thing your program does before exiting, then just execlp). Then if it
goes wrong, you'll at least be able to use perror to get a useful error
message.
 
K

kid joe

Sheldon wrote:
[...]
char cmd[500];

strcpy(cmd," bzip2 -v9 /path/filename /path/filename.bz2");

status = system(cmd);

But the system returned a -1 and exited without any more error
message.

Does anyone know a better way of doing this?

Are you sure bzip2 exists, and is in $PATH?

Some comments:

1 - If you are literally strcpy()ing the command into a buffer to pass to
system(), why not just pass the string itself?

status = system("bzip2 -v9 /path/filename /path/filename.bz2");

2 - (*nix-specific suggestion.) As a test, try passing "sh -c 'command'" to
system():

status = system("sh -c 'bzip2 -v9 /path/filename /path/filename.bz2'");

This should allow you to see any possible error messages that the shell may
generate when trying to execute the command, and perhaps point you in the
right direction.

Hi Kenneth,

If hes using the same version of bzip2 as the one Ive got, hes using the
wrong syntax, it should just be bzip2 /path/filename with no
/path/filename.bz2 on the commandline. This could make the bzip2 call fail.

Cheers,
Joe
 
M

Marston

Sheldon wrote:
[...]
char cmd[500];
strcpy(cmd," bzip2 -v9 /path/filename /path/filename.bz2");
status = system(cmd);
But the system returned a -1 and exited without any more error
message.
Does anyone know a better way of doing this?
Are you sure bzip2 exists, and is in $PATH?
Some comments:
1 - If you are literally strcpy()ing the command into a buffer to pass to
system(), why not just pass the string itself?
     status = system("bzip2 -v9 /path/filename /path/filename.bz2");
2 - (*nix-specific suggestion.)  As a test, try passing "sh -c 'command'" to
system():
     status = system("sh -c 'bzip2 -v9 /path/filename /path/filename.bz2'");
This should allow you to see any possible error messages that the shell may
generate when trying to execute the command, and perhaps point you in the
right direction.

Hi Kenneth,

If hes using the same version of bzip2 as the one Ive got, hes using the
wrong syntax, it should just be bzip2 /path/filename with no
/path/filename.bz2 on the commandline. This could make the bzip2 call fail.

Cheers,
Joe

--
       ......................  o    _______________           _,
      ` Good Evening!  ,      /\_  _|             |        .-'_|
      `................,     _\__`[_______________|       _| (_|
                             ] [ \, ][         ][        (_|

Thanks guys!

I've tried many of your suggestions but still fork returns a -1. The
option of sh -c didn't give any feedback as to what went wrong.
Perhaps this is not possible this way.
Perhaps the best is to create a shell script that does the zipping.

Thanks for your advice!

/M
 
H

Herbert Rosenau

(e-mail address removed) writes

Correction:
Sheldon, Nick is right. We have a troll on this newsgroup named Keith
Thopson who thinks it's funny forging posts. I'm sorry you had to
see his obscenities.

It would be perfect when this troll would give up to troll in the
groups related to programming arespective the language C he knows
nothing real about.


--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2R Deutsch ist da!
 
M

Marston

Marston said:
[...]
char cmd[500];
strcpy(cmd," bzip2 -v9 /path/filename /path/filename.bz2");
status = system(cmd);
But the system returned a -1 and exited without any more error
message.
[...]

Thanks guys!
I've tried many of your suggestions but still fork returns a -1.

"fork"?  I thought you were using system(), like your subject, description,
and sample code shows?
The option of sh -c didn't give any feedback as to what went wrong.

Nothing appeared on the screen?

What about:

     status = system("echo 'this is my echo'");
Perhaps this is not possible this way.

Perhaps if you were to show a complete example, stripped down to the bare
essentials?  ie: assuming the unzipped file already exists, does a simple
main() which system()s out to bzip2 work?  If so, how much of your program
do you need to include before it fails?
Perhaps the best is to create a shell script that does the zipping.

If you can't run "sh -c 'command'", what makes you think a shell script will
work?

This program is very large and uses up a lot of memory. The file to be
zipped is about 504 MB.
I used perror and errno to catch the error message returned by fork.
The Print and nrerror functions are my own functions for printing and
debugging. I kept the sh -c option but the result is the same without
it.

INFO: Zipping file using bzip2 via system call...
INFO: sh -c '/bin/bzip2 /home/marston/c_programming/trunk/complete/
ECMWF_NWP_20080910-0000-00.NC'
INFO: Integer number is -1
Error found!: Cannot allocate memory
errno = 29.
ERROR: Failed to zip output file
....now exiting to system...

.... snip...
Print("Zipping file using bzip2 via system call...");
Print(cmd);
status = system(cmd);
PrintI(status);

if(status != 0) {
perror("Error found!");
printf("errno = %d.\n", errno);
nrerror("Failed to zip output file");
}
......

The program seems to keep the memory locked up until the program exits
completely. I double checked to see if all allocated memory is freed
and they are.
Is there a way to release the memory before exiting the program?
I'm just trying to learn more about C programming here so this is a
test bed.
I appreciate your advice and help.

/M
 
F

Franken Sense

In Dread Ink, the Grave Hand of Antoninus Twink Did Inscribe:
I'd like to make a system call from C to zip the output fie before
exiting.

Why do you want to rely on an external program instead of doing it
programmatically using the libbz2 library?
#include <unistd.h>
#include <sys/syscall.h>

Only stdlib.h is needed for system(3).
char cmd[500];
strcpy(cmd," bzip2 -v9 /path/filename /path/filename.bz2");

Usual warning about fixed-size buffers (assuming /path/filename might
actually be something determined at runtime).
status = system(cmd);

But the system returned a -1 and exited without any more error
message.

Does anyone know a better way of doing this?

Use fork and execlp instead of system (or if it really is the very last
thing your program does before exiting, then just execlp). Then if it
goes wrong, you'll at least be able to use perror to get a useful error
message.

It's nice to see a little glasnost with respect to being able to talk about
OS questions using C. That it's not in the Standard means that if that's
all you know, then you're probably not going to be of much help.
 
K

Keith Thompson

Franken Sense said:
In Dread Ink, the Grave Hand of Antoninus Twink Did Inscribe: [...]
It's nice to see a little glasnost with respect to being able to talk about
OS questions using C. That it's not in the Standard means that if that's
all you know, then you're probably not going to be of much help.

What's wrong with discussing Unix-specific functions in
comp.unix.programmer?
 
R

Richard Bos

Richard said:
nick please stop polluting threads with needless warnings and cries of
foreboding thankyou

Oh, lord. Now he's even faking Richard <Nosurname> posts. Does this stop
at nothing?

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top