Talk:Find common directory path
Much simpler version for the C language:
<lang C>
- define PATH_MAX 127
- include <string.h>
- include <stdio.h>
static void leastCommonPath(const char *fixed, char *moving) { char *t; unsigned n = strspn(moving, fixed); if (strlen(fixed) > n && strlen(moving) == n && fixed[n] == '/') return; strcpy(moving, fixed); moving[n] = '\0'; t = strrchr(moving, '/'); if (t && t != moving) *t = '\0'; }
int main() { char *dir_list[] = { "/home/user1/tmp/coverage/test", "/home/user1/tmp/covert/operator", "/home/user1/tmp/coven/members", NULL }; int i = 0; char tmp[PATH_MAX]; strcpy(tmp, dir_list[0]); while (dir_list[++i]) { leastCommonPath(dir_list[i], tmp); } printf("%s\n", tmp); return 0; } </lang>