]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/PosixLib/Gen/dirname.c
StdLib: Add directory access functions to PosixLib.
[mirror_edk2.git] / StdLib / PosixLib / Gen / dirname.c
CommitLineData
d7ce7006 1/** @file\r
2\r
3 * Copyright (c) 1997, 2002 The NetBSD Foundation, Inc.\r
4 * All rights reserved.\r
5 *\r
6 * This code is derived from software contributed to The NetBSD Foundation\r
7 * by Klaus Klein and Jason R. Thorpe.\r
8 *\r
9 * Redistribution and use in source and binary forms, with or without\r
10 * modification, are permitted provided that the following conditions\r
11 * are met:\r
12 * 1. Redistributions of source code must retain the above copyright\r
13 * notice, this list of conditions and the following disclaimer.\r
14 * 2. Redistributions in binary form must reproduce the above copyright\r
15 * notice, this list of conditions and the following disclaimer in the\r
16 * documentation and/or other materials provided with the distribution.\r
17 *\r
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS\r
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED\r
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS\r
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
28 * POSSIBILITY OF SUCH DAMAGE.\r
29\r
30 NetBSD: dirname.c,v 1.10 2008/05/10 22:39:40 christos Exp\r
31 */\r
32#include <LibConfig.h>\r
33\r
34#include <sys/cdefs.h>\r
35\r
36//#include "namespace.h"\r
37//#include <libgen.h>\r
38#include <limits.h>\r
39#include <string.h>\r
40\r
41#ifdef __weak_alias\r
42__weak_alias(dirname,_dirname)\r
43#endif\r
44\r
45#if !HAVE_DIRNAME\r
46char *\r
47dirname(char *path)\r
48{\r
49 static char singledot[] = ".";\r
50 static char result[PATH_MAX];\r
51 const char *lastp;\r
52 size_t len;\r
53\r
54 /*\r
55 * If `path' is a null pointer or points to an empty string,\r
56 * return a pointer to the string ".".\r
57 */\r
58 if ((path == NULL) || (*path == '\0'))\r
59 return (singledot);\r
60\r
61 /* Strip trailing slashes, if any. */\r
62 lastp = path + strlen(path) - 1;\r
63 while (lastp != path && *lastp == '/')\r
64 lastp--;\r
65\r
66 /* Terminate path at the last occurence of '/'. */\r
67 do {\r
68 if (*lastp == '/') {\r
69 /* Strip trailing slashes, if any. */\r
70 while (lastp != path && *lastp == '/')\r
71 lastp--;\r
72\r
73 /* ...and copy the result into the result buffer. */\r
74 len = (lastp - path) + 1 /* last char */;\r
75 if (len > (PATH_MAX - 1))\r
76 len = PATH_MAX - 1;\r
77\r
78 memcpy(result, path, len);\r
79 result[len] = '\0';\r
80\r
81 return (result);\r
82 }\r
83 } while (--lastp >= path);\r
84\r
85 /* No /'s found, return a pointer to the string ".". */\r
86 return (singledot);\r
87}\r
88#endif\r