diff options
author | Hiltjo Posthuma <hiltjo@codemadness.org> | 2015-06-21 00:20:12 +0200 |
---|---|---|
committer | Hiltjo Posthuma <hiltjo@codemadness.org> | 2015-06-21 00:20:12 +0200 |
commit | 51bc3d210ac7af51373b59dad44faf8ceefb6a8f (patch) | |
tree | e15bd0e18aaf9bee83e3648da858ab0b8e38de78 | |
parent | 10760e447f95a4b6dbeaae90456a7e9efd0a796c (diff) |
improve printlink, escape characters
-rw-r--r-- | sfeed.c | 3 | ||||
-rw-r--r-- | util.c | 27 |
2 files changed, 23 insertions, 7 deletions
@@ -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); @@ -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', |