You are not logged in.
Hello devs,
I've lack of programing experience with popen(); so I want to ask a question.
I'm trying to build up a simple server with C, to have a simple "remote shell". A C program listens to a connection ( e.g. "telnet 225.225.225.221 6000" ) and then it arrives, gives a user a possibility to enter a command ( e.g. "ls -al" ) and then server gives the output of this command to the user.
Now I have problem with popen. The construction of this function is like this:
FILE *popen(const char *command, const char *type);
Then I'm reading the user input, I store everything in a char buffer, but popen is accepting only const char pointers, so I do
command = ( const char * ) buffer;
Here is a sample code:
....
char buffer[255];
const char *command;
...
n = write ( client_socket, "> ", 2 );
n = read ( client_socket, buffer, 256 );
command = ( const char * ) buffer;
if ( ! ( fpipe = ( FILE *) popen ( command, "r" ) ) )
{
perror ( "Piping error" );
error(1);
}
...
But then I fire-up server and give a command to a server ( e.g. "ls" ) it returns
: command not found
So, I suppose this means, that popen(); sends nothing to a shell, although, then I do:
printf("%s\n", command);
I clearly see the sending command.
The
...
if ( ! ( fpipe = ( FILE *) popen ( "ls -al", "r" ) ) )
{
perror ( "Piping error" );
error(1);
}
...
Works just fine
How to correctly convert char buffer to a const char pointer, what popen will understand my sending command ?
Thank You
Last edited by Dummas (2011-01-21 07:10:06)
Arch Linux Lietuva
http://archlinux.lt
Offline
Ok, solution found. Did not expect this kind of char transfer from TCP.
Solution was simply to add:
buffer[n-2] = '\0';
Close/Delete this topic please.
Arch Linux Lietuva
http://archlinux.lt
Offline
Just mark it [SOLVED] by editing your first post and prepending it to the subject header.
There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !
Offline
Do you really need to use the "command" variable? Just because popen()'s prototype says "const char *" doesn't mean it won't accept a "char *", just that it won't modify the data being pointed to.
I have no experience with popen(), but I get the impression that the contents of buffer[] must be modified somewhere... Does it really print the correct command when you insert the printf() just before the popen() call?
Or maybe it's a codepage problem?
Also you've got a 255 bytes buffer but you tell read() to read up to 256 bytes...
Offline
@stqn, firslty, I was thinking that this is casting problem, that's why I was using different variable ( e.g. command ); In my final version, there is no command variable.
Yes, then I pass modified buffer ( see the solution ) to a printf(); and popen(); everything works fine.
This is raw code: https://gist.github.com/789362
( give_command is required by my task, because user passes everything with white spaces, e.g. cat / etc / rc.conf and server must make it like cat /etc/rc.conf )
Last edited by Dummas (2011-01-21 07:24:57)
Arch Linux Lietuva
http://archlinux.lt
Offline
Be careful with the return value of give_command(). This function returns a pointer to a local variable. This variable could be overwritten by any function call as soon as you're no longer inside give_command().
I'm almost sure that if you move "char command[255];" to the very beginning of give_command() then it will be overwritten. As is, it is "protected" by part[60] which will be overwritten first.
A solution would be to copy the contents of command[] into buffer inside give_command(), instead of returning a pointer to command.
I'd also check the return value of read() (you're in trouble if it returns 0 .)
Offline