~sircmpwn/gmni

77b73efbcd3ea7ed9e3e4c0aa19d9247e21d3c87 — Eyal Sawady 3 months ago 93f3052
all: use posix_dirname rather than dirname
2 files changed, 10 insertions(+), 22 deletions(-)

M src/gmnlm.c
M src/tofu.c
M src/gmnlm.c => src/gmnlm.c +6 -13
@@ 162,18 162,15 @@ save_bookmark(struct browser *browser)
	size_t n;

	n = snprintf(path, sizeof(path), path_fmt, "bookmarks.gmi");
	free(path_fmt);
	assert(n < sizeof(path));
	strncpy(dname, dirname(path), sizeof(dname)-1);
	posix_dirname(path, dname);
	if (mkdirs(dname, 0755) != 0) {
		snprintf(path, sizeof(path), path_fmt, "bookmarks.gmi");
		free(path_fmt);
		fprintf(stderr, "Error creating directory %s: %s\n",
				dirname(path), strerror(errno));
				dname, strerror(errno));
		return;
	}

	snprintf(path, sizeof(path), path_fmt, "bookmarks.gmi");
	free(path_fmt);
	FILE *f = fopen(path, "a");
	if (!f) {
		fprintf(stderr, "Error opening %s for writing: %s\n",


@@ 203,19 200,15 @@ open_bookmarks(struct browser *browser)
	size_t n;

	n = snprintf(path, sizeof(path), path_fmt, "bookmarks.gmi");
	free(path_fmt);
	assert(n < sizeof(path));
	strncpy(dname, dirname(path), sizeof(dname)-1);
	posix_dirname(path, dname);
	if (mkdirs(dname, 0755) != 0) {
		snprintf(path, sizeof(path), path_fmt, "bookmarks.gmi");
		free(path_fmt);
		fprintf(stderr, "Error creating directory %s: %s\n",
				dirname(path), strerror(errno));
				dname, strerror(errno));
		return;
	}

	snprintf(path, sizeof(path), path_fmt, "bookmarks.gmi");
	free(path_fmt);

	struct stat buf;
	if (stat(path, &buf) == -1 && errno == ENOENT) {
		// TOCTOU, but we almost certainly don't care

M src/tofu.c => src/tofu.c +4 -9
@@ 177,21 177,16 @@ gemini_tofu_init(struct gemini_tofu *tofu, tofu_callback_t *cb, void *cb_data)
	n = snprintf(tofu->known_hosts_path,
		sizeof(tofu->known_hosts_path),
		path_fmt, "known_hosts");
	free(path_fmt);
	assert(n < sizeof(tofu->known_hosts_path));

	strncpy(dname, dirname(tofu->known_hosts_path), sizeof(dname)-1);
	posix_dirname(tofu->known_hosts_path, dname);
	if (mkdirs(dname, 0755) != 0) {
		snprintf(tofu->known_hosts_path, sizeof(tofu->known_hosts_path),
				path_fmt, "known_hosts");
		fprintf(stderr, "Error creating directory %s: %s\n",
				dirname(tofu->known_hosts_path), strerror(errno));
		fprintf(stderr, "Error creating directory %s: %s\n", dname,
			strerror(errno));
		return;
	}

	snprintf(tofu->known_hosts_path, sizeof(tofu->known_hosts_path),
			path_fmt, "known_hosts");
	free(path_fmt);

	tofu->callback = cb;
	tofu->cb_data = cb_data;