2 * Copyright (C) 1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.4 1995-09-04 12:33:41 adam
8 * Various cleanup. YAZ util used instead.
10 * Revision 1.3 1995/09/01 14:06:35 adam
11 * Split of work into more files.
13 * Revision 1.2 1995/09/01 10:57:07 adam
16 * Revision 1.1 1995/09/01 10:34:51 adam
25 #include <sys/types.h>
32 struct dir_entry *dir_open (const char *rep)
36 size_t entry_max = 20;
38 struct dir_entry *entry;
40 logf (LOG_DEBUG, "dir_open %s", rep);
41 if (!(dir = opendir(rep)))
43 logf (LOG_WARN|LOG_ERRNO, "opendir %s", rep);
48 if (!(entry = malloc (sizeof(*entry) * entry_max)))
50 logf (LOG_FATAL|LOG_ERRNO, "malloc");
53 while ((dent = readdir (dir)))
55 if (strcmp (dent->d_name, ".") == 0 ||
56 strcmp (dent->d_name, "..") == 0)
58 if (idx == entry_max-1)
60 struct dir_entry *entry_n;
62 if (!(entry_n = malloc (sizeof(*entry) * (entry_max + 100))))
64 logf (LOG_FATAL|LOG_ERRNO, "malloc");
67 memcpy (entry_n, entry, idx * sizeof(*entry));
72 if (!(entry[idx].name = malloc (strlen(dent->d_name)+1)))
74 logf (LOG_FATAL|LOG_ERRNO, "malloc");
77 strcpy (entry[idx].name, dent->d_name);
80 entry[idx].name = NULL;
85 static int dir_cmp (const void *p1, const void *p2)
87 return strcmp (((struct dir_entry *) p1)->name,
88 ((struct dir_entry *) p2)->name);
91 void dir_sort (struct dir_entry *e)
96 qsort (e, nmemb, sizeof(*e), dir_cmp);
99 void dir_free (struct dir_entry **e_p)
102 struct dir_entry *e = *e_p;