2 * Copyright (C) 1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.3 1995-09-01 14:06:35 adam
8 * Split of work into more files.
10 * Revision 1.2 1995/09/01 10:57:07 adam
13 * Revision 1.1 1995/09/01 10:34:51 adam
22 #include <sys/types.h>
29 struct dir_entry *dir_open (const char *rep)
33 size_t entry_max = 20;
35 struct dir_entry *entry;
37 log (LOG_DEBUG, "dir_open %s", rep);
38 if (!(dir = opendir(rep)))
40 log (LOG_WARN|LOG_ERRNO, "opendir %s", rep);
45 if (!(entry = malloc (sizeof(*entry) * entry_max)))
47 log (LOG_FATAL|LOG_ERRNO, "malloc");
50 while ((dent = readdir (dir)))
52 if (strcmp (dent->d_name, ".") == 0 ||
53 strcmp (dent->d_name, "..") == 0)
55 if (idx == entry_max-1)
57 struct dir_entry *entry_n;
59 if (!(entry_n = malloc (sizeof(*entry) * (entry_max + 100))))
61 log (LOG_FATAL|LOG_ERRNO, "malloc");
64 memcpy (entry_n, entry, idx * sizeof(*entry));
69 if (!(entry[idx].name = malloc (strlen(dent->d_name)+1)))
71 log (LOG_FATAL|LOG_ERRNO, "malloc");
74 strcpy (entry[idx].name, dent->d_name);
77 entry[idx].name = NULL;
82 static int dir_cmp (const void *p1, const void *p2)
84 return strcmp (((struct dir_entry *) p1)->name,
85 ((struct dir_entry *) p2)->name);
88 void dir_sort (struct dir_entry *e)
93 qsort (e, nmemb, sizeof(*e), dir_cmp);
96 void dir_free (struct dir_entry **e_p)
99 struct dir_entry *e = *e_p;