#ifdef BB_FEATURE_SORT_REVERSE
int reverse = FALSE;
#endif
+#ifdef BB_FEATURE_SORT_UNIQUE
+ int unique = FALSE;
+#endif
- while ((opt = getopt(argc, argv, "nr")) != -1) {
+ while ((opt = getopt(argc, argv, "nru")) != -1) {
switch (opt) {
case 'n':
compare = compare_numeric;
case 'r':
reverse = TRUE;
break;
+#endif
+#ifdef BB_FEATURE_SORT_UNIQUE
+ case 'u':
+ unique = TRUE;
+ break;
#endif
default:
show_usage();
while ((line = get_line_from_file(fp)) != NULL) {
lines = xrealloc(lines, sizeof(char *) * (nlines + 1));
- line[strlen(line) - 1] = '\0';
+ chomp(line);
lines[nlines++] = line;
}
}
/* print it */
#ifdef BB_FEATURE_SORT_REVERSE
- if (reverse)
- for (i = nlines - 1; 0 <= i; i--)
- puts(lines[i]);
- else
+ if (reverse) {
+ for (i = --nlines; 0 <= i; i--)
+#ifdef BB_FEATURE_SORT_UNIQUE
+ if((!unique) || (i == nlines) || (strcmp(lines[i + 1], lines[i])))
+#endif
+ puts(lines[i]);
+ } else
+#endif
+ for (i = 0; i < nlines; i++)
+#ifdef BB_FEATURE_SORT_UNIQUE
+ if((!unique) || (!i) || (strcmp(lines[i - 1], lines[i])))
#endif
- for (i = 0; i < nlines; i++)
- puts(lines[i]);
+ puts(lines[i]);
return EXIT_SUCCESS;
}