Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. #ifndef _NO_DIRNAME
  2.  
  3. /* Copyright 2005 Shaun Jackman
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * is freely granted, provided that this notice is preserved.
  6.  */
  7.  
  8. #include <libgen.h>
  9. #include <string.h>
  10.  
  11. char *
  12. _DEFUN (dirname, (path),
  13.         char *path)
  14. {
  15.         char *p;
  16.         if( path == NULL || *path == '\0' )
  17.                 return ".";
  18.         p = path + strlen(path) - 1;
  19.         while( *p == '/' ) {
  20.                 if( p == path )
  21.                         return path;
  22.                 *p-- = '\0';
  23.         }
  24.         while( p >= path && *p != '/' )
  25.                 p--;
  26.         return
  27.                 p < path ? "." :
  28.                 p == path ? "/" :
  29.                 (*p = '\0', path);
  30. }
  31.  
  32. #endif /* !_NO_DIRNAME  */
  33.