Fallback to lstat() in case the filesystem doesn't support d_type in struct dirent
[platform/upstream/fontconfig.git] / src / fcinit.c
1 /*
2  * fontconfig/src/fcinit.c
3  *
4  * Copyright © 2001 Keith Packard
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 the author(s) not be used in
11  * advertising or publicity pertaining to distribution of the software without
12  * specific, written prior permission.  The authors make 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  * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18  * EVENT SHALL THE AUTHOR(S) 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
25 #include "fcint.h"
26 #include <stdlib.h>
27
28 #if defined(FC_ATOMIC_INT_NIL)
29 #pragma message("Could not find any system to define atomic_int macros, library may NOT be thread-safe.")
30 #endif
31 #if defined(FC_MUTEX_IMPL_NIL)
32 #pragma message("Could not find any system to define mutex macros, library may NOT be thread-safe.")
33 #endif
34 #if defined(FC_ATOMIC_INT_NIL) || defined(FC_MUTEX_IMPL_NIL)
35 #pragma message("To suppress these warnings, define FC_NO_MT.")
36 #endif
37
38 static FcConfig *
39 FcInitFallbackConfig (void)
40 {
41     FcConfig    *config;
42
43     config = FcConfigCreate ();
44     if (!config)
45         goto bail0;
46     if (!FcConfigAddDir (config, (FcChar8 *) FC_DEFAULT_FONTS))
47         goto bail1;
48     if (!FcConfigAddCacheDir (config, (FcChar8 *) FC_CACHEDIR))
49         goto bail1;
50     return config;
51
52 bail1:
53     FcConfigDestroy (config);
54 bail0:
55     return 0;
56 }
57
58 int
59 FcGetVersion (void)
60 {
61     return FC_VERSION;
62 }
63
64 /*
65  * Load the configuration files
66  */
67 FcConfig *
68 FcInitLoadOwnConfig (FcConfig *config)
69 {
70     if (!config)
71     {
72         config = FcConfigCreate ();
73         if (!config)
74             return NULL;
75     }
76
77     FcInitDebug ();
78
79     if (!FcConfigParseAndLoad (config, 0, FcTrue))
80     {
81         FcConfigDestroy (config);
82         return FcInitFallbackConfig ();
83     }
84
85     if (config->cacheDirs && config->cacheDirs->num == 0)
86     {
87         FcChar8 *prefix, *p;
88         size_t plen;
89
90         fprintf (stderr,
91                  "Fontconfig warning: no <cachedir> elements found. Check configuration.\n");
92         fprintf (stderr,
93                  "Fontconfig warning: adding <cachedir>%s</cachedir>\n",
94                  FC_CACHEDIR);
95         prefix = FcConfigXdgCacheHome ();
96         if (!prefix)
97             goto bail;
98         plen = strlen ((const char *)prefix);
99         p = realloc (prefix, plen + 12);
100         if (!p)
101             goto bail;
102         prefix = p;
103         memcpy (&prefix[plen], FC_DIR_SEPARATOR_S "fontconfig", 11);
104         prefix[plen + 11] = 0;
105         fprintf (stderr,
106                  "Fontconfig warning: adding <cachedir prefix=\"xdg\">fontconfig</cachedir>\n");
107
108         if (!FcConfigAddCacheDir (config, (FcChar8 *) FC_CACHEDIR) ||
109             !FcConfigAddCacheDir (config, (FcChar8 *) prefix))
110         {
111           bail:
112             fprintf (stderr,
113                      "Fontconfig error: out of memory");
114             if (prefix)
115                 FcStrFree (prefix);
116             FcConfigDestroy (config);
117             return FcInitFallbackConfig ();
118         }
119         FcStrFree (prefix);
120     }
121
122     return config;
123 }
124
125 FcConfig *
126 FcInitLoadConfig (void)
127 {
128     return FcInitLoadOwnConfig (NULL);
129 }
130
131 /*
132  * Load the configuration files and scan for available fonts
133  */
134 FcConfig *
135 FcInitLoadOwnConfigAndFonts (FcConfig *config)
136 {
137     config = FcInitLoadOwnConfig (config);
138     if (!config)
139         return 0;
140     if (!FcConfigBuildFonts (config))
141     {
142         FcConfigDestroy (config);
143         return 0;
144     }
145     return config;
146 }
147
148 FcConfig *
149 FcInitLoadConfigAndFonts (void)
150 {
151     return FcInitLoadOwnConfigAndFonts (NULL);
152 }
153
154 /*
155  * Initialize the default library configuration
156  */
157 FcBool
158 FcInit (void)
159 {
160     return FcConfigInit ();
161 }
162
163 /*
164  * Free all library-allocated data structures.
165  */
166 void
167 FcFini (void)
168 {
169     FcConfigFini ();
170     FcCacheFini ();
171     FcDefaultFini ();
172 }
173
174 /*
175  * Reread the configuration and available font lists
176  */
177 FcBool
178 FcInitReinitialize (void)
179 {
180     FcConfig    *config;
181
182     config = FcInitLoadConfigAndFonts ();
183     if (!config)
184         return FcFalse;
185     return FcConfigSetCurrent (config);
186 }
187
188 FcBool
189 FcInitBringUptoDate (void)
190 {
191     FcConfig    *config = FcConfigGetCurrent ();
192     time_t      now;
193
194     /*
195      * rescanInterval == 0 disables automatic up to date
196      */
197     if (config->rescanInterval == 0)
198         return FcTrue;
199     /*
200      * Check no more often than rescanInterval seconds
201      */
202     now = time (0);
203     if (config->rescanTime + config->rescanInterval - now > 0)
204         return FcTrue;
205     /*
206      * If up to date, don't reload configuration
207      */
208     if (FcConfigUptoDate (0))
209         return FcTrue;
210     return FcInitReinitialize ();
211 }
212
213 #define __fcinit__
214 #include "fcaliastail.h"
215 #undef __fcinit__