ReactOS 0.4.15-dev-7788-g1ad9096
sort.c
Go to the documentation of this file.
1/*
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
6 *
7 */
8
9#include <stdlib.h>
10#include <stdio.h>
11#include <string.h>
12
13#define MAXRECORDS 65536 /* maximum number of records that can be sorted */
14#define MAXLEN 4095 /* maximum record length */
15
16/* Reverse flag */
17int rev;
18
19/* Help flag */
20int help;
21
22/* Sort column */
24
25/* Error counter */
26int err = 0;
27
28int cmpr(const void *a, const void *b)
29{
30 char *A, *B;
31
32 A = *(char **) a;
33 B = *(char **) b;
34
35 if (sortcol > 0)
36 {
37 if (strlen(A) > sortcol)
38 {
39 A += sortcol;
40 }
41 else
42 {
43 A = "";
44 }
45 if (strlen(B) > sortcol)
46 {
47 B += sortcol;
48 }
49 else
50 {
51 B = "";
52 }
53 }
54
55 if (!rev)
56 {
57 return strcmp(A, B);
58 }
59 else
60 {
61 return strcmp(B, A);
62 }
63}
64
65void usage(void)
66{
67 fputs("SORT\n", stderr);
68 fputs("Sorts input and writes output to a file, console or a device.\n",
69 stderr);
70
71 if (err)
72 {
73 fputs("Invalid parameter\n", stderr);
74 }
75
76 fputs(" SORT [options] < [drive:1][path1]file1 > [drive2:][path2]file2\n",
77 stderr);
78
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);
84}
85
86int main(int argc, char **argv)
87{
88 char temp[MAXLEN + 1];
89 char **list;
90
91 /* Option character pointer */
92 char *cp;
93 int i, nr;
94
95 sortcol = 0;
96 rev = 0;
97 while (--argc)
98 {
99 if (*(cp = *++argv) == '/')
100 {
101 switch (cp[1])
102 {
103 case 'R':
104 case 'r':
105 rev = 1;
106 break;
107
108 case '?':
109 case 'h':
110 case 'H':
111 help = 1;
112 break;
113
114 case '+':
115 sortcol = atoi(cp + 1);
116 if (sortcol)
117 {
118 sortcol--;
119 }
120 break;
121
122 default:
123 err++;
124 }
125 }
126 }
127
128 if (err || help)
129 {
130 usage();
131 exit(1);
132 }
133
134 list = (char **) malloc(MAXRECORDS * sizeof(char *));
135 if (list == NULL)
136 {
137 fputs("SORT: Insufficient memory\n", stderr);
138 exit(3);
139 }
140
141 for (nr = 0; nr < MAXRECORDS; nr++)
142 {
143 if (fgets(temp, MAXLEN, stdin) == NULL)
144 {
145 break;
146 }
147
148 if(strlen(temp))
149 {
150 temp[strlen(temp) - 1] = '\0';
151 }
152
153 list[nr] = (char *) malloc(strlen(temp) + 1);
154 if (list[nr] == NULL)
155 {
156 fputs("SORT: Insufficient memory\n", stderr);
157
158 /* Cleanup memory */
159 for (i = 0; i < nr; i++)
160 {
161 free(list[i]);
162 }
163 free(list);
164 exit(3);
165 }
166
167 strcpy(list[nr], temp);
168 }
169
170 if (nr == MAXRECORDS)
171 {
172 fputs("SORT: number of records exceeds maximum\n", stderr);
173
174 /* Cleanup memory */
175 for (i = 0; i < nr; i++)
176 {
177 free(list[i]);
178 }
179 free(list);
180
181 /* Bail out */
182 exit(4);
183 }
184
185 qsort((void *)list, nr, sizeof(char *), cmpr);
186
187 for (i = 0; i < nr; i++)
188 {
189 fputs(list[i], stdout);
190 fputs("\n", stdout);
191 }
192
193 /* Cleanup memory */
194 for (i = 0; i < nr; i++)
195 {
196 free(list[i]);
197 }
198 free(list);
199 return 0;
200}
201/* EOF */
static int argc
Definition: ServiceArgs.c:12
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
int cmpr(const void *a, const void *b)
Definition: sort.c:28
int rev
Definition: sort.c:17
int sortcol
Definition: sort.c:23
int err
Definition: sort.c:26
int help
Definition: sort.c:20
#define MAXRECORDS
Definition: sort.c:13
#define MAXLEN
Definition: sort.c:14
void usage(void)
Definition: sort.c:65
Definition: ehthrow.cxx:93
Definition: ehthrow.cxx:54
Definition: list.h:37
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
#define NULL
Definition: types.h:112
#define A(row, col)
#define B(row, col)
int main()
Definition: test.c:6
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
GLboolean GLboolean GLboolean GLboolean a
Definition: glext.h:6204
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define stdout
Definition: stdio.h:99
#define stderr
Definition: stdio.h:100
_Check_return_opt_ _CRTIMP int __cdecl fputs(_In_z_ const char *_Str, _Inout_ FILE *_File)
#define stdin
Definition: stdio.h:98
_Check_return_opt_ _CRTIMP char *__cdecl fgets(_Out_writes_z_(_MaxCount) char *_Buf, _In_ int _MaxCount, _Inout_ FILE *_File)
_Check_return_ int __cdecl atoi(_In_z_ const char *_Str)
POINT cp
Definition: magnifier.c:59
ULONG nr
Definition: thread.c:7
#define argv
Definition: mplay32.c:18
#define list
Definition: rosglue.h:35
static calc_node_t temp
Definition: rpn_ieee.c:38
#define exit(n)
Definition: config.h:202
void __cdecl qsort(_Inout_updates_bytes_(_NumOfElements *_SizeOfElements) void *_Base, _In_ size_t _NumOfElements, _In_ size_t _SizeOfElements, _In_ int(__cdecl *_PtFuncCompare)(const void *, const void *))