} END_TEST START_TEST(test_fix_path_basic) { gchar* result = girara_fix_path("test"); fail_unless(g_strcmp0(result, "test") == 0, "Fix path result does not match (got: %s, expected: %s)", result, "test", NULL); g_free(result); result = girara_fix_path("test/test"); fail_unless(g_strcmp0(result, "test/test") == 0, "Fix path result does not match (got: %s, expected: %s)", result, "test/test", NULL); g_free(result); } END_TEST
} END_TEST START_TEST(test_fix_path_extended) { gchar* oldenv = g_getenv("HOME") ? g_strdup(g_getenv("HOME")) : NULL; g_unsetenv("HOME"); girara_list_t* list = read_pwd_info(); GIRARA_LIST_FOREACH(list, pwd_info_t*, iter, pwdinfo) gchar* path = g_strdup_printf("~%s/test", pwdinfo->name); gchar* eres = g_build_filename(pwdinfo->dir, "test", NULL); gchar* result = girara_fix_path(path); fail_unless(g_strcmp0(result, eres) == 0, "Fix path result does not match (got: %s, expected %s)", result, eres, NULL); g_free(result); g_free(eres); g_free(path); GIRARA_LIST_FOREACH_END(list, pwd_info_t*, iter, pwdinfo); girara_list_free(list); if (oldenv) { g_setenv("HOME", oldenv, TRUE); g_free(oldenv); } } END_TEST
FILE* girara_file_open(const char* path, const char* mode) { if (path == NULL || mode == NULL) { return NULL; } char* fixed_path = girara_fix_path(path); if (fixed_path == NULL) { return NULL; } FILE* fp = fopen(fixed_path, mode); g_free(fixed_path); if (fp == NULL) { return NULL; } return fp; }
FILE* girara_file_open(const char* path, const char* mode) { if (path == NULL || mode == NULL) { return NULL; } char* fixed_path = girara_fix_path(path); if (fixed_path == NULL) { return NULL; } FILE* fp = fopen(fixed_path, mode); g_free(fixed_path); if (fp == NULL) { return NULL; } return fp; /* TODO */ /*FILE* fp;*/ /*struct stat lstat;*/ /*struct stat fstat;*/ /*int fd;*/ /*char* mode = "rb+";*/ /*if (lstat(path, &lstat) == -1) {*/ /*if (errno != ENOENT) {*/ /*return NULL;*/ /*}*/ /*if ((fd = open(path, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1) {*/ /*return NULL;*/ /*}*/ /*mode = "wb";*/ /*} else {*/ /*if ((fd = open(path, O_RDONLY)) == -1) {*/ /*return NULL;*/ /*}*/ /*if (fstat(fd, &fstat) == -1) {*/ /*if (lstat.st_mode != fstat.st_mode ||*/ /*lstat.st_ino != fstat.st_ino ||*/ /*lstat.st_dev != fstat.st_dev) {*/ /*close(fd);*/ /*return NULL;*/ /*}*/ /*}*/ /*ftruncate(fd, 0);*/ /*}*/ /*if ((fp = fdopen(fd, mode)) == NULL) {*/ /*close(fd);*/ /*unlink(path);*/ /*return NULL;*/ /*}*/ /*return fp;*/ }
bool cmd_export(girara_session_t* session, girara_list_t* argument_list) { g_return_val_if_fail(session != NULL, false); g_return_val_if_fail(session->global.data != NULL, false); zathura_t* zathura = session->global.data; if (zathura->document == NULL) { girara_notify(session, GIRARA_ERROR, _("No document opened.")); return false; } if (girara_list_size(argument_list) != 2) { girara_notify(session, GIRARA_ERROR, _("Invalid number of arguments given.")); return false; } const char* file_identifier = girara_list_nth(argument_list, 0); const char* file_name = girara_list_nth(argument_list, 1); if (file_name == NULL || file_identifier == NULL) { return false; } char* export_path = girara_fix_path(file_name); if (export_path == NULL) { return false; } /* attachment */ if (strncmp(file_identifier, "attachment-", strlen("attachment-")) == 0) { if (zathura_document_attachment_save(zathura->document, file_identifier + strlen("attachment-"), export_path) == false) { girara_notify(session, GIRARA_ERROR, _("Couldn't write attachment '%s' to '%s'."), file_identifier, file_name); } else { girara_notify(session, GIRARA_INFO, _("Wrote attachment '%s' to '%s'."), file_identifier, export_path); } /* image */ } else if (strncmp(file_identifier, "image-p", strlen("image-p")) == 0 && strlen(file_identifier) >= 10) { /* parse page id */ const char* input = file_identifier + strlen("image-p"); int page_id = atoi(input); if (page_id == 0) { goto image_error; } /* parse image id */ input = strstr(input, "-"); if (input == NULL) { goto image_error; } int image_id = atoi(input + 1); if (image_id == 0) { goto image_error; } /* get image */ zathura_page_t* page = zathura_document_get_page(zathura->document, page_id - 1); if (page == NULL) { goto image_error; } girara_list_t* images = zathura_page_images_get(page, NULL); if (images == NULL) { goto image_error; } zathura_image_t* image = girara_list_nth(images, image_id - 1); if (image == NULL) { goto image_error; } cairo_surface_t* surface = zathura_page_image_get_cairo(page, image, NULL); if (surface == NULL) { goto image_error; } if (cairo_surface_write_to_png(surface, export_path) == CAIRO_STATUS_SUCCESS) { girara_notify(session, GIRARA_INFO, _("Wrote image '%s' to '%s'."), file_identifier, export_path); } else { girara_notify(session, GIRARA_ERROR, _("Couldn't write image '%s' to '%s'."), file_identifier, file_name); } goto error_ret; image_error: girara_notify(session, GIRARA_ERROR, _("Unknown image '%s'."), file_identifier); goto error_ret; /* unknown */ } else { girara_notify(session, GIRARA_ERROR, _("Unknown attachment or image '%s'."), file_identifier); } error_ret: g_free(export_path); return true; }
static girara_completion_t* list_files_for_cc(zathura_t* zathura, const char* input, bool check_file_ext) { girara_completion_t* completion = girara_completion_init(); girara_completion_group_t* group = girara_completion_group_create(zathura->ui.session, NULL); gchar* path = NULL; gchar* current_path = NULL; if (completion == NULL || group == NULL) { goto error_free; } path = girara_fix_path(input); if (path == NULL) { goto error_free; } /* If the path does not begin with a slash we update the path with the current * working directory */ if (strlen(path) == 0 || path[0] != '/') { long path_max; #ifdef PATH_MAX path_max = PATH_MAX; #else path_max = pathconf(path,_PC_PATH_MAX); if (path_max <= 0) path_max = 4096; #endif char cwd[path_max]; if (getcwd(cwd, path_max) == NULL) { goto error_free; } char* tmp_path = g_strdup_printf("%s/%s", cwd, path); g_free(path); path = tmp_path; } /* Append a slash if the given argument is a directory */ bool is_dir = (path[strlen(path) - 1] == '/') ? true : false; if ((g_file_test(path, G_FILE_TEST_IS_DIR) == TRUE) && !is_dir) { char* tmp_path = g_strdup_printf("%s/", path); g_free(path); path = tmp_path; is_dir = true; } /* get current path */ char* tmp = g_strdup(path); current_path = is_dir ? g_strdup(tmp) : g_strdup(dirname(tmp)); g_free(tmp); /* get current file */ gchar* current_file = is_dir ? "" : basename(path); int current_file_length = strlen(current_file); /* read directory */ if (g_file_test(current_path, G_FILE_TEST_IS_DIR) == TRUE) { girara_list_t* names = list_files(zathura, current_path, current_file, current_file_length, is_dir, check_file_ext); if (!names) { goto error_free; } GIRARA_LIST_FOREACH(names, const char*, iter, file) girara_completion_group_add_element(group, file, NULL); GIRARA_LIST_FOREACH_END(names, const char*, iter, file); girara_list_free(names); }