-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirname.c
164 lines (158 loc) · 5.46 KB
/
dirname.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* dirname.c
*
* $Id: dirname.c,v 1.2 2007/03/08 23:15:58 keithmarshall Exp $
*
* Provides an implementation of the "dirname" function, conforming
* to SUSv3, with extensions to accommodate Win32 drive designators,
* and suitable for use on native Microsoft(R) Win32 platforms.
*
* Written by Keith Marshall <[email protected]>
*
* This is free software. You may redistribute and/or modify it as you
* see fit, without restriction of copyright.
*
* This software is provided "as is", in the hope that it may be useful,
* but WITHOUT WARRANTY OF ANY KIND, not even any implied warranty of
* MERCHANTABILITY, nor of FITNESS FOR ANY PARTICULAR PURPOSE. At no
* time will the author accept any form of liability for any damages,
* however caused, resulting from the use of this software.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <locale.h>
#ifndef __cdecl /* If compiling on any non-Win32 platform ... */
#define __cdecl /* this may not be defined. */
#endif
char *__cdecl dirname(char *path)
{
static char *retfail = NULL;
size_t len;
if (path && *path)
{
/* allocate sufficient local storage space,
* in which to create a reference copy of path. */
char refcopy[1 + (len = strlen(path))];
/* create the reference copy of path */
char *refpath = refcopy;
strcpy_s(refpath, sizeof(refcopy), path);
len = strlen(refpath);
/* SUSv3 identifies a special case, where path is exactly equal to "//";
* (we will also accept "\\" in the Win32 context, but not "/\" or "\/",
* and neither will we consider paths with an initial drive designator).
* For this special case, SUSv3 allows the implementation to choose to
* return "/" or "//", (or "\" or "\\", since this is Win32); we will
* simply return the path unchanged, (i.e. "//" or "\\"). */
if (len > 1 && (refpath[0] == '/' || refpath[0] == '\\'))
{
if (refpath[1] == refpath[0] && refpath[2] == '\0')
{
return path;
}
}
/* For all other cases ...
* step over the drive designator, if present ... */
else if (len > 1 && refpath[1] == ':')
{
/* FIXME: maybe should confirm *refpath is a valid drive designator. */
refpath += 2;
}
/* check again, just to ensure we still have a non-empty path name ... */
if (*refpath)
{
#undef basename
#define basename __the_basename /* avoid shadowing. */
/* reproduce the scanning logic of the "basename" function
* to locate the basename component of the current path string,
* (but also remember where the dirname component starts). */
char *refname, *basename;
for (refname = basename = refpath; *refpath; ++refpath)
{
if (*refpath == '/' || *refpath == '\\')
{
/* we found a dir separator ...
* step over it, and any others which immediately follow it. */
while (*refpath == '/' || *refpath == '\\')
++refpath;
/* if we didn't reach the end of the path string ... */
if (*refpath)
/* then we have a new candidate for the base name. */
basename = refpath;
else
/* we struck an early termination of the path string,
* with trailing dir separators following the base name,
* so break out of the for loop, to avoid overrun. */
break;
}
}
/* now check,
* to confirm that we have distinct dirname and basename components. */
if (basename > refname)
{
/* and, when we do ...
* backtrack over all trailing separators on the dirname component,
* (but preserve exactly two initial dirname separators, if identical),
* and add a NUL terminator in their place. */
do
--basename;
while (basename > refname && (*basename == '/' || *basename == '\\'));
if (basename == refname && (refname[0] == '/' || refname[0] == '\\') && refname[1] == refname[0] && refname[2] != '/' && refname[2] != '\\')
++basename;
*++basename = '\0';
/* if the resultant dirname begins with EXACTLY two dir separators,
* AND both are identical, then we preserve them. */
refpath = refcopy;
while ((*refpath == '/' || *refpath == '\\'))
++refpath;
if ((refpath - refcopy) > 2 || refcopy[1] != refcopy[0])
refpath = refcopy;
/* and finally ...
* we remove any residual, redundantly duplicated separators from the dirname,
* reterminate, and return it. */
refname = refpath;
while (*refpath)
{
if ((*refname++ = *refpath) == '/' || *refpath++ == '\\')
{
while (*refpath == '/' || *refpath == '\\')
++refpath;
}
}
*refname = '\0';
/* finally ...return the resultant dirname. */
strcpy_s(path, sizeof(refcopy), refcopy);
}
else
{
/* either there were no dirname separators in the path name,
* or there was nothing else ... */
if (*refname == '/' || *refname == '\\')
{
/* it was all separators, so return one. */
++refname;
}
else
{
/* there were no separators, so return '.'. */
*refname++ = '.';
}
/* add a NUL terminator, in either case*/
*refname = '\0';
retfail = realloc(retfail, len = 1 + strlen(refcopy));
strcpy_s(retfail, sizeof(refcopy), refcopy);
path = retfail;
}
/* return the resolved dirname. */
return path;
}
#undef basename
}
/* path is NULL, or an empty string; default return value is "." ...
* return this in our own buffer, in case the caller trashed it after a previous call.
*/
retfail = ".";
/* return the default dirname. */
return retfail;
}