2 * PROJECT: SORT - Adapted for ReactOS
3 * LICENSE: GPL - See COPYING in the top level directory
4 * PURPOSE: Reads line of a file and sorts them in order
5 * COPYRIGHT: Copyright 1995 Jim Lynch
13 #define MAXRECORDS 65536 /* maximum number of records that can be sorted */
14 #define MAXLEN 4095 /* maximum record length */
28 int cmpr(const void *a
, const void *b
)
37 if (strlen(A
) > sortcol
)
45 if (strlen(B
) > sortcol
)
67 fputs("SORT\n", stderr
);
68 fputs("Sorts input and writes output to a file, console or a device.\n",
73 fputs("Invalid parameter\n", stderr
);
76 fputs(" SORT [options] < [drive:1][path1]file1 > [drive2:][path2]file2\n",
79 fputs(" Command | SORT [options] > [drive:][path]file\n", stderr
);
80 fputs(" Options:\n", stderr
);
81 fputs(" /R Reverse order\n", stderr
);
82 fputs(" /+n Start sorting with column n\n", stderr
);
83 fputs(" /? Help\n", stderr
);
86 int main(int argc
, char **argv
)
88 char temp
[MAXLEN
+ 1];
91 /* Option character pointer */
99 if (*(cp
= *++argv
) == '/')
115 sortcol
= atoi(cp
+ 1);
134 list
= (char **) malloc(MAXRECORDS
* sizeof(char *));
137 fputs("SORT: Insufficient memory\n", stderr
);
141 for (nr
= 0; nr
< MAXRECORDS
; nr
++)
143 if (fgets(temp
, MAXLEN
, stdin
) == NULL
)
150 temp
[strlen(temp
) - 1] = '\0';
153 list
[nr
] = (char *) malloc(strlen(temp
) + 1);
154 if (list
[nr
] == NULL
)
156 fputs("SORT: Insufficient memory\n", stderr
);
159 for (i
= 0; i
< nr
; i
++)
167 strcpy(list
[nr
], temp
);
170 if (nr
== MAXRECORDS
)
172 fputs("SORT: number of records exceeds maximum\n", stderr
);
175 for (i
= 0; i
< nr
; i
++)
185 qsort((void *)list
, nr
, sizeof(char *), cmpr
);
187 for (i
= 0; i
< nr
; i
++)
189 fputs(list
[i
], stdout
);
194 for (i
= 0; i
< nr
; i
++)