home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / app / datafiles.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-04-23  |  4.3 KB  |  196 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * Datafiles module copyight (C) 1996 Federico Mena Quintero
  5.  * federico@nuclecu.unam.mx
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 2 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program 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
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20.  */
  21. #include "config.h"
  22.  
  23. #include <glib.h>
  24.  
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #ifdef HAVE_UNISTD_H
  31. #include <unistd.h>
  32. #endif
  33. #ifdef HAVE_DIRENT_H
  34. #include <dirent.h>
  35. #endif
  36.  
  37. #ifdef G_OS_WIN32
  38. #ifndef S_ISDIR
  39. #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
  40. #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
  41. #endif
  42. #ifndef S_IXUSR
  43. #define S_IXUSR _S_IEXEC
  44. #endif
  45. #endif /* G_OS_WIN32 */
  46.  
  47. #include "datafiles.h"
  48. #include "gimprc.h"
  49.  
  50. #include "libgimp/gimpenv.h"
  51.  
  52. /***** Functions *****/
  53.  
  54. static gboolean    filestat_valid = FALSE;
  55. static struct stat filestat;
  56.  
  57. #ifdef G_OS_WIN32
  58. /*
  59.  * On Windows there is no concept like the Unix executable flag.
  60.  * There is a weak emulation provided by the MS C Runtime using file
  61.  * extensions (com, exe, cmd, bat). This needs to be extended to treat
  62.  * scripts (Python, Perl, ...) as executables, too. We use the PATHEXT
  63.  * variable, which is also used by cmd.exe.
  64.  */
  65. static gboolean
  66. is_script (const gchar *filename)
  67. {
  68.   const gchar *ext = strrchr (filename, '.');
  69.   gchar *pathext;
  70.   static gchar **exts = NULL;
  71.   gint i;
  72.  
  73.   if (exts == NULL)
  74.     {
  75.       pathext = g_getenv ("PATHEXT");
  76.       if (pathext != NULL)
  77.     {
  78.       exts = g_strsplit (pathext, G_SEARCHPATH_SEPARATOR_S, 100);
  79.     }
  80.       else
  81.     {
  82.       exts = g_new (gchar *, 1);
  83.       exts[0] = NULL;
  84.     }
  85.     }
  86.  
  87.   i = 0;
  88.   while (exts[i] != NULL)
  89.     {
  90.       if (g_strcasecmp (ext, exts[i]) == 0)
  91.     return TRUE;
  92.       i++;
  93.     }
  94.  
  95.   return FALSE;
  96. }
  97. #else  /* !G_OS_WIN32 */
  98. #define is_script(filename) FALSE
  99. #endif
  100.  
  101. void
  102. datafiles_read_directories (gchar                  *path_str,
  103.                 GimpDataFileLoaderFunc  loader_func,
  104.                 GimpDataFileFlags       flags)
  105. {
  106.   gchar *local_path;
  107.   GList *path;
  108.   GList *list;
  109.   gchar *filename;
  110.   gint   err;
  111.   DIR   *dir;
  112.   struct dirent *dir_ent;
  113.  
  114.   if (path_str == NULL)
  115.     return;
  116.  
  117.   local_path = g_strdup (path_str);
  118.  
  119. #ifdef __EMX__
  120.   /*
  121.    *  Change drive so opendir works.
  122.    */
  123.   if (local_path[1] == ':')
  124.     {
  125.       _chdrive (local_path[0]);
  126.     }
  127. #endif  
  128.  
  129.   path = gimp_path_parse (local_path, 16, TRUE, NULL);
  130.  
  131.   for (list = path; list; list = g_list_next (list))
  132.     {
  133.       /* Open directory */
  134.       dir = opendir ((gchar *) list->data);
  135.  
  136.       if (!dir)
  137.     {
  138.       g_message ("error reading datafiles directory \"%s\"",
  139.              (gchar *) list->data);
  140.     }
  141.       else
  142.     {
  143.       while ((dir_ent = readdir (dir)))
  144.         {
  145.           filename = g_strdup_printf ("%s%s",
  146.                       (gchar *) list->data,
  147.                       dir_ent->d_name);
  148.  
  149.           /* Check the file and see that it is not a sub-directory */
  150.           err = stat (filename, &filestat);
  151.  
  152.           if (!err && S_ISREG (filestat.st_mode) &&
  153.           (!(flags & MODE_EXECUTABLE) ||
  154.            (filestat.st_mode & S_IXUSR) ||
  155.            is_script (filename)))
  156.         {
  157.           filestat_valid = TRUE;
  158.           (*loader_func) (filename);
  159.           filestat_valid = FALSE;
  160.         }
  161.  
  162.           g_free (filename);
  163.         }
  164.  
  165.       closedir (dir);
  166.     }
  167.     }
  168.  
  169.   gimp_path_free (path);
  170.   g_free (local_path);
  171. }
  172.  
  173. time_t
  174. datafile_atime (void)
  175. {
  176.   if (filestat_valid)
  177.     return filestat.st_atime;
  178.   return 0;
  179. }
  180.  
  181. time_t
  182. datafile_mtime (void)
  183. {
  184.   if (filestat_valid)
  185.     return filestat.st_mtime;
  186.   return 0;
  187. }
  188.  
  189. time_t
  190. datafile_ctime (void)
  191. {
  192.   if (filestat_valid)
  193.     return filestat.st_ctime;
  194.   return 0;
  195. }
  196.