Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Programming linux , read system inputs like ping or ls -l

Tags:

c

linux

system

i trying make a custom method what causes return a char with system output.

the pseudocode like this.

char *my_Out(char *in ){
    in = system ("ping %s",in);
    return in;
}

thanks for the help.

like image 729
Edbrik Avatar asked Feb 28 '23 11:02

Edbrik


2 Answers

You can use popen, which returns you a stream that you can read the output from. By reading until end-of-file, into a string (probably one that dynamically grows as necessary), you can implement what you're asking for.

like image 108
Chris Jester-Young Avatar answered Mar 07 '23 03:03

Chris Jester-Young


A few things

  1. system() is not a printf style function. You'll need to use sprintf() to create your argument before.
  2. system()'s return value is an int, non a char
  3. It's generally not a good idea to overwrite function parameters.

What are you trying to do? It looks like all this function does is run ping (which, without the -c argument, will never finish running on linux).

like image 32
jdizzle Avatar answered Mar 07 '23 01:03

jdizzle