| 282 | | GList *filename_complete(const char *path, const char *default_path) |
| 283 | | { |
| 284 | | GList *list; |
| 285 | | DIR *dirp; |
| 286 | | struct dirent *dp; |
| 287 | | const char *basename; |
| 288 | | char *realpath, *dir, *name; |
| 289 | | int len; |
| 290 | | |
| 291 | | g_return_val_if_fail(path != NULL, NULL);
|
| 292 | | |
| 293 | | list = NULL;
|
| 294 | | |
| 295 | | |
| 296 | | realpath = convert_home(path); |
| 297 | | if (USE_DEFAULT_PATH(realpath, default_path)) {
|
| 298 | | g_free(realpath); |
| 299 | | realpath = g_strconcat(default_path, G_DIR_SEPARATOR_S,
|
| 300 | | path, NULL);
|
| 301 | | } |
| 302 | | |
| 303 | | |
| 304 | | dir = g_path_get_dirname(realpath); |
| 305 | | dirp = opendir(dir); |
| 306 | | g_free(dir); |
| 307 | | g_free(realpath); |
| 308 | | |
| 309 | | if (dirp == NULL)
|
| 310 | | return NULL;
|
| 311 | | |
| 312 | | dir = g_path_get_dirname(path); |
| 313 | | if (*dir == G_DIR_SEPARATOR && dir[1] == '\0') {
|
| 314 | | |
| 315 | | *dir = '\0'; |
| 316 | | } else if (IS_CURRENT_DIR(dir) && !IS_CURRENT_DIR(path)) {
|
| 317 | | |
| 318 | | |
| 319 | | g_free_and_null(dir);
|
| 320 | | } |
| 321 | | |
| 322 | | basename = g_basename(path); |
| 323 | | len = strlen(basename); |
| 324 | | |
| 325 | | |
| 326 | | while ((dp = readdir(dirp)) != NULL) {
|
| 327 | | if (dp->d_name[0] == '.') { |
| 328 | | if (dp->d_name[1] == '\0' || |
| 329 | | (dp->d_name[1] == '.' && dp->d_name[2] == '\0')) |
| 330 | | continue; |
| 331 | | |
| 332 | | if (basename[0] != '.') |
| 333 | | continue; |
| 334 | | } |
| 335 | | |
| 336 | | if (len == 0 || strncmp(dp->d_name, basename, len) == 0) { |
| 337 | | name = dir == NULL ? g_strdup(dp->d_name) :
|
| 338 | | g_strdup_printf("%s"G_DIR_SEPARATOR_S"%s", dir, dp->d_name);
|
| 339 | | list = list_add_file(list, name, default_path); |
| 340 | | g_free(name); |
| 341 | | } |
| 342 | | } |
| 343 | | closedir(dirp); |
| 344 | | |
| 345 | | g_free_not_null(dir);
|
| 346 | | return list; |
| 347 | | } |