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

next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=147472

Change 147472 by konrad@vspredator on 2008/08/15 16:44:48

	Get rid of the global variables.

Affected files ...

.. //depot/projects/soc2008/konrad_collation/test/sort/sort.c#3 edit

Differences ...

==== //depot/projects/soc2008/konrad_collation/test/sort/sort.c#3 (text+ko) ====

@@ -13,9 +13,7 @@
 	char *trans;
 } *lines[MAXSIZE];
 
-static int max_lines;	/* Numer of lines read from input. */
-
-static void
+static int
 read_input(void)
 {
 	char buf[1000];
@@ -35,7 +33,7 @@
 		lines[i] = line;
 		i++;
 	}
-	max_lines = i;
+	return i;
 }
 
 static int
@@ -56,11 +54,11 @@
 }
 
 static void
-write_output(void)
+write_output(int num_lines)
 {
 	int i;
 
-	for (i = 0; i < max_lines; i++) {
+	for (i = 0; i < num_lines; i++) {
 		printf("%s\n", lines[i]->sline);
 	}
 }
@@ -70,19 +68,22 @@
 {
 	char *p;
 	int ch;
+	int num_lines;	/* Number of lines read from input. */
 	int xfrm;	/* Do we use strxfrm? */
 
-	while ((ch = getopt(argc, argv, "x")) != -1) {
+	while ((ch = getopt(argc, argv, "xh")) != -1) {
 		switch (ch) {
 		case 'x':
 			xfrm = 1;
 			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"
+				"%s [-h] [-x]\n"
+				"\tsort lines in standard input according "
+				"to the current collation\n"
+				"\t-h this help message\n"
+				"\t-x use strxfrm and strcmp instead of "
+				"strcoll\n"
 				, argv[0]);
 			return EX_USAGE;
 			break;
@@ -90,13 +91,13 @@
 	}
 	if ((p = setlocale(LC_ALL, "")) == NULL)
 		errx(1, "setlocale");
-	read_input();
+	num_lines = read_input();
 	fprintf(stderr, "setlocale: %s\n", p);
 	if (xfrm)
-		qsort(lines, max_lines, sizeof(struct line *), strcmp_compare);
+		qsort(lines, num_lines, sizeof(struct line *), strcmp_compare);
 	else
-		qsort(lines, max_lines, sizeof(struct line *), strcoll_compare);
-	write_output();
+		qsort(lines, num_lines, sizeof(struct line *), strcoll_compare);
+	write_output(num_lines);
 
 	return 0;
 }



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