aboutsummaryrefslogtreecommitdiffstats
path: root/find-contact-by-organisation.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2021-04-03 12:55:33 +0200
committerMattias Andrée <maandree@kth.se>2021-04-03 12:55:33 +0200
commitc2b33333b84dd0534b2cb153c7a9f31c354ddad4 (patch)
tree21295fb55d63eeffefa43472f76c69de88979c9f /find-contact-by-organisation.c
parentm + Add multicall binary (diff)
downloadcontacts-c2b33333b84dd0534b2cb153c7a9f31c354ddad4.tar.gz
contacts-c2b33333b84dd0534b2cb153c7a9f31c354ddad4.tar.bz2
contacts-c2b33333b84dd0534b2cb153c7a9f31c354ddad4.tar.xz
m + add more tools
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'find-contact-by-organisation.c')
-rw-r--r--find-contact-by-organisation.c79
1 files changed, 79 insertions, 0 deletions
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;
+}