Talk:Find common directory path: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 81:
</lang>
should return "/home/user1". [[User:Per|Per]] 16:14, 14 April 2011 (UTC)
 
:<lang bash>$ cat comm.c
#define PATH_MAX 127
#include <string.h>
#include <stdio.h>
 
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';
}
 
int main() {
char *dir_list[] = {
"/home/user1/tmp/coverage/test",
"/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>
6,951

edits