C Linux programming, reading system inputs like ping or ls -l

I am trying to create my own method that causes a char return with system exit.

pseudo code like this.

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

thanks for the help.

+4
source share
3 answers

You can use popen , which returns a stream from which you can read the result. By reading to the end of the file, in a line (perhaps one that dynamically grows as needed), you can implement what you ask.

+5
source

Few things

  • system() not a printf style function. You will need to use sprintf() to create your argument earlier.
  • system() return value is int, not char
  • As a rule, it is not recommended to rewrite function parameters.

What are you trying to do? It seems that all this function is performed by ping (which without the -c argument will never finish working with linux).

+2
source
  Duplicate the stdout to some other file descriptor by using dup2.After the execution of the command read all the lines from the file using that file descriptor and return it. 
+1
source

Source: https://habr.com/ru/post/1303072/


All Articles