Talk:Find common directory path: Difference between revisions

another version
No edit summary
(another version)
Line 51:
*t = '\0';
}</lang> --[[User:Rdm|Rdm]] 14:22, 14 April 2011 (UTC)
 
That function will break on a path that starts the same and continues different, eg if you add "/home/user1/tmp2/coven/members" to the list. However, there are some good ideas there. Here is my combined version:
 
<lang C>
static void longestSharedPath(const char *fixed, char *moving) {
char *t;
unsigned n = 0;
while (moving[n] == fixed[n] && moving[n] && fixed[n]) n++;
if (!moving[n] && (!fixed[n] || fixed[n] == '/')) return;
moving[n] = '\0';
t = strrchr(moving, '/');
if (t && t != moving) *t = '\0'; // drop conflicting remainder
else moving[1] = '\0'; // keep filesystem root
}
</lang>
 
The thing about that first ''if'' line is that if all of moving is identical to matching chars in fixed, ''and'' either that is all of fixed or fixed continues into subdirectories, then we're done. But not otherwise.
Anonymous user