commit 51bc3d210ac7af51373b59dad44faf8ceefb6a8f
parent 10760e447f95a4b6dbeaae90456a7e9efd0a796c
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Sun, 21 Jun 2015 00:20:12 +0200
improve printlink, escape characters
Diffstat:
2 files changed, 23 insertions(+), 7 deletions(-)
diff --git 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
@@ -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',