tags:

views:

29

answers:

1

C programming, are there any good ways to manage path strings rather than using C string API like strcat on linux? The equivalent of Windows' PathAppend would be great. Thank you!

+1  A: 

Here's a quick-n-dirty, next-to-untested version that concatenates paths with a Unix-friendly '/' separator between the two:

int PathAppend( char* path, char const* more)
{
    size_t pathlen = strlen( path);

    while (*more == '/') {
        /* skip path separators at start of `more` */
        ++more;
    }

    /* 
     * if there's anything to add to the path, make sure there's 
     * a path separator at the end of it
     */

    if (*more && (pathlen > 0) && (path[pathlen - 1] != '/')) {
        strcat( path, "/");
    }

    strcat( path, more);

    return 1; /* not sure when this function would 'fail' */
}

Note that in my opinion this function should have a parameter that indicates the destination size. I also didn't implement the documented functionality that Win32 has of removing "." and ".." components at the start of the path (why is that there?).

Also, what would cause Win32's PathAppend() to return failure?

Use (and/or modify) at your own risk...

Michael Burr