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