rpm2cpio: handle bz2 too; code shrink
[oweals/busybox.git] / coreutils / sort.c
index dad542964680d175fe5b20d37ae3b6b3e365275e..fad6d124465b27b8b6e8770a80794f5261a0a8e7 100644 (file)
  * http://www.opengroup.org/onlinepubs/007904975/utilities/sort.html
  */
 
-#include "busybox.h"
+#include "libbb.h"
+
+/* This is a NOEXEC applet. Be very careful! */
+
 
 /*
        sort [-m][-o output][-bdfinru][-t char][-k keydef]... [file...]
@@ -20,7 +23,7 @@
 */
 
 /* These are sort types */
-static const char OPT_STR[] = "ngMucszbrdfimS:T:o:k:t:";
+static const char OPT_STR[] ALIGN1 = "ngMucszbrdfimS:T:o:k:t:";
 enum {
        FLAG_n  = 1,            /* Numeric sort */
        FLAG_g  = 2,            /* Sort using strtod() */
@@ -29,7 +32,7 @@ enum {
        FLAG_u  = 8,            /* Unique */
        FLAG_c  = 0x10,         /* Check: no output, exit(!ordered) */
        FLAG_s  = 0x20,         /* Stable sort, no ascii fallback at end */
-       FLAG_z  = 0x40,         /* Input is null terminated, not \n */
+       FLAG_z  = 0x40,         /* Input and output is NUL terminated, not \n */
 /* These can be applied to search keys, the previous four can't */
        FLAG_b  = 0x80,         /* Ignore leading blanks */
        FLAG_r  = 0x100,        /* Reverse */
@@ -56,7 +59,8 @@ static struct sort_key {
 
 static char *get_key(char *str, struct sort_key *key, int flags)
 {
-       int start = 0, end = 0, len, i, j;
+       int start = 0, end = 0, len, j;
+       unsigned i;
 
        /* Special case whole string, so we don't have to make a copy */
        if (key->range[0] == 1 && !key->range[1] && !key->range[2] && !key->range[3]
@@ -147,9 +151,9 @@ static struct sort_key *add_key(void)
 #define GET_LINE(fp) \
        ((option_mask32 & FLAG_z) \
        ? bb_get_chunk_from_file(fp, NULL) \
-       : xmalloc_getline(fp))
+       : xmalloc_fgetline(fp))
 #else
-#define GET_LINE(fp) xmalloc_getline(fp)
+#define GET_LINE(fp) xmalloc_fgetline(fp)
 #endif
 
 /* Iterate through keys list and perform comparisons */
@@ -271,8 +275,8 @@ static unsigned str2u(char **str)
 }
 #endif
 
-int sort_main(int argc, char **argv);
-int sort_main(int argc, char **argv)
+int sort_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int sort_main(int argc UNUSED_PARAM, char **argv)
 {
        FILE *fp, *outfile = stdout;
        char *line, **lines = NULL;
@@ -285,11 +289,11 @@ int sort_main(int argc, char **argv)
 
        /* Parse command line options */
        /* -o and -t can be given at most once */
-       opt_complementary = "?:o--o:t--t:" /* -t, -o: maximum one of each */
+       opt_complementary = "o--o:t--t:" /* -t, -o: maximum one of each */
                        "k::"; /* -k takes list */
-       getopt32(argc, argv, OPT_STR, &str_ignored, &str_ignored, &str_o, &lst_k, &str_t);
+       getopt32(argv, OPT_STR, &str_ignored, &str_ignored, &str_o, &lst_k, &str_t);
 #if ENABLE_FEATURE_SORT_BIG
-       if (option_mask32 & FLAG_o) outfile = xfopen(str_o, "w");
+       if (option_mask32 & FLAG_o) outfile = xfopen_for_write(str_o);
        if (option_mask32 & FLAG_t) {
                if (!str_t[0] || str_t[1])
                        bb_error_msg_and_die("bad -t parameter");
@@ -310,7 +314,7 @@ int sort_main(int argc, char **argv)
                        0
                };
                struct sort_key *key = add_key();
-               char *str_k = lst_k->data;
+               char *str_k = llist_pop(&lst_k);
                const char *temp2;
 
                i = 0; /* i==0 before comma, 1 after (-k3,6) */
@@ -340,27 +344,28 @@ int sort_main(int argc, char **argv)
                                str_k++;
                        }
                }
-               /* leaking lst_k... */
-               lst_k = lst_k->link;
        }
 #endif
        /* global b strips leading and trailing spaces */
        if (option_mask32 & FLAG_b) option_mask32 |= FLAG_bb;
 
        /* Open input files and read data */
-       for (i = argv[optind] ? optind : optind-1; argv[i]; i++) {
-               fp = stdin;
-               if (i >= optind && NOT_LONE_DASH(argv[i]))
-                       fp = xfopen(argv[i], "r");
+       argv += optind;
+       if (!*argv)
+               *--argv = (char*)"-";
+       do {
+               /* coreutils 6.9 compat: abort on first open error,
+                * do not continue to next file: */
+               fp = xfopen_stdin(*argv);
                for (;;) {
                        line = GET_LINE(fp);
                        if (!line) break;
-                       if (!(linecount & 63))
-                               lines = xrealloc(lines, sizeof(char *) * (linecount + 64));
+                       lines = xrealloc_vector(lines, 6, linecount);
                        lines[linecount++] = line;
                }
-               fclose(fp);
-       }
+               fclose_if_not_stdin(fp);
+       } while (*++argv);
+
 #if ENABLE_FEATURE_SORT_BIG
        /* if no key, perform alphabetic sort */
        if (!key_list)
@@ -371,9 +376,9 @@ int sort_main(int argc, char **argv)
                for (i = 1; i < linecount; i++)
                        if (compare_keys(&lines[i-1], &lines[i]) > j) {
                                fprintf(stderr, "Check line %d\n", i);
-                               return 1;
+                               return EXIT_FAILURE;
                        }
-               return 0;
+               return EXIT_SUCCESS;
        }
 #endif
        /* Perform the actual sort */
@@ -393,8 +398,9 @@ int sort_main(int argc, char **argv)
                if (linecount) linecount = flag+1;
        }
        /* Print it */
+       flag = (option_mask32 & FLAG_z) ? '\0' : '\n';
        for (i = 0; i < linecount; i++)
-               fprintf(outfile, "%s\n", lines[i]);
+               fprintf(outfile, "%s%c", lines[i], flag);
 
        fflush_stdout_and_exit(EXIT_SUCCESS);
 }