Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 15 Aug 2008 16:20:31 GMT
From:      Konrad Jankowski <konrad@FreeBSD.org>
To:        Perforce Change Reviews <perforce@FreeBSD.org>
Subject:   PERFORCE change 147466 for review
Message-ID:  <200808151620.m7FGKVol010222@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
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 <string.h>
 #include <locale.h>
 #include <stdlib.h>
+#include <sysexits.h>
 #include <unistd.h>
 
 #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;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200808151620.m7FGKVol010222>