From owner-freebsd-standards Thu Jan 30 5: 6:39 2003 Delivered-To: freebsd-standards@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id E481137B401 for ; Thu, 30 Jan 2003 05:06:37 -0800 (PST) Received: from falcon.midgard.homeip.net (h76n3fls20o913.telia.com [213.67.148.76]) by mx1.FreeBSD.org (Postfix) with SMTP id 2054F43F79 for ; Thu, 30 Jan 2003 05:06:34 -0800 (PST) (envelope-from ertr1013@student.uu.se) Received: (qmail 77988 invoked by uid 1001); 30 Jan 2003 13:06:32 -0000 Date: Thu, 30 Jan 2003 14:06:32 +0100 From: Erik Trulsson To: Adrian Penisoara Cc: standards@freebsd.org Subject: Re: strftime(): FreeBSD vs. Linux Message-ID: <20030130130631.GA69654@falcon.midgard.homeip.net> Mail-Followup-To: Adrian Penisoara , standards@freebsd.org References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.3i Sender: owner-freebsd-standards@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG 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. -- Erik Trulsson ertr1013@student.uu.se To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-standards" in the body of the message