4

I try to run a Linux command and read the output from it by using C/C++ code. I search for exec but this don't deal with input/output.

What I am trying to achieve is to get information about wireless LAN by using this command iwconfig, invoking it from C/C++ code.

also i need a suitable code to use it as lib for android using NDK.

i see in android open source they called this function

what do you think about this code ?

int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
                 char *reply, size_t *reply_len,
                 void (*msg_cb)(char *msg, size_t len))
                {
        DWORD written;
        DWORD readlen = *reply_len;

if (!WriteFile(ctrl->pipe, cmd, cmd_len, &written, NULL))
    return -1;

if (!ReadFile(ctrl->pipe, reply, *reply_len, &readlen, NULL))
    return -1;
*reply_len = readlen;

return 0;

}

this is the link

Hana90
  • 943
  • 5
  • 17
  • 37

2 Answers2

3

You could try running the command and outputting the results to a file, then reading it

system("iwconfig > temp.txt");
FILE *fp=fopen("temp.txt","w");
zacaj
  • 1,987
  • 1
  • 19
  • 39
  • +1 Nice and simple solution (not to say `popen()` would achieve the same, but is more complicated to use) ... – πάντα ῥεῖ Mar 01 '13 at 18:44
  • fp will just be a FILE handle. You can read it using fscanf() the way you would use scanf() normally. I'd suggest you google for a basic C file reading tutorial – zacaj Mar 01 '13 at 19:01
  • @DondonAli With `fp` you can read the string information from that file produced by the `"iwconfig > temp.txt"`call. You'll need to read it from the `fp` using `read()` (same for pipe file handle). – πάντα ῥεῖ Mar 01 '13 at 19:02
  • @g-makulik On the contrary, popen() is safer than system() and gives much better control. The only thing against, if anything, popen can be, popen is not part standard C. But I am a POSIX lover ;-) – P.P Mar 01 '13 at 19:03
  • @KingsIndian Of course! I'm trying to explain the principle to the OP! – πάντα ῥεῖ Mar 01 '13 at 19:04
  • 5
    There is a huge concurrency issue with this approach. Solving it properly would be more complicated than using `popen` so it is best to avoid this solution altogether. Just use the right tool for the job: `popen` – syam Mar 01 '13 at 19:18
  • It's not the *correct* approach, no, but if @DondonAli just wants a quick and simple program for personal use, it should be just fine – zacaj Mar 01 '13 at 21:57
0

i see in android open source they called this function

what do you think about this code ?

int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
                     char *reply, size_t *reply_len,
                     void (*msg_cb)(char *msg, size_t len))
{
    DWORD written;
    DWORD readlen = *reply_len;

    if (!WriteFile(ctrl->pipe, cmd, cmd_len, &written, NULL))
        return -1;

    if (!ReadFile(ctrl->pipe, reply, *reply_len, &readlen, NULL))
        return -1;
    *reply_len = readlen;

    return 0;
}

this is the link

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Hana90
  • 943
  • 5
  • 17
  • 37
  • Doesn't really tightly relate to what you've been asking. The key here is how to handle `ctrl->pipe` and how to retrieve (parsing) the information when reading the commands output from the pipe. To avoid downvotes, you'll be better off enclosing this sample to your question ... – πάντα ῥεῖ Mar 01 '13 at 18:57
  • if i use this answer system("iwconfig > temp.txt"); FILE *fp=fopen("temp.txt","w"); how to get string value from fp ? – Hana90 Mar 01 '13 at 19:00
  • @ShafikYaghmour yes i need this code to join it with android via NDK. – Hana90 Mar 01 '13 at 19:01
  • 1
    @DondonAli Well then this changes my vote for dup, also please edit the question to say that you are looking for an android solution. – Shafik Yaghmour Mar 01 '13 at 19:16
  • _'if i use this answer system("iwconfig > temp.txt"); ...'_ For getting a string from a `FILE*` pointer refer to the [`fgets()`](http://pubs.opengroup.org/onlinepubs/009696899/functions/fgets.html) function documentation. – πάντα ῥεῖ Mar 01 '13 at 19:17