Need help with system()

Discussion in 'C Programming' started by Oldbitcollector, Jan 5, 2004.

  1. 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! =-----
    Oldbitcollector, Jan 5, 2004
    #1
    1. Advertising

  2. 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
    Hallvard B Furuseth, Jan 5, 2004
    #2
    1. Advertising

  3. 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
    Thomas Matthews, Jan 5, 2004
    #3
  4. 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! =-----
    Oldbitcollector, Jan 5, 2004
    #4
  5. Oldbitcollector

    Sidney Cadot Guest

    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
    Sidney Cadot, Jan 5, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U0FM?=

    Need Help with System.Web.UI.WebControls.HyperLink

    =?Utf-8?B?U0FM?=, Feb 1, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    522
    Nathan Sokalski
    Feb 3, 2006
  2. darrel
    Replies:
    11
    Views:
    673
    Darren Kopp
    Feb 23, 2006
  3. lawrence
    Replies:
    11
    Views:
    642
    Long - CM web hosting
    Jun 1, 2004
  4. keithb
    Replies:
    3
    Views:
    18,561
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    May 7, 2006
  5. SF
    Replies:
    22
    Views:
    516
    Walter Roberson
    Mar 1, 2006
Loading...

Share This Page