From owner-p4-projects@FreeBSD.ORG Fri Aug 15 16:20:32 2008 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 0A13D1065674; Fri, 15 Aug 2008 16:20:32 +0000 (UTC) Delivered-To: perforce@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id C28471065673 for ; Fri, 15 Aug 2008 16:20:31 +0000 (UTC) (envelope-from konrad@FreeBSD.org) Received: from repoman.freebsd.org (repoman.freebsd.org [IPv6:2001:4f8:fff6::29]) by mx1.freebsd.org (Postfix) with ESMTP id A17538FC19 for ; Fri, 15 Aug 2008 16:20:31 +0000 (UTC) (envelope-from konrad@FreeBSD.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.14.2/8.14.2) with ESMTP id m7FGKVJa010224 for ; Fri, 15 Aug 2008 16:20:31 GMT (envelope-from konrad@FreeBSD.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.14.2/8.14.1/Submit) id m7FGKVol010222 for perforce@freebsd.org; Fri, 15 Aug 2008 16:20:31 GMT (envelope-from konrad@FreeBSD.org) Date: Fri, 15 Aug 2008 16:20:31 GMT Message-Id: <200808151620.m7FGKVol010222@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to konrad@FreeBSD.org using -f From: Konrad Jankowski To: Perforce Change Reviews Cc: Subject: PERFORCE change 147466 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 15 Aug 2008 16:20:32 -0000 http://perforce.freebsd.org/chv.cgi?CH=147466 Change 147466 by konrad@vspredator on 2008/08/15 16:20:14 Fix things identified by Diomidis. Affected files ... .. //depot/projects/soc2008/konrad_collation/test/sort/sort.c#2 edit Differences ... ==== //depot/projects/soc2008/konrad_collation/test/sort/sort.c#2 (text+ko) ==== @@ -3,19 +3,19 @@ #include #include #include +#include #include #define MAXSIZE 10000 -struct line { +static struct line { char *sline; char *trans; } *lines[MAXSIZE]; -int max_lines; /* Numer of lines read from input. */ -int xfrm; /* Do we use strxfrm? */ +static int max_lines; /* Numer of lines read from input. */ -void +static void read_input(void) { char buf[1000]; @@ -24,10 +24,13 @@ while (fgets(buf, sizeof(buf), stdin) != NULL) { buf[strlen(buf) - 1] = 0; - line = malloc(sizeof(struct line)); - line->sline = strdup(buf); + if ((line = malloc(sizeof(struct line))) == NULL) + err(1, "malloc"); + if ((line->sline = strdup(buf)) == NULL) + err(1, "strdup"); len = strxfrm(NULL, buf, 0); - line->trans = malloc(len + 1); + if ((line->trans = malloc(len + 1)) == NULL) + err(1, "malloc"); strxfrm(line->trans, buf, len); lines[i] = line; i++; @@ -35,18 +38,24 @@ max_lines = i; } -int -sort_fun(const void *a, const void *b) +static int +strcmp_compare(const void *a, const void *b) { struct line **l1 = (void *)a, **l2 = (void *)b; - if (xfrm) - return strcmp((*l1)->trans, (*l2)->trans); + return strcmp((*l1)->trans, (*l2)->trans); + +} + +static int +strcoll_compare(const void *a, const void *b) +{ + struct line **l1 = (void *)a, **l2 = (void *)b; return strcoll((*l1)->sline, (*l2)->sline); } -void +static void write_output(void) { int i; @@ -61,20 +70,32 @@ { char *p; int ch; + int xfrm; /* Do we use strxfrm? */ while ((ch = getopt(argc, argv, "x")) != -1) { switch (ch) { case 'x': - fprintf(stderr, "strxfrm mode enabled!\n"); xfrm = 1; - break; + break; + case '?': default: + printf( "usage: " + "%s [-x]\n" + "\tsort lines in standard input according to the " + "current collation\n" + "\t-x use strxfrm and strcmp instead of strcoll\n" + , argv[0]); + return EX_USAGE; + break; } } if ((p = setlocale(LC_ALL, "")) == NULL) errx(1, "setlocale"); read_input(); fprintf(stderr, "setlocale: %s\n", p); - qsort(lines, max_lines, sizeof(struct line *), sort_fun); + if (xfrm) + qsort(lines, max_lines, sizeof(struct line *), strcmp_compare); + else + qsort(lines, max_lines, sizeof(struct line *), strcoll_compare); write_output(); return 0;