aboutsummaryrefslogtreecommitdiffstats
path: root/add-contact.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2021-04-12 13:37:59 +0200
committerMattias Andrée <maandree@kth.se>2021-04-12 13:37:59 +0200
commitfa88a28e46b7ac2d3dcd1938e5e4708157d08b22 (patch)
treec3ca4f98374328f37d71d1079f9e1f7406ed84d8 /add-contact.c
parentMinor fix to set-contact-photos (diff)
downloadcontacts-fa88a28e46b7ac2d3dcd1938e5e4708157d08b22.tar.gz
contacts-fa88a28e46b7ac2d3dcd1938e5e4708157d08b22.tar.bz2
contacts-fa88a28e46b7ac2d3dcd1938e5e4708157d08b22.tar.xz
Deduplicate code and make some small improvements
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'add-contact.c')
-rw-r--r--add-contact.c26
1 files changed, 21 insertions, 5 deletions
diff --git a/add-contact.c b/add-contact.c
index 843eb73..382d1a0 100644
--- a/add-contact.c
+++ b/add-contact.c
@@ -9,13 +9,29 @@ main(int argc, char *argv[])
{
struct passwd *user;
struct libcontacts_contact contact;
+ const char *id = NULL;
char *path;
int fd;
+ size_t i;
NOFLAGS(argc > 1);
- if (argc && (!*argv[0] || strchr(argv[0], '/')))
- usage();
+ if (argc) {
+ id = argv[0];
+ if (!id[0])
+ usage();
+ for (i = 0; id[i]; i++) {
+ if (id[i] == '/')
+ eprintf("contact-id cannot contain /\n");
+ else if (!islower(id[i]) && !isdigit(id[i]) && (i && id[i] != '.') && id[i] != '-')
+ if (id[i] != '~' || id[i + 1])
+ weprintf("it is recommended to only have [a-z0-9-] in contact-id\n");
+ }
+ if (id[i - 1] == '~')
+ eprintf("contact-id cannot end with ~\n");
+ if (id[0] == '.' && strcmp(id, ".me") && strcmp(id, ".groups"))
+ weprintf("it is recommended to only have [a-z0-9-] in contact-id\n");
+ }
errno = 0;
user = getpwuid(getuid());
@@ -26,10 +42,10 @@ main(int argc, char *argv[])
if (access(user->pw_dir, R_OK | W_OK | X_OK))
eprintf("access %s R_OK|W_OK|X_OK:", user->pw_dir);
- if (argc) {
- path = libcontacts_get_path(argv[0], user);
+ if (id) {
+ path = libcontacts_get_path(id, user);
if (!path)
- eprintf("libcontacts_get_path %s:", argv[0]);
+ eprintf("libcontacts_get_path %s:", id);
fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0666);
if (fd < 0)
eprintf("open %s O_WRONLY|O_CREAT|O_EXCL 0666:", path);