httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Greg Marr <>
Subject Re: cvs commit: apache-2.0/src/lib/apr/lib apr_cpystrn.c
Date Thu, 14 Oct 1999 15:31:21 GMT
At 09:31 PM 10/13/99 , you wrote:
>Dean Gaudet <> writes:
> > on win32, both / and \ are path separators... no?  (or is this 
> only used
> > in situations where we've changed / to \ ?)
>I wrote that...
>This was distilled out of main, and it's used to get the
>program name off of what every pathname the user invoked
>it with.  Of course now that it's in APR it ought to be
>right.  "Right", presumably meaning, get the last term off
>of the pathname presuming that the pathname is in the
>syntax of the current platform.  VMS ports will suffer
>as they usually do.

const char *ap_filename_of_pathname(const char *pathname)
    #ifdef WIN32
        const char path_separator = '\\';
        const char path_separator = '/';
        const char *s = strrchr(pathname, path_separator);


If you just want to get the filename (with or without extension), the 
windows API provides methods for you to do this.  They operate a 
little differently than this function in that you have to provide 
storage space.  If you're looking for a pointer to the position in 
the passed string where the filename starts, you need to use 
something like this:

    #ifdef WIN32
         const char *f = strrchr(pathname, '\\');
        const char *s = strrchr(pathname, '/');

        if((r == NULL) && (s == NULL))

        if(r > s)
            return(r + 1);
            return(s + 1);
        const char path_separator = '/';
        const char *s = strrchr(pathname, path_separator);

        return s ? ++s : pathname;

On the other hand, if the pointer doesn't have to be in the original 
string, (this version isn't thread safe as it uses a static buffer, 
making it so is left as an exercise to the reader) you can use

char szDrive[_MAX_DRIVE], szPath[_MAX_PATH], szFilename[_MAX_PATH], 
static char szName[_MAX_PATH];

/* You can pass NULL for any of the last four parameters you don't 
need */
splitpath(pathname, szDrive, szPath, szFilename, szExt);
makepath(szName, NULL, NULL, szFilename, szExt);


This method frees you from handling all the / vs \ problems.

Greg Marr
"We thought you were dead."
"I was, but I'm better now." - Sheridan, "The Summoning"

View raw message