ReactOS  0.4.12-dev-102-g4b7f1e0
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.

Referenced by main().

◆ MAXRECORDS

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

Definition at line 13 of file sort.c.

Referenced by main().

Function Documentation

◆ cmpr()

int cmpr ( const void a,
const void b 
)

Definition at line 28 of file sort.c.

Referenced by main().

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 }
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define B(row, col)
Definition: m_matrix.c:146
int rev
Definition: sort.c:17
#define A(row, col)
Definition: m_matrix.c:145
int sortcol
Definition: sort.c:23
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469

◆ 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
ULONG nr
Definition: thread.c:7
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define free
Definition: debug_ros.c:5
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
int rev
Definition: sort.c:17
void usage(void)
Definition: sort.c:65
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
#define list
Definition: rosglue.h:35
#define malloc
Definition: debug_ros.c:4
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)

◆ usage()

void usage ( void  )

Definition at line 65 of file sort.c.

Referenced by main().

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 }
int err
Definition: sort.c:26
FILE * stderr
_Check_return_opt_ _CRTIMP int __cdecl fputs(_In_z_ const char *_Str, _Inout_ FILE *_File)

Variable Documentation

◆ err

int err = 0

Definition at line 26 of file sort.c.

Referenced by main(), and usage().

◆ help

int help

Definition at line 20 of file sort.c.

Referenced by DIALOG_PAGESETUP_Hook(), main(), ShowHelpWindow(), and test_MsiGetFeatureInfo().

◆ rev

◆ sortcol

int sortcol

Definition at line 23 of file sort.c.

Referenced by cmpr(), and main().