diff options
Diffstat (limited to 'sort.c')
-rw-r--r-- | sort.c | 40 |
1 files changed, 40 insertions, 0 deletions
@@ -0,0 +1,40 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#define MAX_LINE_LENGTH 32 + +static int qsort_strcmp(const void *p1, const void *p2); + +int main(void) +{ + char **lv; + unsigned long lc; + unsigned int i; + + lv = malloc(sizeof(char *)); + for (lc = 0 ; ; lc++) { + fprintf(stderr, "Current char pointers allocated to lv: %lu\n", sizeof(lv) / sizeof(char *)); + if (lc >= sizeof(lv) / sizeof(char *)) { + lv = realloc(lv, (sizeof(lv) + 32) * sizeof(char *)); + } + lv[lc] = calloc(MAX_LINE_LENGTH, sizeof(char)); + if ((fgets(lv[lc], MAX_LINE_LENGTH, stdin)) == NULL) { + break; + } + } + + qsort(&lv, lc, sizeof(char *), qsort_strcmp); + + for (i = 0; i < lc; i++) { + fputs(lv[i], stdout); + } + + exit(EXIT_SUCCESS); +} + +static int qsort_strcmp(const void *p1, const void *p2) +{ + return strcmp(* (char * const *) p1, * (char * const *) p2); +} + |