00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <gimp-print/gimp-print.h>
00024 #include "gimp-print-internal.h"
00025 #include <gimp-print/gimp-print-intl-internal.h>
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <errno.h>
00030 #include <dirent.h>
00031 #include <sys/types.h>
00032 #include <sys/stat.h>
00033 #include <unistd.h>
00034
00035 static int stpi_path_check(const struct dirent *module);
00036 static char *stpi_path_merge(const char *path, const char *file);
00037
00038
00039 static const char *path_check_path;
00040 static const char *path_check_suffix;
00041
00042
00043
00044
00045
00046 stp_list_t *
00047 stp_path_search(stp_list_t *dirlist,
00048 const char *suffix)
00049 {
00050 stp_list_t *findlist;
00051 stp_list_item_t *diritem;
00052 struct dirent** module_dir;
00053 char *module_name;
00054 int n;
00055
00056 if (!dirlist)
00057 return NULL;
00058
00059 path_check_suffix = suffix;
00060
00061 findlist = stp_list_create();
00062 if (!findlist)
00063 return NULL;
00064 stp_list_set_freefunc(findlist, stp_list_node_free_data);
00065
00066 diritem = stp_list_get_start(dirlist);
00067 while (diritem)
00068 {
00069 path_check_path = (const char *) stp_list_item_get_data(diritem);
00070 stp_deprintf(STP_DBG_PATH, "stp-path: directory: %s\n",
00071 (const char *) stp_list_item_get_data(diritem));
00072 n = scandir ((const char *) stp_list_item_get_data(diritem),
00073 &module_dir, stpi_path_check, alphasort);
00074 if (n >= 0)
00075 {
00076 int idx;
00077 for (idx = 0; idx < n; ++idx)
00078 {
00079 module_name = stpi_path_merge((const char *) stp_list_item_get_data(diritem),
00080 module_dir[idx]->d_name);
00081 stp_list_item_create(findlist, NULL, module_name);
00082 free (module_dir[idx]);
00083 }
00084 free (module_dir);
00085 }
00086 diritem = stp_list_item_next(diritem);
00087 }
00088 return findlist;
00089 }
00090
00091
00092
00093
00094
00095
00096 static int
00097 stpi_path_check(const struct dirent *module)
00098 {
00099 int namelen;
00100 int status = 0;
00101 int savederr;
00102 char *filename;
00103 struct stat modstat;
00104
00105 savederr = errno;
00106
00107 filename = stpi_path_merge(path_check_path, module->d_name);
00108
00109 namelen = strlen(filename);
00110
00111
00112 if (namelen >= strlen(path_check_suffix) + 1)
00113 {
00114 if (!stat (filename, &modstat))
00115 {
00116
00117 if (S_ISREG(modstat.st_mode))
00118 status = 1;
00119 if (strncmp(filename + (namelen - strlen(path_check_suffix)),
00120 path_check_suffix,
00121 strlen(path_check_suffix)))
00122 {
00123 status = 0;
00124 }
00125 }
00126 }
00127
00128 if (status)
00129 stp_deprintf(STP_DBG_PATH, "stp-path: file: `%s'\n", filename);
00130
00131 stp_free(filename);
00132 filename = NULL;
00133
00134 errno = savederr;
00135 return status;
00136 }
00137
00138
00139
00140
00141
00142 static char *
00143 stpi_path_merge(const char *path,
00144 const char *file)
00145 {
00146 char *filename;
00147 int namelen = strlen(path) + strlen(file) + 2;
00148 filename = (char *) stp_malloc(namelen * sizeof(char));
00149 strcpy (filename, path);
00150 strcat (filename, "/");
00151 strcat (filename, file);
00152 filename[namelen - 1] = '\0';
00153 return filename;
00154 }
00155
00156
00157
00158
00159
00160
00161 void
00162 stp_path_split(stp_list_t *list,
00163 const char *path)
00164 {
00165 const char *start = path;
00166 const char *end = NULL;
00167 char *dir = NULL;
00168 int len;
00169
00170 while (start)
00171 {
00172 end = (const char *) strchr(start, ':');
00173 if (!end)
00174 len = strlen(start) + 1;
00175 else
00176 len = (end - start);
00177
00178 if (len && !(len == 1 && !end))
00179 {
00180 dir = (char *) stp_malloc(len + 1);
00181 strncpy(dir, start, len);
00182 dir[len] = '\0';
00183 stp_list_item_create(list, NULL, dir);
00184 }
00185 if (!end)
00186 {
00187 start = NULL;
00188 break;
00189 }
00190 start = end + 1;
00191 }
00192 }