On Wed, Dec 07, 2016 at 06:07:00PM +0100, Pino Toscano wrote:
This implementation looks a bit inefficient to me (recursive, moving
It's inefficient (O(n^2)), but not because of recursion -- GCC can
turn the code into a loop using TCO.
bytes and using strlen for every char removed, etc). What about the
following implementation?
void canonicalize(char *s)
{
size_t len = strlen(s);
char *orig = s;
s = strchr(s, '/');
while (s != NULL && *s != 0) {
char *pos = s + 1;
char *p = pos;
while (*p == '/')
++p;
if (p - pos > 0) {
memmove(pos, p, len - (p - orig) + 1);
len -= p - pos;
}
s = strchr(pos, '/');
}
orig[len] = 0;
}
The only behaviour change with the committed implementation is that it
does not remove trailing slashes, but IMHO they could be left there
(or removed after the canonicalization function, just once).
I think it needs to handle the trailing '/'.
The bigger problem here is if we find an fstab that has something like
/etc/../usr/ or a path containing symlinks, but I don't think there's
any good way to deal with that.
Rich.
--
Richard Jones, Virtualization Group, Red Hat
http://people.redhat.com/~rjones
Read my programming and virtualization blog:
http://rwmj.wordpress.com
virt-p2v converts physical machines to virtual machines. Boot with a
live CD or over the network (PXE) and turn machines into KVM guests.
http://libguestfs.org/virt-v2v