summaryrefslogtreecommitdiff
path: root/src/bin/normalpath.c
blob: a80ecda2643af7137986d1943827a3ef96204613 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/param.h>

#define DEBUG 0

int main(int argc,char *argv[]) {
 int i,j,k,l;
 char *s=argv[1];
 char *out=malloc(MAXPATHLEN+1);
 switch(s[0]) {
  case '/':
   strcpy(out,"/");
   break;
  case '~':
   strcpy(out,(char *)getenv("HOME"));
   break;
  default:
//old code, but might be what I decide I /really/ want.
//   getcwd(out,MAXPATHLEN);
   if(getenv("PWD")) strcpy(out,getenv("PWD"));
   strcat(out,"/");
 }
 strcat(out,s);
#if DEBUG
 printf("%s\n",out);
#endif
 l=strlen(out)-1;
 if(out[l] == '.' && out[l-1] == '/') strcat(out,"/");
 if(out[l] == '.' && out[l-1] == '.' && out[l-2] == '/') strcat(out,"/");
 for(i=0;out[i];) {
#if DEBUG
  printf("%s @ %s\n",out,out+i);
#endif
  if(strlen(out+i) >= 4) {
   if(out[i] == '/' && out[i+1] == '.' && out[i+2] == '.' && out[i+3] == '/') {
    for(j=i-1;j>0 && out[j] != '/';j--) {
    }
    if(j<0) j=0;
    for(k=j;out[k];k++) {
     out[k]=out[k+(i-j)+3];
     if(!out[k]) break;
    }
    i=0;
    continue;
   }
  }
  if(strlen(out+i) >= 3) {
   if(out[i] == '/' && out[i+1] == '.' && out[i+2] == '/') {
    for(j=i;out[j];j++) {
     out[j]=out[j+2];
     if(!out[j]) break;
    }
    i=0;
    continue;
   }
  }
  if(strlen(out+i) >= 2) {
   if(out[i] == '/' && out[i+1] == '/') {
    //leftshift over it.
    for(j=i;out[j];j++) {
     out[j]=out[j+1];
     if(!out[j]) break;
    }
    i=0;
    continue;
   }
  }
  i++;
 }
 printf("%s\n",out);
 return 0;
}