aboutsummaryrefslogtreecommitdiffstats
path: root/libcontacts_load_contact.c
blob: af931f9bd17853218dd25a1ebb99cc257a40f9be (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* See LICENSE file for copyright and license details. */
#include "common.h"


int
libcontacts_load_contact(const char *id, struct libcontacts_contact *contactp, const struct passwd *user)
{
	int ret, fd, saved_errno;
	char *data = NULL, *path;
	size_t p = 0, n = 0;
	ssize_t r;
	void *new;

	if (!contactp) {
		errno = EINVAL;
		return -1;
	}

	path = libcontacts_get_path(id, user);
	if (!path)
		return -1;
	fd = open(path, O_RDONLY);
	free(path);
	if (fd < 0)
		return -1;

	for (;;) {
		if (p == n) {
			new = realloc(data, n + (8 << 10) + 1);
			if (!new)
				goto fail;
			n += 8 << 10;
			data = new;
		}
		r = read(fd, &data[p], n - p);
		if (r <= 0) {
			if (r)
				goto fail;
			break;
		}
		p += (size_t)r;
	}
	data[p] = '\0';

	if (memchr(data, '\0', p)) {
		errno = 0;
		goto fail;
	}

	close(fd);
	ret = libcontacts_parse_contact(data, contactp);
	free(data);
	if (!ret && !(contactp->id = strdup(id))) {
		libcontacts_contact_destroy(contactp);
		memset(contactp, 0, sizeof(*contactp));
		return -1;
	}
	return ret;

fail:
	saved_errno = errno;
	free(data);
	close(fd);
	errno = saved_errno;
	return -1;
}