7 #define MYNAME "relpath"
12 "Usage: " MYNAME
" [-n] <DIR-1> <DIR-2>\n"
13 " Print a relative path from <DIR-2> to <DIR-1>.\n"
14 " Useful for creating relative symlinks.\n"
16 " This command creates <DIR-2>/symlink.txt pointing to <DIR-1>/link_target.txt:\n"
17 " ln -s $(" MYNAME
" <DIR-1> <DIR-2>)link_target.txt <DIR-2>/symlink.txt\n"
19 " -n do not print newline\n"
22 " 1 a directory does not exist\n"
23 " 2 parameter error\n");
26 int main(int argc
, char *argv
[]) {
31 unsigned int newline
= 1;
32 char commondir
[_POSIX_PATH_MAX
];
33 char buff
[_POSIX_PATH_MAX
];
34 char route
[_POSIX_PATH_MAX
];
35 char realname1
[_POSIX_PATH_MAX
];
36 char realname2
[_POSIX_PATH_MAX
];
38 while(i
< argc
&& argv
[i
][0] == '-') {
39 if(strcmp(argv
[i
], "-n")==0) {
42 else if(strcmp(argv
[i
], "--help")==0) {
50 if(realpath(argv
[i
], realname1
)==0) exit(1);
52 if(realpath(argv
[i
], realname2
)==0) exit(1);
55 fprintf(stderr
, MYNAME
": Parameter error\n");
62 strcpy(commondir
, "");
65 fprintf(stderr
, " %s\n %s\n", realname1
, realname2
);
67 if(strcmp(realname1
, realname2
)==0) {
68 // directories are identical!
70 if(newline
) printf("\n");
73 if(realname1
[strlen(realname1
)-1] != '/') strcat(realname1
, "/");
74 if(realname2
[strlen(realname2
)-1] != '/') strcat(realname2
, "/");
77 while(strlen(realname1
)>i
&& strlen(realname2
)>i
&& realname1
[i
] == realname2
[i
]) {
78 sprintf(buff
, "%s%c", buff
, realname1
[i
]);
79 if(realname1
[i
]=='/') {
80 strcat(commondir
, buff
);
87 //route = "../"*(1+count("/",a2)).a1
89 while(strlen(realname2
)>=++i
) {
90 if(realname2
[i
]=='/') per
++;
99 while(strlen(realname1
)>=++i2
) {
100 sprintf(route
, "%s%c", route
, realname1
[i2
]);
104 fprintf(stderr
, "%s\n", commondir
);
107 if(newline
) printf("\n");