aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Beppu <beppu@lbox.org>1999-12-22 00:30:29 +0000
committerJohn Beppu <beppu@lbox.org>1999-12-22 00:30:29 +0000
commit38efa7902e7d266fbc78e1d39780ca0866d98dad (patch)
tree885e15f1c38f7c0f62fa0b8720e904e84b9e737f
parentc0ca473af9a5afd17fd6dd916bb6008a036efb20 (diff)
downloadbusybox-w32-38efa7902e7d266fbc78e1d39780ca0866d98dad.tar.gz
busybox-w32-38efa7902e7d266fbc78e1d39780ca0866d98dad.tar.bz2
busybox-w32-38efa7902e7d266fbc78e1d39780ca0866d98dad.zip
work in progress...
-rw-r--r--coreutils/sort.c93
-rw-r--r--sort.c93
2 files changed, 164 insertions, 22 deletions
diff --git a/coreutils/sort.c b/coreutils/sort.c
index 4ab673b4d..d82351797 100644
--- a/coreutils/sort.c
+++ b/coreutils/sort.c
@@ -32,6 +32,7 @@ static const char sort_usage[] =
32"Usage: sort [OPTION]... [FILE]...\n\n" 32"Usage: sort [OPTION]... [FILE]...\n\n"
33; 33;
34 34
35/* structs ________________________________________________________________ */
35 36
36/* line node */ 37/* line node */
37typedef struct { 38typedef struct {
@@ -42,19 +43,51 @@ typedef struct {
42/* singly-linked list of lines */ 43/* singly-linked list of lines */
43typedef struct { 44typedef struct {
44 int len; /* number of Lines */ 45 int len; /* number of Lines */
45 Line *line; /* array fed to qsort */ 46 Line *sorted; /* array fed to qsort */
47
46 Line *head; /* head of List */ 48 Line *head; /* head of List */
49 Line *current /* current Line */
47} List; 50} List;
48 51
49 52
50/* Line methods */ 53/* methods ________________________________________________________________ */
51 54
52static const int max = 1024; 55static const int max = 1024;
53 56
57/* mallocate Line */
54static Line * 58static Line *
55line_new() 59line_alloc()
56{ 60{
57 char buffer[max]; 61 Line *self;
62 self = malloc(1 * sizeof(Line));
63 return self;
64}
65
66/* Initialize Line with string */
67static Line *
68line_init(Line *self, const char *string)
69{
70 self->data = malloc((strlen(string) + 1) * sizeof(char));
71 if (self->data == NULL) { return NULL; }
72 strcpy(self->data, string);
73 self->next = NULL;
74 return self;
75}
76
77/* Construct Line from FILE* */
78static Line *
79line_newFromFile(FILE *src)
80{
81 char buffer[max];
82 Line *self;
83
84 if (fgets(buffer, max, src)) {
85 self = line_alloc();
86 if (self == NULL) { return NULL; }
87 line_init(self, buffer);
88 return self;
89 }
90 return NULL;
58} 91}
59 92
60 93
@@ -69,15 +102,54 @@ compare_numeric(const void *, const void *);
69 102
70/* List */ 103/* List */
71 104
72static void 105/* */
73list_insert(); 106static List *
107list_init(List *self)
108{
109 self->len = 0;
110 self->sorted = NULL;
111 self->head = NULL;
112 self->current = NULL;
113 return self;
114}
74 115
116/* for simplicity, the List gains ownership of the line */
75static void 117static void
76list_sort(); 118list_insert(List *self, Line *line)
119{
120 if (line == NULL) { return NULL; }
77 121
78static void 122 /* first insertion */
79list_print(); 123 if (self->head == NULL) {
124 self->head = line;
125 self->current = line;
126
127 /* all subsequent insertions */
128 } else {
129 self->current->next = line;
130 self->current = line;
131 }
132 self->len++;
133 return self;
134}
80 135
136/* */
137static List *
138list_sort(List *self);
139
140/* precondition: list must be sorted */
141static List *
142list_writeToFile(List *self, FILE* dst)
143{
144 if (self->sorted == NULL) { return NULL; }
145}
146
147/* deallocate */
148static List *
149list_release(List *self)
150{
151 return self;
152}
81 153
82 154
83/* 155/*
@@ -123,5 +195,4 @@ sort_main(int argc, char **argv)
123 exit(0); 195 exit(0);
124} 196}
125 197
126/* $Date: 1999/12/21 20:00:35 $ */ 198/* $Id: sort.c,v 1.2 1999/12/22 00:30:29 beppu Exp $ */
127/* $Id: sort.c,v 1.1 1999/12/21 20:00:35 beppu Exp $ */
diff --git a/sort.c b/sort.c
index 4ab673b4d..d82351797 100644
--- a/sort.c
+++ b/sort.c
@@ -32,6 +32,7 @@ static const char sort_usage[] =
32"Usage: sort [OPTION]... [FILE]...\n\n" 32"Usage: sort [OPTION]... [FILE]...\n\n"
33; 33;
34 34
35/* structs ________________________________________________________________ */
35 36
36/* line node */ 37/* line node */
37typedef struct { 38typedef struct {
@@ -42,19 +43,51 @@ typedef struct {
42/* singly-linked list of lines */ 43/* singly-linked list of lines */
43typedef struct { 44typedef struct {
44 int len; /* number of Lines */ 45 int len; /* number of Lines */
45 Line *line; /* array fed to qsort */ 46 Line *sorted; /* array fed to qsort */
47
46 Line *head; /* head of List */ 48 Line *head; /* head of List */
49 Line *current /* current Line */
47} List; 50} List;
48 51
49 52
50/* Line methods */ 53/* methods ________________________________________________________________ */
51 54
52static const int max = 1024; 55static const int max = 1024;
53 56
57/* mallocate Line */
54static Line * 58static Line *
55line_new() 59line_alloc()
56{ 60{
57 char buffer[max]; 61 Line *self;
62 self = malloc(1 * sizeof(Line));
63 return self;
64}
65
66/* Initialize Line with string */
67static Line *
68line_init(Line *self, const char *string)
69{
70 self->data = malloc((strlen(string) + 1) * sizeof(char));
71 if (self->data == NULL) { return NULL; }
72 strcpy(self->data, string);
73 self->next = NULL;
74 return self;
75}
76
77/* Construct Line from FILE* */
78static Line *
79line_newFromFile(FILE *src)
80{
81 char buffer[max];
82 Line *self;
83
84 if (fgets(buffer, max, src)) {
85 self = line_alloc();
86 if (self == NULL) { return NULL; }
87 line_init(self, buffer);
88 return self;
89 }
90 return NULL;
58} 91}
59 92
60 93
@@ -69,15 +102,54 @@ compare_numeric(const void *, const void *);
69 102
70/* List */ 103/* List */
71 104
72static void 105/* */
73list_insert(); 106static List *
107list_init(List *self)
108{
109 self->len = 0;
110 self->sorted = NULL;
111 self->head = NULL;
112 self->current = NULL;
113 return self;
114}
74 115
116/* for simplicity, the List gains ownership of the line */
75static void 117static void
76list_sort(); 118list_insert(List *self, Line *line)
119{
120 if (line == NULL) { return NULL; }
77 121
78static void 122 /* first insertion */
79list_print(); 123 if (self->head == NULL) {
124 self->head = line;
125 self->current = line;
126
127 /* all subsequent insertions */
128 } else {
129 self->current->next = line;
130 self->current = line;
131 }
132 self->len++;
133 return self;
134}
80 135
136/* */
137static List *
138list_sort(List *self);
139
140/* precondition: list must be sorted */
141static List *
142list_writeToFile(List *self, FILE* dst)
143{
144 if (self->sorted == NULL) { return NULL; }
145}
146
147/* deallocate */
148static List *
149list_release(List *self)
150{
151 return self;
152}
81 153
82 154
83/* 155/*
@@ -123,5 +195,4 @@ sort_main(int argc, char **argv)
123 exit(0); 195 exit(0);
124} 196}
125 197
126/* $Date: 1999/12/21 20:00:35 $ */ 198/* $Id: sort.c,v 1.2 1999/12/22 00:30:29 beppu Exp $ */
127/* $Id: sort.c,v 1.1 1999/12/21 20:00:35 beppu Exp $ */