summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHiltjo Posthuma <hiltjo@codemadness.org>2015-06-21 00:20:12 +0200
committerHiltjo Posthuma <hiltjo@codemadness.org>2015-06-21 00:20:12 +0200
commit51bc3d210ac7af51373b59dad44faf8ceefb6a8f (patch)
treee15bd0e18aaf9bee83e3648da858ab0b8e38de78
parent10760e447f95a4b6dbeaae90456a7e9efd0a796c (diff)
improve printlink, escape characters
-rw-r--r--sfeed.c3
-rw-r--r--util.c27
2 files changed, 23 insertions, 7 deletions
diff --git a/sfeed.c b/sfeed.c
index 416e475..07e49c9 100644
--- a/sfeed.c
+++ b/sfeed.c
@@ -360,7 +360,8 @@ printfields(void)
putchar(FieldSeparator);
string_print(&ctx.item.title);
putchar(FieldSeparator);
- string_print(&ctx.item.link);
+ /* always print absolute urls */
+ printlink(ctx.item.link.data, baseurl, stdout);
putchar(FieldSeparator);
string_print(&ctx.item.content);
putchar(FieldSeparator);
diff --git a/util.c b/util.c
index df0a114..8e26a87 100644
--- a/util.c
+++ b/util.c
@@ -13,6 +13,21 @@
#include "util.h"
+void
+printurlencode(const char *s, size_t len, FILE *fp)
+{
+ size_t i;
+
+ for(i = 0; i < len && s[i]; i++) {
+ if((int)s[i] == ' ')
+ fputs("%20", fp);
+ else if((unsigned char)s[i] > 127 || iscntrl((int)s[i]))
+ fprintf(fp, "%%%02X", (unsigned char)s[i]);
+ else
+ fputc(s[i], fp);
+ }
+}
+
/* print link; if link is relative use baseurl to make it absolute */
void
printlink(const char *link, const char *baseurl, FILE *fp)
@@ -28,7 +43,7 @@ printlink(const char *link, const char *baseurl, FILE *fp)
if(isrelative) {
if((ebaseproto = strstr(baseurl, "://"))) {
ebaseproto += strlen("://");
- fwrite(baseurl, 1, ebaseproto - baseurl, fp);
+ printurlencode(baseurl, ebaseproto - baseurl, fp);
} else {
ebaseproto = baseurl;
if(*baseurl || (link[0] == '/' && link[1] == '/'))
@@ -39,19 +54,19 @@ printlink(const char *link, const char *baseurl, FILE *fp)
link += 2;
else if((ebasedomain = strchr(ebaseproto, '/')))
/* relative to baseurl and baseurl path. */
- fwrite(ebaseproto, 1, ebasedomain - ebaseproto, fp);
+ printurlencode(ebaseproto, ebasedomain - ebaseproto, fp);
else
- fputs(ebaseproto, stdout);
+ printurlencode(ebaseproto, strlen(ebaseproto), fp);
} else if((ebasedomain = strrchr(ebaseproto, '/'))) {
/* relative to baseurl and baseurl path. */
- fwrite(ebaseproto, 1, ebasedomain - ebaseproto + 1, fp);
+ printurlencode(ebaseproto, ebasedomain - ebaseproto + 1, fp);
} else {
- fputs(ebaseproto, fp);
+ printurlencode(ebaseproto, strlen(ebaseproto), fp);
if(*baseurl && *link)
fputc('/', fp);
}
}
- fputs(link, fp);
+ printurlencode(link, strlen(link), fp);
}
/* read a field-separated line from 'fp',