diff options
author | Mattias Andrée <maandree@kth.se> | 2021-04-11 20:39:49 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2021-04-11 20:39:49 +0200 |
commit | bc5c3034cab84e9a7d587c2590c2d3f06283972e (patch) | |
tree | f895ac024c2ff37907781890ee672c74e7a96ed4 /set-contact-numbers.c | |
parent | Rewrite set-contact-numbers to work like set-contact-addresses (diff) | |
download | contacts-bc5c3034cab84e9a7d587c2590c2d3f06283972e.tar.gz contacts-bc5c3034cab84e9a7d587c2590c2d3f06283972e.tar.bz2 contacts-bc5c3034cab84e9a7d587c2590c2d3f06283972e.tar.xz |
Improve set-contact- utils
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r-- | set-contact-numbers.c | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/set-contact-numbers.c b/set-contact-numbers.c index f75f9f5..b3ecfb2 100644 --- a/set-contact-numbers.c +++ b/set-contact-numbers.c @@ -1,14 +1,16 @@ /* See LICENSE file for copyright and license details. */ #include "common.h" -USAGE("[-C old-context] [-c new-context] [-N old-number] [-n new-number] [-F | -f] [-M | -m] [-u] contact-id"); +USAGE("[-C old-context] [-N old-number] [-F | -f] [-M | -m] ([-c new-context] [-n new-number] | -u) contact-id"); int main(int argc, char *argv[]) { int set_facsimile = -1, set_mobile = -1, add = 1, edit = 0, remove = 0; - const char *context, *number, *lookup_context, *lookup_number; + char *context = NULL, *number = NULL; + char *lookup_context = NULL, *lookup_number = NULL; + char *old_context = NULL, *old_number = NULL; struct passwd *user; struct libcontacts_contact contact; struct libcontacts_number **r, **w; @@ -68,7 +70,7 @@ main(int argc, char *argv[]) if (remove == edit) { if (edit) - eprintf("-u cannot be combined with -cn\n"); + usage(); eprintf("at least one of -cnu is required\n"); } @@ -101,12 +103,12 @@ main(int argc, char *argv[]) for (w = r++; (*w++ = *r++);); } else if (*r) { if (context) { - free(contact.numbers[i]->context); - contact.numbers[i]->context = estrdup(context); + old_context = contact.numbers[i]->context; + contact.numbers[i]->context = context; } if (number) { - free(contact.numbers[i]->number); - contact.numbers[i]->number = estrdup(number); + old_number = contact.numbers[i]->number; + contact.numbers[i]->number = number; } if (set_mobile >= 0) contact.numbers[i]->is_mobile = set_mobile; @@ -122,14 +124,17 @@ main(int argc, char *argv[]) contact.numbers = erealloc(contact.numbers, (i + 2) * sizeof(*contact.numbers)); contact.numbers[i + 1] = NULL; contact.numbers[i] = ecalloc(1, sizeof(**contact.numbers)); - contact.numbers[i]->context = estrdup(argv[1]); - contact.numbers[i]->number = estrdup(argv[2]); + contact.numbers[i]->context = context; + contact.numbers[i]->number = number; contact.numbers[i]->is_mobile = set_mobile > 0; contact.numbers[i]->is_facsimile = set_facsimile > 0; } if (libcontacts_save_contact(&contact, user)) eprintf("libcontacts_save_contact %s:", argv[0]); + + contact.numbers[i]->context = old_context; + contact.numbers[i]->number = old_number; libcontacts_contact_destroy(&contact); return 0; |