summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorReuben Thomas <rrt@sc3d.org>2017-02-04 15:43:44 +0000
committerReuben Thomas <rrt@sc3d.org>2017-02-04 15:43:44 +0000
commit566f717e110b0a74ff57d5c2627868b026536835 (patch)
tree9f64a85e986b93216211190c42b4ec6b6e765fc1
parentb70fbe9bddf7acfae2583f79cc1254b3879ed465 (diff)
downloadenchant-566f717e110b0a74ff57d5c2627868b026536835.tar.gz
Fix issue #24: update PWL more compatibly
Add a newline before the new word only if there isn’t one there already, then add a newline after. This is compatible with more update strategies, e.g. hunspell always adds the new word and then a newline. This enables users to share dictionaries between different spell-checkers with symlinks. This is also compatible with enchant <= 1.6.0, in which case (harmless) empty lines may be added to the PWL (e.g. if enchant 1.6.1 adds a word, then enchant 1.6.0).
-rw-r--r--src/pwl.c22
1 files changed, 16 insertions, 6 deletions
diff --git a/src/pwl.c b/src/pwl.c
index 3a2663d..3f97e5f 100644
--- a/src/pwl.c
+++ b/src/pwl.c
@@ -390,20 +390,30 @@ void enchant_pwl_add(EnchantPWL *pwl,
{
FILE *f;
- f = enchant_fopen(pwl->filename, "a");
+ f = enchant_fopen(pwl->filename, "a+");
if (f)
{
struct stat stats;
+ /* Since this function does not signal I/O
+ errors, only use return values to avoid
+ doing things that seem futile. */
+
enchant_lock_file (f);
if(g_stat(pwl->filename, &stats)==0)
pwl->file_changed = stats.st_mtime;
- /* we write the new line first since we can't guarantee
- that the file was terminated by a new line before
- and we are just appending to the end of the file */
- fwrite ("\n", sizeof(char), 1, f);
- fwrite (word, sizeof(char), len, f);
+ /* Add a newline if the file doesn't end with one. */
+ if (fseek (f, -1, SEEK_END) == 0 &&
+ getc (f) != '\n')
+ {
+ putc ('\n', f);
+ }
+
+ if (fwrite (word, sizeof(char), len, f) == len)
+ {
+ putc ('\n', f);
+ }
enchant_unlock_file (f);
fclose (f);
}