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,
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 ?
39 kpathsea_normalize_path(kpse
, fn
);
40 fa
= GetFileAttributes(fn
);
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()));
48 fprintf(stderr
, "path %s %s a directory\n",
49 fn
, (fa
& FILE_ATTRIBUTE_DIRECTORY
) ?
53 #endif /* KPSE_DEBUG */
54 return (fa
!= 0xFFFFFFFF && (fa
& FILE_ATTRIBUTE_DIRECTORY
));
57 return stat (fn
, &stats
) == 0 && S_ISDIR (stats
.st_mode
);
61 #if defined(KPSE_COMPAT_API)
65 return kpathsea_dir_p (kpse_def
, fn
);
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.
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);
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;
93 hash_ret
= hash_lookup (kpse
->link_table
, fn
);
96 if (KPATHSEA_DEBUG_P (KPSE_DEBUG_HASH
))
97 kpse
->debug_hash_lookup_int
= false;
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. */
104 memcpy(&nlinks
, hash_ret
, sizeof(nlinks
));
106 nlinks
= (long) *hash_ret
;
110 /* Insert it only if we have some informations about it. */
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
);
121 if (stat (fn
, &stats
) == 0 && S_ISDIR (stats
.st_mode
))
122 nlinks
= stats
.st_nlink
;
125 /* It's up to us to copy the value. */
126 hash_insert(&(kpse
->link_table
), xstrdup(fn
), (const_string
)nlinks
);
130 if (KPATHSEA_DEBUG_P (KPSE_DEBUG_STAT
))
131 DEBUGF2 ("dir_links(%s) => %ld\n", fn
, nlinks
);
135 /* In any case, return nlinks
136 (either 0, the value inserted or the value retrieved. */
140 #if defined (KPSE_COMPAT_API)
142 dir_links (const_string fn
, long nlinks
)
144 return kpathsea_dir_links (kpse_def
, fn
, nlinks
);