1 /* $Id: dir.c,v 1.23 2002-08-02 19:26:55 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
32 #include <sys/types.h>
38 struct dir_entry *dir_open (const char *rep, const char *base)
45 size_t entry_max = 500;
47 struct dir_entry *entry;
49 if (base && !yaz_is_abspath(rep))
51 strcpy (full_rep, base);
52 strcat (full_rep, "/");
56 strcat (full_rep, rep);
58 logf (LOG_LOG, "dir_open %s", full_rep);
59 if (!(dir = opendir(full_rep)))
61 logf (LOG_WARN|LOG_ERRNO, "opendir %s", rep);
62 if (errno != ENOENT && errno != EACCES)
66 entry = (struct dir_entry *) xmalloc (sizeof(*entry) * entry_max);
68 pathpos = strlen(path);
69 if (!pathpos || path[pathpos-1] != '/')
70 path[pathpos++] = '/';
71 while ((dent = readdir (dir)))
74 if (strcmp (dent->d_name, ".") == 0 ||
75 strcmp (dent->d_name, "..") == 0)
77 if (idx == entry_max-1)
79 struct dir_entry *entry_n;
81 entry_n = (struct dir_entry *)
82 xmalloc (sizeof(*entry) * (entry_max += 1000));
83 memcpy (entry_n, entry, idx * sizeof(*entry));
87 strcpy (path + pathpos, dent->d_name);
89 if (base && !yaz_is_abspath (path))
91 strcpy (full_rep, base);
92 strcat (full_rep, "/");
93 strcat (full_rep, path);
94 stat (full_rep, &finfo);
98 switch (finfo.st_mode & S_IFMT)
101 entry[idx].kind = dirs_file;
102 entry[idx].mtime = finfo.st_mtime;
103 entry[idx].name = (char *) xmalloc (strlen(dent->d_name)+1);
104 strcpy (entry[idx].name, dent->d_name);
108 entry[idx].kind = dirs_dir;
109 entry[idx].mtime = finfo.st_mtime;
110 entry[idx].name = (char *) xmalloc (strlen(dent->d_name)+2);
111 strcpy (entry[idx].name, dent->d_name);
112 strcat (entry[idx].name, "/");
117 entry[idx].name = NULL;
119 logf (LOG_DEBUG, "dir_close");
123 static int dir_cmp (const void *p1, const void *p2)
125 return strcmp (((struct dir_entry *) p1)->name,
126 ((struct dir_entry *) p2)->name);
129 void dir_sort (struct dir_entry *e)
132 while (e[nmemb].name)
134 qsort (e, nmemb, sizeof(*e), dir_cmp);
137 void dir_free (struct dir_entry **e_p)
140 struct dir_entry *e = *e_p;