[FORMAT]
[reactos.git] / rosapps / applications / cmdutils / sort / sort.c
1 /*
2 * SORT - reads line of a file and sorts them in order
3 * Copyright 1995 Jim Lynch
4 *
5 * Adapted for ReactOS
6 *
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.
11 *
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.
16 *
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.
20 */
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <malloc.h>
26
27 #define MAXRECORDS 65536 /* maximum number of records that can be
28 * sorted */
29 #define MAXLEN 4095 /* maximum record length */
30
31 int rev; /* reverse flag */
32 int help; /* help flag */
33 int sortcol; /* sort column */
34 int err = 0; /* error counter */
35
36 int
37 cmpr(const void *a, const void *b)
38 {
39 char *A, *B;
40
41 A = *(char **) a;
42 B = *(char **) b;
43
44 if (sortcol > 0) {
45 if (strlen(A) > sortcol)
46 A += sortcol;
47 else
48 A = "";
49 if (strlen(B) > sortcol)
50 B += sortcol;
51 else
52 B = "";
53 }
54 if (!rev)
55 return strcmp(A, B);
56 else
57 return strcmp(B, A);
58 }
59
60 void
61 usage(void)
62 {
63 fputs("SORT\n", stderr);
64 fputs("Sorts input and writes output to a file, console or a device.\n", stderr);
65 if (err)
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);
73 }
74
75 int main(int argc, char **argv)
76 {
77 char temp[MAXLEN + 1];
78 char **list;
79 char *cp; /* option character pointer */
80 int nr;
81 int i;
82
83
84 sortcol = 0;
85 rev = 0;
86 while (--argc) {
87 if (*(cp = *++argv) == '/') {
88 switch (cp[1]) {
89 case 'R':
90 case 'r':
91 rev = 1;
92 break;
93 case '?':
94 case 'h':
95 case 'H':
96 help = 1;
97 break;
98 case '+':
99 sortcol = atoi(cp + 1);
100 if (sortcol)
101 sortcol--;
102 break;
103 default:
104 err++;
105 }
106 }
107 }
108 if (err || help) {
109 usage();
110 exit(1);
111 }
112 list = (char **) malloc(MAXRECORDS * sizeof(char *));
113 if (list == NULL) {
114 fputs("SORT: Insufficient memory\n", stderr);
115 exit(3);
116 }
117 for (nr = 0; nr < MAXRECORDS; nr++) {
118 if (fgets(temp, MAXLEN, stdin) == NULL)
119 break;
120 if(strlen(temp))
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);
125 exit(3);
126 }
127 strcpy(list[nr], temp);
128 }
129 if (nr == MAXRECORDS) {
130 fputs("SORT: number of records exceeds maximum\n", stderr);
131 exit(4);
132 }
133 qsort((void *) list, nr, sizeof(char *), cmpr);
134 for (i = 0; i < nr; i++) {
135 fputs(list[i], stdout);
136 fputs("\n",stdout);
137 }
138 return 0;
139 }
140
141 /* EOF */