New asort routine; unf. breaks under -Wmissing-prototypes etc :(
[ccan] / ccan / asort / _info
1 #include <stdio.h>
2 #include <string.h>
3
4 /**
5  * asort - typesafe array sort (qsort)
6  *
7  * qsort() is the standard routine for sorting an array of objects.
8  * Unfortunately, it has two problems:
9  *     1) It isn't typesafe,
10  *     2) The comparison function doesn't take a context pointer.
11  *
12  * asort does both.
13  *
14  * Licence: LGPL
15  *
16  * Example:
17  *      #include <ccan/asort/asort.h>
18  *      #include <stdio.h>
19  *      #include <string.h>
20  *      
21  *      static int cmp(const char **a, const char **n, bool *casefold)
22  *      {
23  *              if (*casefold)
24  *                      return strcasecmp(*a, *b);
25  *              else
26  *                      return strcmp(*a, *b);
27  *      }
28  *      
29  *      int main(int argc, char *argv[])
30  *      {
31  *              bool casefold = false;
32  *              unsigned int i;
33  *      
34  *              if (argc < 2) {
35  *                      fprintf(stderr, "Usage: %s [-i] <list>...\n"
36  *                              "Sort arguments (-i = ignore case)\n",
37  *                              argv[0]);
38  *                      exit(1);
39  *              }
40  *      
41  *              if (strcmp(argv[1], "-i") == 0) {
42  *                      casefold = true;
43  *                      argc--;
44  *                      argv++;
45  *              }
46  *              asort(&argv[1], argc-1, cmp, &casefold);
47  *              for (i = 1; i < argc; i++)
48  *                      printf("%s ", argv[i]);
49  *              printf("\n");
50  *              return 0;
51  *      }
52  */
53 int main(int argc, char *argv[])
54 {
55         if (argc != 2)
56                 return 1;
57
58         if (strcmp(argv[1], "depends") == 0) {
59                 printf("ccan/typesafe_cb\n");
60                 printf("ccan/array_size\n");
61                 return 0;
62         }
63
64         return 1;
65 }