2003-02-05 14:00:38 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2003 Tenebrae Team
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
*/
|
2003-02-13 21:25:24 +00:00
|
|
|
|
2003-02-05 14:00:38 +00:00
|
|
|
/*
|
|
|
|
Sys_Find* implementation for UNIX like systems
|
2003-02-13 21:25:24 +00:00
|
|
|
system conformance :
|
2003-02-05 14:00:38 +00:00
|
|
|
|
2003-02-13 21:25:24 +00:00
|
|
|
system not having the glob system call :
|
2003-02-05 14:00:38 +00:00
|
|
|
|
|
|
|
ISO/IEC 9945-2 (fnmatch)
|
|
|
|
BSD 4.3 (dirent syscalls)
|
|
|
|
|
|
|
|
OTHER :
|
|
|
|
|
|
|
|
POSIX.2 (glob)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "quakedef.h"
|
2003-02-13 21:25:24 +00:00
|
|
|
#include <errno.h>
|
|
|
|
|
2004-01-09 15:40:25 +00:00
|
|
|
#if !defined(__FreeBSD__) && !defined(__GLIBC__)
|
2003-02-13 21:25:24 +00:00
|
|
|
|
2003-02-05 14:00:38 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <fnmatch.h>
|
|
|
|
|
|
|
|
#define UXDATA_GRANULARITY 10
|
|
|
|
|
|
|
|
typedef struct {
|
2003-02-13 21:25:24 +00:00
|
|
|
int pathlen;
|
2003-02-05 14:00:38 +00:00
|
|
|
size_t count;
|
|
|
|
size_t lsize;
|
2003-02-13 21:25:24 +00:00
|
|
|
DIR *dir;
|
|
|
|
struct dirent **list;
|
2003-02-05 14:00:38 +00:00
|
|
|
} uxdirdata_t;
|
|
|
|
|
2003-02-13 21:25:24 +00:00
|
|
|
int uxdata_free (uxdirdata_t *ud)
|
2003-02-05 14:00:38 +00:00
|
|
|
{
|
|
|
|
int i;
|
2003-02-13 21:25:24 +00:00
|
|
|
closedir (ud->dir);
|
2003-02-05 14:00:38 +00:00
|
|
|
Z_Free (ud->list);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int direntp_compare (struct dirent **p1, struct dirent **p2)
|
|
|
|
{
|
|
|
|
return strncmp ((*p1)->d_name, (*p2)->d_name, sizeof ((*p1)->d_name));
|
|
|
|
}
|
|
|
|
|
|
|
|
dirdata_t *Sys_Findfirst (char *dirname, char *filter, dirdata_t *dirdata)
|
|
|
|
{
|
|
|
|
uxdirdata_t *uxdata;
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
struct dirent **list;
|
2003-02-13 21:25:24 +00:00
|
|
|
int pathlen;
|
2003-02-05 14:00:38 +00:00
|
|
|
|
|
|
|
if (dirdata && filter){
|
2003-02-13 21:25:24 +00:00
|
|
|
pathlen = strlen (dirname);
|
|
|
|
if (pathlen >= MAX_OSPATH-1)
|
|
|
|
return NULL;
|
|
|
|
strncpy (dirdata->entry, dirname, sizeof(dirdata->entry));
|
|
|
|
dir = opendir (dirdata->entry);
|
2003-02-05 14:00:38 +00:00
|
|
|
if (dir == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-02-13 21:25:24 +00:00
|
|
|
uxdata = Z_Malloc (sizeof(uxdirdata_t));
|
2003-02-05 14:00:38 +00:00
|
|
|
uxdata->count = 0;
|
2003-02-13 21:25:24 +00:00
|
|
|
uxdata->pathlen = pathlen;
|
2003-02-05 14:00:38 +00:00
|
|
|
uxdata->lsize = 10;
|
|
|
|
uxdata->list = Z_Malloc (sizeof(struct dirent *) * (uxdata->lsize));
|
2003-02-13 21:25:24 +00:00
|
|
|
|
|
|
|
while (entry = readdir (dir)){
|
|
|
|
int code;
|
|
|
|
|
2003-02-05 14:00:38 +00:00
|
|
|
// realloc entry list
|
|
|
|
if (uxdata->lsize == uxdata->count) {
|
|
|
|
list = Z_Malloc (sizeof(struct dirent *) * (uxdata->lsize += UXDATA_GRANULARITY));
|
|
|
|
memcpy (list, uxdata->list, uxdata->count);
|
|
|
|
Z_Free (uxdata->list);
|
|
|
|
uxdata->list = list;
|
|
|
|
}
|
|
|
|
// check name matching filter
|
2003-02-13 21:25:24 +00:00
|
|
|
code = fnmatch (filter, entry->d_name, 0);
|
2003-02-05 14:00:38 +00:00
|
|
|
switch (code){
|
|
|
|
case 0: /* match */
|
|
|
|
uxdata->list[uxdata->count] = entry;
|
|
|
|
uxdata->count++;
|
|
|
|
break;
|
2003-02-13 21:25:24 +00:00
|
|
|
case FNM_NOMATCH:
|
2003-02-05 14:00:38 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Sys_Error ("Sys_Glob_select : fnmatch call (%d)\n",errno);
|
|
|
|
}
|
2003-02-13 21:25:24 +00:00
|
|
|
}
|
|
|
|
if (uxdata->count) {
|
|
|
|
uxdata->lsize = uxdata->count;
|
|
|
|
uxdata->count = 0;
|
|
|
|
// sort the entry list
|
|
|
|
qsort(uxdata->list, uxdata->lsize, sizeof(struct dirent *),direntp_compare);
|
|
|
|
if (dirname[pathlen-1] != '/') {
|
|
|
|
dirdata->entry[pathlen]='/';
|
|
|
|
uxdata->pathlen++;
|
|
|
|
dirdata->entry[uxdata->pathlen]=0;
|
|
|
|
}
|
|
|
|
strncpy (dirdata->entry+uxdata->pathlen, uxdata->list[0]->d_name, sizeof(dirdata->entry)-uxdata->pathlen);
|
|
|
|
uxdata->dir = dir;
|
|
|
|
dirdata->internal = uxdata;
|
|
|
|
return dirdata;
|
|
|
|
}
|
|
|
|
else uxdata_free (uxdata);
|
2003-02-05 14:00:38 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dirdata_t *Sys_Findnext (dirdata_t *dirdata)
|
|
|
|
{
|
|
|
|
uxdirdata_t *uxdata;
|
|
|
|
if (dirdata){
|
|
|
|
uxdata=dirdata->internal;
|
|
|
|
if (uxdata) {
|
|
|
|
uxdata->count++;
|
|
|
|
// next entry ?
|
|
|
|
if (uxdata->count<uxdata->lsize){
|
2003-02-13 21:25:24 +00:00
|
|
|
strncpy (dirdata->entry+uxdata->pathlen, uxdata->list[0]->d_name, sizeof(dirdata->entry)-uxdata->pathlen);
|
2003-02-05 14:00:38 +00:00
|
|
|
return dirdata;
|
|
|
|
}
|
|
|
|
// no -> close (just in case Findclose isn't called)
|
|
|
|
uxdata_free (dirdata->internal);
|
|
|
|
dirdata->internal=NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sys_Findclose (dirdata_t *dirdata)
|
|
|
|
{
|
|
|
|
uxdirdata_t *uxdata;
|
|
|
|
if (dirdata){
|
|
|
|
uxdata=dirdata->internal;
|
|
|
|
if (uxdata){
|
|
|
|
uxdata_free (uxdata);
|
|
|
|
dirdata->internal=NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <glob.h>
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
glob_t globbuf;
|
|
|
|
size_t count;
|
|
|
|
} uxdirdata_t;
|
|
|
|
|
|
|
|
|
|
|
|
dirdata_t *Sys_Findfirst (char *dir, char *filter, dirdata_t *dirdata)
|
|
|
|
{
|
|
|
|
uxdirdata_t *uxdata;
|
|
|
|
if (dirdata && filter){
|
|
|
|
char dirfilter[MAX_OSPATH];
|
|
|
|
uxdata=Z_Malloc (sizeof(uxdirdata_t));
|
|
|
|
sprintf (dirfilter,"%s/%s", dir, filter);
|
|
|
|
glob (dirfilter,0,NULL,&uxdata->globbuf);
|
|
|
|
if (uxdata->globbuf.gl_pathc){
|
|
|
|
dirdata->internal=uxdata;
|
|
|
|
strncpy (dirdata->entry,uxdata->globbuf.gl_pathv[0],sizeof(dirdata->entry));
|
|
|
|
uxdata->count=0;
|
|
|
|
return dirdata;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dirdata_t *Sys_Findnext (dirdata_t *dirdata)
|
|
|
|
{
|
|
|
|
uxdirdata_t *uxdata;
|
|
|
|
if (dirdata){
|
|
|
|
uxdata=dirdata->internal;
|
|
|
|
if (uxdata) {
|
|
|
|
uxdata->count++;
|
|
|
|
// next entry ?
|
|
|
|
if (uxdata->count<uxdata->globbuf.gl_pathc){
|
|
|
|
strncpy (dirdata->entry,uxdata->globbuf.gl_pathv[uxdata->count],sizeof(dirdata->entry));
|
|
|
|
return dirdata;
|
|
|
|
}
|
|
|
|
// no -> close (just in case Findclose isn't called)
|
|
|
|
globfree (&uxdata->globbuf);
|
|
|
|
Z_Free (dirdata->internal);
|
|
|
|
dirdata->internal=NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sys_Findclose (dirdata_t *dirdata)
|
|
|
|
{
|
|
|
|
uxdirdata_t *uxdata;
|
|
|
|
if (dirdata){
|
|
|
|
uxdata=dirdata->internal;
|
|
|
|
if (uxdata){
|
|
|
|
globfree (&uxdata->globbuf);
|
|
|
|
Z_Free (uxdata);
|
|
|
|
dirdata->internal=NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2003-02-13 21:25:24 +00:00
|
|
|
|