mirror of
https://github.com/reactos/reactos.git
synced 2025-07-27 15:31:55 +00:00
[SORT]
- Don't leak memory, see CORE-8205 for more details. - Replace tabs with spaces. svn path=/trunk/; revision=66972
This commit is contained in:
parent
ee70d4e15d
commit
5d7944ca6f
1 changed files with 139 additions and 91 deletions
|
@ -1,60 +1,61 @@
|
||||||
/*
|
/*
|
||||||
* SORT - reads line of a file and sorts them in order
|
* SORT - reads line of a file and sorts them in order
|
||||||
* Copyright 1995 Jim Lynch
|
* Copyright 1995 Jim Lynch
|
||||||
*
|
*
|
||||||
* Adapted for ReactOS
|
* Adapted for ReactOS
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation; either version 2 of the License, or
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
* (at your option) any later version.
|
* (at your option) any later version.
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program; if not, write to the Free Software
|
* along with this program; if not, write to the Free Software
|
||||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
|
|
||||||
#define MAXRECORDS 65536 /* maximum number of records that can be
|
#define MAXRECORDS 65536 /* maximum number of records that can be sorted */
|
||||||
* sorted */
|
#define MAXLEN 4095 /* maximum record length */
|
||||||
#define MAXLEN 4095 /* maximum record length */
|
|
||||||
|
|
||||||
int rev; /* reverse flag */
|
int rev; /* reverse flag */
|
||||||
int help; /* help flag */
|
int help; /* help flag */
|
||||||
int sortcol; /* sort column */
|
int sortcol; /* sort column */
|
||||||
int err = 0; /* error counter */
|
int err = 0; /* error counter */
|
||||||
|
|
||||||
int
|
int
|
||||||
cmpr(const void *a, const void *b)
|
cmpr(const void *a, const void *b)
|
||||||
{
|
{
|
||||||
char *A, *B;
|
char *A, *B;
|
||||||
|
|
||||||
A = *(char **) a;
|
A = *(char **) a;
|
||||||
B = *(char **) b;
|
B = *(char **) b;
|
||||||
|
|
||||||
if (sortcol > 0) {
|
if (sortcol > 0)
|
||||||
if (strlen(A) > sortcol)
|
{
|
||||||
A += sortcol;
|
if (strlen(A) > sortcol)
|
||||||
else
|
A += sortcol;
|
||||||
A = "";
|
else
|
||||||
if (strlen(B) > sortcol)
|
A = "";
|
||||||
B += sortcol;
|
if (strlen(B) > sortcol)
|
||||||
else
|
B += sortcol;
|
||||||
B = "";
|
else
|
||||||
|
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
|
||||||
|
@ -62,79 +63,126 @@ 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);
|
||||||
fputs(" /+n Start sorting with column n\n", stderr);
|
fputs(" /+n Start sorting with column n\n", stderr);
|
||||||
fputs(" /? Help\n", stderr);
|
fputs(" /? Help\n", stderr);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
char temp[MAXLEN + 1];
|
char temp[MAXLEN + 1];
|
||||||
char **list;
|
char **list;
|
||||||
char *cp; /* option character pointer */
|
char *cp; /* option character pointer */
|
||||||
int nr;
|
int nr;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
|
||||||
sortcol = 0;
|
sortcol = 0;
|
||||||
rev = 0;
|
rev = 0;
|
||||||
while (--argc) {
|
while (--argc)
|
||||||
if (*(cp = *++argv) == '/') {
|
{
|
||||||
switch (cp[1]) {
|
if (*(cp = *++argv) == '/')
|
||||||
case 'R':
|
{
|
||||||
case 'r':
|
switch (cp[1])
|
||||||
rev = 1;
|
{
|
||||||
break;
|
case 'R':
|
||||||
case '?':
|
case 'r':
|
||||||
case 'h':
|
rev = 1;
|
||||||
case 'H':
|
break;
|
||||||
help = 1;
|
|
||||||
break;
|
case '?':
|
||||||
case '+':
|
case 'h':
|
||||||
sortcol = atoi(cp + 1);
|
case 'H':
|
||||||
if (sortcol)
|
help = 1;
|
||||||
sortcol--;
|
break;
|
||||||
break;
|
|
||||||
default:
|
case '+':
|
||||||
err++;
|
sortcol = atoi(cp + 1);
|
||||||
}
|
if (sortcol)
|
||||||
}
|
sortcol--;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
err++;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (err || help) {
|
|
||||||
usage();
|
if (err || help)
|
||||||
exit(1);
|
{
|
||||||
|
usage();
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
list = (char **) malloc(MAXRECORDS * sizeof(char *));
|
list = (char **) malloc(MAXRECORDS * sizeof(char *));
|
||||||
if (list == NULL) {
|
if (list == NULL)
|
||||||
|
{
|
||||||
fputs("SORT: Insufficient memory\n", stderr);
|
fputs("SORT: Insufficient memory\n", stderr);
|
||||||
exit(3);
|
exit(3);
|
||||||
}
|
}
|
||||||
for (nr = 0; nr < MAXRECORDS; nr++) {
|
|
||||||
if (fgets(temp, MAXLEN, stdin) == NULL)
|
for (nr = 0; nr < MAXRECORDS; nr++)
|
||||||
break;
|
{
|
||||||
if(strlen(temp))
|
if (fgets(temp, MAXLEN, stdin) == NULL)
|
||||||
temp[strlen(temp)-1]='\0';
|
break;
|
||||||
list[nr] = (char *) malloc(strlen(temp) + 1);
|
|
||||||
if (list[nr] == NULL) {
|
if(strlen(temp))
|
||||||
fputs("SORT: Insufficient memory\n", stderr);
|
temp[strlen(temp)-1]='\0';
|
||||||
exit(3);
|
|
||||||
}
|
list[nr] = (char *) malloc(strlen(temp) + 1);
|
||||||
strcpy(list[nr], temp);
|
if (list[nr] == NULL)
|
||||||
|
{
|
||||||
|
fputs("SORT: Insufficient memory\n", stderr);
|
||||||
|
|
||||||
|
/* Cleanup memory */
|
||||||
|
for (i = 0; i < nr; i++)
|
||||||
|
{
|
||||||
|
free(list[i]);
|
||||||
|
}
|
||||||
|
free(list);
|
||||||
|
|
||||||
|
exit(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
strcpy(list[nr], temp);
|
||||||
}
|
}
|
||||||
if (nr == MAXRECORDS) {
|
|
||||||
fputs("SORT: number of records exceeds maximum\n", stderr);
|
if (nr == MAXRECORDS)
|
||||||
exit(4);
|
{
|
||||||
|
fputs("SORT: number of records exceeds maximum\n", stderr);
|
||||||
|
|
||||||
|
/* Cleanup memory */
|
||||||
|
for (i = 0; i < nr; i++)
|
||||||
|
{
|
||||||
|
free(list[i]);
|
||||||
|
}
|
||||||
|
free(list);
|
||||||
|
|
||||||
|
/* Bail out */
|
||||||
|
exit(4);
|
||||||
}
|
}
|
||||||
|
|
||||||
qsort((void *) list, nr, sizeof(char *), cmpr);
|
qsort((void *) list, nr, sizeof(char *), cmpr);
|
||||||
for (i = 0; i < nr; i++) {
|
|
||||||
fputs(list[i], stdout);
|
for (i = 0; i < nr; i++)
|
||||||
fputs("\n",stdout);
|
{
|
||||||
|
fputs(list[i], stdout);
|
||||||
|
fputs("\n",stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Cleanup memory */
|
||||||
|
for (i = 0; i < nr; i++)
|
||||||
|
{
|
||||||
|
free(list[i]);
|
||||||
|
}
|
||||||
|
free(list);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue