Newsgroups: fj.lang.c
Path: galaxy.trc.rwcp.or.jp!coconuts.jaist!wnoc-tyo-news!sinfony-news01!tokyonet.ad.jp!tokyonet.ad.jp!astecgw!news
From: Hisao Aoyama <aoyama@astec.co.jp>
Subject: Re: sprintf
X-Nntp-Posting-Host: kamui.astec.co.jp
Content-Type: text/plain; charset=ISO-2022-JP
Message-ID: <v4sp215yuu.fsf@astec.co.jp>
Lines: 72
Sender: aoyama@kamui
Organization: ASTEC, Inc. Tokyo, Japan
X-Newsreader: Gnus v5.3/Emacs 19.34
References: <v4endmsisr.fsf@astec.co.jp> <5g52ct$41q@csdnews.sm.sony.co.jp>
	<v4u3mh69kg.fsf@astec.co.jp>
Mime-Version: 1.0 (generated by tm-edit 7.105)
Date: Wed, 12 Mar 1997 07:54:49 GMT
Xref: galaxy.trc.rwcp.or.jp fj.lang.c:3779
X-originally-archived-at: http://galaxy.rwcp.or.jp/text/cgi-bin/newsarticle2?ng=fj.lang.c&nb=3779&hd=a
X-reformat-date: Mon, 18 Oct 2004 15:18:22 +0900
X-reformat-comment: Tabs were expanded into 4 column tabstops by the Galaxy's archiver. See http://katsu.watanabe.name/ancientfj/galaxy-format.html for more info.

$BNI$/FI$J$$$G=q$$$F$$$?$N$G!$D{@5$7$^$9!%(B

Hisao Aoyama <aoyama@astec.co.jp> writes:

> > $B2?$KBP$7$F(B strlen() $B$9$k$s$G$9$+!#(B
> > $B$^$5$+!"<!$N$h$&$J%3!<%I$rA[Dj$7$F$$$^$;$s$+!#(B
> 
> $B$=$N!V$^$5$+!W$G$9!%(B

$B!V$^$5$+!W$N%3!<%I$O;d$NA[Dj$7$F$$$k$b$N$G$O$"$j$^$;$s$G$7$?!%(B
$B!V(Bstrdup() $B$NBe$j$K(B strlen() $B$9$k!W$H=q$$$?$N$G!$$^$5$+8m2r(B
$B$5$l$k$H$O;W$o$J$+$C$?$H$$$&$*AFKv$G$9!%(B

$B;d$,A[Dj$7$F$$$?%3!<%I$O!$:dK\$5$s$NDs<($7$?(B

> char *cnv_sprintf(const char *format, ...)
> {
>         char buf[4096];    /* XXX */
>         va_list ap;
> 
>         va_start(ap, format);
>         vsprintf(buf, format, ap);
>         va_end(ap);
>         return strdup(buf);
> }

$B$r4p$K!$(B

char *cnv_sprintf(const char *format, ...)
{
static char *buf = NULL;
static int buf_size = 0;
        char tmp[4096];    /* XXX */
int len;
        va_list ap;

        va_start(ap, format);
        vsprintf(tmp, format, ap);
        va_end(ap);

len = strlen(tmp) + 1;
if(buf_size == 0){
buf = malloc(len);/* ignore failure */
buf_size = len;
}
else{
if(len > buf_size){
buf = realloc(buf,len);/* ignore failure */
buf_size = len;
}
}
strcpy(buf,tmp);
        return buf;
}

$B$N$h$&$KJQ99$7$?J*$G$9!%(B

$B:dK\$5$s$NNc$K$/$i$Y$F!$2?$+$^$:$$E@$,$"$k$G$7$g$&$+(B?

$BNI$$$+$b$7$l$J$$$N$O!$(B

- $B8F$VB&$,(B free() $B$7$J$/$FNI$$(B
- malloc()/realloc() $B$N2s?t$,>/$/$J$k$+$iAa$/$J$k$+$b$7$l$J$$(B

$B$/$i$$$G$7$g$&!%(B

# $B7k6I%3!<%I$r=q$$$F$7$^$C$?!%(B

----
Hisao Aoyama $B@D;3(B $B>0IW(B
ASTEC, Inc.  $B!J3t!K%"%9%F%C%/(B
aoyama@astec.co.jp
