Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 30 Jan 2003 14:06:32 +0100
From:      Erik Trulsson <ertr1013@student.uu.se>
To:        Adrian Penisoara <ady@freebsd.ady.ro>
Cc:        standards@freebsd.org
Subject:   Re: strftime(): FreeBSD vs. Linux
Message-ID:  <20030130130631.GA69654@falcon.midgard.homeip.net>
In-Reply-To: <Pine.BSF.4.10.10301301357001.46520-100000@ady.warpnet.ro>
References:  <Pine.BSF.4.10.10301301357001.46520-100000@ady.warpnet.ro>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, Jan 30, 2003 at 02:15:52PM +0200, Adrian Penisoara wrote:
> Hi,
> 
>   Recently I had to "backport" a C program written under FreeBSD to
> Linux and suprisingly I found that the strftime() function behaves
> different in FreeBSD and Linux: while in FreeBSD strftime() would fill
> the string buffer with whatever data was specified in the format string
> up to the specified buffer length, in Linux strftime() would refuse to
> place the last token specified in the format string if there was not
> enough space to hold that token and a string terminator (\0).
> 
>   E.g.:
> 
>     #define LEN 12
>     char buffer[LEN+1];
>     time_t t=time(NULL);
> 
>     strftime(buffer, LEN, "%b %d %R", localtime(&t));
>     buffer[LEN] = '\0';
> 
>     printf("Result: [%s]\n", buffer);
> 
>   This snippet would behave as expected in FreeBSD but not in Linux. In
> Linux you would need to put LEN+1 as the second argument for strftime().
> 
>   I find this to be quite disturbing and I wonder which OS steps on the
> standards. What is the correct behavior ?

I would suggest you check what the return value from strftime is.
I suspect in this case it will be 0 for both FreeBSD and Linux, which
means that the whole string (including the termintating '\0') didn't
fit in the buffer.  If strftime returns 0 the contents of the buffer is
undefined.  (A non-zero return value is the number of characters (not
including the terminating '\0') that was but into the buffer.

I.e. it is not really working as you expect in FreeBSD either, but you are
ignoring the return code that says so.


IMO, both systems follow the standard just fine in this case, and the
bug is in your code.



-- 
<Insert your favourite quote here.>
Erik Trulsson
ertr1013@student.uu.se

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-standards" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030130130631.GA69654>