]> git.wh0rd.org - fontconfig.git/blame - src/fcdir.c
Segfault scanning non-font files. Disallow scan edit of user vars. (#8767)
[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 37
00f059e9
KP
38static FcBool
39FcFileScanFontConfig (FcFontSet *set,
40 FcBlanks *blanks,
41 const FcChar8 *file,
42 FcConfig *config)
24330d27 43{
00f059e9
KP
44 FcPattern *font;
45 FcBool ret = FcTrue;
46 int id;
47 int count = 0;
24330d27
KP
48
49 id = 0;
50 do
51 {
327a7fd4
KP
52 font = 0;
53 /*
eb0cf671 54 * Nothing in the cache, scan the file
327a7fd4 55 */
eb0cf671 56 if (FcDebug () & FC_DBG_SCAN)
24330d27 57 {
eb0cf671
PL
58 printf ("\tScanning file %s...", file);
59 fflush (stdout);
24330d27 60 }
eb0cf671
PL
61 font = FcFreeTypeQuery (file, id, blanks, &count);
62 if (FcDebug () & FC_DBG_SCAN)
63 printf ("done\n");
c2c6976d
KP
64
65 /*
66 * Edit pattern with user-defined rules
67 */
0f963b0d 68 if (font && config && !FcConfigSubstituteWithPat (config, font, NULL, FcMatchScan))
c2c6976d
KP
69 {
70 FcPatternDestroy (font);
71 font = NULL;
72 ret = FcFalse;
73 }
74
327a7fd4
KP
75 /*
76 * Add the font
77 */
4f27c1c0 78 if (font && (!config || FcConfigAcceptFont (config, font)))
24330d27 79 {
c2c6976d
KP
80 if (FcDebug() & FC_DBG_SCANV)
81 {
82 printf ("Final font pattern:\n");
83 FcPatternPrint (font);
84 }
24330d27
KP
85 if (!FcFontSetAdd (set, font))
86 {
87 FcPatternDestroy (font);
c2c6976d 88 font = NULL;
24330d27
KP
89 ret = FcFalse;
90 }
91 }
47b49bf1
KP
92 else if (font)
93 FcPatternDestroy (font);
24330d27
KP
94 id++;
95 } while (font && ret && id < count);
96 return ret;
97}
98
00f059e9
KP
99FcBool
100FcFileScanConfig (FcFontSet *set,
101 FcStrSet *dirs,
102 FcBlanks *blanks,
103 const FcChar8 *file,
00f059e9
KP
104 FcConfig *config)
105{
00f059e9
KP
106 if (FcFileIsDir (file))
107 return FcStrSetAdd (dirs, file);
108 else
109 return FcFileScanFontConfig (set, blanks, file, config);
110}
111
d47c9d6e
KP
112FcBool
113FcFileScan (FcFontSet *set,
114 FcStrSet *dirs,
00f059e9 115 FcFileCache *cache, /* XXX unused */
d47c9d6e
KP
116 FcBlanks *blanks,
117 const FcChar8 *file,
118 FcBool force)
119{
bc5e487f 120 return FcFileScanConfig (set, dirs, blanks, file, NULL);
d47c9d6e
KP
121}
122
e3c6d336
PL
123/*
124 * Strcmp helper that takes pointers to pointers, copied from qsort(3) manpage
125 */
e3c6d336
PL
126static int
127cmpstringp(const void *p1, const void *p2)
128{
129 return strcmp(* (char **) p1, * (char **) p2);
130}
131
c8d5753c 132/*
bc5e487f 133 * Scan the specified directory and construct a cache of its contents
c8d5753c 134 */
bc5e487f
KP
135FcCache *
136FcDirCacheScan (const FcChar8 *dir, FcConfig *config)
24330d27 137{
327a7fd4
KP
138 DIR *d;
139 struct dirent *e;
bc5e487f
KP
140 FcStrSet *files;
141 FcStrSet *dirs;
327a7fd4
KP
142 FcChar8 *file;
143 FcChar8 *base;
144 FcBool ret = FcTrue;
bc5e487f 145 FcFontSet *set;
eb0cf671 146 int i;
bc5e487f
KP
147 FcBlanks *blanks = FcConfigGetBlanks (config);
148 FcCache *cache = NULL;
24330d27 149
00f059e9 150 if (FcDebug () & FC_DBG_FONTSET)
bc5e487f 151 printf ("cache scan dir %s\n", dir);
00f059e9 152
9dac3c59 153 /* freed below */
bc5e487f 154 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
0d9e31c8
KP
155 if (!file) {
156 ret = FcFalse;
157 goto bail;
158 }
24330d27 159
bc5e487f 160 strcpy ((char *) file, (char *) dir);
ccb3e93b
KP
161 strcat ((char *) file, "/");
162 base = file + strlen ((char *) file);
24330d27 163
a8386abc 164 if (FcDebug () & FC_DBG_SCAN)
bc5e487f 165 printf ("\tScanning dir %s\n", dir);
a8386abc 166
bc5e487f 167 d = opendir ((char *) dir);
24330d27
KP
168 if (!d)
169 {
179c3995
KP
170 /* Don't complain about missing directories */
171 if (errno == ENOENT)
0d9e31c8
KP
172 ret = FcTrue;
173 else
174 ret = FcFalse;
175 goto bail_1;
24330d27 176 }
eb0cf671 177
bc5e487f
KP
178 set = FcFontSetCreate();
179 if (!set)
65448e8b
PL
180 {
181 ret = FcFalse;
182 goto bail0;
eb0cf671
PL
183 }
184
bc5e487f
KP
185 files = FcStrSetCreate ();
186 if (!files)
65448e8b
PL
187 {
188 ret = FcFalse;
189 goto bail1;
190 }
e3c6d336 191 while ((e = readdir (d)))
24330d27 192 {
179c3995 193 if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN)
24330d27 194 {
00f059e9 195 strcpy ((char *) base, (char *) e->d_name);
bc5e487f
KP
196 if (!FcStrSetAdd (files, file)) {
197 ret = FcFalse;
198 goto bail2;
65448e8b 199 }
24330d27
KP
200 }
201 }
bc5e487f 202
00f059e9 203 /*
bc5e487f 204 * Sort files to make things prettier
00f059e9 205 */
bc5e487f 206 qsort(files->strs, files->num, sizeof(FcChar8 *), cmpstringp);
00f059e9 207
bc5e487f
KP
208 dirs = FcStrSetCreate ();
209 if (!dirs)
210 goto bail2;
00f059e9 211
af180c40 212 /*
bc5e487f 213 * Scan file files to build font patterns
af180c40 214 */
bc5e487f
KP
215 for (i = 0; i < files->num; i++)
216 FcFileScanConfig (set, dirs, blanks, files->strs[i], config);
217
00f059e9 218 /*
bc5e487f 219 * Build the cache object
00f059e9 220 */
bc5e487f
KP
221 cache = FcDirCacheBuild (set, dir, dirs);
222 if (!cache)
223 goto bail3;
224
00f059e9 225 /*
bc5e487f 226 * Write out the cache file, ignoring any troubles
00f059e9 227 */
bc5e487f 228 FcDirCacheWrite (cache, config);
00f059e9
KP
229
230 bail3:
bc5e487f 231 FcStrSetDestroy (dirs);
65448e8b 232 bail2:
bc5e487f 233 FcStrSetDestroy (files);
65448e8b 234 bail1:
bc5e487f 235 FcFontSetDestroy (set);
65448e8b
PL
236
237 bail0:
238 closedir (d);
239
0d9e31c8 240 bail_1:
65448e8b 241 free (file);
0d9e31c8 242 bail:
bc5e487f
KP
243 return cache;
244}
245
246/*
247 * Read (or construct) the cache for a directory
248 */
249FcCache *
250FcDirCacheRead (const FcChar8 *dir, FcBool force, FcConfig *config)
251{
252 FcCache *cache = NULL;
bc5e487f 253
9b511b29
KP
254 if (config && !FcConfigAcceptFilename (config, dir))
255 return NULL;
bc5e487f
KP
256
257 /* Try to use existing cache file */
258 if (!force)
9b511b29 259 cache = FcDirCacheLoad (dir, config, NULL);
bc5e487f
KP
260
261 /* Not using existing cache file, construct new cache */
262 if (!cache)
9b511b29 263 cache = FcDirCacheScan (dir, config);
bc5e487f
KP
264
265 return cache;
266}
267
268FcBool
269FcDirScanConfig (FcFontSet *set,
270 FcStrSet *dirs,
271 FcBlanks *blanks,
272 const FcChar8 *dir,
273 FcBool force,
274 FcConfig *config)
275{
97c3d5b6 276 return FcFalse; /* XXX deprecated */
24330d27
KP
277}
278
d47c9d6e
KP
279FcBool
280FcDirScan (FcFontSet *set,
281 FcStrSet *dirs,
00f059e9 282 FcFileCache *cache, /* XXX unused */
d47c9d6e
KP
283 FcBlanks *blanks,
284 const FcChar8 *dir,
285 FcBool force)
286{
97c3d5b6 287 return FcFalse; /* XXX deprecated */
d47c9d6e
KP
288}
289
24330d27 290FcBool
2304e38f 291FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir)
24330d27 292{
bc5e487f 293 return FcFalse; /* XXX deprecated */
24330d27 294}
23816bf9
KP
295#define __fcdir__
296#include "fcaliastail.h"
297#undef __fcdir__