1236769Sobrien/* $NetBSD: dirname.c,v 1.11 2009/11/24 13:34:20 tnozaki Exp $ */ 2236769Sobrien 3236769Sobrien/*- 4236769Sobrien * Copyright (c) 1997, 2002 The NetBSD Foundation, Inc. 5236769Sobrien * All rights reserved. 6236769Sobrien * 7236769Sobrien * This code is derived from software contributed to The NetBSD Foundation 8236769Sobrien * by Klaus Klein and Jason R. Thorpe. 9236769Sobrien * 10236769Sobrien * Redistribution and use in source and binary forms, with or without 11236769Sobrien * modification, are permitted provided that the following conditions 12236769Sobrien * are met: 13236769Sobrien * 1. Redistributions of source code must retain the above copyright 14236769Sobrien * notice, this list of conditions and the following disclaimer. 15236769Sobrien * 2. Redistributions in binary form must reproduce the above copyright 16236769Sobrien * notice, this list of conditions and the following disclaimer in the 17236769Sobrien * documentation and/or other materials provided with the distribution. 18236769Sobrien * 19236769Sobrien * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20236769Sobrien * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21236769Sobrien * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22236769Sobrien * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23236769Sobrien * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24236769Sobrien * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25236769Sobrien * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26236769Sobrien * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27236769Sobrien * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28236769Sobrien * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29236769Sobrien * POSSIBILITY OF SUCH DAMAGE. 30236769Sobrien */ 31236769Sobrien 32236769Sobrien#ifdef HAVE_CONFIG_H 33236769Sobrien# include <config.h> 34236769Sobrien#endif 35236769Sobrien#ifndef HAVE_DIRNAME 36236769Sobrien 37236769Sobrien#include <sys/cdefs.h> 38236769Sobrien 39236769Sobrien#ifdef HAVE_LIMITS_H 40236769Sobrien#include <limits.h> 41236769Sobrien#endif 42236769Sobrien#ifdef HAVE_STRING_H 43236769Sobrien#include <string.h> 44236769Sobrien#endif 45236769Sobrien#ifndef PATH_MAX 46236769Sobrien# define PATH_MAX 1024 47236769Sobrien#endif 48236769Sobrien 49236769Sobrienchar * 50236769Sobriendirname(char *path) 51236769Sobrien{ 52236769Sobrien static char result[PATH_MAX]; 53236769Sobrien const char *lastp; 54236769Sobrien size_t len; 55236769Sobrien 56236769Sobrien /* 57236769Sobrien * If `path' is a null pointer or points to an empty string, 58236769Sobrien * return a pointer to the string ".". 59236769Sobrien */ 60236769Sobrien if ((path == NULL) || (*path == '\0')) 61236769Sobrien goto singledot; 62236769Sobrien 63236769Sobrien 64236769Sobrien /* Strip trailing slashes, if any. */ 65236769Sobrien lastp = path + strlen(path) - 1; 66236769Sobrien while (lastp != path && *lastp == '/') 67236769Sobrien lastp--; 68236769Sobrien 69236769Sobrien /* Terminate path at the last occurence of '/'. */ 70236769Sobrien do { 71236769Sobrien if (*lastp == '/') { 72236769Sobrien /* Strip trailing slashes, if any. */ 73236769Sobrien while (lastp != path && *lastp == '/') 74236769Sobrien lastp--; 75236769Sobrien 76236769Sobrien /* ...and copy the result into the result buffer. */ 77236769Sobrien len = (lastp - path) + 1 /* last char */; 78236769Sobrien if (len > (PATH_MAX - 1)) 79236769Sobrien len = PATH_MAX - 1; 80236769Sobrien 81236769Sobrien memcpy(result, path, len); 82236769Sobrien result[len] = '\0'; 83236769Sobrien 84236769Sobrien return (result); 85236769Sobrien } 86236769Sobrien } while (--lastp >= path); 87236769Sobrien 88236769Sobrien /* No /'s found, return a pointer to the string ".". */ 89236769Sobriensingledot: 90236769Sobrien result[0] = '.'; 91236769Sobrien result[1] = '\0'; 92236769Sobrien 93236769Sobrien return (result); 94236769Sobrien} 95236769Sobrien#endif 96