aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-03-17 10:56:33 +1300
committerTom Ryder <tom@sanctum.geek.nz>2016-03-17 10:56:33 +1300
commit82ca1b812c68b169220f855001e88624467c34a4 (patch)
tree221e16c9e5bf12531fd4bb66b6c2df238590f863
parentAdd sort to targets and ignores (diff)
downloadtunics-82ca1b812c68b169220f855001e88624467c34a4.tar.gz
tunics-82ca1b812c68b169220f855001e88624467c34a4.zip
Add sort source (builds but segfaults atm)
-rw-r--r--sort.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/sort.c b/sort.c
new file mode 100644
index 0000000..4c0f50b
--- /dev/null
+++ b/sort.c
@@ -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);
+}
+