Blame misc/dirname.c

Packit 6c4009
/* dirname - return directory part of PATH.
Packit 6c4009
   Copyright (C) 1996-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <libgen.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
char *
Packit 6c4009
dirname (char *path)
Packit 6c4009
{
Packit 6c4009
  static const char dot[] = ".";
Packit 6c4009
  char *last_slash;
Packit 6c4009
Packit 6c4009
  /* Find last '/'.  */
Packit 6c4009
  last_slash = path != NULL ? strrchr (path, '/') : NULL;
Packit 6c4009
Packit 6c4009
  if (last_slash != NULL && last_slash != path && last_slash[1] == '\0')
Packit 6c4009
    {
Packit 6c4009
      /* Determine whether all remaining characters are slashes.  */
Packit 6c4009
      char *runp;
Packit 6c4009
Packit 6c4009
      for (runp = last_slash; runp != path; --runp)
Packit 6c4009
	if (runp[-1] != '/')
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
      /* The '/' is the last character, we have to look further.  */
Packit 6c4009
      if (runp != path)
Packit 6c4009
	last_slash = __memrchr (path, '/', runp - path);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (last_slash != NULL)
Packit 6c4009
    {
Packit 6c4009
      /* Determine whether all remaining characters are slashes.  */
Packit 6c4009
      char *runp;
Packit 6c4009
Packit 6c4009
      for (runp = last_slash; runp != path; --runp)
Packit 6c4009
	if (runp[-1] != '/')
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
      /* Terminate the path.  */
Packit 6c4009
      if (runp == path)
Packit 6c4009
	{
Packit 6c4009
	  /* The last slash is the first character in the string.  We have to
Packit 6c4009
	     return "/".  As a special case we have to return "//" if there
Packit 6c4009
	     are exactly two slashes at the beginning of the string.  See
Packit 6c4009
	     XBD 4.10 Path Name Resolution for more information.  */
Packit 6c4009
	  if (last_slash == path + 1)
Packit 6c4009
	    ++last_slash;
Packit 6c4009
	  else
Packit 6c4009
	    last_slash = path + 1;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	last_slash = runp;
Packit 6c4009
Packit 6c4009
      last_slash[0] = '\0';
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    /* This assignment is ill-designed but the XPG specs require to
Packit 6c4009
       return a string containing "." in any case no directory part is
Packit 6c4009
       found and so a static and constant string is required.  */
Packit 6c4009
    path = (char *) dot;
Packit 6c4009
Packit 6c4009
  return path;
Packit 6c4009
}