dirname.c
Go to the documentation of this file.
1 /* dirname - return directory part of PATH.
2  Copyright (C) 1996 Free Software Foundation, Inc.
3  This file is part of the GNU C Library.
4  Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
5 
6  The GNU C Library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public License as
8  published by the Free Software Foundation; either version 2 of the
9  License, or (at your option) any later version.
10 
11  The GNU C Library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public
17  License along with the GNU C Library; see the file COPYING.LIB. If not,
18  write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  Boston, MA 02111-1307, USA. */
20 
21 /*
22  * $Id: dirname.c,v 1.1 2002-12-05 01:50:22 rtv Exp $
23  *
24  * taken from glibc, modified slightly for standalone compilation, and used as
25  * a fallback implementation when dirname() is not available. - BPG
26  */
27 
28 #include <string.h>
29 
30 char *
31 dirname (char *path)
32 {
33  static const char dot[] = ".";
34  char *last_slash;
35 
36  /* Find last '/'. */
37  last_slash = path != NULL ? strrchr (path, '/') : NULL;
38 
39  if (last_slash == path)
40  /* The last slash is the first character in the string. We have to
41  return "/". */
42  ++last_slash;
43  else if (last_slash != NULL && last_slash[1] == '\0')
44  /* The '/' is the last character, we have to look further. */
45  last_slash = memchr (path, last_slash - path, '/');
46 
47  if (last_slash != NULL)
48  /* Terminate the path. */
49  last_slash[0] = '\0';
50  else
51  /* This assignment is ill-designed but the XPG specs require to
52  return a string containing "." in any case no directory part is
53  found and so a static and constant string is required. */
54  path = (char *) dot;
55 
56  return path;
57 }
58 
char * dirname(char *path)
Definition: dirname.c:31


stage
Author(s): Richard Vaughan , Brian Gerkey , Reed Hedges , Andrew Howard , Toby Collett , Pooya Karimian , Jeremy Asher , Alex Couture-Beil , Geoff Biggs , Rich Mattes , Abbas Sadat
autogenerated on Mon Jun 10 2019 15:06:09