ReactOS  0.4.12-dev-14-gd0c8636
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 */
17 int rev;
18 
19 /* Help flag */
20 int help;
21 
22 /* Sort column */
23 int sortcol;
24 
25 /* Error counter */
26 int err = 0;
27 
28 int 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 
65 void 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 
86 int 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 main(int argc, char **argv)
Definition: sort.c:86
ULONG nr
Definition: thread.c:7
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define free
Definition: debug_ros.c:5
#define B(row, col)
Definition: m_matrix.c:146
int err
Definition: sort.c:26
FILE * stdin
static char ** argv
Definition: ServiceArgs.c:11
FILE * stdout
#define MAXRECORDS
Definition: sort.c:13
GLenum GLclampf GLint i
Definition: glfuncs.h:14
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 *))
smooth NULL
Definition: ftsmooth.c:416
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
int rev
Definition: sort.c:17
void usage(void)
Definition: sort.c:65
#define A(row, col)
Definition: m_matrix.c:145
int cmpr(const void *a, const void *b)
Definition: sort.c:28
static stack_node_t temp
Definition: rpn.c:18
_Check_return_opt_ _CRTIMP char *__cdecl fgets(_Out_writes_z_(_MaxCount) char *_Buf, _In_ int _MaxCount, _Inout_ FILE *_File)
#define MAXLEN
Definition: sort.c:14
_Check_return_ int __cdecl atoi(_In_z_ const char *_Str)
int sortcol
Definition: sort.c:23
POINT cp
Definition: magnifier.c:60
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
FILE * stderr
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
#define list
Definition: rosglue.h:35
#define malloc
Definition: debug_ros.c:4
GLboolean GLboolean GLboolean GLboolean a
Definition: glext.h:6204
void exit(int exitcode)
Definition: _exit.c:33
int help
Definition: sort.c:20
_Check_return_opt_ _CRTIMP int __cdecl fputs(_In_z_ const char *_Str, _Inout_ FILE *_File)