1*0Sstevel@tonic-gate /* $OpenBSD: dirname.c,v 1.7 2002/05/24 21:22:37 deraadt Exp $ */ 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gate /* 4*0Sstevel@tonic-gate * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> 5*0Sstevel@tonic-gate * All rights reserved. 6*0Sstevel@tonic-gate * 7*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 8*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 9*0Sstevel@tonic-gate * are met: 10*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 11*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 12*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 13*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 14*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 15*0Sstevel@tonic-gate * 3. The name of the author may not be used to endorse or promote products 16*0Sstevel@tonic-gate * derived from this software without specific prior written permission. 17*0Sstevel@tonic-gate * 18*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 19*0Sstevel@tonic-gate * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 20*0Sstevel@tonic-gate * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21*0Sstevel@tonic-gate * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22*0Sstevel@tonic-gate * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23*0Sstevel@tonic-gate * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 24*0Sstevel@tonic-gate * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25*0Sstevel@tonic-gate * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26*0Sstevel@tonic-gate * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 27*0Sstevel@tonic-gate * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28*0Sstevel@tonic-gate */ 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate #include "includes.h" 31*0Sstevel@tonic-gate #ifndef HAVE_DIRNAME 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint) 34*0Sstevel@tonic-gate static char rcsid[] = "$OpenBSD: dirname.c,v 1.7 2002/05/24 21:22:37 deraadt Exp $"; 35*0Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */ 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gate #include <errno.h> 38*0Sstevel@tonic-gate #include <string.h> 39*0Sstevel@tonic-gate #include <sys/param.h> 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate char * 42*0Sstevel@tonic-gate dirname(path) 43*0Sstevel@tonic-gate const char *path; 44*0Sstevel@tonic-gate { 45*0Sstevel@tonic-gate static char bname[MAXPATHLEN]; 46*0Sstevel@tonic-gate register const char *endp; 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate /* Empty or NULL string gets treated as "." */ 49*0Sstevel@tonic-gate if (path == NULL || *path == '\0') { 50*0Sstevel@tonic-gate (void)strlcpy(bname, ".", sizeof bname); 51*0Sstevel@tonic-gate return(bname); 52*0Sstevel@tonic-gate } 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate /* Strip trailing slashes */ 55*0Sstevel@tonic-gate endp = path + strlen(path) - 1; 56*0Sstevel@tonic-gate while (endp > path && *endp == '/') 57*0Sstevel@tonic-gate endp--; 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate /* Find the start of the dir */ 60*0Sstevel@tonic-gate while (endp > path && *endp != '/') 61*0Sstevel@tonic-gate endp--; 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate /* Either the dir is "/" or there are no slashes */ 64*0Sstevel@tonic-gate if (endp == path) { 65*0Sstevel@tonic-gate (void)strlcpy(bname, *endp == '/' ? "/" : ".", sizeof bname); 66*0Sstevel@tonic-gate return(bname); 67*0Sstevel@tonic-gate } else { 68*0Sstevel@tonic-gate do { 69*0Sstevel@tonic-gate endp--; 70*0Sstevel@tonic-gate } while (endp > path && *endp == '/'); 71*0Sstevel@tonic-gate } 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate if (endp - path + 2 > sizeof(bname)) { 74*0Sstevel@tonic-gate errno = ENAMETOOLONG; 75*0Sstevel@tonic-gate return(NULL); 76*0Sstevel@tonic-gate } 77*0Sstevel@tonic-gate strlcpy(bname, path, endp - path + 2); 78*0Sstevel@tonic-gate return(bname); 79*0Sstevel@tonic-gate } 80*0Sstevel@tonic-gate #endif 81*0Sstevel@tonic-gate 82*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 83