By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 1,017 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

Need help with system()

P: n/a
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

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Oldbitcollector wrote:
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.

--
Hallvard
Nov 14 '05 #2

P: n/a
Oldbitcollector wrote:
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

Nov 14 '05 #3

P: n/a
Thomas Matthews wrote:
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

Nov 14 '05 #4

P: n/a
eff

Try this:
char buffer[256];

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

Worked pefectly! Thanks

Oldbitcollector


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.