aboutsummaryrefslogtreecommitdiff
path: root/sort.c
blob: 4c0f50bd3621614d921f6c60b5dc5bde5f299a23 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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);
}