#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
/* #define DEBUG */
#if defined(DEBUG)
#include "xmalloc.h"
#else
#define xmalloc(x, y) malloc(x)
#define xfree(x, y) free(x)
#define xrealloc(x, y, z) realloc(x, y)
#define xmallocdump()
#endif
#define ID_GETLINE 1001
#define ID_DATANODE 1002
#define BUFFSIZE 1024 /* >= 2 */
char *mygetline(FILE *fp) {
static char inbuff[BUFFSIZE];
char *outbuff_malloc, *tmpbuff;
char *p, *r;
int fEOL;
if ((outbuff_malloc = xmalloc(1, ID_GETLINE)) == NULL) {
return NULL;
}
*outbuff_malloc = '\0';
fEOL = 0;
do {
r = fgets(inbuff, BUFFSIZE, fp);
if (r == NULL)
break;
for (p = inbuff; *p != '\0'; p++)
;
if (*(p - 1) == '\n')
fEOL = 1;
if ((tmpbuff = xrealloc(outbuff_malloc, strlen(outbuff_malloc) + strlen(inbuff) + 1, ID_GETLINE)) == NULL) {
xfree(outbuff_malloc, ID_GETLINE);
return NULL;
}
strcat(tmpbuff, inbuff);
outbuff_malloc = tmpbuff;
} while (!fEOL);
if (strlen(outbuff_malloc) > 0) {
for (p = outbuff_malloc; *p != '\0'; p++)
;
if (*(p - 1) == '\n')
*(p - 1) = '\0';
return outbuff_malloc;
}
xfree(outbuff_malloc, ID_GETLINE);
return NULL;
}
struct datanode {
void *data;
struct datanode *next;
};
void push(struct datanode **root, void *data) {
struct datanode *p;
if ((p = xmalloc(sizeof(struct datanode), ID_DATANODE)) != 0) {
p->data = data;
p->next = *root;
*root = p;
}
}
void *pop(struct datanode **root) {
void *data;
struct datanode *p;
data = 0;
if (*root != 0) {
p = *root;
*root = p->next;
data = p->data;
xfree(p, ID_DATANODE);
}
return data;
}
struct datanode **sub_min(struct datanode **p, int (*cmp)(void *, void *)) {
struct datanode **q, **min;
char *min_str;
min = p;
min_str = (*p)->data;
for (q = p; *q; q = &((*q)->next)) {
if (cmp((*q)->data, min_str) < 0) {
min_str = (*q)->data;
min = q;
}
}
return min;
}
void sub_swap(struct datanode **p, struct datanode **q) {
struct datanode *t, *s;
if (&((*p)->next) == q) {
assert((*p)->next == *q);
t = *p;
s = (*q)->next;
*p = (*p)->next;
(*q)->next = t;
*q = s;
} else {
t = *p;
*p = *q;
*q = t;
s = (*p)->next; /* in fact *p<=>*q but neet to do (*p)->next <=> (*q)->next */
(*p)->next = (*q)->next;
(*q)->next = s;
}
}
static int c = 0;
void task_sort(struct datanode **root, int (*comp)(void *, void *)) {
struct datanode **p;
if (!*root)
return;
c++;
if (c % 100 == 0) fprintf(stderr, "%d\n", c);
p = sub_min(root, comp);
if (p != root)
sub_swap(root, p);
task_sort(&((*root)->next), comp);
}
int cmp_ascent(char *p, char *q) {
return strcmp(p, q);
}
int cmp_descent(char *p, char *q) {
return -strcmp(p, q);
}
int main(int argc, char *argv[]) {
struct datanode *root;
char *p;
int (*cmp)(char *, char *);
if (argc != 2) {
label_usage:
fprintf(stderr, "usage: %s <option>\n", argv[0]);
fprintf(stderr, "option: -a: ascent, -d: descent, input/output: stdio\n");
return 1;
}
if (*argv[1] == '-' && *(argv[1] + 1) == 'a')
cmp = cmp_ascent;
else if (*argv[1] == '-' && *(argv[1] + 1) == 'd')
cmp = cmp_descent;
else
goto label_usage;
root = 0;
while ((p = mygetline(stdin)) != 0)
push(&root, p);
task_sort(&root, (int (*)(void *, void *))cmp);
while ((p = pop(&root)) != 0) {
printf("%s\n", p);
xfree(p, ID_GETLINE);
}
xmallocdump();
return 0;
}
/* end */