1 | /** @file
|
---|
2 |
|
---|
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.
|
---|
8 |
|
---|
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.
|
---|
11 |
|
---|
12 | * Copyright (c) 1997, 2002 The NetBSD Foundation, Inc.
|
---|
13 | * All rights reserved.
|
---|
14 | *
|
---|
15 | * This code is derived from software contributed to The NetBSD Foundation
|
---|
16 | * by Klaus Klein and Jason R. Thorpe.
|
---|
17 | *
|
---|
18 | * Redistribution and use in source and binary forms, with or without
|
---|
19 | * modification, are permitted provided that the following conditions
|
---|
20 | * are met:
|
---|
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.
|
---|
26 | *
|
---|
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.
|
---|
38 |
|
---|
39 | NetBSD: dirname.c,v 1.10 2008/05/10 22:39:40 christos Exp
|
---|
40 | */
|
---|
41 | #include <LibConfig.h>
|
---|
42 | #include <sys/cdefs.h>
|
---|
43 |
|
---|
44 | #include <limits.h>
|
---|
45 | #include <ctype.h>
|
---|
46 | #include <string.h>
|
---|
47 |
|
---|
48 | #ifdef __weak_alias
|
---|
49 | __weak_alias(dirname,_dirname)
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | #ifdef HAVE_DIRNAME
|
---|
53 | char *
|
---|
54 | dirname(char *path)
|
---|
55 | {
|
---|
56 | static char singledot[] = ".";
|
---|
57 | static char result[PATH_MAX];
|
---|
58 | const char *lastp;
|
---|
59 | size_t len;
|
---|
60 |
|
---|
61 | /*
|
---|
62 | * If `path' is a null pointer or points to an empty string,
|
---|
63 | * return a pointer to the string ".".
|
---|
64 | */
|
---|
65 | if ((path == NULL) || (*path == '\0'))
|
---|
66 | return (singledot);
|
---|
67 |
|
---|
68 | /* Strip trailing slashes, if any. */
|
---|
69 | lastp = path + strlen(path) - 1;
|
---|
70 | while (lastp != path && isDirSep(*lastp))
|
---|
71 | lastp--;
|
---|
72 |
|
---|
73 | /* Terminate path at the last occurence of '/'. */
|
---|
74 | do {
|
---|
75 | if (isDirSep(*lastp)) {
|
---|
76 | /* Strip trailing slashes, if any. */
|
---|
77 | while (lastp != path && isDirSep(*lastp))
|
---|
78 | lastp--;
|
---|
79 |
|
---|
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.
|
---|
83 | */
|
---|
84 | len = (lastp - path) + 1 /* last char */;
|
---|
85 | if (len > (PATH_MAX - 2))
|
---|
86 | len = PATH_MAX - 2;
|
---|
87 |
|
---|
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. */
|
---|
91 | result[len++] = '/';
|
---|
92 | }
|
---|
93 | else {
|
---|
94 | result[len++] = '.';
|
---|
95 | }
|
---|
96 | }
|
---|
97 | result[len] = '\0';
|
---|
98 | return (result);
|
---|
99 | }
|
---|
100 | } while (--lastp >= path);
|
---|
101 |
|
---|
102 | /* No /'s found, return a pointer to the string ".". */
|
---|
103 | return (singledot);
|
---|
104 | }
|
---|
105 | #endif
|
---|