[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 <stdio.h> </font><br> \
<font color="#993300">#include <ctype.h> \
</font><br> <font color="#993300">#include<stdlib.h> </font><br> \
<font color="#993300"> #include <stdarg.h> \
</font><br> <font color="#993300">#include <string.h></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"> void main(void) \
</font><br> <font color="#993300"> { </font><br> <font \
color="#993300"> msg("char","a","b","c","d");</font><br> <font \
color="#993300"> } </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">----------� ا���|------------</font><br> \
<font color="#ff6600">----------��</font><br> <font \
color="#ff6600"> \
�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 </font><br dir="RTL"> <font \
color="#ff6600">�|�|� ا���|------------</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 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> \
void foo(char *fmt, ...) {</div><div> \
va_list ap;</div><div> \
int d;</div><div> \
char c, *s;</div><div><br \
class="webkit-block-placeholder"></div><div> \
va_start(ap, fmt);</div><div> \
while (*fmt)</div><div> \
switch(*fmt++) \
{</div><div> \
case 's': /* string \
*/</div><div> \
s = va_arg(ap, char *);</div><div> \
\
printf("string %s\n", s);</div><div> \
\
break;</div><div> \
case 'd': /* int \
*/</div><div> \
d = va_arg(ap, int);</div><div> \
\
printf("int %d\n", d);</div><div> \
break;</div><div> \
case 'c': \
/* char */</div><div> \
/* need \
a cast here since va_arg only</div><div> \
takes \
fully promoted types */</div><div> \
c = (char) va_arg(ap, \
int);</div><div> \
printf("char %c\n", c);</div><div> \
\
break;</div><div> \
}</div><div> \
va_end(ap);</div><div> \
}</div><div><br \
class="webkit-block-placeholder"></div><div>perhaps you can adapt that to your \
testcase for solaris. otherwise, check what man va_start says on your \
system. </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