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

next in thread | previous in thread | raw e-mail | index | archive | help
Hi,

On Thu, 30 Jan 2003, Erik Trulsson wrote:

> 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.

 It's zero both on Linux and FreeBSD. It's 12 both on Linux and FreeBSD
when I use LEN+1 as the 2nd argment.

> 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.

 Yep, that's what strftime(2) sais, now that I checked it out.

> 
> 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.

  Yes, you are right. Next time I should RTFM more closely... :-S.

 Thanks !
 Ady (@warpnet.ro)
____________________________________________________________________
| An age is called Dark not because the light fails to shine, but  |
| because people refuse to see it.                                 |
|               -- James Michener, "Space"                         |



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?Pine.BSF.4.10.10301301522001.53987-100000>