Fixed segfault with 'cut -f 1 -d:' and added 'cut -s' suport.
[oweals/busybox.git] / sort.c
diff --git a/sort.c b/sort.c
index 4301f4303db6e63d06db40389cbc274e23e9a3f0..93062faa4050f86a16134531ea92c1f7484b9804 100644 (file)
--- a/sort.c
+++ b/sort.c
@@ -33,7 +33,11 @@ static const char sort_usage[] = "sort [-n]"
 #ifdef BB_FEATURE_SORT_REVERSE
 " [-r]"
 #endif
-" [FILE]...\n\nSorts lines of text in the specified files\n";
+" [FILE]...\n"
+#ifndef BB_FEATURE_TRIVIAL_HELP
+"\nSorts lines of text in the specified files\n"
+#endif
+;
 
 #ifdef BB_FEATURE_SORT_REVERSE
 #define APPLY_REVERSE(x) (reverse ? -(x) : (x))
@@ -54,7 +58,6 @@ typedef struct Line {
 typedef struct {
        int len;                                        /* number of Lines */
        Line **sorted;                          /* array fed to qsort */
-
        Line *head;                                     /* head of List */
        Line *current;                          /* current Line */
 } List;
@@ -71,36 +74,23 @@ static const int max = 1024;
 static Line *line_alloc()
 {
        Line *self;
-
        self = malloc(1 * sizeof(Line));
        return self;
 }
 
-/* Initialize Line with string */
-static Line *line_init(Line * self, const char *string)
-{
-       self->data = malloc((strlen(string) + 1) * sizeof(char));
-
-       if (self->data == NULL) {
-               return NULL;
-       }
-       strcpy(self->data, string);
-       self->next = NULL;
-       return self;
-}
-
 /* Construct Line from FILE* */
 static Line *line_newFromFile(FILE * src)
 {
-       char buffer[max];
        Line *self;
+       char *cstring = NULL;
 
-       if (fgets(buffer, max, src)) {
+       if ((cstring = cstring_lineFromFile(src))) {
                self = line_alloc();
                if (self == NULL) {
                        return NULL;
                }
-               line_init(self, buffer);
+               self->data = cstring;
+               self->next = NULL;
                return self;
        }
        return NULL;
@@ -177,7 +167,7 @@ static List *list_insert(List * self, Line * line)
                self->head = line;
                self->current = line;
 
-               /* all subsequent insertions */
+       /* all subsequent insertions */
        } else {
                self->current->next = line;
                self->current = line;
@@ -241,12 +231,6 @@ static void list_release(List * self)
 }
 
 
-/*
- * 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)
 {
@@ -317,7 +301,7 @@ int sort_main(int argc, char **argv)
                list_release(&list);
        }
 
-       exit(0);
+       return(0);
 }
 
-/* $Id: sort.c,v 1.14 2000/04/15 16:34:54 erik Exp $ */
+/* $Id: sort.c,v 1.17 2000/06/19 17:25:40 andersen Exp $ */