summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDom Lachowicz <domlachowicz@gmail.com>2007-10-29 12:04:21 +0000
committerDom Lachowicz <domlachowicz@gmail.com>2007-10-29 12:04:21 +0000
commit055bbd79414b50cb035e0cfdc74fcb5a202480c2 (patch)
tree140e2c79fdf36e698c25e41c62b3a6c27f83bd94
parentadf68fe304ae9e86eee334b9fbf9d5fb6dc53447 (diff)
downloadenchant-055bbd79414b50cb035e0cfdc74fcb5a202480c2.tar.gz
ensure that enchant_pwl_init_with_file() can actually create the requested file. otherwise, the pwl additions just get dropped on the floor. from eric albright.
git-svn-id: svn+ssh://svn.abisource.com/svnroot/enchant/trunk@22222 bcba8976-2d24-0410-9c9c-aab3bd5fdfd6
-rw-r--r--src/pwl.c29
1 files changed, 26 insertions, 3 deletions
diff --git a/src/pwl.c b/src/pwl.c
index 499f8d0..9a0238d 100644
--- a/src/pwl.c
+++ b/src/pwl.c
@@ -55,11 +55,22 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#ifdef _WIN32
+#include <io.h>
+#endif
#include <glib.h>
#include "pwl.h"
+#if defined(_MSC_VER)
+#pragma warning(disable: 4996) /* The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name. */
+#endif
+
#if defined(HAVE_FLOCK) || defined(HAVE_LOCKF)
#include <unistd.h>
#include <sys/file.h>
@@ -199,7 +210,8 @@ enchant_unlock_file (FILE * f)
/**
* enchant_pwl_init
*
- * Returns: a new PWL object used to store/check/suggest words.
+ * Returns: a new PWL object used to store/check/suggest words
+ * or NULL if the file cannot be opened or created
*/
EnchantPWL* enchant_pwl_init(void)
{
@@ -223,14 +235,21 @@ EnchantPWL* enchant_pwl_init(void)
EnchantPWL* enchant_pwl_init_with_file(const char * file)
{
FILE *f;
+ int fd;
EnchantPWL *pwl;
g_return_val_if_fail (file != NULL, NULL);
+ fd = open(file, O_CREAT | O_RDONLY, S_IREAD | S_IWRITE);
+ if(fd == -1)
+ {
+ return NULL;
+ }
+
pwl = enchant_pwl_init();
pwl->filename = g_strdup(file);
- f = fopen (file, "r");
+ f = fdopen(fd, "r");
if (f)
{
char line[BUFSIZ];
@@ -248,7 +267,11 @@ EnchantPWL* enchant_pwl_init_with_file(const char * file)
enchant_unlock_file (f);
fclose (f);
- }
+ }
+ else
+ {
+ close(fd);
+ }
return pwl;
}