2002-09-13 21:40:28 +00:00
|
|
|
/*
|
2015-03-30 09:25:59 +00:00
|
|
|
* PROJECT: SORT - Adapted for ReactOS
|
|
|
|
* 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
|
2015-03-29 22:26:24 +00:00
|
|
|
*
|
|
|
|
*/
|
2002-09-13 21:40:28 +00:00
|
|
|
|
2015-03-29 22:26:24 +00:00
|
|
|
#include <stdlib.h>
|
2015-03-30 09:25:59 +00:00
|
|
|
#include <stdio.h>
|
2002-09-13 21:40:28 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-03-30 09:25:59 +00:00
|
|
|
#define MAXRECORDS 65536 /* maximum number of records that can be sorted */
|
|
|
|
#define MAXLEN 4095 /* maximum record length */
|
|
|
|
|
|
|
|
/* Reverse flag */
|
|
|
|
int rev;
|
|
|
|
|
|
|
|
/* Help flag */
|
|
|
|
int help;
|
|
|
|
|
|
|
|
/* Sort column */
|
|
|
|
int sortcol;
|
2002-09-13 21:40:28 +00:00
|
|
|
|
2015-03-30 09:25:59 +00:00
|
|
|
/* Error counter */
|
|
|
|
int err = 0;
|
2002-09-13 21:40:28 +00:00
|
|
|
|
2015-03-30 09:25:59 +00:00
|
|
|
int cmpr(const void *a, const void *b)
|
2002-09-13 21:40:28 +00:00
|
|
|
{
|
2015-03-29 22:26:24 +00:00
|
|
|
char *A, *B;
|
2002-09-13 21:40:28 +00:00
|
|
|
|
|
|
|
A = *(char **) a;
|
|
|
|
B = *(char **) b;
|
|
|
|
|
2015-03-29 22:26:24 +00:00
|
|
|
if (sortcol > 0)
|
|
|
|
{
|
|
|
|
if (strlen(A) > sortcol)
|
2015-03-30 09:25:59 +00:00
|
|
|
{
|
2015-03-29 22:26:24 +00:00
|
|
|
A += sortcol;
|
2015-03-30 09:25:59 +00:00
|
|
|
}
|
2015-03-29 22:26:24 +00:00
|
|
|
else
|
2015-03-30 09:25:59 +00:00
|
|
|
{
|
2015-03-29 22:26:24 +00:00
|
|
|
A = "";
|
2015-03-30 09:25:59 +00:00
|
|
|
}
|
2015-03-29 22:26:24 +00:00
|
|
|
if (strlen(B) > sortcol)
|
2015-03-30 09:25:59 +00:00
|
|
|
{
|
2015-03-29 22:26:24 +00:00
|
|
|
B += sortcol;
|
2015-03-30 09:25:59 +00:00
|
|
|
}
|
2015-03-29 22:26:24 +00:00
|
|
|
else
|
2015-03-30 09:25:59 +00:00
|
|
|
{
|
2015-03-29 22:26:24 +00:00
|
|
|
B = "";
|
2015-03-30 09:25:59 +00:00
|
|
|
}
|
2002-09-13 21:40:28 +00:00
|
|
|
}
|
2015-03-29 22:26:24 +00:00
|
|
|
|
2002-09-13 21:40:28 +00:00
|
|
|
if (!rev)
|
2015-03-30 09:25:59 +00:00
|
|
|
{
|
2015-03-29 22:26:24 +00:00
|
|
|
return strcmp(A, B);
|
2015-03-30 09:25:59 +00:00
|
|
|
}
|
2002-09-13 21:40:28 +00:00
|
|
|
else
|
2015-03-30 09:25:59 +00:00
|
|
|
{
|
2015-03-29 22:26:24 +00:00
|
|
|
return strcmp(B, A);
|
2015-03-30 09:25:59 +00:00
|
|
|
}
|
2002-09-13 21:40:28 +00:00
|
|
|
}
|
|
|
|
|
2015-03-30 09:25:59 +00:00
|
|
|
void usage(void)
|
2002-09-13 21:40:28 +00:00
|
|
|
{
|
|
|
|
fputs("SORT\n", stderr);
|
2015-03-30 09:25:59 +00:00
|
|
|
fputs("Sorts input and writes output to a file, console or a device.\n",
|
|
|
|
stderr);
|
2015-03-29 22:26:24 +00:00
|
|
|
|
2002-09-13 21:40:28 +00:00
|
|
|
if (err)
|
2015-03-30 09:25:59 +00:00
|
|
|
{
|
2015-03-29 22:26:24 +00:00
|
|
|
fputs("Invalid parameter\n", stderr);
|
2015-03-30 09:25:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fputs(" SORT [options] < [drive:1][path1]file1 > [drive2:][path2]file2\n",
|
|
|
|
stderr);
|
2015-03-29 22:26:24 +00:00
|
|
|
|
2002-09-13 21:40:28 +00:00
|
|
|
fputs(" Command | SORT [options] > [drive:][path]file\n", stderr);
|
|
|
|
fputs(" Options:\n", stderr);
|
2015-03-29 22:26:24 +00:00
|
|
|
fputs(" /R Reverse order\n", stderr);
|
|
|
|
fputs(" /+n Start sorting with column n\n", stderr);
|
|
|
|
fputs(" /? Help\n", stderr);
|
2002-09-13 21:40:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2015-03-29 22:26:24 +00:00
|
|
|
char temp[MAXLEN + 1];
|
|
|
|
char **list;
|
2015-03-30 09:25:59 +00:00
|
|
|
|
|
|
|
/* Option character pointer */
|
|
|
|
char *cp;
|
|
|
|
int i, nr;
|
2002-09-13 21:40:28 +00:00
|
|
|
|
|
|
|
sortcol = 0;
|
|
|
|
rev = 0;
|
2015-03-29 22:26:24 +00:00
|
|
|
while (--argc)
|
|
|
|
{
|
|
|
|
if (*(cp = *++argv) == '/')
|
|
|
|
{
|
|
|
|
switch (cp[1])
|
|
|
|
{
|
|
|
|
case 'R':
|
|
|
|
case 'r':
|
|
|
|
rev = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
case 'h':
|
|
|
|
case 'H':
|
|
|
|
help = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '+':
|
|
|
|
sortcol = atoi(cp + 1);
|
|
|
|
if (sortcol)
|
2015-03-30 09:25:59 +00:00
|
|
|
{
|
2015-03-29 22:26:24 +00:00
|
|
|
sortcol--;
|
2015-03-30 09:25:59 +00:00
|
|
|
}
|
2015-03-29 22:26:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
err++;
|
|
|
|
}
|
|
|
|
}
|
2002-09-13 21:40:28 +00:00
|
|
|
}
|
2015-03-29 22:26:24 +00:00
|
|
|
|
|
|
|
if (err || help)
|
|
|
|
{
|
|
|
|
usage();
|
|
|
|
exit(1);
|
2002-09-13 21:40:28 +00:00
|
|
|
}
|
2015-03-29 22:26:24 +00:00
|
|
|
|
2002-09-13 21:40:28 +00:00
|
|
|
list = (char **) malloc(MAXRECORDS * sizeof(char *));
|
2015-03-29 22:26:24 +00:00
|
|
|
if (list == NULL)
|
|
|
|
{
|
2002-09-13 21:40:28 +00:00
|
|
|
fputs("SORT: Insufficient memory\n", stderr);
|
|
|
|
exit(3);
|
|
|
|
}
|
2015-03-29 22:26:24 +00:00
|
|
|
|
|
|
|
for (nr = 0; nr < MAXRECORDS; nr++)
|
|
|
|
{
|
|
|
|
if (fgets(temp, MAXLEN, stdin) == NULL)
|
2015-03-30 09:25:59 +00:00
|
|
|
{
|
2015-03-29 22:26:24 +00:00
|
|
|
break;
|
2015-03-30 09:25:59 +00:00
|
|
|
}
|
2015-03-29 22:26:24 +00:00
|
|
|
|
|
|
|
if(strlen(temp))
|
2015-03-30 09:25:59 +00:00
|
|
|
{
|
|
|
|
temp[strlen(temp) - 1] = '\0';
|
|
|
|
}
|
2015-03-29 22:26:24 +00:00
|
|
|
|
|
|
|
list[nr] = (char *) malloc(strlen(temp) + 1);
|
|
|
|
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);
|
2002-09-13 21:40:28 +00:00
|
|
|
}
|
2015-03-29 22:26:24 +00:00
|
|
|
|
|
|
|
if (nr == MAXRECORDS)
|
|
|
|
{
|
|
|
|
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);
|
2002-09-13 21:40:28 +00:00
|
|
|
}
|
2015-03-29 22:26:24 +00:00
|
|
|
|
2015-03-30 09:25:59 +00:00
|
|
|
qsort((void *)list, nr, sizeof(char *), cmpr);
|
2015-03-29 22:26:24 +00:00
|
|
|
|
|
|
|
for (i = 0; i < nr; i++)
|
|
|
|
{
|
|
|
|
fputs(list[i], stdout);
|
2015-03-30 09:25:59 +00:00
|
|
|
fputs("\n", stdout);
|
2002-09-13 21:40:28 +00:00
|
|
|
}
|
2015-03-29 22:26:24 +00:00
|
|
|
|
|
|
|
/* Cleanup memory */
|
|
|
|
for (i = 0; i < nr; i++)
|
|
|
|
{
|
|
|
|
free(list[i]);
|
|
|
|
}
|
|
|
|
free(list);
|
2002-09-13 21:40:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* EOF */
|