[SORT] Addendum to r66972. Since we're still playing the "Guess what the real changes are" game despite talking about it over and over, I just decided to join you guys. Fix the an IRP leak. Also tabs => spaces. More will come as long as the game does not stop. No svn history was unnecessarily changed (further) while making this commit.

svn path=/trunk/; revision=66973
This commit is contained in:
Amine Khaldi 2015-03-30 09:25:59 +00:00
parent 5d7944ca6f
commit 2c29a8c1ef

View file

@ -1,39 +1,31 @@
/* /*
* SORT - reads line of a file and sorts them in order * PROJECT: SORT - Adapted for ReactOS
* Copyright 1995 Jim Lynch * LICENSE: GPL - See COPYING in the top level directory
* PURPOSE: Reads line of a file and sorts them in order
* COPYRIGHT: Copyright 1995 Jim Lynch
* *
* Adapted for ReactOS
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/ */
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#include <malloc.h>
#define MAXRECORDS 65536 /* maximum number of records that can be sorted */ #define MAXRECORDS 65536 /* maximum number of records that can be sorted */
#define MAXLEN 4095 /* maximum record length */ #define MAXLEN 4095 /* maximum record length */
int rev; /* reverse flag */ /* Reverse flag */
int help; /* help flag */ int rev;
int sortcol; /* sort column */
int err = 0; /* error counter */
int /* Help flag */
cmpr(const void *a, const void *b) int help;
/* Sort column */
int sortcol;
/* Error counter */
int err = 0;
int cmpr(const void *a, const void *b)
{ {
char *A, *B; char *A, *B;
@ -43,31 +35,47 @@ cmpr(const void *a, const void *b)
if (sortcol > 0) if (sortcol > 0)
{ {
if (strlen(A) > sortcol) if (strlen(A) > sortcol)
{
A += sortcol; A += sortcol;
}
else else
{
A = ""; A = "";
}
if (strlen(B) > sortcol) if (strlen(B) > sortcol)
{
B += sortcol; B += sortcol;
}
else else
{
B = ""; B = "";
}
} }
if (!rev) if (!rev)
{
return strcmp(A, B); return strcmp(A, B);
}
else else
{
return strcmp(B, A); return strcmp(B, A);
}
} }
void void usage(void)
usage(void)
{ {
fputs("SORT\n", stderr); fputs("SORT\n", stderr);
fputs("Sorts input and writes output to a file, console or a device.\n", stderr); fputs("Sorts input and writes output to a file, console or a device.\n",
stderr);
if (err) if (err)
{
fputs("Invalid parameter\n", stderr); fputs("Invalid parameter\n", stderr);
}
fputs(" SORT [options] < [drive:1][path1]file1 > [drive2:][path2]file2\n",
stderr);
fputs(" SORT [options] < [drive:1][path1]file1 > [drive2:][path2]file2\n", stderr);
fputs(" Command | SORT [options] > [drive:][path]file\n", stderr); fputs(" Command | SORT [options] > [drive:][path]file\n", stderr);
fputs(" Options:\n", stderr); fputs(" Options:\n", stderr);
fputs(" /R Reverse order\n", stderr); fputs(" /R Reverse order\n", stderr);
@ -79,9 +87,10 @@ int main(int argc, char **argv)
{ {
char temp[MAXLEN + 1]; char temp[MAXLEN + 1];
char **list; char **list;
char *cp; /* option character pointer */
int nr; /* Option character pointer */
int i; char *cp;
int i, nr;
sortcol = 0; sortcol = 0;
rev = 0; rev = 0;
@ -105,7 +114,9 @@ int main(int argc, char **argv)
case '+': case '+':
sortcol = atoi(cp + 1); sortcol = atoi(cp + 1);
if (sortcol) if (sortcol)
{
sortcol--; sortcol--;
}
break; break;
default: default:
@ -130,10 +141,14 @@ int main(int argc, char **argv)
for (nr = 0; nr < MAXRECORDS; nr++) for (nr = 0; nr < MAXRECORDS; nr++)
{ {
if (fgets(temp, MAXLEN, stdin) == NULL) if (fgets(temp, MAXLEN, stdin) == NULL)
{
break; break;
}
if(strlen(temp)) if(strlen(temp))
temp[strlen(temp)-1]='\0'; {
temp[strlen(temp) - 1] = '\0';
}
list[nr] = (char *) malloc(strlen(temp) + 1); list[nr] = (char *) malloc(strlen(temp) + 1);
if (list[nr] == NULL) if (list[nr] == NULL)
@ -146,7 +161,6 @@ int main(int argc, char **argv)
free(list[i]); free(list[i]);
} }
free(list); free(list);
exit(3); exit(3);
} }
@ -168,12 +182,12 @@ int main(int argc, char **argv)
exit(4); exit(4);
} }
qsort((void *) list, nr, sizeof(char *), cmpr); qsort((void *)list, nr, sizeof(char *), cmpr);
for (i = 0; i < nr; i++) for (i = 0; i < nr; i++)
{ {
fputs(list[i], stdout); fputs(list[i], stdout);
fputs("\n",stdout); fputs("\n", stdout);
} }
/* Cleanup memory */ /* Cleanup memory */
@ -182,8 +196,6 @@ int main(int argc, char **argv)
free(list[i]); free(list[i]);
} }
free(list); free(list);
return 0; return 0;
} }
/* EOF */ /* EOF */