2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 PrefFile *prefs_read_open(const gchar *path)
37 g_return_val_if_fail(path != NULL, NULL);
39 if ((fp = fopen(path, "rb")) == NULL) {
40 FILE_OP_ERROR(tmppath, "fopen");
44 pfile = g_new(PrefFile, 1);
46 pfile->orig_fp = NULL;
47 pfile->path = g_strdup(path);
48 pfile->writing = FALSE;
53 PrefFile *prefs_write_open(const gchar *path)
59 g_return_val_if_fail(path != NULL, NULL);
61 if (prefs_is_readonly(path)) {
62 g_warning("no permission - %s\n", path);
66 tmppath = g_strconcat(path, ".tmp", NULL);
67 if ((fp = fopen(tmppath, "wb")) == NULL) {
68 FILE_OP_ERROR(tmppath, "fopen");
73 if (change_file_mode_rw(fp, tmppath) < 0)
74 FILE_OP_ERROR(tmppath, "chmod");
78 pfile = g_new(PrefFile, 1);
80 pfile->orig_fp = NULL;
81 pfile->path = g_strdup(path);
82 pfile->writing = TRUE;
87 gint prefs_file_close(PrefFile *pfile)
92 gchar *bakpath = NULL;
95 g_return_val_if_fail(pfile != NULL, -1);
98 orig_fp = pfile->orig_fp;
102 if (!pfile->writing) {
108 while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
111 if (fputs(buf, fp) == EOF) {
112 g_warning("failed to write configuration to file\n");
114 prefs_file_close_revert(pfile);
122 while (fgets(buf, sizeof(buf), orig_fp) != NULL)
123 if (fputs(buf, fp) == EOF) {
124 g_warning("failed to write configuration to file\n");
126 prefs_file_close_revert(pfile);
132 tmppath = g_strconcat(path, ".tmp", NULL);
133 if (fclose(fp) == EOF) {
134 FILE_OP_ERROR(tmppath, "fclose");
141 if (is_file_exist(path)) {
142 bakpath = g_strconcat(path, ".bak", NULL);
143 if (rename(path, bakpath) < 0) {
144 FILE_OP_ERROR(path, "rename");
153 if (rename(tmppath, path) < 0) {
154 FILE_OP_ERROR(tmppath, "rename");
168 gint prefs_file_close_revert(PrefFile *pfile)
172 g_return_val_if_fail(pfile != NULL, -1);
175 fclose(pfile->orig_fp);
177 tmppath = g_strconcat(pfile->path, ".tmp", NULL);
179 if (pfile->writing) {
180 if (unlink(tmppath) < 0) FILE_OP_ERROR(tmppath, "unlink");
189 gboolean prefs_is_readonly(const gchar * path)
194 return (access(path, W_OK) != 0 && access(path, F_OK) == 0);
197 gboolean prefs_rc_is_readonly(const gchar * rcfile)
205 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
206 result = prefs_is_readonly(rcpath);
212 gint prefs_set_block_label(PrefFile *pfile, const gchar *label)
217 block_label = g_strdup_printf("[%s]", label);
218 if (!pfile->writing) {
219 while (fgets(buf, sizeof(buf), pfile->fp) != NULL) {
222 val = strncmp(buf, block_label, strlen(block_label));
224 debug_print("Found %s\n", block_label);
229 if ((pfile->orig_fp = fopen(pfile->path, "rb")) != NULL) {
230 gboolean block_matched = FALSE;
232 while (fgets(buf, sizeof(buf), pfile->orig_fp) != NULL) {
235 val = strncmp(buf, block_label, strlen(block_label));
237 debug_print("Found %s\n", block_label);
238 block_matched = TRUE;
241 if (fputs(buf, pfile->fp) == EOF) {
242 g_warning("failed to write configuration to file\n");
243 fclose(pfile->orig_fp);
244 prefs_file_close_revert(pfile);
252 if (!block_matched) {
253 fclose(pfile->orig_fp);
254 pfile->orig_fp = NULL;
257 if (fputs(block_label, pfile->fp) == EOF ||
258 fputc('\n', pfile->fp) == EOF) {
259 g_warning("failed to write configuration to file\n");
260 fclose(pfile->orig_fp);
261 prefs_file_close_revert(pfile);