Example #1
0
/*
 * Read the file "name" into the returned buffer.
 * Set "url" to be a pointer to the file-name 
 */
static char *
read_whole_file(const char *name)
{
	int		 fd;
	char		*p;
	void		*pp;
	char		 buf[BUFSIZ];
	ssize_t		 ssz;
	size_t		 sz;
	struct ical	*ical;

	/*
	 * Open the file readonly and read it in BUFSIZ at a time til
	 * we've read the whole file.
	 * Nil-terminate always.
	 */
	if (-1 == (fd = open(name, O_RDONLY, 0))) {
		perror(name);
		return(NULL);
	}
	ical = NULL;
	p = NULL;
	sz = 0;
	while ((ssz = read(fd, buf, sizeof(buf))) > 0) {
		pp = realloc(p, sz + ssz + 1);
		if (NULL == pp) {
			perror(NULL);
			goto err;
		}
		p = pp;
		memcpy(&p[sz], buf, ssz);
		p[sz + ssz] = '\0';
		sz += ssz;
	}
	if (ssz < 0) {
		perror(name);
		goto err;
	}
	close(fd);
	fd = -1;

	/* Parse as an iCalendar. */
	if (NULL == (ical = ical_parse(name, p, sz))) 
		goto err;

	ical_free(ical);
	return(p);
err:
	ical_free(ical);
	free(p);
	if (-1 != fd)
		close(fd);
	return(NULL);
}
Example #2
0
int
main(int argc, char *argv[])
{
	int		 fd, c;
	struct stat	 st;
	size_t		 i, sz;
	char		*map;
	struct ical	*p = NULL;

	if (-1 != (c = getopt(argc, argv, "")))
		return(EXIT_FAILURE);

	argc -= optind;
	argv += optind;

	if (0 == argc)
		return(EXIT_FAILURE);

	if (-1 == (fd = open(argv[0], O_RDONLY, 0))) {
		perror(argv[0]);
		return(EXIT_FAILURE);
	} else if (-1 == fstat(fd, &st)) {
		perror(argv[0]);
		close(fd);
		return(EXIT_FAILURE);
	} 

	sz = st.st_size;
	map = mmap(NULL, sz, PROT_READ, MAP_SHARED, fd, 0);
	close(fd);

	if (MAP_FAILED == map) {
		perror(argv[0]);
		return(EXIT_FAILURE);
	} else if (NULL != (p = ical_parse(argv[0], map, sz))) {
		for (i = 0; i < ICALTYPE__MAX; i++)
			if (NULL != p->comps[i])
				ical_printcomp(p->comps[i]);
		fflush(stdout);
		ical_printfile(STDOUT_FILENO, p);
	}

	munmap(map, sz);
	ical_free(p);
	return(NULL == p ? EXIT_FAILURE : EXIT_SUCCESS);
}