problem with sprintf snprintf in Solaris

********************************
Following is not the real issue. The issue is with popen.
Plz continue forward with the thread to get a better picture.
********************************

Hi,

I am working on a customised ftp application. In it we have used sprintf to store a UNIX command like "ls *test" to a temporary variable to execute it on the server and return the result to the client. However the sprintf and snprintf is not working properly. The following code segment will give an idea.

--------------------------
char name[] = "*test";
char cmd[] = "/bin/ls -l %s";
char line[1024];

sprintf (line, cmd, name);
---------------------------
After sprintf the
expected result is,
line = "/bin/ls -l *test"

but what we are getting is,
line = "/bin/ls"
----------------

Can anyone tell me what is wrong with the code? I will be highly indebted.

Regards,
Diganta

I cut and pasted your code and it works for me. You don't say how you displayed line. I added a line:
printf ("line = %s \n", line);

Could you have a bug in your debugging statement?

the above code is working if I copy paste it and then compile it. However in my application there is one more line after the call to sprintf

name = line;

the value are ok before this line. only after this line is the data getting truncated. Can you give me any idea why.

Also, in the actual applycation the variable are like this,

char line[1024]; //local variable of the function.
char *cmd, *line: //passed as arguments to the function.

It seems I have found where the problem is. The problem is not with the assignment statement as stated above. It is in a call to popen. After calling popen as

fin = popen (line, "r");

the value of line gets truncated.

Is there some issue with popen in Solaris? I have searched the man pages but could not find anything.

The problem is not supposed to be with popen. popen is a standard function and I do not think that it will modify the value of "line". It must be getting modified somewhere else somehow. The value of line is perfect just before popen. Just after popen it is changed. Any idea how i can find how it is getting changed?

Also, the behaviour of the application is strange. Sometimes the client - server link gets disconnected after the first 'ls' when we try another 'ls'. Sometimes it happens only after we give a 'cd' command followed by a 'ls' command.

If anyone can help me, I'll be hightly obliged.

Sorry, I didn't get time to look into the real issue.

After putting some debugging logs I found that popen is throwing "Bad file number" error on the first call with "/bin/ls -l" as the argument. On the second call it throws "No child processes". (By throws I mean it sets the value at errno).

Another strange behaviour is that after a put command the error thrown is "Illegal seek".

I would like to point it out here that all these error are in Solaris only. The same application is being compiled for AIX also. There it is working just fine.

Any idea why?

You have bugs in your code. Buggy code often works differently on different platforms

Can the problem be related to the makefile? The application uses a very complex makefile to compile. Can the problem be due to wrong way of compiling (if there is any such thing)? It uses a lot of custom made libraries. The libraries are well tested and should not have any errors in it.