1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "utlist.h"
5
6 #define BUFLEN 20
7
8 typedef struct el {
9 char bname[BUFLEN];
10 struct el *next, *prev;
11 } el;
12
namecmp(void * _a,void * _b)13 static int namecmp(void *_a, void *_b)
14 {
15 el *a = (el*)_a;
16 el *b = (el*)_b;
17 return strcmp(a->bname,b->bname);
18 }
19
main(int argc,char * argv[])20 int main(int argc, char *argv[])
21 {
22 el *name, *tmp;
23 el *head = NULL;
24
25 char linebuf[BUFLEN];
26 FILE *file;
27
28 file = fopen( "test11.dat", "r" );
29 if (file == NULL) {
30 perror("can't open: ");
31 exit(-1);
32 }
33
34 while (fgets(linebuf,BUFLEN,file) != NULL) {
35 name = (el*)malloc(sizeof(el));
36 if (name == NULL) {
37 exit(-1);
38 }
39 strcpy(name->bname, linebuf);
40 CDL_PREPEND(head, name);
41 }
42 CDL_SORT(head, namecmp);
43 CDL_FOREACH(head,tmp) {
44 printf("%s", tmp->bname);
45 }
46
47 fclose(file);
48
49 return 0;
50 }
51