1 /* LIBGIMP - The GIMP Library
2 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
3 *
4 * gimpenv.c
5 * Copyright (C) 1999 Tor Lillqvist <tml@iki.fi>
6 *
7 * This library is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library. If not, see
19 * <https://www.gnu.org/licenses/>.
20 */
21
22 #include "config.h"
23
24 #include <errno.h>
25 #include <string.h>
26 #include <sys/types.h>
27
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31
32 #ifdef PLATFORM_OSX
33 #include <AppKit/AppKit.h>
34 #endif
35
36 #include <gio/gio.h>
37 #include <glib/gstdio.h>
38
39 #undef GIMP_DISABLE_DEPRECATED
40 #include "gimpbasetypes.h"
41
42 #define __GIMP_ENV_C__
43 #include "gimpenv.h"
44 #include "gimpversion.h"
45 #include "gimpreloc.h"
46
47 #ifdef G_OS_WIN32
48 #define STRICT
49 #define WIN32_LEAN_AND_MEAN
50 #include <windows.h>
51 #include <io.h>
52 #ifndef S_IWUSR
53 # define S_IWUSR _S_IWRITE
54 #endif
55 #ifndef S_IWGRP
56 #define S_IWGRP (_S_IWRITE>>3)
57 #define S_IWOTH (_S_IWRITE>>6)
58 #endif
59 #ifndef S_ISDIR
60 # define __S_ISTYPE(mode, mask) (((mode) & _S_IFMT) == (mask))
61 # define S_ISDIR(mode) __S_ISTYPE((mode), _S_IFDIR)
62 #endif
63 #define uid_t gint
64 #define gid_t gint
65 #define geteuid() 0
66 #define getegid() 0
67
68 #include <shlobj.h>
69
70 /* Constant available since Shell32.dll 4.72 */
71 #ifndef CSIDL_APPDATA
72 #define CSIDL_APPDATA 0x001a
73 #endif
74
75 #endif
76
77
78 /**
79 * SECTION: gimpenv
80 * @title: gimpenv
81 * @short_description: Functions to access the GIMP environment.
82 *
83 * A set of functions to find the locations of GIMP's data directories
84 * and configuration files.
85 **/
86
87
88 static gchar * gimp_env_get_dir (const gchar *gimp_env_name,
89 const gchar *compile_time_dir,
90 const gchar *relative_subdir);
91 #ifdef G_OS_WIN32
92 static gchar * get_special_folder (gint csidl);
93 #endif
94
95
96 const guint gimp_major_version = GIMP_MAJOR_VERSION;
97 const guint gimp_minor_version = GIMP_MINOR_VERSION;
98 const guint gimp_micro_version = GIMP_MICRO_VERSION;
99
100
101 /**
102 * gimp_env_init:
103 * @plug_in: must be %TRUE if this function is called from a plug-in
104 *
105 * You don't need to care about this function. It is being called for
106 * you automatically (by means of the MAIN() macro that every plug-in
107 * runs). Calling it again will cause a fatal error.
108 *
109 * Since: 2.4
110 */
111 void
gimp_env_init(gboolean plug_in)112 gimp_env_init (gboolean plug_in)
113 {
114 static gboolean gimp_env_initialized = FALSE;
115 const gchar *data_home = g_get_user_data_dir ();
116
117 if (gimp_env_initialized)
118 g_error ("gimp_env_init() must only be called once!");
119
120 gimp_env_initialized = TRUE;
121
122 #ifndef G_OS_WIN32
123 if (plug_in)
124 {
125 _gimp_reloc_init_lib (NULL);
126 }
127 else if (_gimp_reloc_init (NULL))
128 {
129 /* Set $LD_LIBRARY_PATH to ensure that plugins can be loaded. */
130
131 const gchar *ldpath = g_getenv ("LD_LIBRARY_PATH");
132 gchar *libdir = g_build_filename (gimp_installation_directory (),
133 "lib",
134 NULL);
135
136 if (ldpath && *ldpath)
137 {
138 gchar *tmp = g_strconcat (libdir, ":", ldpath, NULL);
139
140 g_setenv ("LD_LIBRARY_PATH", tmp, TRUE);
141
142 g_free (tmp);
143 }
144 else
145 {
146 g_setenv ("LD_LIBRARY_PATH", libdir, TRUE);
147 }
148
149 g_free (libdir);
150 }
151 #endif
152
153 /* The user data directory (XDG_DATA_HOME on Unix) is used to store
154 * various data, like crash logs (win32) or recently used file history
155 * (by GTK+). Yet it may be absent, in particular on non-Linux
156 * platforms. Make sure it exists.
157 */
158 if (! g_file_test (data_home, G_FILE_TEST_IS_DIR))
159 {
160 if (g_mkdir_with_parents (data_home, S_IRUSR | S_IWUSR | S_IXUSR) != 0)
161 {
162 g_warning ("Failed to create the data directory '%s': %s",
163 data_home, g_strerror (errno));
164 }
165 }
166 }
167
168 /**
169 * gimp_directory:
170 *
171 * Returns the user-specific GIMP settings directory. If the
172 * environment variable GIMP2_DIRECTORY exists, it is used. If it is
173 * an absolute path, it is used as is. If it is a relative path, it
174 * is taken to be a subdirectory of the home directory. If it is a
175 * relative path, and no home directory can be determined, it is taken
176 * to be a subdirectory of gimp_data_directory().
177 *
178 * The usual case is that no GIMP2_DIRECTORY environment variable
179 * exists, and then we use the GIMPDIR subdirectory of the local
180 * configuration directory:
181 *
182 * - UNIX: $XDG_CONFIG_HOME (defaults to $HOME/.config/)
183 *
184 * - Windows: CSIDL_APPDATA
185 *
186 * - OSX (UNIX exception): the Application Support Directory.
187 *
188 * If neither the configuration nor home directory exist,
189 * g_get_user_config_dir() will return {tmp}/{user_name}/.config/ where
190 * the temporary directory {tmp} and the {user_name} are determined
191 * according to platform rules.
192 *
193 * In any case, we always return some non-empty string, whether it
194 * corresponds to an existing directory or not.
195 *
196 * In config files such as gimprc, the string ${gimp_dir} expands to
197 * this directory.
198 *
199 * The returned string is owned by GIMP and must not be modified or
200 * freed. The returned string is in the encoding used for filenames by
201 * GLib, which isn't necessarily UTF-8 (on Windows it is always
202 * UTF-8.)
203 *
204 * Returns: The user-specific GIMP settings directory.
205 **/
206 const gchar *
gimp_directory(void)207 gimp_directory (void)
208 {
209 static gchar *gimp_dir = NULL;
210 static gchar *last_env_gimp_dir = NULL;
211
212 const gchar *env_gimp_dir;
213
214 env_gimp_dir = g_getenv ("GIMP2_DIRECTORY");
215
216 if (gimp_dir)
217 {
218 gboolean gimp2_directory_changed = FALSE;
219
220 /* We have constructed the gimp_dir already. We can return
221 * gimp_dir unless some parameter gimp_dir depends on has
222 * changed. For now we just check for changes to GIMP2_DIRECTORY
223 */
224 gimp2_directory_changed =
225 (env_gimp_dir == NULL &&
226 last_env_gimp_dir != NULL) ||
227 (env_gimp_dir != NULL &&
228 last_env_gimp_dir == NULL) ||
229 (env_gimp_dir != NULL &&
230 last_env_gimp_dir != NULL &&
231 strcmp (env_gimp_dir, last_env_gimp_dir) != 0);
232
233 if (! gimp2_directory_changed)
234 {
235 return gimp_dir;
236 }
237 else
238 {
239 /* Free the old gimp_dir and go on to update it */
240 g_free (gimp_dir);
241 gimp_dir = NULL;
242 }
243 }
244
245 /* Remember the GIMP2_DIRECTORY to next invocation so we can check
246 * if it changes
247 */
248 g_free (last_env_gimp_dir);
249 last_env_gimp_dir = g_strdup (env_gimp_dir);
250
251 if (env_gimp_dir)
252 {
253 if (g_path_is_absolute (env_gimp_dir))
254 {
255 gimp_dir = g_strdup (env_gimp_dir);
256 }
257 else
258 {
259 const gchar *home_dir = g_get_home_dir ();
260
261 if (home_dir)
262 gimp_dir = g_build_filename (home_dir, env_gimp_dir, NULL);
263 else
264 gimp_dir = g_build_filename (gimp_data_directory (), env_gimp_dir, NULL);
265 }
266 }
267 else if (g_path_is_absolute (GIMPDIR))
268 {
269 gimp_dir = g_strdup (GIMPDIR);
270 }
271 else
272 {
273 #ifdef PLATFORM_OSX
274
275 NSAutoreleasePool *pool;
276 NSArray *path;
277 NSString *library_dir;
278
279 pool = [[NSAutoreleasePool alloc] init];
280
281 path = NSSearchPathForDirectoriesInDomains (NSApplicationSupportDirectory,
282 NSUserDomainMask, YES);
283 library_dir = [path objectAtIndex:0];
284
285 gimp_dir = g_build_filename ([library_dir UTF8String],
286 GIMPDIR, GIMP_USER_VERSION, NULL);
287
288 [pool drain];
289
290 #elif defined G_OS_WIN32
291
292 gchar *conf_dir = get_special_folder (CSIDL_APPDATA);
293
294 gimp_dir = g_build_filename (conf_dir,
295 GIMPDIR, GIMP_USER_VERSION, NULL);
296 g_free(conf_dir);
297
298 #else /* UNIX */
299
300 /* g_get_user_config_dir () always returns a path as a non-null
301 * and non-empty string
302 */
303 gimp_dir = g_build_filename (g_get_user_config_dir (),
304 GIMPDIR, GIMP_USER_VERSION, NULL);
305
306 #endif /* PLATFORM_OSX */
307 }
308
309 return gimp_dir;
310 }
311
312 #ifdef G_OS_WIN32
313
314 /* Taken from glib 2.35 code. */
315 static gchar *
get_special_folder(int csidl)316 get_special_folder (int csidl)
317 {
318 wchar_t path[MAX_PATH+1];
319 HRESULT hr;
320 LPITEMIDLIST pidl = NULL;
321 BOOL b;
322 gchar *retval = NULL;
323
324 hr = SHGetSpecialFolderLocation (NULL, csidl, &pidl);
325 if (hr == S_OK)
326 {
327 b = SHGetPathFromIDListW (pidl, path);
328 if (b)
329 retval = g_utf16_to_utf8 (path, -1, NULL, NULL, NULL);
330 CoTaskMemFree (pidl);
331 }
332
333 return retval;
334 }
335
336 static HMODULE libgimpbase_dll = NULL;
337
338 /* Minimal DllMain that just stores the handle to this DLL */
339
340 BOOL WINAPI /* Avoid silly "no previous prototype" gcc warning */
341 DllMain (HINSTANCE hinstDLL,
342 DWORD fdwReason,
343 LPVOID lpvReserved);
344
345 BOOL WINAPI
DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)346 DllMain (HINSTANCE hinstDLL,
347 DWORD fdwReason,
348 LPVOID lpvReserved)
349 {
350 switch (fdwReason)
351 {
352 case DLL_PROCESS_ATTACH:
353 libgimpbase_dll = hinstDLL;
354 break;
355 }
356
357 return TRUE;
358 }
359
360 #endif
361
362 /**
363 * gimp_installation_directory:
364 *
365 * Returns the top installation directory of GIMP. On Unix the
366 * compile-time defined installation prefix is used. On Windows, the
367 * installation directory as deduced from the executable's full
368 * filename is used. On OSX we ask [NSBundle mainBundle] for the
369 * resource path to check if GIMP is part of a relocatable bundle.
370 *
371 * In config files such as gimprc, the string ${gimp_installation_dir}
372 * expands to this directory.
373 *
374 * The returned string is owned by GIMP and must not be modified or
375 * freed. The returned string is in the encoding used for filenames by
376 * GLib, which isn't necessarily UTF-8. (On Windows it always is
377 * UTF-8.)
378 *
379 * Since: 2.8
380 *
381 * Returns: The toplevel installation directory of GIMP.
382 **/
383 const gchar *
gimp_installation_directory(void)384 gimp_installation_directory (void)
385 {
386 static gchar *toplevel = NULL;
387
388 if (toplevel)
389 return toplevel;
390
391 #ifdef G_OS_WIN32
392
393 toplevel = g_win32_get_package_installation_directory_of_module (libgimpbase_dll);
394 if (! toplevel)
395 g_error ("g_win32_get_package_installation_directory_of_module() failed");
396
397 #elif PLATFORM_OSX
398
399 {
400 NSAutoreleasePool *pool;
401 NSString *resource_path;
402 gchar *basename;
403 gchar *basepath;
404 gchar *dirname;
405
406 pool = [[NSAutoreleasePool alloc] init];
407
408 resource_path = [[NSBundle mainBundle] resourcePath];
409
410 basename = g_path_get_basename ([resource_path UTF8String]);
411 basepath = g_path_get_dirname ([resource_path UTF8String]);
412 dirname = g_path_get_basename (basepath);
413
414 if (! strcmp (basename, ".libs"))
415 {
416 /* we are running from the source dir, do normal unix things */
417
418 toplevel = _gimp_reloc_find_prefix (PREFIX);
419 }
420 else if (! strcmp (basename, "bin"))
421 {
422 /* we are running the main app, but not from a bundle, the resource
423 * path is the directory which contains the executable
424 */
425
426 toplevel = g_strdup (basepath);
427 }
428 else if (! strcmp (basename, "plug-ins"))
429 {
430 /* same for plug-ins, go three levels up from prefix/lib/gimp/x.y */
431
432 gchar *tmp = g_path_get_dirname (basepath);
433 gchar *tmp2 = g_path_get_dirname (tmp);
434
435 toplevel = g_path_get_dirname (tmp2);
436
437 g_free (tmp);
438 g_free (tmp2);
439 }
440 else if (! strcmp (dirname, "plug-ins"))
441 {
442 /* same for plug-ins in subdirectory, go three levels up from prefix/lib/gimp/x.y */
443
444 gchar *tmp = g_path_get_dirname (basepath);
445 gchar *tmp2 = g_path_get_dirname (tmp);
446 gchar *tmp3 = g_path_get_dirname (tmp2);
447
448 toplevel = g_path_get_dirname (tmp3);
449
450 g_free (tmp);
451 g_free (tmp2);
452 g_free (tmp3);
453 }
454 else
455 {
456 /* if none of the above match, we assume that we are really in a bundle */
457
458 toplevel = g_strdup ([resource_path UTF8String]);
459 }
460
461 g_free (basename);
462 g_free (basepath);
463 g_free (dirname);
464
465 [pool drain];
466 }
467
468 #else
469
470 toplevel = _gimp_reloc_find_prefix (PREFIX);
471
472 #endif
473
474 return toplevel;
475 }
476
477 /**
478 * gimp_data_directory:
479 *
480 * Returns the default top directory for GIMP data. If the environment
481 * variable GIMP2_DATADIR exists, that is used. It should be an
482 * absolute pathname. Otherwise, on Unix the compile-time defined
483 * directory is used. On Windows, the installation directory as
484 * deduced from the executable's full filename is used.
485 *
486 * Note that the actual directories used for GIMP data files can be
487 * overridden by the user in the preferences dialog.
488 *
489 * In config files such as gimprc, the string ${gimp_data_dir} expands
490 * to this directory.
491 *
492 * The returned string is owned by GIMP and must not be modified or
493 * freed. The returned string is in the encoding used for filenames by
494 * GLib, which isn't necessarily UTF-8. (On Windows it always is
495 * UTF-8.)
496 *
497 * Returns: The top directory for GIMP data.
498 **/
499 const gchar *
gimp_data_directory(void)500 gimp_data_directory (void)
501 {
502 static gchar *gimp_data_dir = NULL;
503
504 if (! gimp_data_dir)
505 {
506 gchar *tmp = g_build_filename ("share",
507 GIMP_PACKAGE,
508 GIMP_DATA_VERSION,
509 NULL);
510
511 gimp_data_dir = gimp_env_get_dir ("GIMP2_DATADIR", GIMPDATADIR, tmp);
512 g_free (tmp);
513 }
514
515 return gimp_data_dir;
516 }
517
518 /**
519 * gimp_locale_directory:
520 *
521 * Returns the top directory for GIMP locale files. If the environment
522 * variable GIMP2_LOCALEDIR exists, that is used. It should be an
523 * absolute pathname. Otherwise, on Unix the compile-time defined
524 * directory is used. On Windows, the installation directory as deduced
525 * from the executable's full filename is used.
526 *
527 * The returned string is owned by GIMP and must not be modified or
528 * freed. The returned string is in the encoding used for filenames by
529 * the C library, which isn't necessarily UTF-8. (On Windows, unlike
530 * the other similar functions here, the return value from this
531 * function is in the system codepage, never in UTF-8. It can thus be
532 * passed directly to the bindtextdomain() function from libintl which
533 * does not handle UTF-8.)
534 *
535 * Returns: The top directory for GIMP locale files.
536 */
537 const gchar *
gimp_locale_directory(void)538 gimp_locale_directory (void)
539 {
540 static gchar *gimp_locale_dir = NULL;
541
542 if (! gimp_locale_dir)
543 {
544 gchar *tmp = g_build_filename ("share",
545 "locale",
546 NULL);
547
548 gimp_locale_dir = gimp_env_get_dir ("GIMP2_LOCALEDIR", LOCALEDIR, tmp);
549 g_free (tmp);
550
551 #ifdef G_OS_WIN32
552 /* FIXME: g_win32_locale_filename_from_utf8() can actually return
553 * NULL (we had actual cases of this). Not sure exactly what
554 * gimp_locale_directory() should do when this happens. Anyway
555 * that's really broken, and something should be done some day
556 * about this!
557 */
558 tmp = g_win32_locale_filename_from_utf8 (gimp_locale_dir);
559 g_free (gimp_locale_dir);
560 gimp_locale_dir = tmp;
561 #endif
562 }
563
564 return gimp_locale_dir;
565 }
566
567 /**
568 * gimp_sysconf_directory:
569 *
570 * Returns the top directory for GIMP config files. If the environment
571 * variable GIMP2_SYSCONFDIR exists, that is used. It should be an
572 * absolute pathname. Otherwise, on Unix the compile-time defined
573 * directory is used. On Windows, the installation directory as deduced
574 * from the executable's full filename is used.
575 *
576 * In config files such as gimprc, the string ${gimp_sysconf_dir}
577 * expands to this directory.
578 *
579 * The returned string is owned by GIMP and must not be modified or
580 * freed. The returned string is in the encoding used for filenames by
581 * GLib, which isn't necessarily UTF-8. (On Windows it always is
582 * UTF-8.).
583 *
584 * Returns: The top directory for GIMP config files.
585 **/
586 const gchar *
gimp_sysconf_directory(void)587 gimp_sysconf_directory (void)
588 {
589 static gchar *gimp_sysconf_dir = NULL;
590
591 if (! gimp_sysconf_dir)
592 {
593 gchar *tmp = g_build_filename ("etc",
594 GIMP_PACKAGE,
595 GIMP_SYSCONF_VERSION,
596 NULL);
597
598 gimp_sysconf_dir = gimp_env_get_dir ("GIMP2_SYSCONFDIR", GIMPSYSCONFDIR, tmp);
599 g_free (tmp);
600 }
601
602 return gimp_sysconf_dir;
603 }
604
605 /**
606 * gimp_plug_in_directory:
607 *
608 * Returns the default top directory for GIMP plug-ins and modules. If
609 * the environment variable GIMP2_PLUGINDIR exists, that is used. It
610 * should be an absolute pathname. Otherwise, on Unix the compile-time
611 * defined directory is used. On Windows, the installation directory
612 * as deduced from the executable's full filename is used.
613 *
614 * Note that the actual directories used for GIMP plug-ins and modules
615 * can be overridden by the user in the preferences dialog.
616 *
617 * In config files such as gimprc, the string ${gimp_plug_in_dir}
618 * expands to this directory.
619 *
620 * The returned string is owned by GIMP and must not be modified or
621 * freed. The returned string is in the encoding used for filenames by
622 * GLib, which isn't necessarily UTF-8. (On Windows it always is
623 * UTF-8.)
624 *
625 * Returns: The top directory for GIMP plug_ins and modules.
626 **/
627 const gchar *
gimp_plug_in_directory(void)628 gimp_plug_in_directory (void)
629 {
630 static gchar *gimp_plug_in_dir = NULL;
631
632 if (! gimp_plug_in_dir)
633 {
634 gchar *tmp = g_build_filename ("lib",
635 GIMP_PACKAGE,
636 GIMP_PLUGIN_VERSION,
637 NULL);
638
639 gimp_plug_in_dir = gimp_env_get_dir ("GIMP2_PLUGINDIR", PLUGINDIR, tmp);
640 g_free (tmp);
641 }
642
643 return gimp_plug_in_dir;
644 }
645
646 /**
647 * gimp_cache_directory:
648 *
649 * Returns the default top directory for GIMP cached files. If the
650 * environment variable GIMP2_CACHEDIR exists, that is used. It
651 * should be an absolute pathname. Otherwise, a subdirectory of the
652 * directory returned by g_get_user_cache_dir() is used.
653 *
654 * Note that the actual directories used for GIMP caches files can
655 * be overridden by the user in the preferences dialog.
656 *
657 * In config files such as gimprc, the string ${gimp_cache_dir}
658 * expands to this directory.
659 *
660 * The returned string is owned by GIMP and must not be modified or
661 * freed. The returned string is in the encoding used for filenames by
662 * GLib, which isn't necessarily UTF-8. (On Windows it always is
663 * UTF-8.).
664 *
665 * Since: 2.10.10
666 *
667 * Returns: The default top directory for GIMP cached files.
668 **/
669 const gchar *
gimp_cache_directory(void)670 gimp_cache_directory (void)
671 {
672 static gchar *gimp_cache_dir = NULL;
673
674 if (! gimp_cache_dir)
675 {
676 gchar *tmp = g_build_filename (g_get_user_cache_dir (),
677 GIMP_PACKAGE,
678 GIMP_USER_VERSION,
679 NULL);
680
681 gimp_cache_dir = gimp_env_get_dir ("GIMP2_CACHEDIR", NULL, tmp);
682 g_free (tmp);
683 }
684
685 return gimp_cache_dir;
686 }
687
688 /**
689 * gimp_temp_directory:
690 *
691 * Returns the default top directory for GIMP temporary files. If the
692 * environment variable GIMP2_TEMPDIR exists, that is used. It
693 * should be an absolute pathname. Otherwise, a subdirectory of the
694 * directory returned by g_get_tmp_dir() is used.
695 *
696 * In config files such as gimprc, the string ${gimp_temp_dir} expands
697 * to this directory.
698 *
699 * Note that the actual directories used for GIMP temporary files can
700 * be overridden by the user in the preferences dialog.
701 *
702 * The returned string is owned by GIMP and must not be modified or
703 * freed. The returned string is in the encoding used for filenames by
704 * GLib, which isn't necessarily UTF-8. (On Windows it always is
705 * UTF-8.).
706 *
707 * Since: 2.10.10
708 *
709 * Returns: The default top directory for GIMP temporary files.
710 **/
711 const gchar *
gimp_temp_directory(void)712 gimp_temp_directory (void)
713 {
714 static gchar *gimp_temp_dir = NULL;
715
716 if (! gimp_temp_dir)
717 {
718 gchar *tmp = g_build_filename (g_get_tmp_dir (),
719 GIMP_PACKAGE,
720 GIMP_USER_VERSION,
721 NULL);
722
723 gimp_temp_dir = gimp_env_get_dir ("GIMP2_TEMPDIR", NULL, tmp);
724 g_free (tmp);
725 }
726
727 return gimp_temp_dir;
728 }
729
730 static GFile *
gimp_child_file(const gchar * parent,const gchar * element,va_list args)731 gimp_child_file (const gchar *parent,
732 const gchar *element,
733 va_list args)
734 {
735 GFile *file = g_file_new_for_path (parent);
736
737 while (element)
738 {
739 GFile *child = g_file_get_child (file, element);
740
741 g_object_unref (file);
742 file = child;
743
744 element = va_arg (args, const gchar *);
745 }
746
747 return file;
748 }
749
750 /**
751 * gimp_directory_file:
752 * @first_element: the first element of a path to a file in the
753 * user's GIMP directory, or %NULL.
754 * @...: a %NULL terminated list of the remaining elements of the path
755 * to the file.
756 *
757 * Returns a #GFile in the user's GIMP directory, or the data
758 * directory itself if @first_element is %NULL.
759 *
760 * See also: gimp_directory().
761 *
762 * Since: 2.10
763 *
764 * Returns: a new @GFile for the path, Free with g_object_unref().
765 **/
766 GFile *
gimp_directory_file(const gchar * first_element,...)767 gimp_directory_file (const gchar *first_element,
768 ...)
769 {
770 GFile *file;
771 va_list args;
772
773 va_start (args, first_element);
774 file = gimp_child_file (gimp_directory (), first_element, args);
775 va_end (args);
776
777 return file;
778 }
779
780 /**
781 * gimp_installation_directory_file:
782 * @first_element: the first element of a path to a file in the
783 * top installation directory, or %NULL.
784 * @...: a %NULL terminated list of the remaining elements of the path
785 * to the file.
786 *
787 * Returns a #GFile in the installation directory, or the installation
788 * directory itself if @first_element is %NULL.
789 *
790 * See also: gimp_installation_directory().
791 *
792 * Since: 2.10.10
793 *
794 * Returns: a new @GFile for the path, Free with g_object_unref().
795 **/
796 GFile *
gimp_installation_directory_file(const gchar * first_element,...)797 gimp_installation_directory_file (const gchar *first_element,
798 ...)
799 {
800 GFile *file;
801 va_list args;
802
803 va_start (args, first_element);
804 file = gimp_child_file (gimp_installation_directory (), first_element, args);
805 va_end (args);
806
807 return file;
808 }
809
810 /**
811 * gimp_data_directory_file:
812 * @first_element: the first element of a path to a file in the
813 * data directory, or %NULL.
814 * @...: a %NULL terminated list of the remaining elements of the path
815 * to the file.
816 *
817 * Returns a #GFile in the data directory, or the data directory
818 * itself if @first_element is %NULL.
819 *
820 * See also: gimp_data_directory().
821 *
822 * Since: 2.10
823 *
824 * Returns: a new @GFile for the path, Free with g_object_unref().
825 **/
826 GFile *
gimp_data_directory_file(const gchar * first_element,...)827 gimp_data_directory_file (const gchar *first_element,
828 ...)
829 {
830 GFile *file;
831 va_list args;
832
833 va_start (args, first_element);
834 file = gimp_child_file (gimp_data_directory (), first_element, args);
835 va_end (args);
836
837 return file;
838 }
839
840 /**
841 * gimp_locale_directory_file:
842 * @first_element: the first element of a path to a file in the
843 * locale directory, or %NULL.
844 * @...: a %NULL terminated list of the remaining elements of the path
845 * to the file.
846 *
847 * Returns a #GFile in the locale directory, or the locale directory
848 * itself if @first_element is %NULL.
849 *
850 * See also: gimp_locale_directory().
851 *
852 * Since: 2.10
853 *
854 * Returns: a new @GFile for the path, Free with g_object_unref().
855 **/
856 GFile *
gimp_locale_directory_file(const gchar * first_element,...)857 gimp_locale_directory_file (const gchar *first_element,
858 ...)
859 {
860 GFile *file;
861 va_list args;
862
863 va_start (args, first_element);
864 file = gimp_child_file (gimp_locale_directory (), first_element, args);
865 va_end (args);
866
867 return file;
868 }
869
870 /**
871 * gimp_sysconf_directory_file:
872 * @first_element: the first element of a path to a file in the
873 * sysconf directory, or %NULL.
874 * @...: a %NULL terminated list of the remaining elements of the path
875 * to the file.
876 *
877 * Returns a #GFile in the sysconf directory, or the sysconf directory
878 * itself if @first_element is %NULL.
879 *
880 * See also: gimp_sysconf_directory().
881 *
882 * Since: 2.10
883 *
884 * Returns: a new @GFile for the path, Free with g_object_unref().
885 **/
886 GFile *
gimp_sysconf_directory_file(const gchar * first_element,...)887 gimp_sysconf_directory_file (const gchar *first_element,
888 ...)
889 {
890 GFile *file;
891 va_list args;
892
893 va_start (args, first_element);
894 file = gimp_child_file (gimp_sysconf_directory (), first_element, args);
895 va_end (args);
896
897 return file;
898 }
899
900 /**
901 * gimp_plug_in_directory_file:
902 * @first_element: the first element of a path to a file in the
903 * plug-in directory, or %NULL.
904 * @...: a %NULL terminated list of the remaining elements of the path
905 * to the file.
906 *
907 * Returns a #GFile in the plug-in directory, or the plug-in directory
908 * itself if @first_element is %NULL.
909 *
910 * See also: gimp_plug_in_directory().
911 *
912 * Since: 2.10
913 *
914 * Returns: a new @GFile for the path, Free with g_object_unref().
915 **/
916 GFile *
gimp_plug_in_directory_file(const gchar * first_element,...)917 gimp_plug_in_directory_file (const gchar *first_element,
918 ...)
919 {
920 GFile *file;
921 va_list args;
922
923 va_start (args, first_element);
924 file = gimp_child_file (gimp_plug_in_directory (), first_element, args);
925 va_end (args);
926
927 return file;
928 }
929
930 /**
931 * gimp_user_directory:
932 * @type: the type of user directory to retrieve
933 *
934 * This procedure is deprecated! Use g_get_user_special_dir() instead.
935 *
936 * Returns: The path to the specified user directory, or %NULL if the
937 * logical ID was not found.
938 *
939 * Since: 2.4
940 **/
941 const gchar *
gimp_user_directory(GimpUserDirectory type)942 gimp_user_directory (GimpUserDirectory type)
943 {
944 return g_get_user_special_dir ((GUserDirectory) type);
945 }
946
947 /**
948 * gimp_personal_rc_file:
949 * @basename: The basename of a rc_file.
950 *
951 * Returns the name of a file in the user-specific GIMP settings directory.
952 *
953 * The returned string is newly allocated and should be freed with
954 * g_free() after use. The returned string is in the encoding used for
955 * filenames by GLib, which isn't necessarily UTF-8. (On Windows it
956 * always is UTF-8.)
957 *
958 * Returns: The name of a file in the user-specific GIMP settings directory.
959 **/
960 gchar *
gimp_personal_rc_file(const gchar * basename)961 gimp_personal_rc_file (const gchar *basename)
962 {
963 return g_build_filename (gimp_directory (), basename, NULL);
964 }
965
966 /**
967 * gimp_gtkrc:
968 *
969 * Returns the name of GIMP's application-specific gtkrc file.
970 *
971 * The returned string is owned by GIMP and must not be modified or
972 * freed. The returned string is in the encoding used for filenames by
973 * GLib, which isn't necessarily UTF-8. (On Windows it always is
974 * UTF-8.)
975 *
976 * Returns: The name of GIMP's application-specific gtkrc file.
977 **/
978 const gchar *
gimp_gtkrc(void)979 gimp_gtkrc (void)
980 {
981 static gchar *gimp_gtkrc_filename = NULL;
982
983 if (! gimp_gtkrc_filename)
984 gimp_gtkrc_filename = g_build_filename (gimp_data_directory (),
985 "themes", "System", "gtkrc",
986 NULL);
987
988 return gimp_gtkrc_filename;
989 }
990
991 /**
992 * gimp_path_runtime_fix:
993 * @path: A pointer to a string (allocated with g_malloc) that is
994 * (or could be) a pathname.
995 *
996 * On Windows, this function checks if the string pointed to by @path
997 * starts with the compile-time prefix, and in that case, replaces the
998 * prefix with the run-time one. @path should be a pointer to a
999 * dynamically allocated (with g_malloc, g_strconcat, etc) string. If
1000 * the replacement takes place, the original string is deallocated,
1001 * and *@path is replaced with a pointer to a new string with the
1002 * run-time prefix spliced in.
1003 *
1004 * On Linux, it does the same thing, but only if BinReloc support is enabled.
1005 * On other Unices, it does nothing because those platforms don't have a
1006 * way to find out where our binary is.
1007 */
1008 static void
gimp_path_runtime_fix(gchar ** path)1009 gimp_path_runtime_fix (gchar **path)
1010 {
1011 #if defined (G_OS_WIN32) && defined (PREFIX)
1012 gchar *p;
1013
1014 /* Yes, I do mean forward slashes below */
1015 if (strncmp (*path, PREFIX "/", strlen (PREFIX "/")) == 0)
1016 {
1017 /* This is a compile-time entry. Replace the path with the
1018 * real one on this machine.
1019 */
1020 p = *path;
1021 *path = g_strconcat (gimp_installation_directory (),
1022 "\\",
1023 *path + strlen (PREFIX "/"),
1024 NULL);
1025 g_free (p);
1026 }
1027 /* Replace forward slashes with backslashes, just for
1028 * completeness */
1029 p = *path;
1030 while ((p = strchr (p, '/')) != NULL)
1031 {
1032 *p = '\\';
1033 p++;
1034 }
1035 #elif defined (G_OS_WIN32)
1036 /* without defineing PREFIX do something useful too */
1037 gchar *p = *path;
1038 if (!g_path_is_absolute (p))
1039 {
1040 *path = g_build_filename (gimp_installation_directory (), *path, NULL);
1041 g_free (p);
1042 }
1043 #else
1044 gchar *p;
1045
1046 if (strncmp (*path, PREFIX G_DIR_SEPARATOR_S,
1047 strlen (PREFIX G_DIR_SEPARATOR_S)) == 0)
1048 {
1049 /* This is a compile-time entry. Replace the path with the
1050 * real one on this machine.
1051 */
1052 p = *path;
1053 *path = g_build_filename (gimp_installation_directory (),
1054 *path + strlen (PREFIX G_DIR_SEPARATOR_S),
1055 NULL);
1056 g_free (p);
1057 }
1058 #endif
1059 }
1060
1061 /**
1062 * gimp_path_parse:
1063 * @path: A list of directories separated by #G_SEARCHPATH_SEPARATOR.
1064 * @max_paths: The maximum number of directories to return.
1065 * @check: %TRUE if you want the directories to be checked.
1066 * @check_failed: Returns a #GList of path elements for which the
1067 * check failed.
1068 *
1069 * Returns: A #GList of all directories in @path.
1070 **/
1071 GList *
gimp_path_parse(const gchar * path,gint max_paths,gboolean check,GList ** check_failed)1072 gimp_path_parse (const gchar *path,
1073 gint max_paths,
1074 gboolean check,
1075 GList **check_failed)
1076 {
1077 gchar **patharray;
1078 GList *list = NULL;
1079 GList *fail_list = NULL;
1080 gint i;
1081 gboolean exists = TRUE;
1082
1083 if (!path || !*path || max_paths < 1 || max_paths > 256)
1084 return NULL;
1085
1086 patharray = g_strsplit (path, G_SEARCHPATH_SEPARATOR_S, max_paths);
1087
1088 for (i = 0; i < max_paths; i++)
1089 {
1090 GString *dir;
1091
1092 if (! patharray[i])
1093 break;
1094
1095 #ifndef G_OS_WIN32
1096 if (*patharray[i] == '~')
1097 {
1098 dir = g_string_new (g_get_home_dir ());
1099 g_string_append (dir, patharray[i] + 1);
1100 }
1101 else
1102 #endif
1103 {
1104 gimp_path_runtime_fix (&patharray[i]);
1105 dir = g_string_new (patharray[i]);
1106 }
1107
1108 if (check)
1109 exists = g_file_test (dir->str, G_FILE_TEST_IS_DIR);
1110
1111 if (exists)
1112 {
1113 GList *dup;
1114
1115 /* check for duplicate entries, see bug #784502 */
1116 for (dup = list; dup; dup = g_list_next (dup))
1117 {
1118 if (! strcmp (dir->str, dup->data))
1119 break;
1120 }
1121
1122 /* only add to the list if it's not a duplicate */
1123 if (! dup)
1124 list = g_list_prepend (list, g_strdup (dir->str));
1125 }
1126 else if (check_failed)
1127 {
1128 fail_list = g_list_prepend (fail_list, g_strdup (dir->str));
1129 }
1130
1131 g_string_free (dir, TRUE);
1132 }
1133
1134 g_strfreev (patharray);
1135
1136 list = g_list_reverse (list);
1137
1138 if (check && check_failed)
1139 {
1140 fail_list = g_list_reverse (fail_list);
1141 *check_failed = fail_list;
1142 }
1143
1144 return list;
1145 }
1146
1147 /**
1148 * gimp_path_to_str:
1149 * @path: A list of directories as returned by gimp_path_parse().
1150 *
1151 * Returns: A searchpath string separated by #G_SEARCHPATH_SEPARATOR.
1152 **/
1153 gchar *
gimp_path_to_str(GList * path)1154 gimp_path_to_str (GList *path)
1155 {
1156 GString *str = NULL;
1157 GList *list;
1158 gchar *retval = NULL;
1159
1160 for (list = path; list; list = g_list_next (list))
1161 {
1162 gchar *dir = list->data;
1163
1164 if (str)
1165 {
1166 g_string_append_c (str, G_SEARCHPATH_SEPARATOR);
1167 g_string_append (str, dir);
1168 }
1169 else
1170 {
1171 str = g_string_new (dir);
1172 }
1173 }
1174
1175 if (str)
1176 retval = g_string_free (str, FALSE);
1177
1178 return retval;
1179 }
1180
1181 /**
1182 * gimp_path_free:
1183 * @path: A list of directories as returned by gimp_path_parse().
1184 *
1185 * This function frees the memory allocated for the list and the strings
1186 * it contains.
1187 **/
1188 void
gimp_path_free(GList * path)1189 gimp_path_free (GList *path)
1190 {
1191 g_list_free_full (path, (GDestroyNotify) g_free);
1192 }
1193
1194 /**
1195 * gimp_path_get_user_writable_dir:
1196 * @path: A list of directories as returned by gimp_path_parse().
1197 *
1198 * Note that you have to g_free() the returned string.
1199 *
1200 * Returns: The first directory in @path where the user has write permission.
1201 **/
1202 gchar *
gimp_path_get_user_writable_dir(GList * path)1203 gimp_path_get_user_writable_dir (GList *path)
1204 {
1205 GList *list;
1206 uid_t euid;
1207 gid_t egid;
1208 GStatBuf filestat;
1209 gint err;
1210
1211 g_return_val_if_fail (path != NULL, NULL);
1212
1213 euid = geteuid ();
1214 egid = getegid ();
1215
1216 for (list = path; list; list = g_list_next (list))
1217 {
1218 gchar *dir = list->data;
1219
1220 /* check if directory exists */
1221 err = g_stat (dir, &filestat);
1222
1223 /* this is tricky:
1224 * if a file is e.g. owned by the current user but not user-writable,
1225 * the user has no permission to write to the file regardless
1226 * of his group's or other's write permissions
1227 */
1228 if (!err && S_ISDIR (filestat.st_mode) &&
1229
1230 ((filestat.st_mode & S_IWUSR) ||
1231
1232 ((filestat.st_mode & S_IWGRP) &&
1233 (euid != filestat.st_uid)) ||
1234
1235 ((filestat.st_mode & S_IWOTH) &&
1236 (euid != filestat.st_uid) &&
1237 (egid != filestat.st_gid))))
1238 {
1239 return g_strdup (dir);
1240 }
1241 }
1242
1243 return NULL;
1244 }
1245
1246 static gchar *
gimp_env_get_dir(const gchar * gimp_env_name,const gchar * compile_time_dir,const gchar * relative_subdir)1247 gimp_env_get_dir (const gchar *gimp_env_name,
1248 const gchar *compile_time_dir,
1249 const gchar *relative_subdir)
1250 {
1251 const gchar *env = g_getenv (gimp_env_name);
1252
1253 if (env)
1254 {
1255 if (! g_path_is_absolute (env))
1256 g_error ("%s environment variable should be an absolute path.",
1257 gimp_env_name);
1258
1259 return g_strdup (env);
1260 }
1261 else if (compile_time_dir)
1262 {
1263 gchar *retval = g_strdup (compile_time_dir);
1264
1265 gimp_path_runtime_fix (&retval);
1266
1267 return retval;
1268 }
1269 else if (! g_path_is_absolute (relative_subdir))
1270 {
1271 return g_build_filename (gimp_installation_directory (),
1272 relative_subdir,
1273 NULL);
1274 }
1275
1276 return g_strdup (relative_subdir);
1277 }
1278