]> git.wh0rd.org - fontconfig.git/blame - src/fcdir.c
Fix double free (spotted by Coverity, CID #1965).
[fontconfig.git] / src / fcdir.c
CommitLineData
24330d27 1/*
4bd4418a 2 * $RCSId: xc/lib/fontconfig/src/fcdir.c,v 1.9 2002/08/31 22:17:32 keithp Exp $
24330d27 3 *
46b51147 4 * Copyright © 2000 Keith Packard
24330d27
KP
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
15 *
16 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
23 */
24
24330d27 25#include "fcint.h"
179c3995
KP
26#include <dirent.h>
27
2d9c79c0 28FcBool
179c3995
KP
29FcFileIsDir (const FcChar8 *file)
30{
31 struct stat statb;
24330d27 32
179c3995
KP
33 if (stat ((const char *) file, &statb) != 0)
34 return FcFalse;
35 return S_ISDIR(statb.st_mode);
36}
24330d27
KP
37
38FcBool
d47c9d6e
KP
39FcFileScanConfig (FcFontSet *set,
40 FcStrSet *dirs,
41 FcGlobalCache *cache,
42 FcBlanks *blanks,
43 const FcChar8 *file,
44 FcBool force,
45 FcConfig *config)
24330d27 46{
327a7fd4 47 int id;
327a7fd4
KP
48 FcPattern *font;
49 FcBool ret = FcTrue;
327a7fd4 50 int count = 0;
24330d27 51
d47c9d6e
KP
52 if (config && !FcConfigAcceptFilename (config, file))
53 return FcTrue;
54
98592bbb
PL
55 if (FcFileIsDir (file))
56 return FcStrSetAdd (dirs, file);
57
24330d27
KP
58 id = 0;
59 do
60 {
327a7fd4
KP
61 font = 0;
62 /*
eb0cf671 63 * Nothing in the cache, scan the file
327a7fd4 64 */
eb0cf671 65 if (FcDebug () & FC_DBG_SCAN)
24330d27 66 {
eb0cf671
PL
67 printf ("\tScanning file %s...", file);
68 fflush (stdout);
24330d27 69 }
eb0cf671
PL
70 font = FcFreeTypeQuery (file, id, blanks, &count);
71 if (FcDebug () & FC_DBG_SCAN)
72 printf ("done\n");
327a7fd4
KP
73 /*
74 * Add the font
75 */
4f27c1c0 76 if (font && (!config || FcConfigAcceptFont (config, font)))
24330d27
KP
77 {
78 if (!FcFontSetAdd (set, font))
79 {
80 FcPatternDestroy (font);
81 font = 0;
82 ret = FcFalse;
83 }
84 }
47b49bf1
KP
85 else if (font)
86 FcPatternDestroy (font);
24330d27
KP
87 id++;
88 } while (font && ret && id < count);
89 return ret;
90}
91
d47c9d6e
KP
92FcBool
93FcFileScan (FcFontSet *set,
94 FcStrSet *dirs,
95 FcGlobalCache *cache,
96 FcBlanks *blanks,
97 const FcChar8 *file,
98 FcBool force)
99{
100 return FcFileScanConfig (set, dirs, cache, blanks, file, force, 0);
101}
102
e3c6d336
PL
103/*
104 * Strcmp helper that takes pointers to pointers, copied from qsort(3) manpage
105 */
106
107static int
108cmpstringp(const void *p1, const void *p2)
109{
110 return strcmp(* (char **) p1, * (char **) p2);
111}
112
c8d5753c
KP
113/*
114 * Scan 'dir', adding font files to 'set' and
115 * subdirectories to 'dirs'
116 */
117
24330d27 118FcBool
d47c9d6e
KP
119FcDirScanConfig (FcFontSet *set,
120 FcStrSet *dirs,
121 FcGlobalCache *cache,
122 FcBlanks *blanks,
123 const FcChar8 *dir,
124 FcBool force,
125 FcConfig *config)
24330d27 126{
327a7fd4
KP
127 DIR *d;
128 struct dirent *e;
e3c6d336
PL
129 FcChar8 **dirlist;
130 int dirlistlen, dirlistalloc;
327a7fd4 131 FcChar8 *file;
5c3deb29 132 const FcChar8 *d_can = 0;
327a7fd4
KP
133 FcChar8 *base;
134 FcBool ret = FcTrue;
eb0cf671
PL
135 FcFontSet *tmpSet;
136 int i;
24330d27 137
d47c9d6e
KP
138 if (config && !FcConfigAcceptFilename (config, dir))
139 return FcTrue;
140
58bdd296 141 if (config)
5c3deb29
PL
142 d_can = FcConfigNormalizeFontDir (config, dir);
143 if (d_can)
144 dir = d_can;
58bdd296 145
327a7fd4
KP
146 if (!force)
147 {
327a7fd4 148 /*
f21f40f3 149 * Check ~/.fonts.cache-<version> file
327a7fd4 150 */
8245771d 151 if (cache && FcGlobalCacheReadDir (set, dirs, cache, (char *)dir, config))
327a7fd4 152 return FcTrue;
2eb84374 153
530e66b0
PL
154 if (FcDirCacheValid (dir) &&
155 FcDirCacheHasCurrentArch (dir) &&
156 FcDirCacheRead (set, dirs, dir, config))
2eb84374 157 return FcTrue;
327a7fd4
KP
158 }
159
9dac3c59 160 /* freed below */
179c3995 161 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
24330d27
KP
162 if (!file)
163 return FcFalse;
164
ccb3e93b
KP
165 strcpy ((char *) file, (char *) dir);
166 strcat ((char *) file, "/");
167 base = file + strlen ((char *) file);
24330d27 168
a8386abc
KP
169 if (FcDebug () & FC_DBG_SCAN)
170 printf ("\tScanning dir %s\n", dir);
171
ccb3e93b 172 d = opendir ((char *) dir);
24330d27
KP
173 if (!d)
174 {
175 free (file);
179c3995
KP
176 /* Don't complain about missing directories */
177 if (errno == ENOENT)
178 return FcTrue;
24330d27
KP
179 return FcFalse;
180 }
eb0cf671
PL
181
182 tmpSet = FcFontSetCreate();
183 if (!tmpSet)
184 {
185 free (file);
186 return FcFalse;
187 }
188
e3c6d336
PL
189 dirlistlen = 0;
190 dirlistalloc = 8;
191 dirlist = malloc(dirlistalloc * sizeof(FcChar8 *));
192 if (!dirlist)
193 return FcFalse;
194 while ((e = readdir (d)))
24330d27 195 {
179c3995 196 if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN)
24330d27 197 {
e3c6d336
PL
198 if (dirlistlen == dirlistalloc)
199 {
200 dirlistalloc *= 2;
201 dirlist = realloc(dirlist, dirlistalloc * sizeof(FcChar8 *));
202 if (!dirlist)
203 return FcFalse;
204 }
205 dirlist[dirlistlen] = malloc(strlen (e->d_name) + 1);
206 if (!dirlist[dirlistlen])
207 return FcFalse;
3a342c5a 208 strcpy((char *)dirlist[dirlistlen], e->d_name);
e3c6d336 209 dirlistlen++;
24330d27
KP
210 }
211 }
e3c6d336
PL
212 qsort(dirlist, dirlistlen, sizeof(FcChar8 *), cmpstringp);
213 i = 0;
214 while (ret && i < dirlistlen)
215 {
216 strcpy ((char *) base, (char *) dirlist[i]);
217 ret = FcFileScanConfig (tmpSet, dirs, cache, blanks, file, force, config);
218 i++;
219 }
220 for (i = 0; i < dirlistlen; i++)
221 free(dirlist[i]);
222 free (dirlist);
24330d27
KP
223 free (file);
224 closedir (d);
c8d5753c
KP
225 /*
226 * Now that the directory has been scanned,
227 * add the cache entry
228 */
327a7fd4 229 if (ret && cache)
971cf180 230 FcGlobalCacheUpdate (cache, dirs, (char *)dir, tmpSet, config);
eb0cf671
PL
231
232 for (i = 0; i < tmpSet->nfont; i++)
233 FcFontSetAdd (set, tmpSet->fonts[i]);
234
235 if (tmpSet->fonts)
236 {
237 FcMemFree (FC_MEM_FONTPTR, tmpSet->sfont * sizeof (FcPattern *));
238 free (tmpSet->fonts);
239 }
240 FcMemFree (FC_MEM_FONTSET, sizeof (FcFontSet));
241 free (tmpSet);
327a7fd4 242
24330d27
KP
243 return ret;
244}
245
d47c9d6e
KP
246FcBool
247FcDirScan (FcFontSet *set,
248 FcStrSet *dirs,
249 FcGlobalCache *cache,
250 FcBlanks *blanks,
251 const FcChar8 *dir,
252 FcBool force)
253{
254 return FcDirScanConfig (set, dirs, cache, blanks, dir, force, 0);
255}
256
24330d27 257FcBool
2304e38f 258FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir)
24330d27 259{
2304e38f 260 return FcDirCacheWrite (set, dirs, dir);
24330d27 261}