From c2b33333b84dd0534b2cb153c7a9f31c354ddad4 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 3 Apr 2021 12:55:33 +0200 Subject: m + add more tools MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- find-contact-by-organisation.c | 79 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 find-contact-by-organisation.c (limited to 'find-contact-by-organisation.c') diff --git a/find-contact-by-organisation.c b/find-contact-by-organisation.c new file mode 100644 index 0000000..8287225 --- /dev/null +++ b/find-contact-by-organisation.c @@ -0,0 +1,79 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + +USAGE("[-o organisation] [-t title] [-OT]"); + + +int +main(int argc, char *argv[]) +{ + int display_organisation = 0, display_title = 0; + struct passwd *user; + struct libcontacts_contact **contacts; + struct libcontacts_organisation **orgs, *org; + char *organisation = NULL, *title = NULL; + size_t i; + + ARGBEGIN { + case 'o': + if (organisation) + usage(); + organisation = ARG(); + break; + case 't': + if (title) + usage(); + title = ARG(); + break; + case 'O': + display_organisation = 1; + break; + case 'T': + display_title = 1; + break; + default: + usage(); + } ARGEND; + + if (argc) + usage(); + + if (!organisation && !title && !display_organisation && !display_title) + display_organisation = 1; + + errno = 0; + user = getpwuid(getuid()); + if (!user) + eprintf("getpwuid: %s\n", errno ? strerror(errno) : "user does not exist"); + + if (libcontacts_load_contacts(&contacts, user)) + eprintf("libcontacts_load_contacts:"); + for (i = 0; contacts[i]; i++) { + if ((orgs = contacts[i]->organisations)) { + for (; (org = *orgs); orgs++) { + if (organisation && strcmpnul(org->organisation, organisation)) + continue; + if (title && strcmpnul(org->title, title)) + continue; + if (display_organisation && display_title) { + printf("%s (%s: %s)\n", contacts[i]->id, org->organisation, org->title); + } else if (display_organisation) { + printf("%s (%s)\n", contacts[i]->id, org->organisation); + } else if (display_title) { + printf("%s (%s)\n", contacts[i]->id, org->title); + } else { + printf("%s\n", contacts[i]->id); + break; + } + } + } + libcontacts_contact_destroy(contacts[i]); + free(contacts[i]); + } + free(contacts); + + if (fflush(stdout) || ferror(stdout) || fclose(stdout)) + eprintf("printf:"); + + return 0; +} -- cgit v1.2.3-70-g09d2