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