aboutsummaryrefslogtreecommitdiff
path: root/sort.c
diff options
context:
space:
mode:
Diffstat (limited to 'sort.c')
-rw-r--r--sort.c32
1 files changed, 6 insertions, 26 deletions
diff --git a/sort.c b/sort.c
index 4301f4303..49eb4fd72 100644
--- a/sort.c
+++ b/sort.c
@@ -54,7 +54,6 @@ typedef struct Line {
54typedef struct { 54typedef struct {
55 int len; /* number of Lines */ 55 int len; /* number of Lines */
56 Line **sorted; /* array fed to qsort */ 56 Line **sorted; /* array fed to qsort */
57
58 Line *head; /* head of List */ 57 Line *head; /* head of List */
59 Line *current; /* current Line */ 58 Line *current; /* current Line */
60} List; 59} List;
@@ -71,36 +70,23 @@ static const int max = 1024;
71static Line *line_alloc() 70static Line *line_alloc()
72{ 71{
73 Line *self; 72 Line *self;
74
75 self = malloc(1 * sizeof(Line)); 73 self = malloc(1 * sizeof(Line));
76 return self; 74 return self;
77} 75}
78 76
79/* Initialize Line with string */
80static Line *line_init(Line * self, const char *string)
81{
82 self->data = malloc((strlen(string) + 1) * sizeof(char));
83
84 if (self->data == NULL) {
85 return NULL;
86 }
87 strcpy(self->data, string);
88 self->next = NULL;
89 return self;
90}
91
92/* Construct Line from FILE* */ 77/* Construct Line from FILE* */
93static Line *line_newFromFile(FILE * src) 78static Line *line_newFromFile(FILE * src)
94{ 79{
95 char buffer[max];
96 Line *self; 80 Line *self;
81 char *cstring = NULL;
97 82
98 if (fgets(buffer, max, src)) { 83 if ((cstring = cstring_lineFromFile(src))) {
99 self = line_alloc(); 84 self = line_alloc();
100 if (self == NULL) { 85 if (self == NULL) {
101 return NULL; 86 return NULL;
102 } 87 }
103 line_init(self, buffer); 88 self->data = cstring;
89 self->next = NULL;
104 return self; 90 return self;
105 } 91 }
106 return NULL; 92 return NULL;
@@ -177,7 +163,7 @@ static List *list_insert(List * self, Line * line)
177 self->head = line; 163 self->head = line;
178 self->current = line; 164 self->current = line;
179 165
180 /* all subsequent insertions */ 166 /* all subsequent insertions */
181 } else { 167 } else {
182 self->current->next = line; 168 self->current->next = line;
183 self->current = line; 169 self->current = line;
@@ -241,12 +227,6 @@ static void list_release(List * self)
241} 227}
242 228
243 229
244/*
245 * I need a list
246 * to insert lines into
247 * then I need to sort this list
248 * and finally print it
249 */
250 230
251int sort_main(int argc, char **argv) 231int sort_main(int argc, char **argv)
252{ 232{
@@ -320,4 +300,4 @@ int sort_main(int argc, char **argv)
320 exit(0); 300 exit(0);
321} 301}
322 302
323/* $Id: sort.c,v 1.14 2000/04/15 16:34:54 erik Exp $ */ 303/* $Id: sort.c,v 1.15 2000/04/17 04:22:09 beppu Exp $ */