Talk:Find common directory path: Difference between revisions

no edit summary
No edit summary
No edit summary
 
(3 intermediate revisions by 2 users not shown)
Line 82:
should return "/home/user1". [[User:Per|Per]] 16:14, 14 April 2011 (UTC)
 
/home/user1/tmp</lang>(elided) --[[User:Rdm|Rdm]] 16:54, 14 April 2011 (UTC)
:<lang bash>$ cat comm.c
#define PATH_MAX 127
#include <string.h>
#include <stdio.h>
 
"home/user1/tmp" is not the longest common shared path any more. How about you drop by on irc to discuss this, instead of us abusing this talk page further? ;-) [[User:Per|Per]] 17:03, 14 April 2011 (UTC)
static void longestSharedPath(const char *fixed, char *moving) {
char *t;
unsigned n = 0;
while (moving[n] == fixed[n] && moving[n]) n++;
if (!moving[n]) return;
t = strrchr(moving, '/');
if (t)
if (t == moving)
moving[1]= '\0';
else
*t = '\0';
}
 
Correct version of python without reimplementing commonprefix
int main() {
<lang Python>
char *dir_list[] = {
os.path.sep.join(os.path.commonprefix([p.split(os.path.sep) for p in ['/home/user1/tmp/coverage/test', '/home/user1/tmp/covert/operator', '/home/user1/tmp/coven/members']]))
"/home/user1/tmp/coverage/test",
</lang>
"/home/user1/tmp/covert/operator",
"/home/user1/tmp/coven/members",
"/home/user1/tmp2/coven/members",
NULL
};
int i = 0;
char tmp[PATH_MAX];
strcpy(tmp, dir_list[0]);
while (dir_list[++i]) {
longestSharedPath(dir_list[i], tmp);
}
printf("%s\n", tmp);
return 0;
}
 
$ make comm && ./comm
cc comm.c -o comm
/home/user1/tmp</lang> --[[User:Rdm|Rdm]] 16:54, 14 April 2011 (UTC)
Anonymous user