dirname.c revision 65294
1158115Sume/*	$OpenBSD: dirname.c,v 1.4 1999/05/30 17:10:30 espie Exp $	*/
2158115Sume/*	$FreeBSD: head/lib/libc/gen/dirname.c 65294 2000-08-31 15:56:15Z des $	*/
3158115Sume
4158115Sume/*
5158115Sume * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
6171795Sbushman * All rights reserved.
7158115Sume *
8158115Sume * Redistribution and use in source and binary forms, with or without
9180564Sdougb * modification, are permitted provided that the following conditions
10158115Sume * are met:
11158115Sume * 1. Redistributions of source code must retain the above copyright
12172377Sbushman *    notice, this list of conditions and the following disclaimer.
13158115Sume * 2. Redistributions in binary form must reproduce the above copyright
14171795Sbushman *    notice, this list of conditions and the following disclaimer in the
15158115Sume *    documentation and/or other materials provided with the distribution.
16171795Sbushman * 3. The name of the author may not be used to endorse or promote products
17158115Sume *    derived from this software without specific prior written permission.
18158115Sume *
19158115Sume * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20158115Sume * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21193119Sdougb * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
22158115Sume * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23158115Sume * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24171795Sbushman * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25159348Sume * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26159348Sume * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27158115Sume * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28172377Sbushman * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29172377Sbushman */
30172377Sbushman
31172377Sbushman#ifndef lint
32172377Sbushmanstatic char rcsid[] = "$OpenBSD: dirname.c,v 1.4 1999/05/30 17:10:30 espie Exp $";
33172377Sbushman#endif /* not lint */
34158115Sume
35172377Sbushman#include <errno.h>
36172377Sbushman#include <libgen.h>
37172377Sbushman#include <string.h>
38172377Sbushman#include <sys/param.h>
39172377Sbushman
40172377Sbushmanchar *
41172377Sbushmandirname(path)
42172377Sbushman	const char *path;
43172377Sbushman{
44172377Sbushman	static char bname[MAXPATHLEN];
45172377Sbushman	register const char *endp;
46172377Sbushman
47172377Sbushman	/* Empty or NULL string gets treated as "." */
48158115Sume	if (path == NULL || *path == '\0') {
49172377Sbushman		(void)strcpy(bname, ".");
50172377Sbushman		return(bname);
51172377Sbushman	}
52158115Sume
53172377Sbushman	/* Strip trailing slashes */
54	endp = path + strlen(path) - 1;
55	while (endp > path && *endp == '/')
56		endp--;
57
58	/* Find the start of the dir */
59	while (endp > path && *endp != '/')
60		endp--;
61
62	/* Either the dir is "/" or there are no slashes */
63	if (endp == path) {
64		(void)strcpy(bname, *endp == '/' ? "/" : ".");
65		return(bname);
66	} else {
67		do {
68			endp--;
69		} while (endp > path && *endp == '/');
70	}
71
72	if (endp - path + 1 > sizeof(bname)) {
73		errno = ENAMETOOLONG;
74		return(NULL);
75	}
76	(void)strncpy(bname, path, endp - path + 1);
77	bname[endp - path + 1] = '\0';
78	return(bname);
79}
80