summaryrefslogtreecommitdiff
path: root/util.c
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 /util.c
parent10760e447f95a4b6dbeaae90456a7e9efd0a796c (diff)
improve printlink, escape characters
Diffstat (limited to 'util.c')
-rw-r--r--util.c27
1 files changed, 21 insertions, 6 deletions
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',