Category : OS/2 Files
Archive   : EMXTST8F.ZIP
Filename : SORT.C

 
Output of file : SORT.C contained in archive : EMXTST8F.ZIP
/* sort.c (emx+gcc) */

#include
#include
#include

#define FALSE 0
#define TRUE 1

static int qcmp (const void *p1, const void *p2)
{
return (strcmp (*(char **)p1, *(char **)p2));
}

static void usage (void)
{
fputs ("Usage: sort [-c] [-v] []\n", stderr);
exit (1);
}

static int elements;
static int allocated;
static char **vector;

int main (int argc, char *argv[])
{
char buffer[512], *p;
char check, verbose;
FILE *f;
int i;

check = verbose = FALSE;
for (i = 1; i < argc; ++i)
{
if (argv[i][0] != '-')
break;
if (strcmp (argv[i]+1, "c") == 0)
check = TRUE;
else if (strcmp (argv[i]+1, "v") == 0)
verbose = TRUE;
else
usage ();
}
if (i == argc)
f = stdin;
else if (i+1 == argc)
{
f = fopen (argv[i], "rt");
if (f == NULL)
{
fputs ("Cannot open input file\n", stderr);
return (1);
}
}
else
usage();
elements = 0; allocated = 0; vector = NULL;
while (!feof (f))
{
if (fgets (buffer, sizeof (buffer), f) == NULL)
break;
p = strchr (buffer, '\n');
if (p != NULL) *p = 0;
if (elements >= allocated)
{
allocated += 1000;
vector = (char **)realloc (vector, allocated * sizeof (char *));
if (vector == NULL)
{
fputs ("Out of memory\n", stderr);
return (2);
}
}
p = strdup (buffer);
if (p == NULL)
{
fputs ("Out of memory\n", stderr);
return (2);
}
vector[elements++] = p;
}
if (ferror (f))
{
fputs ("Error reading input file\n", stderr);
return (2);
}
if (verbose)
fprintf (stderr, "%d line%s read\n", elements, (elements == 1 ? "" : "s"));
qsort (vector, elements, sizeof (char *), qcmp);
for (i = 0; i < elements; ++i)
if (puts (vector[i]) == EOF)
{
fputs ("Error writing output file\n", stderr);
return (2);
}
if (check)
{
for (i = 1; i < elements; ++i)
if (strcmp (vector[i-1], vector[i]) > 0)
fprintf (stderr, "Internal error, i=%d\n", i);
if (verbose)
fputs ("OK\n", stderr);
}
return (0);
}


  3 Responses to “Category : OS/2 Files
Archive   : EMXTST8F.ZIP
Filename : SORT.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/