problem with sprintf snprintf in Solaris


 
Thread Tools Search this Thread
Operating Systems Solaris problem with sprintf snprintf in Solaris
# 1  
Old 12-13-2004
Question popen truncates the command line passed to it

********************************
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

Last edited by diganta; 12-15-2004 at 02:32 AM..
# 2  
Old 12-13-2004
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?
# 3  
Old 12-14-2004
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.
# 4  
Old 12-15-2004
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.
# 5  
Old 12-17-2004
serious problem with popen

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.
# 6  
Old 12-23-2004
popen throws 'bad file number' error

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?

Last edited by diganta; 12-23-2004 at 02:45 AM..
# 7  
Old 12-23-2004
Re: popen throws 'bad file number' error

Quote:
Originally posted by diganta
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
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Programming

Problem with Sprintf

Hi, I have the below sample code to hash the input number read from file. File will have 16 to 19 digit number and executable hash the number using some logic and returns the hashed value. Each digit in the 16 digit number is converted to a 4 byte value. That if the input is 16digit integer, the... (6 Replies)
Discussion started by: ramkrix
6 Replies

2. Shell Programming and Scripting

Problem with awk awk: program limit exceeded: sprintf buffer size=1020

Hi I have many problems with a script. I have a script that formats a text file but always prints the same error when i try to execute it The code is that: { if (NF==17){ print $0 }else{ fields=NF; all=$0; while... (2 Replies)
Discussion started by: fate
2 Replies

3. Programming

solaris appl migration: sprintf decimal separator

Hi, I am trying to migrate an application from Solaris to AIX 5.3.1. In both platforms the locale settings are as follows: Result of executing "locale" LANG=C LC_CTYPE="C" LC_NUMERIC="C" LC_TIME="C" LC_COLLATE="C" LC_MONETARY="C" LC_MESSAGES="C" In solaris when you execute this... (5 Replies)
Discussion started by: coldas57
5 Replies

4. Shell Programming and Scripting

search sprintf and replace with snprintf

Hi, anyone knows the perl search-and-replace expression for strcpy (char * destination, const char * source ); to strncpy ( char * destination, const char * source, size_t num ); ? the first and second arguments are the same (destination and source), the difference being that strncpy... (1 Reply)
Discussion started by: ChaMeN
1 Replies

5. Shell Programming and Scripting

Perl script to search sprintf and replace with snprintf

Dear all, I am new to perl script and would need some help for my 1st script. I wrote a script to search sprintf(buf,"%s", sourcestring) and replace with snprintf(buf, sizeof(buf),"%s", sourcestring). As snprintf() requires an extra argument, so it is not a simple search-and-replace. I need to... (1 Reply)
Discussion started by: ChaMeN
1 Replies

6. Programming

equivalent of sprintf in C++

Hi My requirement is to convert the following to C++ char buffer; sprintf(buffer,"%s %-50s %6s %-6d %s\n",a.substr(0,5),a.substr(10,20)) Since the buffer is of varying length, i cannot hardcode the value as 90. i would like to convert the buffer to string object so that it can receive any... (1 Reply)
Discussion started by: dhanamurthy
1 Replies

7. Programming

does snprintf guarantee null termination?

Hi All, I was reading the man page of snprintf function and it saids that snprintf adds a null terminator at the end of the string, but I remember once someone told me that snprintf doesn't guarantee the insertion of a null terminator character. What do you think? Does anyone have experience... (4 Replies)
Discussion started by: lagigliaivan
4 Replies

8. Programming

sprintf function

Hi, Can someone help me to figure out whether this code is to write file to /tmp/TIMECLOCK directory or just to asign a variable with "/tmp/TIMECLOCK/name.log_copy.pid" as the string? I am looking into an old C program and could not figure out where in the code that creates... (1 Reply)
Discussion started by: whatisthis
1 Replies
Login or Register to Ask a Question