Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /* dirname - return directory part of PATH.
  2.    Copyright (C) 1996-2019 Free Software Foundation, Inc.
  3.    This file is part of the GNU C Library.
  4.    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
  5.    The GNU C Library is free software; you can redistribute it and/or
  6.    modify it under the terms of the GNU Lesser General Public
  7.    License as published by the Free Software Foundation; either
  8.    version 2.1 of the License, or (at your option) any later version.
  9.    The GNU C Library is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.    Lesser General Public License for more details.
  13.    You should have received a copy of the GNU Lesser General Public
  14.    License along with the GNU C Library; if not, see
  15.    <http://www.gnu.org/licenses/>.  */
  16.  
  17. #include <libgen.h>
  18. #include <string.h>
  19.  
  20. char *
  21. dirname (char *path)
  22. {
  23.   static const char dot[] = ".";
  24.   char *last_slash;
  25.   /* Find last '/'.  */
  26.   last_slash = path != NULL ? strrchr (path, '/') : NULL;
  27.   if (last_slash != NULL && last_slash != path && last_slash[1] == '\0')
  28.     {
  29.       /* Determine whether all remaining characters are slashes.  */
  30.       char *runp;
  31.       for (runp = last_slash; runp != path; --runp)
  32.         if (runp[-1] != '/')
  33.           break;
  34.       /* The '/' is the last character, we have to look further.  */
  35.       if (runp != path)
  36.         last_slash = memrchr (path, '/', runp - path);
  37.     }
  38.   if (last_slash != NULL)
  39.     {
  40.       /* Determine whether all remaining characters are slashes.  */
  41.       char *runp;
  42.       for (runp = last_slash; runp != path; --runp)
  43.         if (runp[-1] != '/')
  44.           break;
  45.       /* Terminate the path.  */
  46.       if (runp == path)
  47.         {
  48.           /* The last slash is the first character in the string.  We have to
  49.              return "/".  As a special case we have to return "//" if there
  50.              are exactly two slashes at the beginning of the string.  See
  51.              XBD 4.10 Path Name Resolution for more information.  */
  52.           if (last_slash == path + 1)
  53.             ++last_slash;
  54.           else
  55.             last_slash = path + 1;
  56.         }
  57.       else
  58.         last_slash = runp;
  59.       last_slash[0] = '\0';
  60.     }
  61.   else
  62.     /* This assignment is ill-designed but the XPG specs require to
  63.        return a string containing "." in any case no directory part is
  64.        found and so a static and constant string is required.  */
  65.     path = (char *) dot;
  66.   return path;
  67. }