00001 /* dirname - return directory part of PATH. 00002 Copyright (C) 1996 Free Software Foundation, Inc. 00003 This file is part of the GNU C Library. 00004 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996. 00005 00006 The GNU C Library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public License as 00008 published by the Free Software Foundation; either version 2 of the 00009 License, or (at your option) any later version. 00010 00011 The GNU C Library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public 00017 License along with the GNU C Library; see the file COPYING.LIB. If not, 00018 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00019 Boston, MA 02111-1307, USA. */ 00020 00021 /* 00022 * $Id: dirname.c,v 1.1 2002-12-05 01:50:22 rtv Exp $ 00023 * 00024 * taken from glibc, modified slightly for standalone compilation, and used as 00025 * a fallback implementation when dirname() is not available. - BPG 00026 */ 00027 00028 #include <string.h> 00029 00030 char * 00031 dirname (char *path) 00032 { 00033 static const char dot[] = "."; 00034 char *last_slash; 00035 00036 /* Find last '/'. */ 00037 last_slash = path != NULL ? strrchr (path, '/') : NULL; 00038 00039 if (last_slash == path) 00040 /* The last slash is the first character in the string. We have to 00041 return "/". */ 00042 ++last_slash; 00043 else if (last_slash != NULL && last_slash[1] == '\0') 00044 /* The '/' is the last character, we have to look further. */ 00045 last_slash = memchr (path, last_slash - path, '/'); 00046 00047 if (last_slash != NULL) 00048 /* Terminate the path. */ 00049 last_slash[0] = '\0'; 00050 else 00051 /* This assignment is ill-designed but the XPG specs require to 00052 return a string containing "." in any case no directory part is 00053 found and so a static and constant string is required. */ 00054 path = (char *) dot; 00055 00056 return path; 00057 } 00058