ReactOS 0.4.15-dev-7788-g1ad9096
sort.c File Reference
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Include dependency graph for sort.c:

Go to the source code of this file.

Macros

#define MAXRECORDS   65536 /* maximum number of records that can be sorted */
 
#define MAXLEN   4095 /* maximum record length */
 

Functions

int cmpr (const void *a, const void *b)
 
void usage (void)
 
int main (int argc, char **argv)
 

Variables

int rev
 
int help
 
int sortcol
 
int err = 0
 

Macro Definition Documentation

◆ MAXLEN

#define MAXLEN   4095 /* maximum record length */

Definition at line 14 of file sort.c.

◆ MAXRECORDS

#define MAXRECORDS   65536 /* maximum number of records that can be sorted */

Definition at line 13 of file sort.c.

Function Documentation

◆ cmpr()

int cmpr ( const void a,
const void b 
)

Definition at line 28 of file sort.c.

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}
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
int rev
Definition: sort.c:17
int sortcol
Definition: sort.c:23
Definition: ehthrow.cxx:93
Definition: ehthrow.cxx:54
#define A(row, col)
#define B(row, col)
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
GLboolean GLboolean GLboolean GLboolean a
Definition: glext.h:6204

Referenced by main().

◆ main()

int main ( int argc  ,
char **  argv 
)

Definition at line 86 of file sort.c.

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}
static int argc
Definition: ServiceArgs.c:12
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
int cmpr(const void *a, const void *b)
Definition: sort.c:28
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: list.h:37
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
#define NULL
Definition: types.h:112
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 *))

◆ usage()

void usage ( void  )

Definition at line 65 of file sort.c.

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}

Referenced by main().

Variable Documentation

◆ err

int err = 0

Definition at line 26 of file sort.c.

Referenced by main(), and usage().

◆ help

◆ rev

◆ sortcol

int sortcol

Definition at line 23 of file sort.c.

Referenced by cmpr(), and main().