dirname.c revision 89999
165294Sdes/* 265294Sdes * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> 365294Sdes * All rights reserved. 465294Sdes * 565294Sdes * Redistribution and use in source and binary forms, with or without 665294Sdes * modification, are permitted provided that the following conditions 765294Sdes * are met: 865294Sdes * 1. Redistributions of source code must retain the above copyright 965294Sdes * notice, this list of conditions and the following disclaimer. 1065294Sdes * 2. Redistributions in binary form must reproduce the above copyright 1165294Sdes * notice, this list of conditions and the following disclaimer in the 1265294Sdes * documentation and/or other materials provided with the distribution. 1365294Sdes * 3. The name of the author may not be used to endorse or promote products 1465294Sdes * derived from this software without specific prior written permission. 1565294Sdes * 1665294Sdes * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 1765294Sdes * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 1865294Sdes * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 1965294Sdes * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 2065294Sdes * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2165294Sdes * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 2265294Sdes * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 2365294Sdes * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 2465294Sdes * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 2565294Sdes * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2665294Sdes */ 2765294Sdes 2865294Sdes#ifndef lint 2989999Sobrien#if 0 3065294Sdesstatic char rcsid[] = "$OpenBSD: dirname.c,v 1.4 1999/05/30 17:10:30 espie Exp $"; 3189999Sobrien#endif 3265294Sdes#endif /* not lint */ 3389999Sobrien__FBSDID("$FreeBSD: head/lib/libc/gen/dirname.c 89999 2002-01-30 21:36:57Z obrien $"); 3465294Sdes 3565294Sdes#include <errno.h> 3665294Sdes#include <libgen.h> 3765294Sdes#include <string.h> 3865294Sdes#include <sys/param.h> 3965294Sdes 4065294Sdeschar * 4165294Sdesdirname(path) 4265294Sdes const char *path; 4365294Sdes{ 4465294Sdes static char bname[MAXPATHLEN]; 4565294Sdes register const char *endp; 4665294Sdes 4765294Sdes /* Empty or NULL string gets treated as "." */ 4865294Sdes if (path == NULL || *path == '\0') { 4965294Sdes (void)strcpy(bname, "."); 5065294Sdes return(bname); 5165294Sdes } 5265294Sdes 5365294Sdes /* Strip trailing slashes */ 5465294Sdes endp = path + strlen(path) - 1; 5565294Sdes while (endp > path && *endp == '/') 5665294Sdes endp--; 5765294Sdes 5865294Sdes /* Find the start of the dir */ 5965294Sdes while (endp > path && *endp != '/') 6065294Sdes endp--; 6165294Sdes 6265294Sdes /* Either the dir is "/" or there are no slashes */ 6365294Sdes if (endp == path) { 6465294Sdes (void)strcpy(bname, *endp == '/' ? "/" : "."); 6565294Sdes return(bname); 6665294Sdes } else { 6765294Sdes do { 6865294Sdes endp--; 6965294Sdes } while (endp > path && *endp == '/'); 7065294Sdes } 7165294Sdes 7278936Sdd if (endp - path + 2 > sizeof(bname)) { 7365294Sdes errno = ENAMETOOLONG; 7465294Sdes return(NULL); 7565294Sdes } 7665294Sdes (void)strncpy(bname, path, endp - path + 1); 7765294Sdes bname[endp - path + 1] = '\0'; 7865294Sdes return(bname); 7965294Sdes} 80