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