aboutsummaryrefslogtreecommitdiffstats
path: root/uriunescape.c
blob: 2261da9c249f30d90e91025faaa4da6556dbbd8e (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
44
45
46
#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];
  char *ab;
  int blen;
  int hack=0;
  if(argc > 1) {
    for(argv++,argc--;argc;argc--,argv++) {
      ab=malloc(strlen(*argv));
      len=uriunescape(*argv,ab);
      write(1,ab,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)];
        }
      }
    }
    if(blen < 0) perror("read");
  }
  return 0;
}