From owner-freebsd-hackers@FreeBSD.ORG Tue Feb 14 21:25:16 2006 Return-Path: X-Original-To: hackers@freebsd.org 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 09F0D16A420 for ; Tue, 14 Feb 2006 21:25:16 +0000 (GMT) (envelope-from q@galgenberg.net) Received: from wrzx28.rz.uni-wuerzburg.de (wrzx28.rz.uni-wuerzburg.de [132.187.3.28]) by mx1.FreeBSD.org (Postfix) with ESMTP id 61C4B43D60 for ; Tue, 14 Feb 2006 21:25:07 +0000 (GMT) (envelope-from q@galgenberg.net) Received: from virusscan.mail (amavis2.rz.uni-wuerzburg.de [132.187.3.47]) by wrzx28.rz.uni-wuerzburg.de (Postfix) with ESMTP id 3A045146F1D for ; Tue, 14 Feb 2006 22:25:06 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by virusscan.mail (Postfix) with ESMTP id 2C4DD6BA4 for ; Tue, 14 Feb 2006 22:25:06 +0100 (CET) Received: from frodo.galgenberg.net (wwsx14.win-screen.uni-wuerzburg.de [132.187.253.14]) by wrzx28.rz.uni-wuerzburg.de (Postfix) with ESMTP id 05457146F1D for ; Tue, 14 Feb 2006 22:25:06 +0100 (CET) Received: from coyote.q.local (gb-21-237.galgenberg.net [172.16.21.237]) by frodo.galgenberg.net (8.13.1/8.13.1) with ESMTP id k1ELP5xF052873 for ; Tue, 14 Feb 2006 22:25:05 +0100 (CET) (envelope-from q@galgenberg.net) Received: from roadrunner.q.local (roadrunner.q.local [192.168.0.148]) by coyote.q.local (8.13.4/8.13.4) with ESMTP id k1ELP4pa013245 for ; Tue, 14 Feb 2006 22:25:05 +0100 (CET) (envelope-from q@galgenberg.net) Received: from roadrunner.q.local (localhost [127.0.0.1]) by roadrunner.q.local (8.13.4/8.13.4) with ESMTP id k1ELP4mG012543 for ; Tue, 14 Feb 2006 22:25:04 +0100 (CET) (envelope-from q@galgenberg.net) Received: (from q@localhost) by roadrunner.q.local (8.13.4/8.13.4/Submit) id k1ELP4o4012542 for hackers@freebsd.org; Tue, 14 Feb 2006 22:25:04 +0100 (CET) (envelope-from q@galgenberg.net) Date: Tue, 14 Feb 2006 22:25:03 +0100 From: Ulrich Spoerlein To: hackers@freebsd.org Message-ID: <20060214212503.GE1107@galgenberg.net> Mail-Followup-To: hackers@freebsd.org MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="EDJsL2R9iCFAt7IV" Content-Disposition: inline X-Virus-Scanned: by amavisd-new at uni-wuerzburg.de Cc: Subject: Naive implementation of strverscmp(3) X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 14 Feb 2006 21:25:16 -0000 --EDJsL2R9iCFAt7IV Content-Type: multipart/mixed; boundary="kbCYTQG2MZjuOjyn" Content-Disposition: inline --kbCYTQG2MZjuOjyn Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hello Hackers, this is probably not the right list, but I'd like to collect reviews of=20 a strverscmp(3) function I wrote. It is used by the graphics/gqview port=20 (if present in libc) and since I want/need that functionality I whipped=20 up a somewhat working version. It tries to sort strings like "jan1", "jan2", "jan10", etc. into the=20 "natural" order. Is there a chance this might get included into libc? Or is it considered=20 bloat? The GNU version can be found here http://refspecs.freestandards.org/LSB_2.0.1/LSB-generic/LSB-generic/baselib= -strverscmp.html Quite frankly, I don't understand their integral/fraction distinction,=20 and my version differs in that regard. See the return values of the=20 attached sample code. Ulrich Spoerlein --=20 PGP Key ID: 20FEE9DD Encrypted mail welcome! Fingerprint: AEC9 AF5E 01AC 4EE1 8F70 6CBD E76E 2227 20FE E9DD Which is worse: ignorance or apathy? Don't know. Don't care. --kbCYTQG2MZjuOjyn Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="strverscmp.c" Content-Transfer-Encoding: quoted-printable #include #include #include #include #ifdef __FreeBSD__ int strverscmp(const char *s1, const char *s2); int strverscmp(const char *s1, const char *s2) { static const char *digits =3D "0123456789"; int ret; long n1, n2; size_t p1, p2; do { p1 =3D strcspn(s1, digits); p2 =3D strcspn(s2, digits); =20 /* Different prefix */ if ((ret =3D strncmp(s1, s2, p1)) !=3D 0) return ret; s1 +=3D p1; s2 +=3D p2; n1 =3D strtol(s1, NULL, 10); n2 =3D strtol(s2, NULL, 10); =20 if (n1 < n2) return -1; else if (n1 > n2) return 1; /* Numbers are equal or not present, try with next ones. */ p1 =3D strspn(s1, digits); p2 =3D strspn(s2, digits); s1 +=3D p1; s2 +=3D p2; } while (p1 =3D=3D p2 && p1 !=3D 0 && p1 !=3D 0); =20 return strcmp(s1, s2); } #endif int main(int argc, char **argv) { char **array, *temp; int i, j, n =3D 10; array =3D (char **) calloc(n, sizeof(char *)); array[0] =3D strdup("jan2"); array[1] =3D strdup("jan10"); array[2] =3D strdup("jan"); array[3] =3D strdup("jan0a"); array[4] =3D strdup("jan17b"); array[5] =3D strdup("jan17a"); array[6] =3D strdup("jan17x1234"); array[7] =3D strdup("jan17x123"); array[8] =3D strdup("jan17x123a"); array[9] =3D strdup("jan9"); /* Bubble sort */ for (i=3D0; i 0) { temp =3D array[j]; array[j] =3D array[i]; array[i] =3D temp; } } } for (i=3D0; i