Logo Search packages:      
Sourcecode: qfits version File versions  Download package

char* qfits_get_dir_name ( const char *  filename  ) 

Find the directory name in the given string.

Parameters:
filename Full path name to scan.
Returns:
Pointer to statically allocated string.
Provide a full path name and you get in return a pointer to a statically allocated string containing the name of the directory only, without trailing slash. If the input string does not contain a slash (i.e. it is not a full path), the returned string is '.', corresponding to the current working directory. Since the returned string is statically allocated, do not try to free it or modify it.

This function does not check for the existence of the path or the file.

Examples:

    qfits_get_dir_name("/cdrom/data/image.fits") returns "/cdrom/data"
    qfits_get_dir_name("filename.fits") returns "."
  

Definition at line 110 of file qfits_filename.c.

{
    static char path[MAXNAMESZ];
    char *last_slash;

    if (strlen(filename)>MAXNAMESZ) return NULL ;
    strcpy(path, filename);
    /* Find last '/'.  */
    last_slash = path != NULL ? strrchr (path, '/') : NULL;

    if (last_slash == path)
    /* The last slash is the first character in the string.  We have to
    return "/".  */
        ++last_slash;
    else if (last_slash != NULL && last_slash[1] == '\0')
        /* The '/' is the last character, we have to look further.  */
        last_slash = memchr (path, last_slash - path, '/');

    if (last_slash != NULL)
        /* Terminate the path.  */
        last_slash[0] = '\0';
    else
        /* This assignment is ill-designed but the XPG specs require to
        return a string containing "." in any case no directory part is
        found and so a static and constant string is required.  */
        strcpy(path, ".");
    return path;
}


Generated by  Doxygen 1.6.0   Back to index