2 * SORT - reads line of a file and sorts them in order
3 * Copyright 1995 Jim Lynch
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #define MAXRECORDS 65536 /* maximum number of records that can be
29 #define MAXLEN 4095 /* maximum record length */
31 int rev
; /* reverse flag */
32 int help
; /* help flag */
33 int sortcol
; /* sort column */
34 int err
= 0; /* error counter */
37 cmpr(const void *a
, const void *b
)
45 if (strlen(A
) > sortcol
)
49 if (strlen(B
) > sortcol
)
63 fputs("SORT\n", stderr
);
64 fputs("Sorts input and writes output to a file, console or a device.\n", stderr
);
66 fputs("Invalid parameter\n", stderr
);
67 fputs(" SORT [options] < [drive:1][path1]file1 > [drive2:][path2]file2\n", stderr
);
68 fputs(" Command | SORT [options] > [drive:][path]file\n", stderr
);
69 fputs(" Options:\n", stderr
);
70 fputs(" /R Reverse order\n", stderr
);
71 fputs(" /+n Start sorting with column n\n", stderr
);
72 fputs(" /? Help\n", stderr
);
75 int main(int argc
, char **argv
)
77 char temp
[MAXLEN
+ 1];
79 char *cp
; /* option character pointer */
87 if (*(cp
= *++argv
) == '/') {
99 sortcol
= atoi(cp
+ 1);
112 list
= (char **) malloc(MAXRECORDS
* sizeof(char *));
114 fputs("SORT: Insufficient memory\n", stderr
);
117 for (nr
= 0; nr
< MAXRECORDS
; nr
++) {
118 if (fgets(temp
, MAXLEN
, stdin
) == NULL
)
121 temp
[strlen(temp
)-1]='\0';
122 list
[nr
] = (char *) malloc(strlen(temp
) + 1);
123 if (list
[nr
] == NULL
) {
124 fputs("SORT: Insufficient memory\n", stderr
);
127 strcpy(list
[nr
], temp
);
129 if (nr
== MAXRECORDS
) {
130 fputs("SORT: number of records exceeds maximum\n", stderr
);
133 qsort((void *) list
, nr
, sizeof(char *), cmpr
);
134 for (i
= 0; i
< nr
; i
++) {
135 fputs(list
[i
], stdout
);