Need help with system()

  • Thread starter Oldbitcollector
  • Start date
O

Oldbitcollector

I'm currently learning C (linux based compiler)
and like most other languarges, I'm using a project
to modivate me into learning the language.

Ive got a routine that looks like this...

FILE *fp;
int size=0;
u_int8_t *ptr=NULL;

if (!(fp=fopen(name,"rb"))) {
system("program /dev/fd0 -read"???????);


For some reason it completely escapes me how to
pass the contents of the "name" variable to the system command.


In Perl, I would have done something like this:

system("program /dev/fd0 -read $name");

Can you guys offer a little help?

Thanks
Jeff
 
H

Hallvard B Furuseth

Oldbitcollector said:
if (!(fp=fopen(name,"rb"))) {
system("program /dev/fd0 -read"???????);

For some reason it completely escapes me how to
pass the contents of the "name" variable to the system command.

#include <stdlib.h>

char *cmdbuf;
const char cmdfmt[] = "program /dev/fd0 -read %s";
cmdbuf = malloc(sizeof(cmdfmt) + strlen(name));
if (cmdbuf == NULL)
return <error>;
sprintf(cmdbuf, cmdfmt, name);
...
system(cmdbuf);
free(cmdbuf);

cmdbuf must be large enough to hold the command plus a terminating \0
character.
Actually I could have subtracted 2 from the size because
the two characters '%s' do not count, but it looks simpler this way.
Space for the terminating \0 comes from the sizeof(), which unlike
strlen() counts the terminating \0 in the array.
 
T

Thomas Matthews

Oldbitcollector said:
I'm currently learning C (linux based compiler)
and like most other languarges, I'm using a project
to modivate me into learning the language.

Ive got a routine that looks like this...

FILE *fp;
int size=0;
u_int8_t *ptr=NULL;

if (!(fp=fopen(name,"rb"))) {
system("program /dev/fd0 -read"???????);


For some reason it completely escapes me how to
pass the contents of the "name" variable to the system command.


In Perl, I would have done something like this:

system("program /dev/fd0 -read $name");

Can you guys offer a little help?

Thanks
Jeff
Try this:
char buffer[256];

sprintf(buffer, "program / dev/fd0 -fread%s", name);
system(buffer);


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
S

Sidney Cadot

Thomas said:
Try this:
char buffer[256];

sprintf(buffer, "program / dev/fd0 -fread%s", name);
system(buffer);

That's a buffer overflow bug right there.

This may be a useful technique for a throw-away program (or sometimes
even in a real program, when guarantees can be given for the size of the
"name" string), but this is definitely /not/ good advice on how to solve
the OPs problem, without even a hint on the possible dangers.


Best regards,

Sidney
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top