[prev in list] [next in list] [prev in thread] [next in thread] 

List:       linux-ha-dev
Subject:    Re: [Linux-ha-dev] Linux-HA-Question
From:       Andrew Beekhof <beekhof () gmail ! com>
Date:       2008-01-31 11:25:20
Message-ID: B6F8961E-4E10-405A-BDC7-48B695A3FD7B () gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


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.


[Attachment #5 (text/html)]

<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; \
-webkit-line-break: after-white-space; "><br><div><div>On Jan 31, 2008, at 11:09 AM, \
bare wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"> <div> \
Dear Sir:<br> 	I am sorry to trouble you, but this is important for me, and I need \
you help, thank you very much.<br> 	The problem is the following cod:<br> <font \
color="#993300">#include&nbsp;&nbsp;&nbsp; &lt;stdio.h&gt;&nbsp;&nbsp; </font><br> \
<font color="#993300">#include&nbsp;&nbsp;&nbsp; &lt;ctype.h&gt;&nbsp;&nbsp; \
</font><br> <font color="#993300">#include&lt;stdlib.h&gt;&nbsp;&nbsp; </font><br> \
<font color="#993300"> #include&nbsp;&nbsp;&nbsp; &lt;stdarg.h&gt;&nbsp;&nbsp; \
</font><br> <font color="#993300">#include &lt;string.h&gt;</font><br> <br> <font \
color="#993300">void msg(const char* type, ...)</font><br> <font \
color="#993300">{</font><br> <font color="#993300">	</font><br> <font \
color="#993300">	va_list ap;</font><br> <font color="#993300">	</font><br> <font \
color="#993300">	char* buf;</font><br> <font \
color="#993300">	va_start(ap,type);</font></div></blockquote><blockquote \
type="cite"><div><br> <font color="#993300">	while(1) {</font><br> <font \
color="#993300">		char* arg = va_arg(ap, char*);</font><br> <font \
color="#993300">		if (arg == NULL || strcmp(arg, "") == 0) {</font><br> <font \
color="#993300">			printf("break...\n");</font><br> <font \
color="#993300">			break;</font><br> <font color="#993300">		}</font><br> <font \
color="#993300">		printf("----------%s------------\n",arg);</font><br> <font \
color="#993300">		//len += strnlen(arg, 200)+1;</font><br> <font \
color="#993300">	}</font><br> <font color="#993300">	</font><br> <font \
color="#993300">	va_end(ap);</font><br> <br> <font color="#993300">}</font><br> <font \
color="#993300">&nbsp;&nbsp; void&nbsp;&nbsp;&nbsp; main(void)&nbsp;&nbsp; \
</font><br> <font color="#993300">&nbsp;&nbsp; {&nbsp;&nbsp; </font><br> <font \
color="#993300">	msg("char","a","b","c","d");</font><br> <font \
color="#993300">&nbsp;&nbsp; }&nbsp;&nbsp; </font><br> <br> when it is run in Red \
hat, the result is :<br> <font color="#ff6600">begin........</font><br> <font \
color="#ff6600">----------a------------</font><br> <font \
color="#ff6600">----------b------------</font><br> <font \
color="#ff6600">----------c------------</font><br> <font \
color="#ff6600">----------d------------</font><br> <font \
color="#ff6600">break...</font><br> but in solaris:<br> <font \
color="#ff6600">----------a------------</font><br> <font \
color="#ff6600">----------b------------</font><br> <font \
color="#ff6600">----------c------------</font><br> <font \
color="#ff6600">----------d------------</font><br dir="RTL"> <font \
color="#ff6600">----------�&nbsp;&nbsp;&nbsp; ا���|------------</font><br> \
<font color="#ff6600">----------��</font><br> <font \
color="#ff6600">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
�ZYX��]���------------</font><br> <font \
color="#ff6600">----------Z�2------------</font><br> <font \
color="#ff6600">----------����------------</font><br> <font \
color="#ff6600">----------[�â------------</font><br> <font \
color="#ff6600">----------P</font><br> <font \
color="#ff6600">�������_��1��n~------------</font><br> <font \
color="#ff6600">----------n&nbsp;&nbsp;&nbsp; </font><br dir="RTL"> <font \
color="#ff6600">�|�|�&nbsp;&nbsp;&nbsp; ا���|------------</font><br> \
<font color="#ff6600">----------�|------------</font><br> <font \
color="#ff6600">----------[��------------</font><br> <font \
color="#ff6600">----------�z------------</font><br> <font \
color="#ff6600">break...</font><br> That means if the program is run in solaris, some \
problems will occur like <font color="#993300">'core dumped</font>".I don't know how \
to resolve this problem. Of course we&nbsp; 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.<br> <br> The same problem is \
also occured in lib<b>/mgmt/mgmt_common_lib.c</b><br> <font \
color="#993300">char*</font><br> <font color="#993300">mgmt_new_msg(const char* type, \
...)</font><br> <font color="#993300">{</font><br> <font color="#993300">	va_list \
ap;</font><br> <font color="#993300">	int len;</font><br> <font \
color="#993300">	char* buf;</font><br> <font color="#993300">	</font><br> <font \
color="#993300">	/* count the total len of fields */	</font><br> <font \
color="#993300">	len = strnlen(type, MAX_STRLEN)+1;</font><br> <font \
color="#993300">	va_start(ap,type);</font></div></blockquote><div><br \
class="webkit-block-placeholder"></div><div>i don't like the look of this while-loop \
at all</div><br><blockquote type="cite"><div><br> <font color="#993300">	while(1) \
{</font><br> <font color="#993300">		char* arg = va_arg(ap, char*);</font><br> <font \
color="#993300">		if (arg == NULL) {</font><br> <font \
color="#993300">			break;</font><br> <font color="#993300">		}</font><br> <font \
color="#993300">		len += strnlen(arg, MAX_STRLEN)+1;</font><br> <font \
color="#993300">	}</font><br> <font color="#993300">	va_end(ap);</font><br> <font \
color="#993300">	</font><br> <font color="#993300">	/* alloc memory */</font><br> \
<font color="#993300">	buf = (char*)mgmt_malloc(len+1);</font><br> <font \
color="#993300">	if (buf == NULL) {</font><br> <font color="#993300">		return \
NULL;</font><br> <font color="#993300">	}</font><br> <br> <font color="#993300">	/* \
assign the first field */</font><br> <font color="#993300">	snprintf(buf,len,"%s", \
type);</font><br> <font color="#993300">	</font><br> <font color="#993300">	/* then \
the others */</font><br> <font color="#993300">	va_start(ap, type);</font><br> <font \
color="#993300">	while(1) {</font><br> <font color="#993300">		char* arg = va_arg(ap, \
char*);</font><br> <font color="#993300">		if (arg == NULL) {</font><br> <font \
color="#993300">			break;</font><br> <font color="#993300">		}</font><br> <font \
color="#993300">		strncat(buf, "\n", len);</font><br> <font \
color="#993300">		strncat(buf, arg, len);</font><br> <font \
color="#993300">	}</font><br> <font color="#993300">	va_end(ap);</font><br> <font \
color="#993300">	</font><br> <font color="#993300">	return buf;</font><br> <font \
color="#993300">}</font><br> <br></div></blockquote><div><br \
class="webkit-block-placeholder"></div><div><br \
class="webkit-block-placeholder"></div><div>this is the loop suggested by "man \
va_start" on linux</div><div><br \
class="webkit-block-placeholder"></div><div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; void foo(char *fmt, ...) {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; va_list ap;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int d;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char c, *s;</div><div><br \
class="webkit-block-placeholder"></div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; va_start(ap, fmt);</div><div>&nbsp;&nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (*fmt)</div><div>&nbsp;&nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;switch(*fmt++) \
{</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp;case 's': &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* string \
*/</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s = va_arg(ap, char *);</div><div>&nbsp;&nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; printf("string %s\n", s);</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
break;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp;case 'd': &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* int \
*/</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d = va_arg(ap, int);</div><div>&nbsp;&nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
printf("int %d\n", d);</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;</div><div>&nbsp;&nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 'c': \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* char */</div><div>&nbsp;&nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* need \
a cast here since va_arg only</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;takes \
fully promoted types */</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c = (char) va_arg(ap, \
int);</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("char %c\n", c);</div><div>&nbsp;&nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; break;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp;}</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; va_end(ap);</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp;}</div><div><br \
class="webkit-block-placeholder"></div><div>perhaps you can adapt that to your \
testcase for solaris. &nbsp;otherwise, check what man va_start says on your \
system.&nbsp;</div><div><br \
class="webkit-block-placeholder"></div></div></div></body></html>



_______________________________________________________
Linux-HA-Dev: Linux-HA-Dev@lists.linux-ha.org
http://lists.linux-ha.org/mailman/listinfo/linux-ha-dev
Home Page: http://linux-ha.org/


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic