1 | // |
---|
2 | // "$Id$" |
---|
3 | // |
---|
4 | // Directory detection routines for the Fast Light Tool Kit (FLTK). |
---|
5 | // |
---|
6 | // Copyright 1998-2005 by Bill Spitzak and others. |
---|
7 | // |
---|
8 | // This library is free software; you can redistribute it and/or |
---|
9 | // modify it under the terms of the GNU Library General Public |
---|
10 | // License as published by the Free Software Foundation; either |
---|
11 | // version 2 of the License, or (at your option) any later version. |
---|
12 | // |
---|
13 | // This library is distributed in the hope that it will be useful, |
---|
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | // Library General Public License for more details. |
---|
17 | // |
---|
18 | // You should have received a copy of the GNU Library General Public |
---|
19 | // License along with this library; if not, write to the Free Software |
---|
20 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
---|
21 | // USA. |
---|
22 | // |
---|
23 | // Please report all bugs and problems on the following page: |
---|
24 | // |
---|
25 | // http://www.fltk.org/str.php |
---|
26 | // |
---|
27 | |
---|
28 | // Used by fl_file_chooser |
---|
29 | |
---|
30 | #include "flstring.h" |
---|
31 | #include <sys/types.h> |
---|
32 | #include <sys/stat.h> |
---|
33 | #include <ctype.h> |
---|
34 | #include <FL/filename.H> |
---|
35 | |
---|
36 | |
---|
37 | #if defined(WIN32) || defined(__EMX__) && !defined(__CYGWIN__) |
---|
38 | static inline int isdirsep(char c) {return c=='/' || c=='\\';} |
---|
39 | #else |
---|
40 | #define isdirsep(c) ((c)=='/') |
---|
41 | #endif |
---|
42 | |
---|
43 | int _fl_filename_isdir_quick(const char* n) { |
---|
44 | // Do a quick optimization for filenames with a trailing slash... |
---|
45 | if (*n && isdirsep(n[strlen(n) - 1])) return 1; |
---|
46 | return fl_filename_isdir(n); |
---|
47 | } |
---|
48 | |
---|
49 | int fl_filename_isdir(const char* n) { |
---|
50 | struct stat s; |
---|
51 | char fn[1024]; |
---|
52 | int length; |
---|
53 | |
---|
54 | length = strlen(n); |
---|
55 | |
---|
56 | #ifdef WIN32 |
---|
57 | // This workaround brought to you by the fine folks at Microsoft! |
---|
58 | // (read lots of sarcasm in that...) |
---|
59 | if (length < (int)(sizeof(fn) - 1)) { |
---|
60 | if (length < 4 && isalpha(n[0]) && n[1] == ':' && |
---|
61 | (isdirsep(n[2]) || !n[2])) { |
---|
62 | // Always use D:/ for drive letters |
---|
63 | fn[0] = n[0]; |
---|
64 | strcpy(fn + 1, ":/"); |
---|
65 | n = fn; |
---|
66 | } else if (length > 0 && isdirsep(n[length - 1])) { |
---|
67 | // Strip trailing slash from name... |
---|
68 | length --; |
---|
69 | memcpy(fn, n, length); |
---|
70 | fn[length] = '\0'; |
---|
71 | n = fn; |
---|
72 | } |
---|
73 | } |
---|
74 | #else |
---|
75 | // Matt: Just in case, we strip the slash for other operating |
---|
76 | // systems as well, avoid bugs by sloppy implementations |
---|
77 | // of "stat". |
---|
78 | if (length > 1 && isdirsep(n[length - 1])) { |
---|
79 | length --; |
---|
80 | memcpy(fn, n, length); |
---|
81 | fn[length] = '\0'; |
---|
82 | n = fn; |
---|
83 | } |
---|
84 | #endif |
---|
85 | |
---|
86 | return !stat(n, &s) && (s.st_mode&0170000)==0040000; |
---|
87 | } |
---|
88 | |
---|
89 | // |
---|
90 | // End of "$Id$". |
---|
91 | // |
---|