beta-0.89.2
[luatex.git] / source / texk / kpathsea / dir.c
blobf2b4eaf3fb1a1422b0b13ca222e5f22ab38041a5
1 /* dir.c: directory operations.
3 Copyright 1992, 1993, 1994, 1995, 2008-2012 Karl Berry.
4 Copyright 2000, 2002, 2005 Olaf Weber.
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with this library; if not, see <http://www.gnu.org/licenses/>. */
19 #include <kpathsea/config.h>
21 #include <kpathsea/c-dir.h>
22 #include <kpathsea/c-stat.h>
23 #include <kpathsea/hash.h>
24 #include <kpathsea/pathsearch.h>
26 /* Return true if FN is a directory or a symlink to a directory,
27 false if not. */
29 boolean
30 kpathsea_dir_p (kpathsea kpse, string fn)
32 /* FIXME : using the stat() replacement in gnuw32,
33 we could avoid this win32 specific code. However,
34 I wonder if it would be as fast as this one is ?
36 #ifdef WIN32
37 int fa;
39 kpathsea_normalize_path(kpse, fn);
40 fa = GetFileAttributes(fn);
42 #ifdef KPSE_DEBUG
43 if (KPATHSEA_DEBUG_P (KPSE_DEBUG_STAT)) {
44 if (fa == 0xFFFFFFFF) {
45 fprintf(stderr, "failed to get file attributes for %s (%d)\n",
46 fn, (int)(GetLastError()));
47 } else {
48 fprintf(stderr, "path %s %s a directory\n",
49 fn , (fa & FILE_ATTRIBUTE_DIRECTORY) ?
50 "is" : "is not");
53 #endif /* KPSE_DEBUG */
54 return (fa != 0xFFFFFFFF && (fa & FILE_ATTRIBUTE_DIRECTORY));
55 #else /* !WIN32 */
56 struct stat stats;
57 return stat (fn, &stats) == 0 && S_ISDIR (stats.st_mode);
58 #endif /* !WIN32 */
61 #if defined(KPSE_COMPAT_API)
62 boolean
63 dir_p (string fn)
65 return kpathsea_dir_p (kpse_def, fn);
67 #endif
71 Return -1 if FN isn't a directory, else its number of links.
72 Duplicate the call to stat; no need to incur overhead of a function
73 call for that little bit of cleanliness.
75 The process is a bit different under Win32 : the first call
76 memoizes the nlinks value, the following ones retrieve it.
78 int
79 kpathsea_dir_links (kpathsea kpse, const_string fn, long nlinks)
81 const_string *hash_ret;
83 if (kpse->link_table.size == 0)
84 kpse->link_table = hash_create (457);
86 #ifdef KPSE_DEBUG
87 /* This is annoying, but since we're storing integers as pointers, we
88 can't print them as strings. */
89 if (KPATHSEA_DEBUG_P (KPSE_DEBUG_HASH))
90 kpse->debug_hash_lookup_int = true;
91 #endif
93 hash_ret = hash_lookup (kpse->link_table, fn);
95 #ifdef KPSE_DEBUG
96 if (KPATHSEA_DEBUG_P (KPSE_DEBUG_HASH))
97 kpse->debug_hash_lookup_int = false;
98 #endif
100 /* Have to cast the int we need to/from the const_string that the hash
101 table stores for values. Let's hope an int fits in a pointer. */
102 if (hash_ret) {
103 #ifdef WIN32
104 memcpy(&nlinks, hash_ret, sizeof(nlinks));
105 #else
106 nlinks = (long) *hash_ret;
107 #endif
108 } else {
109 #ifdef WIN32
110 /* Insert it only if we have some informations about it. */
111 if (nlinks) {
112 char str_nlinks[sizeof(nlinks)+1];
113 memcpy(str_nlinks, (char *)&nlinks, sizeof(nlinks));
114 str_nlinks[sizeof(nlinks)] = '\0';
115 /* It's up to us to copy the value. */
116 hash_insert(&(kpse->link_table), xstrdup (fn),
117 (const_string) str_nlinks);
119 #else
120 struct stat stats;
121 if (stat (fn, &stats) == 0 && S_ISDIR (stats.st_mode))
122 nlinks = stats.st_nlink;
123 else
124 nlinks = -1;
125 /* It's up to us to copy the value. */
126 hash_insert(&(kpse->link_table), xstrdup(fn), (const_string)nlinks);
127 #endif
129 #ifdef KPSE_DEBUG
130 if (KPATHSEA_DEBUG_P (KPSE_DEBUG_STAT))
131 DEBUGF2 ("dir_links(%s) => %ld\n", fn, nlinks);
132 #endif
135 /* In any case, return nlinks
136 (either 0, the value inserted or the value retrieved. */
137 return nlinks;
140 #if defined (KPSE_COMPAT_API)
142 dir_links (const_string fn, long nlinks)
144 return kpathsea_dir_links (kpse_def, fn, nlinks);
146 #endif