| 1 |
/************************************************************************** |
| 2 |
* This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is * |
| 3 |
* provided by Jonathan and Jovehacks without charge and without * |
| 4 |
* warranty. You may copy, modify, and/or distribute JOVE, provided that * |
| 5 |
* this notice is included in all the source files and documentation. * |
| 6 |
**************************************************************************/ |
| 7 |
|
| 8 |
/* |
| 9 |
* This file is used as a compiled module by Jove and also included as |
| 10 |
* source in recover.c |
| 11 |
*/ |
| 12 |
#ifndef TUNED |
| 13 |
# include "jove.h" |
| 14 |
#endif |
| 15 |
#include "scandir.h" |
| 16 |
|
| 17 |
#ifdef F_COMPLETION /* the body is the rest of this file */ |
| 18 |
|
| 19 |
#ifdef MAC |
| 20 |
# include "mac.h" |
| 21 |
#else |
| 22 |
# include <sys/stat.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
#ifdef UNIX |
| 26 |
|
| 27 |
# if defined(M_XENIX) && !defined(M_UNIX) |
| 28 |
/* XENIX, but not SCO UNIX, which pretends to be XENIX! */ |
| 29 |
# include <sys/ndir.h> |
| 30 |
# ifndef dirent |
| 31 |
# define dirent direct |
| 32 |
# endif |
| 33 |
# endif |
| 34 |
|
| 35 |
# ifdef BSD_DIR |
| 36 |
# include <sys/dir.h> |
| 37 |
# ifndef dirent |
| 38 |
# define dirent direct |
| 39 |
# endif |
| 40 |
# endif |
| 41 |
|
| 42 |
/* default to dirent.h */ |
| 43 |
# if !defined(dirent) && !defined(DIRENT_EMULATE) |
| 44 |
# include <dirent.h> |
| 45 |
# endif |
| 46 |
|
| 47 |
# ifdef DIRENT_EMULATE |
| 48 |
|
| 49 |
typedef struct { |
| 50 |
int d_fd; /* File descriptor for this directory */ |
| 51 |
} DIR; |
| 52 |
|
| 53 |
private int |
| 54 |
closedir(dp) |
| 55 |
DIR *dp; |
| 56 |
{ |
| 57 |
(void) close(dp->d_fd); |
| 58 |
free(dp); |
| 59 |
return 0; /* don't know how to fail */ |
| 60 |
} |
| 61 |
|
| 62 |
private DIR * |
| 63 |
opendir(dir) |
| 64 |
const char *dir; |
| 65 |
{ |
| 66 |
int fd; |
| 67 |
|
| 68 |
if ((fd = open(dir, O_RDONLY | O_BINARY | O_CLOEXEC)) != -1) { |
| 69 |
struct stat stbuf; |
| 70 |
|
| 71 |
if ((fstat(fd, &stbuf) != -1) |
| 72 |
&& (stbuf.st_mode & S_IFMT) == S_IFDIR) |
| 73 |
{ |
| 74 |
/* Success! */ |
| 75 |
DIR *dp = (DIR *) emalloc(sizeof *dp); |
| 76 |
|
| 77 |
dp->d_fd = fd; |
| 78 |
return dp; |
| 79 |
} |
| 80 |
/* this isn't a directory! */ |
| 81 |
(void) close(fd); |
| 82 |
} |
| 83 |
return NULL; |
| 84 |
} |
| 85 |
|
| 86 |
private dirent * |
| 87 |
readdir(dp) |
| 88 |
DIR *dp; |
| 89 |
{ |
| 90 |
static dirent dir; |
| 91 |
|
| 92 |
do { |
| 93 |
if (read(dp->d_fd, &dir, sizeof dir) != sizeof dir) |
| 94 |
return NULL; |
| 95 |
} while (dir.d_ino == 0); |
| 96 |
|
| 97 |
return &dir; |
| 98 |
} |
| 99 |
|
| 100 |
#endif /* DIRENT_EMULATE */ |
| 101 |
|
| 102 |
/* jscandir returns the number of entries or -1 if the directory cannot |
| 103 |
* be opened or malloc fails. |
| 104 |
*/ |
| 105 |
int |
| 106 |
jscandir(const char *dir, char ***nmptr, |
| 107 |
jbool (*qualify)(const char *), |
| 108 |
int (*sorter)(const void *, const void *)) |
| 109 |
{ |
| 110 |
DIR *dirp; |
| 111 |
struct dirent *entry; |
| 112 |
char **ourarray; |
| 113 |
unsigned int nalloc = 10; |
| 114 |
size_t nentries = 0; |
| 115 |
|
| 116 |
if ((dirp = opendir(dir)) == NULL) |
| 117 |
return -1; |
| 118 |
|
| 119 |
ourarray = (char **) emalloc(nalloc * sizeof (char *)); |
| 120 |
while ((entry = readdir(dirp)) != NULL) { |
| 121 |
if (qualify != NULL && !(*qualify)(entry->d_name)) |
| 122 |
continue; |
| 123 |
|
| 124 |
/* note: test ensures one space left in ourarray for NULL */ |
| 125 |
if (nentries+1 == nalloc) |
| 126 |
ourarray = (char **) erealloc(ourarray, |
| 127 |
(nalloc += 10) * sizeof (char *)); |
| 128 |
ourarray[nentries++] = copystr(entry->d_name); |
| 129 |
} |
| 130 |
closedir(dirp); |
| 131 |
ourarray[nentries] = NULL; |
| 132 |
|
| 133 |
if (sorter != NULL) |
| 134 |
qsort((void *) ourarray, nentries, sizeof (char **), sorter); |
| 135 |
*nmptr = ourarray; |
| 136 |
|
| 137 |
return nentries; |
| 138 |
} |
| 139 |
|
| 140 |
#endif /* UNIX */ |
| 141 |
|
| 142 |
#ifdef MSFILESYSTEM |
| 143 |
/* NOTE: MatchDir affects any call to jscandir! |
| 144 |
* Currently, the only calls to jscandir are from: |
| 145 |
* - the recover program (which never touches MatchDir) |
| 146 |
* - descendants of ask_file or ask_dir (which always set it) |
| 147 |
*/ |
| 148 |
jbool MatchDir = NO; |
| 149 |
#endif |
| 150 |
|
| 151 |
#ifdef MSDOS |
| 152 |
# include <dos.h> |
| 153 |
|
| 154 |
# ifndef ZTCDOS |
| 155 |
# include <search.h> |
| 156 |
# endif |
| 157 |
|
| 158 |
/* Scandir returns the number of entries or -1 if the directory cannot |
| 159 |
* be opened or malloc fails. |
| 160 |
*/ |
| 161 |
int |
| 162 |
jscandir(const char *dir, char ***nmptr, jbool (*qualify)(const char *), |
| 163 |
int (*sorter)(const void *, const void *)) |
| 164 |
{ |
| 165 |
struct find_t entry; |
| 166 |
char **ourarray; |
| 167 |
unsigned int nalloc = 10, |
| 168 |
nentries = 0; |
| 169 |
|
| 170 |
{ |
| 171 |
char dirname[FILESIZE]; |
| 172 |
char *ptr; |
| 173 |
|
| 174 |
strcpy(dirname, dir); |
| 175 |
ptr = &dirname[strlen(dirname)-1]; |
| 176 |
if (!((dirname[1] == ':' && dirname[2] == '\0') || *ptr == '/' || *ptr == '\\')) |
| 177 |
*++ptr = '/'; |
| 178 |
strcpy(ptr+1, "*.*"); |
| 179 |
|
| 180 |
if (_dos_findfirst(dirname, _A_NORMAL|_A_RDONLY|_A_HIDDEN|_A_SUBDIR, &entry)) |
| 181 |
return -1; |
| 182 |
} |
| 183 |
ourarray = (char **) emalloc(nalloc * sizeof (char *)); |
| 184 |
do { |
| 185 |
char filename[FILESIZE]; |
| 186 |
|
| 187 |
if (MatchDir && (entry.attrib&_A_SUBDIR) == 0) |
| 188 |
continue; |
| 189 |
|
| 190 |
strlwr(entry.name); |
| 191 |
if (qualify != NULL && !(*qualify)(entry.name)) |
| 192 |
continue; |
| 193 |
|
| 194 |
/* note: test ensures one space left in ourarray for NULL */ |
| 195 |
if (nentries+1 == nalloc) |
| 196 |
ourarray = (char **) erealloc((char *) ourarray, (nalloc += 10) * sizeof (char *)); |
| 197 |
strcpy(filename, entry.name); |
| 198 |
#ifdef DIRECTORY_ADD_SLASH |
| 199 |
if ((entry.attrib&_A_SUBDIR) != 0) |
| 200 |
strcat(filename, "/"); |
| 201 |
#endif |
| 202 |
ourarray[nentries++] = copystr(filename); |
| 203 |
} while (_dos_findnext(&entry) == 0); |
| 204 |
ourarray[nentries] = NULL; |
| 205 |
|
| 206 |
if (sorter != (int (*)(const void *, const void *))NULL) |
| 207 |
qsort((char *) ourarray, nentries, sizeof (char **), sorter); |
| 208 |
*nmptr = ourarray; |
| 209 |
|
| 210 |
return nentries; |
| 211 |
} |
| 212 |
|
| 213 |
#endif /* MSDOS */ |
| 214 |
|
| 215 |
#ifdef WIN32 |
| 216 |
# undef Fill /* sigh, used as a field name in some windows header! */ |
| 217 |
# undef CR /* sigh, used as a field name in some windows header! */ |
| 218 |
# include <windows.h> |
| 219 |
|
| 220 |
/* Scandir returns the number of entries or -1 if the directory cannot |
| 221 |
* be opened or malloc fails. |
| 222 |
*/ |
| 223 |
int |
| 224 |
jscandir(const char *dir, char ***nmptr, jbool (*qualify)(const char *), |
| 225 |
int (*sorter)(const void *, const void *)) |
| 226 |
{ |
| 227 |
WIN32_FIND_DATA entry; |
| 228 |
HANDLE findHand; |
| 229 |
char **ourarray; |
| 230 |
unsigned int nalloc = 10, |
| 231 |
nentries = 0; |
| 232 |
|
| 233 |
{ |
| 234 |
char dirname[_MAX_PATH]; |
| 235 |
char *ptr; |
| 236 |
|
| 237 |
strcpy(dirname, dir); |
| 238 |
ptr = &dirname[strlen(dirname)-1]; |
| 239 |
if (!((dirname[1] == ':' && dirname[2] == '\0') || *ptr == '/' || *ptr == '\\')) |
| 240 |
*++ptr = '/'; |
| 241 |
strcpy(ptr+1, "*.*"); |
| 242 |
|
| 243 |
if ((findHand = FindFirstFile(dirname, &entry)) == INVALID_HANDLE_VALUE) |
| 244 |
return -1; |
| 245 |
} |
| 246 |
ourarray = (char **) emalloc(nalloc * sizeof (char *)); |
| 247 |
do { |
| 248 |
char filename[_MAX_PATH]; |
| 249 |
|
| 250 |
if (MatchDir && (entry.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) == 0) |
| 251 |
continue; |
| 252 |
|
| 253 |
strcpy(filename, entry.cFileName); |
| 254 |
strlwr(entry.cFileName); |
| 255 |
if (qualify != NULL && !(*qualify)(entry.cFileName)) |
| 256 |
continue; |
| 257 |
|
| 258 |
/* note: test ensures one space left in ourarray for NULL */ |
| 259 |
if (nentries+1 == nalloc) |
| 260 |
ourarray = (char **) erealloc((char *) ourarray, (nalloc += 10) * sizeof (char *)); |
| 261 |
#ifdef DIRECTORY_ADD_SLASH |
| 262 |
if ((entry.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) != 0) |
| 263 |
strcat(filename, "/"); |
| 264 |
#endif |
| 265 |
ourarray[nentries++] = copystr(filename); |
| 266 |
} while (FindNextFile(findHand, &entry)); |
| 267 |
FindClose(findHand); |
| 268 |
ourarray[nentries] = NULL; |
| 269 |
|
| 270 |
if (sorter != (int (*)(const void *, const void *))NULL) |
| 271 |
qsort((char *) ourarray, nentries, sizeof (char **), sorter); |
| 272 |
*nmptr = ourarray; |
| 273 |
|
| 274 |
return nentries; |
| 275 |
} |
| 276 |
|
| 277 |
#endif /* WIN32 */ |
| 278 |
|
| 279 |
void |
| 280 |
freedir(char ***nmptr, int nentries) |
| 281 |
{ |
| 282 |
char **ourarray = *nmptr; |
| 283 |
|
| 284 |
while (--nentries >= 0) |
| 285 |
free(*ourarray++); |
| 286 |
free(*nmptr); |
| 287 |
*nmptr = NULL; |
| 288 |
} |
| 289 |
#endif /* F_COMPLETION */ |