2 * fontconfig/test/test-issue107.c
4 * Copyright © 2000 Keith Packard
5 * Copyright © 2018 Akira TAGOH
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of the author(s) not be used in
12 * advertising or publicity pertaining to distribution of the software without
13 * specific, written prior permission. The authors make no
14 * representations about the suitability of this software for any purpose. It
15 * is provided "as is" without express or implied warranty.
17 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
23 * PERFORMANCE OF THIS SOFTWARE.
36 #ifndef HAVE_STRUCT_DIRENT_D_TYPE
37 #include <sys/types.h>
40 #include <fontconfig/fontconfig.h>
43 # define FC_DIR_SEPARATOR '\\'
44 # define FC_DIR_SEPARATOR_S "\\"
46 # define FC_DIR_SEPARATOR '/'
47 # define FC_DIR_SEPARATOR_S "/"
52 #define mkdir(path,mode) _mkdir(path)
55 setenv(const char *name, const char *value, int o)
57 size_t len = strlen(name) + strlen(value) + 1;
58 char *s = malloc(len+1);
61 snprintf(s, len, "%s=%s", name, value);
68 extern FcChar8 *FcConfigRealFilename (FcConfig *, FcChar8 *);
71 #define fc_mkdtemp mkdtemp
74 fc_mkdtemp (char *template)
76 if (!mktemp (template) || mkdir (template, 0700))
84 mkdir_p (const char *dir)
89 if (strlen (dir) == 0)
91 parent = (char *) FcStrDirname ((const FcChar8 *) dir);
94 if (access (parent, F_OK) == 0)
95 ret = mkdir (dir, 0755) == 0 && chmod (dir, 0755) == 0;
96 else if (access (parent, F_OK) == -1)
97 ret = mkdir_p (parent) && (mkdir (dir, 0755) == 0) && chmod (dir, 0755) == 0;
106 unlink_dirs (const char *dir)
108 DIR *d = opendir (dir);
110 size_t len = strlen (dir);
113 #ifndef HAVE_STRUCT_DIRENT_D_TYPE
119 while ((e = readdir (d)) != NULL)
123 if (strcmp (e->d_name, ".") == 0 ||
124 strcmp (e->d_name, "..") == 0)
126 l = strlen (e->d_name) + 1;
129 n = malloc (l + len + 1);
136 n[len] = FC_DIR_SEPARATOR;
137 strcpy (&n[len + 1], e->d_name);
138 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
139 if (e->d_type == DT_DIR)
141 if (stat (n, &statb) == -1)
143 fprintf (stderr, "E: %s\n", n);
147 if (S_ISDIR (statb.st_mode))
150 if (!unlink_dirs (n))
152 fprintf (stderr, "E: %s\n", n);
159 if (unlink (n) == -1)
161 fprintf (stderr, "E: %s\n", n);
171 if (rmdir (dir) == -1)
173 fprintf (stderr, "E: %s\n", dir);
183 FcConfig *cfg = FcConfigCreate ();
184 char *basedir = NULL, template[512] = "/tmp/fc107-XXXXXX";
185 char *sysroot = NULL, systempl[512] = "/tmp/fc107-XXXXXX";
186 FcChar8 *d = NULL, *dd = NULL;
188 const FcChar8 *doc = (const FcChar8 *) ""
197 basedir = fc_mkdtemp (template);
200 fprintf (stderr, "%s: %s\n", template, strerror (errno));
204 sysroot = fc_mkdtemp (systempl);
207 fprintf (stderr, "%s: %s\n", systempl, strerror (errno));
211 fprintf (stderr, "D: Creating %s\n", basedir);
213 len = strlen ((const char *) doc) + strlen (basedir) + 1;
215 snprintf ((char *) dd, len, (char *) doc, basedir);
216 if (!FcConfigParseAndLoadFromMemory (cfg, dd, FcFalse))
218 fprintf (stderr, "%s: Unable to load a config\n", basedir);
222 c = FcDirCacheRead ((const FcChar8 *) basedir, FcFalse, cfg);
223 FcDirCacheUnload (c);
226 if (!FcConfigUptoDate (cfg))
228 fprintf (stderr, "updated. need to reload.\n");
231 setenv ("FONTCONFIG_SYSROOT", sysroot, 1);
232 fprintf (stderr, "D: Creating %s\n", sysroot);
235 d = FcStrBuildFilename ((const FcChar8 *) sysroot, basedir, NULL);
236 fprintf (stderr, "D: Creating %s\n", d);
237 mkdir_p ((const char *) d);
241 len = strlen ((const char *) doc) + strlen (basedir) + 1;
243 snprintf ((char *) dd, len, (char *) doc, basedir);
244 if (!FcConfigParseAndLoadFromMemory (cfg, dd, FcFalse))
246 fprintf (stderr, "%s: Unable to load a config\n", basedir);
250 c = FcDirCacheRead ((const FcChar8 *) basedir, FcFalse, cfg);
251 FcDirCacheUnload (c);
254 if (!FcConfigUptoDate (cfg))
256 fprintf (stderr, "updated. need to reload (sysroot)\n");
261 fprintf (stderr, "Cleaning up\n");
263 unlink_dirs (basedir);
265 unlink_dirs (sysroot);
268 FcConfigDestroy (cfg);