Fix new trailing slash usage, and restore correct OSX filesystem listing

(added filter to hide /dev and /.vol)

src/Fl_File_Browser.cxx:
    - Fl_File_Browser::load(): use getfsstat() API on OSX, and don't
      bother adding a trailing slash to directories since we already
      have them!

src/filename_list.cxx:
    - fl_filename_list(): Add 3 bytes (two possible slashes + nul byte)
      to temporary buffer, and document why we are using memcpy() instead
      of strcpy().

src/filename_isdir.cxx:
    - fl_filename_isdir(): Add check for trailing directory separator
      before wasting time doing a stat() call.



git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@4526 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
Michael R Sweet 2005-08-18 14:08:17 +00:00
parent eca40efb6e
commit c1ba87140e
3 changed files with 33 additions and 16 deletions

View File

@ -67,7 +67,6 @@
// CodeWarrior (__MWERKS__) gets its include paths confused, so we
// temporarily disable this...
#if defined(__APPLE__) && !defined(__MWERKS__)
# include <Carbon/Carbon.h>
# include <sys/param.h>
# include <sys/ucred.h>
# include <sys/mount.h>
@ -496,20 +495,34 @@ Fl_File_Browser::load(const char *directory,// I - Directory to load
num_files ++;
}
#elif defined(__APPLE__) && !defined(__MWERKS__)
// All mounted volumes are in a directory called '/Volumes/'
// This seems to be the case on international installations, too.
// MacOS X and Darwin use getfsstat() system call...
int numfs; // Number of file systems
struct statfs *fs; // Buffer for file system info
// We always have the root filesystem.
add("/", icon);
dirent **dir;
int n = fl_filename_list("/Volumes/", &dir, 0);
if (n>=0) {
int i;
for (i=0; i<n; i++) {
if (dir[i]->d_name[0]=='.') continue;
sprintf(filename, "/Volumes/%s", dir[i]->d_name);
// Get the mounted filesystems...
numfs = getfsstat(NULL, 0, MNT_NOWAIT);
if (numfs > 0) {
// We have file systems, get them...
fs = new struct statfs[numfs];
getfsstat(fs, sizeof(struct statfs) * numfs, MNT_NOWAIT);
// Add filesystems to the list...
for (i = 0; i < numfs; i ++) {
// Ignore "/", "/dev", and "/.vol"...
if (fs[i].f_mntonname[1] && strcmp(fs[i].f_mntonname, "/dev") &&
strcmp(fs[i].f_mntonname, "/.vol")) {
snprintf(filename, sizeof(filename), "%s/", fs[i].f_mntonname);
add(filename, icon);
free(dir[i]);
}
free(dir);
num_files ++;
}
// Free the memory used for the file system info array...
delete[] fs;
}
#else
//
@ -578,8 +591,7 @@ Fl_File_Browser::load(const char *directory,// I - Directory to load
return (0);
for (i = 0, num_dirs = 0; i < num_files; i ++) {
if (strcmp(files[i]->d_name, ".") &&
strcmp(files[i]->d_name, "./")) {
if (strcmp(files[i]->d_name, "./")) {
snprintf(filename, sizeof(filename), "%s/%s", directory_,
files[i]->d_name);

View File

@ -43,6 +43,9 @@ static inline int isdirsep(char c) {return c=='/' || c=='\\';}
int fl_filename_isdir(const char* n) {
struct stat s;
// Do a quick optimization for filenames with a trailing slash...
if (*n && isdirsep(n[strlen(n) - 1])) return 1;
#ifdef WIN32
char fn[1024];
int length;

View File

@ -79,7 +79,8 @@ int fl_filename_list(const char *d, dirent ***list,
#else
// append a '/' to all filenames that are directories
int i, dirlen = strlen(d);
char *fullname = (char*)malloc(dirlen+FL_PATH_MAX+2);
char *fullname = (char*)malloc(dirlen+FL_PATH_MAX+3); // Add enough extra for two /'s and a nul
// Use memcpy for speed since we already know the length of the string...
memcpy(fullname, d, dirlen+1);
char *name = fullname + dirlen;
if (name!=fullname && name[-1]!='/') *name++ = '/';
@ -87,6 +88,7 @@ int fl_filename_list(const char *d, dirent ***list,
dirent *de = (*list)[i];
int len = strlen(de->d_name);
if (de->d_name[len-1]=='/' || len>FL_PATH_MAX) continue;
// Use memcpy for speed since we already know the length of the string...
memcpy(name, de->d_name, len+1);
if (fl_filename_isdir(fullname)) {
if (len<FL_PATH_MAX) {