File: datafiles.c

package info (click to toggle)
gimp 1.0.2-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 17,116 kB
  • ctags: 16,070
  • sloc: ansic: 226,067; lisp: 8,497; sh: 4,965; makefile: 4,543
file content (157 lines) | stat: -rw-r--r-- 3,522 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * Datafiles module copyight (C) 1996 Federico Mena Quintero
 * federico@nuclecu.unam.mx
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>

#include <glib.h>
#include "datafiles.h"
#include "errors.h"
#include "general.h"
#include "gimprc.h"


/***** Functions *****/

/*****/

static int filestat_valid = 0;
static struct stat filestat;

void
datafiles_read_directories (char *path_str,
			    datafile_loader_t loader_func,
			    int flags)
{
  char          *home;
  char 	        *local_path;
  char          *path;
  char 	        *filename;
  char 	        *token;
  char          *next_token;
  int            err;
  DIR           *dir;
  struct dirent *dir_ent;

  if (path_str == NULL)
    return;

  /* Set local path to contain temp_path, where (supposedly)
   * there may be working files.
   */
  home = getenv("HOME");
  local_path = g_strdup (path_str);

  /* Search through all directories in the local path */

  next_token = local_path;

  token = xstrsep(&next_token, ":");

  while (token)
    {
      if (*token == '~')
	{
	  path = g_malloc(strlen(home) + strlen(token) + 2);
	  sprintf(path, "%s%s", home, token + 1);
	}
      else
	{
	  path = g_malloc(strlen(token) + 2);
	  strcpy(path, token);
	} /* else */

      /* Check if directory exists and if it has any items in it */
      err = stat(path, &filestat);

      if (!err && S_ISDIR(filestat.st_mode))
	{
	  if (path[strlen(path) - 1] != '/')
	    strcat(path, "/");

	  /* Open directory */
	  dir = opendir(path);

	  if (!dir)
	    g_message ("error reading datafiles directory \"%s\"", path);
	  else
	    {
	      while ((dir_ent = readdir(dir)))
		{
		  filename = g_malloc(strlen(path) + strlen(dir_ent->d_name) + 1);

		  sprintf(filename, "%s%s", path, dir_ent->d_name);

		  /* Check the file and see that it is not a sub-directory */
		  err = stat(filename, &filestat);

		  if (!err && S_ISREG(filestat.st_mode) &&
		      (!(flags & MODE_EXECUTABLE) || (filestat.st_mode & S_IXUSR)))
		    {
		      filestat_valid = 1;
		      (*loader_func) (filename);
		      filestat_valid = 0;
		    }

		  g_free(filename);
		} /* while */

	      closedir(dir);
	    } /* else */
	} /* if */

      g_free(path);

      token = xstrsep(&next_token, ":");
    } /* while */

  g_free(local_path);
} /* datafiles_read_directories */

time_t
datafile_atime ()
{
  if (filestat_valid)
    return filestat.st_atime;
  return 0;
}

time_t
datafile_mtime ()
{
  if (filestat_valid)
    return filestat.st_mtime;
  return 0;
}

time_t
datafile_ctime ()
{
  if (filestat_valid)
    return filestat.st_ctime;
  return 0;
}