+/* vi: set sw=4 ts=4: */
/*
- * Mini find implementation for busybox
+ * Mini sort implementation for busybox
*
- *
- * Copyright (C) 1999 by Lineo, inc.
- * Written by John Beppu <beppu@lineo.com>
+ * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
*
*/
-#include "internal.h"
-#include <sys/types.h>
-#include <fcntl.h>
-#include <dirent.h>
-#include <stdio.h>
-#include <errno.h>
-
-static const char sort_usage[] =
-"Usage: sort [OPTION]... [FILE]...\n\n"
-;
-
+/* BB_AUDIT SUSv3 _NOT_ compliant -- a number of options are not supported. */
+/* http://www.opengroup.org/onlinepubs/007904975/utilities/sort.html */
-/* line node */
-typedef struct {
- char *data; /* line data */
- struct Line *next; /* pointer to next line node */
-} Line;
-
-/* singly-linked list of lines */
-typedef struct {
- int len; /* number of Lines */
- Line *line; /* array fed to qsort */
- Line *head; /* head of List */
-} List;
-
-
-/* Line methods */
+/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
+ *
+ * Now does proper error checking on i/o. Plus some space savings.
+ */
-static const int max = 1024;
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "busybox.h"
+#include "libcoreutils/coreutils.h"
-static Line *
-line_new()
+static int compare_ascii(const void *x, const void *y)
{
- char buffer[max];
+ return strcmp(*(char **)x, *(char **)y);
}
+static int compare_numeric(const void *x, const void *y)
+{
+ int z = atoi(*(char **)x) - atoi(*(char **)y);
+ return z ? z : strcmp(*(char **)x, *(char **)y);
+}
-/* Comparison */
-
-static int
-compare_ascii(const void *, const void *);
-
-static int
-compare_numeric(const void *, const void *);
-
-
-/* List */
-
-static void
-list_insert();
-
-static void
-list_sort();
-
-static void
-list_print();
-
-
-
-/*
- * I need a list
- * to insert lines into
- * then I need to sort this list
- * and finally print it
- */
-
-int
-sort_main(int argc, char **argv)
+int sort_main(int argc, char **argv)
{
- int i;
- char opt;
+ FILE *fp;
+ char *line, **lines = NULL;
+ int i, nlines = 0, inc;
+ int (*compare)(const void *, const void *) = compare_ascii;
+
+ int flags;
- /* default behaviour */
+ bb_default_error_retval = 2;
- /* parse argv[] */
- for (i = 1; i < argc; i++) {
- if (argv[i][0] == '-') {
- opt = argv[i][1];
- switch (opt) {
- case 'h':
- usage(sort_usage);
- break;
- default:
- fprintf(stderr, "sort: invalid option -- %c\n", opt);
- usage(sort_usage);
- }
- } else {
- break;
+ flags = bb_getopt_ulflags(argc, argv, "nru");
+ if (flags & 1) {
+ compare = compare_numeric;
}
- }
- /* go through remaining args (if any) */
- if (i >= argc) {
+ argv += optind;
+ if (!*argv) {
+ *--argv = "-";
+ }
- } else {
- for ( ; i < argc; i++) {
+ do {
+ fp = xgetoptfile_sort_uniq(argv, "r");
+ while ((line = bb_get_chomped_line_from_file(fp)) != NULL) {
+ lines = xrealloc(lines, sizeof(char *) * (nlines + 1));
+ lines[nlines++] = line;
+ }
+ bb_xferror(fp, *argv);
+ bb_fclose_nonstdin(fp);
+ } while (*++argv);
+
+ /* sort it */
+ qsort(lines, nlines, sizeof(char *), compare);
+
+ /* print it */
+ i = 0;
+ --nlines;
+ if ((inc = 1 - (flags & 2)) < 0) { /* reverse */
+ i = nlines;
+ }
+ flags &= 4;
+
+ while (nlines >= 0) {
+ if (!flags || !nlines || strcmp(lines[i+inc], lines[i])) {
+ puts(lines[i]);
+ }
+ i += inc;
+ --nlines;
}
- }
- exit(0);
+ bb_fflush_stdout_and_exit(EXIT_SUCCESS);
}
-
-/* $Date: 1999/12/21 20:00:35 $ */
-/* $Id: sort.c,v 1.1 1999/12/21 20:00:35 beppu Exp $ */