From owner-freebsd-hackers@FreeBSD.ORG Sat Oct 16 17:23:15 2004 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 7E7AD16A4CE for ; Sat, 16 Oct 2004 17:23:15 +0000 (GMT) Received: from mail.freebsd.org.cn (dns3.freebsd.org.cn [61.129.66.75]) by mx1.FreeBSD.org (Postfix) with SMTP id A077D43D2F for ; Sat, 16 Oct 2004 17:23:13 +0000 (GMT) (envelope-from delphij@frontfree.net) Received: (qmail 59002 invoked by uid 0); 16 Oct 2004 17:18:31 -0000 Received: from unknown (HELO beastie.frontfree.net) (219.239.98.7) by mail.freebsd.org.cn with SMTP; 16 Oct 2004 17:18:31 -0000 Received: from localhost (localhost.frontfree.net [127.0.0.1]) by beastie.frontfree.net (Postfix) with ESMTP id 204D3130E92 for ; Sun, 17 Oct 2004 01:23:08 +0800 (CST) Received: from beastie.frontfree.net ([127.0.0.1]) by localhost (beastie.frontfree.net [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 01797-02 for ; Sun, 17 Oct 2004 01:23:04 +0800 (CST) Received: by beastie.frontfree.net (Postfix, from userid 1001) id 55A4D130E1D; Sun, 17 Oct 2004 01:23:02 +0800 (CST) Date: Sun, 17 Oct 2004 01:23:02 +0800 From: Xin LI To: freebsd-hackers@FreeBSD.org Message-ID: <20041016172302.GA2764@frontfree.net> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="7AUc2qLy4jB3hD7Z" Content-Disposition: inline User-Agent: Mutt/1.4.2.1i X-GPG-key-ID/Fingerprint: 0xCAEEB8C0 / 43B8 B703 B8DD 0231 B333 DC28 39FB 93A0 CAEE B8C0 X-GPG-Public-Key: http://www.delphij.net/delphij.asc X-Operating-System: FreeBSD beastie.frontfree.net 5.3-delphij FreeBSD 5.3-delphij #4: Mon Sep 13 12:44:05 CST 2004 delphij@beastie.frontfree.net:/usr/obj/usr/src/sys/BEASTIE i386 X-URL: http://www.delphij.net X-By: delphij@beastie.frontfree.net X-Location: Beijing, China X-Virus-Scanned: by amavisd-new at frontfree.net Subject: [PATCH] add '-' glibc extension to strftime(3) X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 16 Oct 2004 17:23:15 -0000 --7AUc2qLy4jB3hD7Z Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hi, folks, It turns out that the GNU extension '-' in their strftime(3) implementation is somewhat popular in several applications. The patch in the last part of this e-mail will add a simulate implementation for it. My question is: (1) Am I doing things cleanly and correctly? I have attempted to keep the code style consistent with the old one and style(9) but maybe I have missed something else, or did not do it sufficently? (2) Is the way of implementing it clean enough? Thanks for any comments! Index: strftime.3 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /home/fcvs/src/lib/libc/stdtime/strftime.3,v retrieving revision 1.34 diff -u -r1.34 strftime.3 --- strftime.3 2 Jul 2004 23:52:12 -0000 1.34 +++ strftime.3 16 Oct 2004 17:13:08 -0000 @@ -36,7 +36,7 @@ .\" @(#)strftime.3 8.1 (Berkeley) 6/4/93 .\" $FreeBSD: src/lib/libc/stdtime/strftime.3,v 1.34 2004/07/02 23:52:12 r= u Exp $ .\" -.Dd January 4, 2003 +.Dd October 17, 2004 .Dt STRFTIME 3 .Os .Sh NAME @@ -216,6 +216,8 @@ is replaced by national representation of the date and time (the format is similar to that produced by .Xr date 1 ) . +.It Cm %-* +GLIBC extensions. Do not do padding when making output. .It Cm %% is replaced by .Ql % . Index: strftime.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /home/fcvs/src/lib/libc/stdtime/strftime.c,v retrieving revision 1.40 diff -u -r1.40 strftime.c --- strftime.c 14 Jun 2004 10:31:52 -0000 1.40 +++ strftime.c 16 Oct 2004 17:14:24 -0000 @@ -59,6 +59,13 @@ #define IN_THIS 2 #define IN_ALL 3 =20 +#define PAD_DEFAULT 0 +#define PAD_LESS 1 +#if 0 /* XXX NOT IMPLEMENTED YET */ +#define PAD_SPACE 2 +#define PAD_ZERO 3 +#endif + size_t strftime(char * __restrict s, size_t maxsize, const char * __restrict form= at, const struct tm * __restrict t) @@ -99,13 +106,14 @@ const char * const ptlim; int * warnp; { - int Ealternative, Oalternative; + int Ealternative, Oalternative, Palternative; struct lc_time_T *tptr =3D __get_current_time_locale(); =20 for ( ; *format; ++format) { if (*format =3D=3D '%') { Ealternative =3D 0; Oalternative =3D 0; + Palternative =3D PAD_DEFAULT; label: switch (*++format) { case '\0': @@ -188,21 +196,27 @@ Oalternative++; goto label; case 'e': - pt =3D _conv(t->tm_mday, "%2d", pt, ptlim); + pt =3D _conv(t->tm_mday, (Palternative =3D=3D PAD_LESS) ? + "%d" : "%2d", + pt, ptlim); continue; case 'F': pt =3D _fmt("%Y-%m-%d", t, pt, ptlim, warnp); continue; case 'H': - pt =3D _conv(t->tm_hour, "%02d", pt, ptlim); + pt =3D _conv(t->tm_hour, (Palternative =3D=3D PAD_LESS) ? + "%d" : "%02d", + pt, ptlim); continue; case 'I': pt =3D _conv((t->tm_hour % 12) ? - (t->tm_hour % 12) : 12, - "%02d", pt, ptlim); + (t->tm_hour % 12) : 12, (Palternative =3D=3D PAD_LESS) ? + "%d" : "%02d", + pt, ptlim); continue; case 'j': - pt =3D _conv(t->tm_yday + 1, "%03d", pt, ptlim); + pt =3D _conv(t->tm_yday + 1, (Palternative =3D=3D PAD_LESS) ? + "%d" : "%03d", pt, ptlim); continue; case 'k': /* @@ -215,7 +229,9 @@ ** "%l" have been swapped. ** (ado, 1993-05-24) */ - pt =3D _conv(t->tm_hour, "%2d", pt, ptlim); + pt =3D _conv(t->tm_hour, (Palternative =3D=3D PAD_LESS) ? + "%d": "%2d", + pt, ptlim); continue; #ifdef KITCHEN_SINK case 'K': @@ -236,14 +252,19 @@ ** (ado, 1993-05-24) */ pt =3D _conv((t->tm_hour % 12) ? - (t->tm_hour % 12) : 12, - "%2d", pt, ptlim); + (t->tm_hour % 12) : 12, (Palternative =3D=3D PAD_LESS) ? + "%d" : "%2d", + pt, ptlim); continue; case 'M': - pt =3D _conv(t->tm_min, "%02d", pt, ptlim); + pt =3D _conv(t->tm_min, (Palternative =3D=3D PAD_LESS) ? + "%d" : "%02d", + pt, ptlim); continue; case 'm': - pt =3D _conv(t->tm_mon + 1, "%02d", pt, ptlim); + pt =3D _conv(t->tm_mon + 1, (Palternative =3D=3D PAD_LESS) ? + "%d" : "%02d", + pt, ptlim); continue; case 'n': pt =3D _add("\n", pt, ptlim); @@ -262,7 +283,9 @@ warnp); continue; case 'S': - pt =3D _conv(t->tm_sec, "%02d", pt, ptlim); + pt =3D _conv(t->tm_sec, (Palternative =3D=3D PAD_LESS) ? + "%d" : "%02d", + pt, ptlim); continue; case 's': { @@ -289,8 +312,8 @@ continue; case 'U': pt =3D _conv((t->tm_yday + DAYSPERWEEK - - t->tm_wday) / DAYSPERWEEK, - "%02d", pt, ptlim); + t->tm_wday) / DAYSPERWEEK, (Palternative =3D=3D PAD_LESS) ? + "%d" : "%02d", pt, ptlim); continue; case 'u': /* @@ -423,11 +446,13 @@ continue; case 'y': *warnp =3D IN_ALL; - pt =3D _conv((t->tm_year + TM_YEAR_BASE) % 100, - "%02d", pt, ptlim); + pt =3D _conv((t->tm_year + TM_YEAR_BASE) % 100, (Palternative =3D=3D P= AD_LESS) ? + "%d" : "%02d", + pt, ptlim); continue; case 'Y': - pt =3D _conv(t->tm_year + TM_YEAR_BASE, "%04d", + pt =3D _conv(t->tm_year + TM_YEAR_BASE, (Palternative =3D=3D PAD_LESS)= ? + "%d" : "%04d", pt, ptlim); continue; case 'Z': @@ -501,6 +526,23 @@ pt =3D _fmt(tptr->date_fmt, t, pt, ptlim, warnp); continue; + case '-': + if (Palternative !=3D PAD_DEFAULT) + break; + Palternative =3D PAD_LESS; + goto label; +#if 0 /* XXX NOT IMPLEMENTED YET */ + case '_': + if (Palternative !=3D PAD_DEFAULT) + break; + Palternative =3D PAD_SPACE; + goto label; + case '0': + if (Palternative !=3D PAD_DEFAULT) + break; + Palternative =3D PAD_ZERO; + goto label; +#endif case '%': /* ** X311J/88-090 (4.12.3.5): if conversion char is --=20 Xin LI http://www.delphij.net/ See complete headers for GPG key and other information. --7AUc2qLy4jB3hD7Z Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.6 (FreeBSD) iD8DBQFBcVj2/cVsHxFZiIoRAi+pAKCGPA4g2L71AyR1kHfNBOo4KyaBPgCdFu4K QEphXtBLZogNaLH6fGL3l0g= =nI+R -----END PGP SIGNATURE----- --7AUc2qLy4jB3hD7Z--