diff --git a/src/gmnlm.c b/src/gmnlm.c

index 66db54c7e0cf7dd6a1f58872621cc28e69944134..a0bc81f851a69ec34c7e9baf643c10728632c7e2 100644

--- a/src/gmnlm.c

+++ b/src/gmnlm.c

@@ -88,6 +88,7 @@ "b\t\tBack (in the page history)\n"

"f\t\tForward (in the page history)\n"

"H\t\tView all page history\n"

"a\t\tSave bookmark\n"

+ "s\tRemove bookmark for current URL\n"

"B\t\tBrowse bookmarks\n"

"r\t\tReload the page\n"

"/<text>\t\tsearch for text (POSIX regular expression)\n"

@@ -169,18 +170,16 @@ static char dname[PATH_MAX+1];

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);

+ strncpy(dname, path, sizeof(dname)-1);

+ dirname(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",

@@ -202,6 +201,49 @@ title ? title : browser->plain_url);

}

static void

+remove_bookmark(struct browser *browser)

+{

+ char *path_fmt = get_data_pathfmt();

+ static char path[PATH_MAX+1];

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

+ free(path_fmt);

+

+ static char tempfile[PATH_MAX+2];

+ snprintf(tempfile, sizeof(tempfile), "%s2", path);

+ FILE *fi = fopen(path, "r");

+ FILE *fo = fopen(tempfile, "w");

+ if(fi == NULL) {

+ fprintf(stderr, "Bookmark file not available!\n");

+ return;

+ }

+ if(fo == NULL) {

+ fprintf(stderr, "tempfile not available!\n");

+ return;

+ }

+

+ char *line = NULL;

+ size_t len = 0;

+ size_t n = 0;

+

+ static char url[1024];

+ n = snprintf(url, sizeof(url), "=> %s ", browser->plain_url);

+ while(getline(&line, &len, fi) != -1) {

+ if (strncmp(line, url, n)==0) {

+ fprintf(browser->tty, "Bookmark removed!\n");

+ } else {

+ fprintf(fo, line);

+ }

+ }

+

+ fclose(fi);

+ fclose(fo);

+ free(line);

+ if ( rename(tempfile, path) != 0) {

+ fprintf(browser->tty, "Failed to udpate bookmarks: %s\n", strerror(errno));

+ }

+}

+

+static void

open_bookmarks(struct browser *browser)

{

char *path_fmt = get_data_pathfmt();

@@ -210,18 +252,16 @@ static char dname[PATH_MAX+1];

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);

+ strncpy(dname, path, sizeof(dname)-1);

+ dirname(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) {

@@ -566,6 +606,11 @@ goto exit;

case 'a':

if (in[1]) break;

save_bookmark(browser);

+ result = PROMPT_AGAIN;

+ goto exit;

+ case 's':

+ if (in[1]) break;

+ remove_bookmark(browser);

result = PROMPT_AGAIN;

goto exit;

case 'B':