summaryrefslogtreecommitdiff
path: root/uriunescape.c
blob: c99cdfc1a812e967258b76563b203a26ea7adc45 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "uri.h"
#include <unistd.h>
#include <stdio.h>

#define BUFSIZE 4096

int main(int argc,char *argv[]) {
  int i=0,j=0;
  int len;
  char buf[BUFSIZE];
  char buf2[BUFSIZE];
  int blen;
  int hack;
  if(argc > 1) {
    for(argv++,argc--;argc;argc--,argv++) {
      len=uriunescape(*argv,*argv);
      write(1,*argv,len);
      if(argc-1) write(1," ",1);
    }
  } else {
    while((blen=read(0,buf+hack,BUFSIZE-hack-1)) > 0) {
      buf[blen+hack]=0;
      blen=strlen(buf);
      hack=0;
      for(i=0;i<2;i++) {//2 being max length of a %XX thing. and we're starting at the last.
        if(buf[blen-i-1] == '%') {//we're looping from the end first...
          buf[blen-i-1] = 0;//zero out the %
          hack=i+1;
          break;
        }
      }
      len=uriunescape(buf,buf2);//uriunescape wants null terminated strings
      write(1,buf2,len);
      if(hack) {
        buf[0]='%';
        for(j=0;j<hack;j++) {//move the entity to the start of the string
          buf[j+1]=buf[blen-(hack-1-j)];
        }
      }
    }
  }
  return 0;
}