Hi,
I am trying to implement telnet and execute "ls" command on the server side (called from the client side). For that, i have popen() a file and passed "ls" in a buffer from the client side to the server.
I am trying to read the output of this file in the same buffer using fgets(), line at a time.
Then writing the contents of this buffer to the client using write() system call.
But it only displays the last file read from the file and not the whole files in the directory.Whereas, it prints the whole list on the server side.
Code:
fp = popen(buffer, "r");
if (fp == NULL) {
printf("Failed to run command\n" );
exit;
}
while (fgets(buffer, sizeof(buffer)-1, fp) != NULL) {
printf("%s", buffer);
}
pclose(fp);
n = write(newsockfd,buffer,strlen(buffer));
if (n < 0) error("ERROR writing to socket");
Is there anyway that i can pass the whole list of files read from the file to the client using write system call???