On Jan 31, 2008, at 11:09 AM, bare wrote:
Dear Sir:
I am sorry to trouble you, but this is important for me, and I need
you help, thank you very much.
The problem is the following cod:
#include <stdio.h>
#include <ctype.h>
#include<stdlib.h>
#include <stdarg.h>
#include <string.h>
void msg(const char* type, ...)
{
va_list ap;
char* buf;
va_start(ap,type);
while(1) {
char* arg = va_arg(ap, char*);
if (arg == NULL || strcmp(arg, "") == 0) {
printf("break...\n");
break;
}
printf("----------%s------------\n",arg);
//len += strnlen(arg, 200)+1;
}
va_end(ap);
}
void main(void)
{
msg("char","a","b","c","d");
}
when it is run in Red hat, the result is :
begin........
----------a------------
----------b------------
----------c------------
----------d------------
break...
but in solaris:
----------a------------
----------b------------
----------c------------
----------d------------
----------� ا���|------------
----------��
�ZYX��]���------------
----------Z�2------------
----------����------------
----------[�â------------
----------P
�������_��1��n~------------
----------n
�|�|� ا���|------------
----------�|------------
----------[��------------
----------�z------------
break...
That means if the program is run in solaris, some problems will
occur like 'core dumped".I don't know how to resolve this problem.
Of course we can add a parameter of the count of parameters and do
something in a for iterator, but there are many place to modify, so
can you give me some suggestions to resolve this problems in solaris
environment? thank you very much. I am looking forward to you reply.
The same problem is also occured in lib/mgmt/mgmt_common_lib.c
char*
mgmt_new_msg(const char* type, ...)
{
va_list ap;
int len;
char* buf;
/* count the total len of fields */
len = strnlen(type, MAX_STRLEN)+1;
va_start(ap,type);
i don't like the look of this while-loop at all
while(1) {
char* arg = va_arg(ap, char*);
if (arg == NULL) {
break;
}
len += strnlen(arg, MAX_STRLEN)+1;
}
va_end(ap);
/* alloc memory */
buf = (char*)mgmt_malloc(len+1);
if (buf == NULL) {
return NULL;
}
/* assign the first field */
snprintf(buf,len,"%s", type);
/* then the others */
va_start(ap, type);
while(1) {
char* arg = va_arg(ap, char*);
if (arg == NULL) {
break;
}
strncat(buf, "\n", len);
strncat(buf, arg, len);
}
va_end(ap);
return buf;
}
this is the loop suggested by "man va_start" on linux
void foo(char *fmt, ...) {
va_list ap;
int d;
char c, *s;
va_start(ap, fmt);
while (*fmt)
switch(*fmt++) {
case 's': /* string */
s = va_arg(ap, char *);
printf("string %s\n", s);
break;
case 'd': /* int */
d = va_arg(ap, int);
printf("int %d\n", d);
break;
case 'c': /* char */
/* need a cast here since va_arg only
takes fully promoted types */
c = (char) va_arg(ap, int);
printf("char %c\n", c);
break;
}
va_end(ap);
}
perhaps you can adapt that to your testcase for solaris. otherwise,
check what man va_start says on your system.
_______________________________________________________
Linux-HA-Dev: [email protected]
http://lists.linux-ha.org/mailman/listinfo/linux-ha-dev
Home Page: http://linux-ha.org/