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 static int
00044 dirent_sort(const void *a,
00045 const void *b)
00046 {
00047 return strcoll ((*(const struct dirent **) a)->d_name,
00048 (*(const struct dirent **) b)->d_name);
00049 }
00050
00051
00052
00053
00054
00055 stp_list_t *
00056 stp_path_search(stp_list_t *dirlist,
00057 const char *suffix)
00058 {
00059 stp_list_t *findlist;
00060 stp_list_item_t *diritem;
00061 struct dirent** module_dir;
00062 char *module_name;
00063 int n;
00064
00065 if (!dirlist)
00066 return NULL;
00067
00068 path_check_suffix = suffix;
00069
00070 findlist = stp_list_create();
00071 if (!findlist)
00072 return NULL;
00073 stp_list_set_freefunc(findlist, stp_list_node_free_data);
00074
00075 diritem = stp_list_get_start(dirlist);
00076 while (diritem)
00077 {
00078 path_check_path = (const char *) stp_list_item_get_data(diritem);
00079 stp_deprintf(STP_DBG_PATH, "stp-path: directory: %s\n",
00080 (const char *) stp_list_item_get_data(diritem));
00081 n = scandir ((const char *) stp_list_item_get_data(diritem),
00082 &module_dir, stpi_path_check, dirent_sort);
00083 if (n >= 0)
00084 {
00085 int idx;
00086 for (idx = 0; idx < n; ++idx)
00087 {
00088 module_name = stpi_path_merge((const char *) stp_list_item_get_data(diritem),
00089 module_dir[idx]->d_name);
00090 stp_list_item_create(findlist, NULL, module_name);
00091 free (module_dir[idx]);
00092 }
00093 free (module_dir);
00094 }
00095 diritem = stp_list_item_next(diritem);
00096 }
00097 return findlist;
00098 }
00099
00100
00101
00102
00103
00104
00105 static int
00106 stpi_path_check(const struct dirent *module)
00107 {
00108 int namelen;
00109 int status = 0;
00110 int savederr;
00111 char *filename;
00112 struct stat modstat;
00113
00114 savederr = errno;
00115
00116 filename = stpi_path_merge(path_check_path, module->d_name);
00117
00118 namelen = strlen(filename);
00119
00120
00121 if (namelen >= strlen(path_check_suffix) + 1)
00122 {
00123 if (!stat (filename, &modstat))
00124 {
00125
00126 if (S_ISREG(modstat.st_mode))
00127 status = 1;
00128 if (strncmp(filename + (namelen - strlen(path_check_suffix)),
00129 path_check_suffix,
00130 strlen(path_check_suffix)))
00131 {
00132 status = 0;
00133 }
00134 }
00135 }
00136
00137 if (status)
00138 stp_deprintf(STP_DBG_PATH, "stp-path: file: `%s'\n", filename);
00139
00140 stp_free(filename);
00141 filename = NULL;
00142
00143 errno = savederr;
00144 return status;
00145 }
00146
00147
00148
00149
00150
00151 static char *
00152 stpi_path_merge(const char *path,
00153 const char *file)
00154 {
00155 char *filename;
00156 int namelen = strlen(path) + strlen(file) + 2;
00157 filename = (char *) stp_malloc(namelen * sizeof(char));
00158 strcpy (filename, path);
00159 strcat (filename, "/");
00160 strcat (filename, file);
00161 filename[namelen - 1] = '\0';
00162 return filename;
00163 }
00164
00165
00166
00167
00168
00169
00170 void
00171 stp_path_split(stp_list_t *list,
00172 const char *path)
00173 {
00174 const char *start = path;
00175 const char *end = NULL;
00176 char *dir = NULL;
00177 int len;
00178
00179 while (start)
00180 {
00181 end = (const char *) strchr(start, ':');
00182 if (!end)
00183 len = strlen(start) + 1;
00184 else
00185 len = (end - start);
00186
00187 if (len && !(len == 1 && !end))
00188 {
00189 dir = (char *) stp_malloc(len + 1);
00190 strncpy(dir, start, len);
00191 dir[len] = '\0';
00192 stp_list_item_create(list, NULL, dir);
00193 }
00194 if (!end)
00195 {
00196 start = NULL;
00197 break;
00198 }
00199 start = end + 1;
00200 }
00201 }