So what I am trying to do is to invoke a system command with system()
function and then whatever its output is I would like to take it and send it over to the client (socket connection).
Client can send various messages. It can be ls
but it might be even qwerty
. I would like to take the ouput and place it in the write()
function as a const void* buffer
argument. I have seen this topic but I can get it done to work. So far I thought it could go somewhere of these lines but whatever I tried it did not work.
/* buffer is message from the client: ls, ls -l, whatever*/
system(buffer)
fp = popen(buffer, "r");
if(fp == NULL)
printf("Failed ot run command\n");
while(fgets(path, sizeof(path), fp) != NULL) {
//modify output here?
}
pclose(fp);
write(socket_fd, output, strlen(buffer));