diff options
author | Mattias Andrée <maandree@kth.se> | 2021-04-03 02:28:44 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2021-04-03 02:28:44 +0200 |
commit | 49e4bd18d29d5ef9c85d106026b09d86de6ff19c (patch) | |
tree | acaad00d8d6df3a9dd6f0780fb837933b6ccddfd /set-contact-notes.c | |
download | contacts-49e4bd18d29d5ef9c85d106026b09d86de6ff19c.tar.gz contacts-49e4bd18d29d5ef9c85d106026b09d86de6ff19c.tar.bz2 contacts-49e4bd18d29d5ef9c85d106026b09d86de6ff19c.tar.xz |
First commmit
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r-- | set-contact-notes.c | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/set-contact-notes.c b/set-contact-notes.c new file mode 100644 index 0000000..6eeeeb4 --- /dev/null +++ b/set-contact-notes.c @@ -0,0 +1,62 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + +USAGE("[-u] contact-id"); + + +int +main(int argc, char *argv[]) +{ + int unset = 0; + struct passwd *user; + struct libcontacts_contact contact; + size_t size = 0, len = 0; + ssize_t r; + + ARGBEGIN { + case 'u': + unset = 1; + break; + default: + usage(); + } ARGEND; + + if (argc != 1) + usage(); + + if (!*argv[0] || strchr(argv[0], '/')) + usage(); + + errno = 0; + user = getpwuid(getuid()); + if (!user) + eprintf("getpwuid: %s\n", errno ? strerror(errno) : "user does not exist"); + + 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 (!unset) { + for (;;) { + if (len == size) + contact.notes = erealloc(contact.notes, size += 512); + r = read(STDIN_FILENO, &contact.notes[len], size - len); + if (r <= 0) { + if (!r) + break; + eprintf("read <stdin>:"); + } + len += (size_t)r; + } + if (size) + contact.notes[len] = '\0'; + } + + if (libcontacts_save_contact(&contact, user)) + eprintf("libcontacts_save_contact %s:", argv[0]); + + libcontacts_contact_destroy(&contact); + + return 0; +} |