dirname.c revision 90016
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 2890016Sbde#if 0 2965294Sdes#ifndef lint 3065294Sdesstatic char rcsid[] = "$OpenBSD: dirname.c,v 1.4 1999/05/30 17:10:30 espie Exp $"; 3190016Sbde#endif /* not lint */ 3289999Sobrien#endif 3390016Sbde 3490016Sbde#include <sys/cdefs.h> 3589999Sobrien__FBSDID("$FreeBSD: head/lib/libc/gen/dirname.c 90016 2002-01-31 12:58:36Z bde $"); 3665294Sdes 3765294Sdes#include <errno.h> 3865294Sdes#include <libgen.h> 3965294Sdes#include <string.h> 4065294Sdes#include <sys/param.h> 4165294Sdes 4265294Sdeschar * 4365294Sdesdirname(path) 4465294Sdes const char *path; 4565294Sdes{ 4665294Sdes static char bname[MAXPATHLEN]; 4765294Sdes register const char *endp; 4865294Sdes 4965294Sdes /* Empty or NULL string gets treated as "." */ 5065294Sdes if (path == NULL || *path == '\0') { 5165294Sdes (void)strcpy(bname, "."); 5265294Sdes return(bname); 5365294Sdes } 5465294Sdes 5565294Sdes /* Strip trailing slashes */ 5665294Sdes endp = path + strlen(path) - 1; 5765294Sdes while (endp > path && *endp == '/') 5865294Sdes endp--; 5965294Sdes 6065294Sdes /* Find the start of the dir */ 6165294Sdes while (endp > path && *endp != '/') 6265294Sdes endp--; 6365294Sdes 6465294Sdes /* Either the dir is "/" or there are no slashes */ 6565294Sdes if (endp == path) { 6665294Sdes (void)strcpy(bname, *endp == '/' ? "/" : "."); 6765294Sdes return(bname); 6865294Sdes } else { 6965294Sdes do { 7065294Sdes endp--; 7165294Sdes } while (endp > path && *endp == '/'); 7265294Sdes } 7365294Sdes 7478936Sdd if (endp - path + 2 > sizeof(bname)) { 7565294Sdes errno = ENAMETOOLONG; 7665294Sdes return(NULL); 7765294Sdes } 7865294Sdes (void)strncpy(bname, path, endp - path + 1); 7965294Sdes bname[endp - path + 1] = '\0'; 8065294Sdes return(bname); 8165294Sdes} 82