]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/PosixLib/Gen/dirname.c
3 Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials are licensed and made available under
5 the terms and conditions of the BSD License that accompanies this distribution.
6 The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 * Copyright (c) 1997, 2002 The NetBSD Foundation, Inc.
13 * All rights reserved.
15 * This code is derived from software contributed to The NetBSD Foundation
16 * by Klaus Klein and Jason R. Thorpe.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
39 NetBSD: dirname.c,v 1.10 2008/05/10 22:39:40 christos Exp
41 #include <LibConfig.h>
42 #include <sys/cdefs.h>
49 __weak_alias(dirname
,_dirname
)
56 static char singledot
[] = ".";
57 static char result
[PATH_MAX
];
62 * If `path' is a null pointer or points to an empty string,
63 * return a pointer to the string ".".
65 if ((path
== NULL
) || (*path
== '\0'))
68 /* Strip trailing slashes, if any. */
69 lastp
= path
+ strlen(path
) - 1;
70 while (lastp
!= path
&& isDirSep(*lastp
))
73 /* Terminate path at the last occurence of '/'. */
75 if (isDirSep(*lastp
)) {
76 /* Strip trailing slashes, if any. */
77 while (lastp
!= path
&& isDirSep(*lastp
))
80 /* ...and copy the result into the result buffer.
81 We make sure that there will be room for the terminating NUL
82 and for a final '/', if necessary.
84 len
= (lastp
- path
) + 1 /* last char */;
85 if (len
> (PATH_MAX
- 2))
88 memcpy(result
, path
, len
);
89 if(*lastp
== ':') { /* Have we stripped off all except the Volume name? */
90 if(isDirSep(lastp
[1])) { /* Was ...":/"... so we want the root of the volume. */
100 } while (--lastp
>= path
);
102 /* No /'s found, return a pointer to the string ".". */