home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / prof_c / util / getname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  909 b   |  39 lines

  1. /*
  2.  *    getname -- strip drive identifier and directory node
  3.  *    list, if any, from a pathname;  returns a pointer to
  4.  *    the resulting filename[.ext] string or NULL for error
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <sys\types.h>
  9. #include <sys\stat.h>
  10.  
  11. #define MAXFSPEC 13
  12.  
  13. char *
  14. getname(path)
  15. char *path;    /* string to modify */
  16. {
  17.     register char *cp;    /* character pointer */
  18.     struct stat buf;
  19.  
  20.     /* try to get information about the pathname */
  21.     if (stat(path, &buf) != 0)
  22.         return (NULL);    /* bad pathname */
  23.  
  24.     /* locate the end of the pathname string */
  25.     cp = path;
  26.     while (*cp != '\0')
  27.         ++cp;
  28.     --cp;        /* went one too far */
  29.  
  30.     /* find the start of the filename part */
  31.     while (cp > path && *cp != '\\' && *cp != ':' && *cp != '/')
  32.         --cp;
  33.     if (cp > path)
  34.         ++cp;    /* on a separator (\, :, or /) -- move one past */
  35.  
  36.     /* return the full filespec (filename[.ext]) */
  37.     return (cp);
  38. }
  39.