9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

commit 51cdf1141edd627428ecf0b8bd643142dd5c3773
parent 19a07111123964b50fff2644882430398fded904
Author: anselm@garbe.us <unknown>
Date:   Thu,  1 Nov 2012 18:42:41 +0100

added missing urlencode, thanks sl for checking this
Diffstat:
Aurlencode/Makefile | 10++++++++++
Aurlencode/urlencode.1 | 20++++++++++++++++++++
Aurlencode/urlencode.c | 103+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 133 insertions(+), 0 deletions(-)

diff --git a/urlencode/Makefile b/urlencode/Makefile @@ -0,0 +1,10 @@ +# urlencode - url encode and decode files +# Depends on ../lib9 + +TARG = urlencode + +include ../std.mk + +pre-uninstall: + +post-install: diff --git a/urlencode/urlencode.1 b/urlencode/urlencode.1 @@ -0,0 +1,20 @@ +.TH URLENCODE 1 +.SH NAME +urlencode \- URL encode and decode files +.SH SYNOPSIS +.B urlencode +[ +.B -d +] [ +.I file +] +.SH DESCRIPTION +.I Urlencode +is a helper program to URL encode and decode files. The +.B -d +flag insead of encoding, decodes URL encoded file. If no +.I file +is given, standard input is read. The resulting data +is written to standard output. +.SH SOURCE +.B \*9/src/urlencode/urlencode.c diff --git a/urlencode/urlencode.c b/urlencode/urlencode.c @@ -0,0 +1,103 @@ +#include <u.h> +#include <libc.h> +#include <bio.h> + +Biobuf bin; +Biobuf bout; +int dflag; + +char hex[] = "0123456789abcdef"; +char Hex[] = "0123456789ABCDEF"; + +int +hexdigit(int c) +{ + char *p; + + if(c > 0){ + if((p = strchr(Hex, c)) != 0) + return p - Hex; + if((p = strchr(hex, c)) != 0) + return p - hex; + } + return -1; +} + +void +usage(void) +{ + fprint(2, "Usage: %s [ -d ] [ file ]\n", argv0); + exits("usage"); +} + +void +main(int argc, char *argv[]) +{ + int c; + + ARGBEGIN { + case 'd': + dflag = 1; + break; + default: + usage(); + } ARGEND; + + if(argc == 1){ + int fd; + + fd = open(*argv, OREAD); + if(fd < 0) + sysfatal("%r"); + if(fd != 0) dup(fd, 0); + } else if(argc > 1) + usage(); + + Binit(&bin, 0, OREAD); + Binit(&bout, 1, OWRITE); + + if(dflag){ + while((c = Bgetc(&bin)) >= 0){ + if(c == '%'){ + int c1, c2, x1, x2; + + if((c1 = Bgetc(&bin)) < 0) + break; + if((x1 = hexdigit(c1)) < 0){ + Bungetc(&bin); + Bputc(&bout, c); + continue; + } + if((c2 = Bgetc(&bin)) < 0) + break; + if((x2 = hexdigit(c2)) < 0){ + Bungetc(&bin); + Bputc(&bout, c); + Bputc(&bout, c1); + continue; + } + c = x1<<4 | x2; + } else if(c == '+') + c = ' '; + Bputc(&bout, c); + } + } else { + while((c = Bgetc(&bin)) >= 0){ + if(c>0 && strchr("/$-_@.!*'(),", c) + || 'a'<=c && c<='z' + || 'A'<=c && c<='Z' + || '0'<=c && c<='9') + Bputc(&bout, c); + else if(c == ' ') + Bputc(&bout, '+'); + else { + Bputc(&bout, '%'); + Bputc(&bout, Hex[c>>4]); + Bputc(&bout, Hex[c&15]); + } + } + } + + Bflush(&bout); + exits(0); +}