Function rset_open changed its wflag parameter to general flags.
[idzebra-moved-to-github.git] / index / dir.c
1 /*
2  * Copyright (C) 1994-1995, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: dir.c,v $
7  * Revision 1.8  1995-10-10 13:59:23  adam
8  * Function rset_open changed its wflag parameter to general flags.
9  *
10  * Revision 1.7  1995/09/28  09:19:40  adam
11  * xfree/xmalloc used everywhere.
12  * Extract/retrieve method seems to work for text records.
13  *
14  * Revision 1.6  1995/09/08  14:52:26  adam
15  * Minor changes. Dictionary is lower case now.
16  *
17  * Revision 1.5  1995/09/06  16:11:16  adam
18  * Option: only one word key per file.
19  *
20  * Revision 1.4  1995/09/04  12:33:41  adam
21  * Various cleanup. YAZ util used instead.
22  *
23  * Revision 1.3  1995/09/01  14:06:35  adam
24  * Split of work into more files.
25  *
26  * Revision 1.2  1995/09/01  10:57:07  adam
27  * Minor changes.
28  *
29  * Revision 1.1  1995/09/01  10:34:51  adam
30  * Added dir.c
31  *
32  */
33 #include <stdio.h>
34 #include <assert.h>
35 #include <unistd.h>
36 #include <dirent.h>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39 #include <fcntl.h>
40 #include <ctype.h>
41
42 #include <alexutil.h>
43 #include "index.h"
44
45 struct dir_entry *dir_open (const char *rep)
46 {
47     DIR *dir;
48     struct dirent *dent;
49     size_t entry_max = 500;
50     size_t idx = 0;
51     struct dir_entry *entry;
52
53     logf (LOG_LOG, "dir_open %s", rep);
54     if (!(dir = opendir(rep)))
55     {
56         logf (LOG_WARN|LOG_ERRNO, "opendir %s", rep);
57         if (errno != ENOENT)
58             exit (1);
59         return NULL;
60     }
61     entry = xmalloc (sizeof(*entry) * entry_max);
62     while ((dent = readdir (dir)))
63     {
64         if (strcmp (dent->d_name, ".") == 0 ||
65             strcmp (dent->d_name, "..") == 0)
66             continue;
67         if (idx == entry_max-1)
68         {
69             struct dir_entry *entry_n;
70
71             entry_n = xmalloc (sizeof(*entry) * (entry_max + 1000));
72             memcpy (entry_n, entry, idx * sizeof(*entry));
73             xfree (entry);
74             entry = entry_n;
75             entry_max += 100;
76         }
77         entry[idx].name = xmalloc (strlen(dent->d_name)+1);
78         strcpy (entry[idx].name, dent->d_name);
79         idx++;
80     }
81     entry[idx].name = NULL;
82     closedir (dir);
83     return entry;
84 }
85
86 static int dir_cmp (const void *p1, const void *p2)
87 {
88     return strcmp (((struct dir_entry *) p1)->name,
89                    ((struct dir_entry *) p2)->name);
90 }
91
92 void dir_sort (struct dir_entry *e)
93 {
94     size_t nmemb = 0;
95     while (e[nmemb].name)
96         nmemb++;
97     qsort (e, nmemb, sizeof(*e), dir_cmp); 
98 }
99
100 void dir_free (struct dir_entry **e_p)
101 {
102     size_t i = 0;
103     struct dir_entry *e = *e_p;
104
105     assert (e);
106     while (e[i].name)
107         xfree (e[i++].name);
108     xfree (e);
109     *e_p = NULL;
110 }