From 4534ef242d82173755fedda1d8b233e89ed05d3f Mon Sep 17 00:00:00 2001 From: Emanuele Giaquinta Date: Fri, 16 Jan 2009 17:12:27 +0000 Subject: Use an io channel to write the config file. git-svn-id: file:///var/www/svn.irssi.org/SVN/irssi/trunk@4990 dbcabf3a-b0e7-0310-adc4-f8d773084564 --- src/lib-config/write.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'src/lib-config/write.c') diff --git a/src/lib-config/write.c b/src/lib-config/write.c index c5f0a617..5a06b177 100644 --- a/src/lib-config/write.c +++ b/src/lib-config/write.c @@ -32,7 +32,8 @@ static int config_write_indent(CONFIG_REC *rec) int n; for (n = 0; n < rec->tmp_indent_level/CONFIG_INDENT_SIZE; n++) { - if (write(rec->handle, indent_block, CONFIG_INDENT_SIZE) == -1) + if (g_io_channel_write_chars(rec->handle, indent_block, CONFIG_INDENT_SIZE, + NULL, NULL) == G_IO_STATUS_ERROR) return -1; } @@ -57,12 +58,14 @@ static int config_write_str(CONFIG_REC *rec, const char *str) p = strchr(strpos, '\n'); if (p == NULL) { - if (write(rec->handle, strpos, strlen(strpos)) == -1) + if (g_io_channel_write_chars(rec->handle, strpos, strlen(strpos), + NULL, NULL) == G_IO_STATUS_ERROR) return -1; strpos = ""; rec->tmp_last_lf = FALSE; } else { - if (write(rec->handle, strpos, (int) (p-strpos)+1) == -1) + if (g_io_channel_write_chars(rec->handle, strpos, (int) (p-strpos)+1, + NULL, NULL) == G_IO_STATUS_ERROR) return -1; strpos = p+1; rec->tmp_last_lf = TRUE; @@ -297,20 +300,20 @@ static int config_write_block(CONFIG_REC *rec, CONFIG_NODE *node, int list, int int config_write(CONFIG_REC *rec, const char *fname, int create_mode) { int ret; + int fd; g_return_val_if_fail(rec != NULL, -1); g_return_val_if_fail(fname != NULL || rec->fname != NULL, -1); g_return_val_if_fail(create_mode != -1 || rec->create_mode != -1, -1); - if (rec->handle != -1) - close(rec->handle); - - rec->handle = open(fname != NULL ? fname : rec->fname, + fd = open(fname != NULL ? fname : rec->fname, O_WRONLY | O_TRUNC | O_CREAT, create_mode != -1 ? create_mode : rec->create_mode); - if (rec->handle == -1) + if (fd == -1) return config_error(rec, g_strerror(errno)); + rec->handle = g_io_channel_unix_new(fd); + g_io_channel_set_encoding(rec->handle, NULL, NULL); rec->tmp_indent_level = 0; rec->tmp_last_lf = TRUE; ret = config_write_block(rec, rec->mainnode, FALSE, TRUE); @@ -319,8 +322,8 @@ int config_write(CONFIG_REC *rec, const char *fname, int create_mode) config_error(rec, errno == 0 ? "bug" : g_strerror(errno)); } - close(rec->handle); - rec->handle = -1; + g_io_channel_unref(rec->handle); + rec->handle = NULL; return ret; } -- cgit v1.2.3