home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 February / CMCD0205.ISO / Linux / gimp-2.2.0.tar.gz / gimp-2.2.0.tar / gimp-2.2.0 / libgimpbase / gimpdatafiles.c < prev    next >
C/C++ Source or Header  |  2004-11-13  |  5KB  |  196 lines

  1. /* LIBGIMP - The GIMP Library
  2.  * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
  3.  *
  4.  * Datafiles module copyight (C) 1996 Federico Mena Quintero
  5.  * federico@nuclecu.unam.mx
  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 2 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, write to the
  19.  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20.  * Boston, MA 02111-1307, USA.
  21.  */
  22.  
  23. #include "config.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.  
  34. #include <glib-object.h>
  35.  
  36. #ifdef G_OS_WIN32
  37. #include "gimpwin32-io.h"
  38. #endif /* G_OS_WIN32 */
  39.  
  40. #include "gimpbasetypes.h"
  41.  
  42. #include "gimpdatafiles.h"
  43. #include "gimpenv.h"
  44.  
  45.  
  46. #ifdef G_OS_WIN32
  47. /*
  48.  * On Windows there is no concept like the Unix executable flag.
  49.  * There is a weak emulation provided by the MS C Runtime using file
  50.  * extensions (com, exe, cmd, bat). This needs to be extended to treat
  51.  * scripts (Python, Perl, ...) as executables, too. We use the PATHEXT
  52.  * variable, which is also used by cmd.exe.
  53.  */
  54. static gboolean
  55. is_script (const gchar *filename)
  56. {
  57.   static gchar **exts = NULL;
  58.  
  59.   const gchar   *ext = strrchr (filename, '.');
  60.   gchar         *pathext;
  61.   gint           i;
  62.  
  63.   if (exts == NULL)
  64.     {
  65.       pathext = g_getenv ("PATHEXT");
  66.       if (pathext != NULL)
  67.     {
  68.       exts = g_strsplit (pathext, G_SEARCHPATH_SEPARATOR_S, 100);
  69.     }
  70.       else
  71.     {
  72.       exts = g_new (gchar *, 1);
  73.       exts[0] = NULL;
  74.     }
  75.     }
  76.  
  77.   i = 0;
  78.   while (exts[i] != NULL)
  79.     {
  80.       if (g_strcasecmp (ext, exts[i]) == 0)
  81.     return TRUE;
  82.       i++;
  83.     }
  84.  
  85.   return FALSE;
  86. }
  87. #else  /* !G_OS_WIN32 */
  88. #define is_script(filename) FALSE
  89. #endif
  90.  
  91. gboolean
  92. gimp_datafiles_check_extension (const gchar *filename,
  93.                 const gchar *extension)
  94. {
  95.   gint name_len;
  96.   gint ext_len;
  97.  
  98.   g_return_val_if_fail (filename != NULL, FALSE);
  99.   g_return_val_if_fail (extension != NULL, FALSE);
  100.  
  101.   name_len = strlen (filename);
  102.   ext_len  = strlen (extension);
  103.  
  104.   if (! (name_len && ext_len && (name_len > ext_len)))
  105.     return FALSE;
  106.  
  107.   return (g_ascii_strcasecmp (&filename[name_len - ext_len], extension) == 0);
  108. }
  109.  
  110. void
  111. gimp_datafiles_read_directories (const gchar            *path_str,
  112.                  GFileTest               flags,
  113.                  GimpDatafileLoaderFunc  loader_func,
  114.                  gpointer                user_data)
  115. {
  116.   GimpDatafileData  file_data;
  117.   struct stat       filestat;
  118.   gchar            *local_path;
  119.   GList            *path;
  120.   GList            *list;
  121.   gchar            *filename;
  122.   gint              err;
  123.   GDir             *dir;
  124.   const gchar      *dir_ent;
  125.  
  126.   g_return_if_fail (path_str != NULL);
  127.   g_return_if_fail (loader_func != NULL);
  128.  
  129.   local_path = g_strdup (path_str);
  130.  
  131.   path = gimp_path_parse (local_path, 16, TRUE, NULL);
  132.  
  133.   for (list = path; list; list = g_list_next (list))
  134.     {
  135.       dir = g_dir_open ((gchar *) list->data, 0, NULL);
  136.  
  137.       if (dir)
  138.     {
  139.       while ((dir_ent = g_dir_read_name (dir)))
  140.         {
  141.           filename = g_build_filename ((gchar *) list->data,
  142.                                            dir_ent, NULL);
  143.  
  144.           err = stat (filename, &filestat);
  145.  
  146.               file_data.filename = filename;
  147.               file_data.dirname  = (gchar *) list->data;
  148.               file_data.basename = dir_ent;
  149.               file_data.atime    = filestat.st_atime;
  150.               file_data.mtime    = filestat.st_mtime;
  151.               file_data.ctime    = filestat.st_ctime;
  152.  
  153.           if (! err)
  154.         {
  155.                   if (flags & G_FILE_TEST_EXISTS)
  156.                     {
  157.                       (* loader_func) (&file_data, user_data);
  158.                     }
  159.                   else if ((flags & G_FILE_TEST_IS_REGULAR) &&
  160.                            S_ISREG (filestat.st_mode))
  161.                     {
  162.                       (* loader_func) (&file_data, user_data);
  163.                     }
  164.           else if ((flags & G_FILE_TEST_IS_DIR) &&
  165.                            S_ISDIR (filestat.st_mode))
  166.             {
  167.               (* loader_func) (&file_data, user_data);
  168.             }
  169. #ifndef G_OS_WIN32
  170.           else if ((flags & G_FILE_TEST_IS_SYMLINK) &&
  171.                            S_ISLNK (filestat.st_mode))
  172.             {
  173.               (* loader_func) (&file_data, user_data);
  174.             }
  175. #endif
  176.           else if ((flags & G_FILE_TEST_IS_EXECUTABLE) &&
  177.                            (((filestat.st_mode & S_IXUSR) &&
  178.                  !S_ISDIR (filestat.st_mode)) ||
  179.                             (S_ISREG (filestat.st_mode) &&
  180.                              is_script (filename))))
  181.             {
  182.               (* loader_func) (&file_data, user_data);
  183.             }
  184.         }
  185.  
  186.           g_free (filename);
  187.         }
  188.  
  189.       g_dir_close (dir);
  190.     }
  191.     }
  192.  
  193.   gimp_path_free (path);
  194.   g_free (local_path);
  195. }
  196.