diff options
author | Mattias Andrée <maandree@kth.se> | 2021-04-12 14:26:49 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2021-04-12 14:26:49 +0200 |
commit | dfb540fcbbbfb12a2cf598ca7fae34d174da79b1 (patch) | |
tree | 298a6c882fc972c1a4b5b5d68955d32ca36cb635 /set-contact-notes.c | |
parent | m (diff) | |
download | contacts-dfb540fcbbbfb12a2cf598ca7fae34d174da79b1.tar.gz contacts-dfb540fcbbbfb12a2cf598ca7fae34d174da79b1.tar.bz2 contacts-dfb540fcbbbfb12a2cf598ca7fae34d174da79b1.tar.xz |
Fix bug in set-contact-notes and add -a
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'set-contact-notes.c')
-rw-r--r-- | set-contact-notes.c | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/set-contact-notes.c b/set-contact-notes.c index 6fde429..d2f636d 100644 --- a/set-contact-notes.c +++ b/set-contact-notes.c @@ -1,19 +1,22 @@ /* See LICENSE file for copyright and license details. */ #include "common.h" -USAGE("[-u] contact-id"); /* TODO add -a (append) */ +USAGE("[-a | -u] contact-id"); int main(int argc, char *argv[]) { - int unset = 0; + int append = 0, unset = 0; struct passwd *user; struct libcontacts_contact contact; size_t size = 0, len = 0; ssize_t r; ARGBEGIN { + case 'a': + append = 1; + break; case 'u': unset = 1; break; @@ -21,7 +24,7 @@ main(int argc, char *argv[]) usage(); } ARGEND; - if (argc != 1) + if (argc != 1 || (append && unset)) usage(); if (!*argv[0] || strchr(argv[0], '/')) @@ -35,8 +38,15 @@ main(int argc, char *argv[]) if (libcontacts_load_contact(argv[0], &contact, user)) eprintf("libcontacts_load_contact %s: %s\n", argv[0], errno ? strerror(errno) : "contact file is malformatted"); - free(contact.notes); - contact.notes = NULL; + if (contact.notes) { + if (append) { + len = strlen(contact.notes); + size = len + 1; + } else { + free(contact.notes); + contact.notes = NULL; + } + } if (!unset) { for (;;) { if (len == size) @@ -49,8 +59,11 @@ main(int argc, char *argv[]) } len += (size_t)r; } - if (size) + if (size) { + if (len == size) + contact.notes = erealloc(contact.notes, size += 1); contact.notes[len] = '\0'; + } } if (libcontacts_save_contact(&contact, user)) |