aboutsummaryrefslogtreecommitdiffstats
path: root/get-contact-groups.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 /get-contact-groups.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 '')
-rw-r--r--get-contact-groups.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/get-contact-groups.c b/get-contact-groups.c
new file mode 100644
index 0000000..fa1eb77
--- /dev/null
+++ b/get-contact-groups.c
@@ -0,0 +1,68 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+USAGE("[-g group | -n] contact-id ...");
+
+
+int
+main(int argc, char *argv[])
+{
+ int lookup_unassigned = 0;
+ struct passwd *user;
+ struct libcontacts_contact contact;
+ char *lookup_group = NULL;
+ char **groups;
+ int ret = 0;
+ size_t i;
+
+ ARGBEGIN {
+ case 'g':
+ if (lookup_group)
+ usage();
+ lookup_group = ARG();
+ break;
+ case 'n':
+ lookup_unassigned = 1;
+ break;
+ default:
+ usage();
+ } ARGEND;
+
+ if (!argc)
+ usage();
+
+ for (i = 0; argv[i]; i++)
+ if (!*argv[i] || strchr(argv[i], '/'))
+ usage();
+
+ errno = 0;
+ user = getpwuid(getuid());
+ if (!user)
+ eprintf("getpwuid: %s\n", errno ? strerror(errno) : "user does not exist");
+
+ for (; *argv; argv++) {
+ if (libcontacts_load_contact(*argv, &contact, user)) {
+ weprintf("libcontacts_load_contact %s: %s\n", *argv, errno ? strerror(errno) : "contact file is malformatted");
+ ret = 1;
+ }
+ if (lookup_unassigned) {
+ if (!contact.groups || !*contact.groups)
+ printf("%s\n", *argv);
+ } else if ((groups = contact.groups)) {
+ for (; *groups; groups++) {
+ if (lookup_group) {
+ printf("%s\n", *argv);
+ } else {
+ if (argc > 1)
+ printf("%s: ", *argv);
+ printf("%s\n", *groups);
+ }
+ }
+ }
+ libcontacts_contact_destroy(&contact);
+ }
+
+ if (fflush(stdout) || ferror(stdout) || fclose(stdout))
+ eprintf("printf:");
+ return ret;
+}