-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathdirname.c
154 lines (149 loc) · 5.11 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
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1992-2012 AT&T Intellectual Property *
* Copyright (c) 2020-2024 Contributors to ksh 93u+m *
* and is licensed under the *
* Eclipse Public License, Version 2.0 *
* *
* A copy of the License is available at *
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html *
* (with md5 checksum 84283fa8859daf213bdda5a9f8d1be1d) *
* *
* Glenn Fowler <[email protected]> *
* David Korn <[email protected]> *
* Martijn Dekker <[email protected]> *
* Johnothan King <[email protected]> *
* dnewhall <[email protected]> *
* *
***********************************************************************/
/*
* David Korn
* AT&T Bell Laboratories
*
* dirname path [suffix]
*
* print the dirname of a pathname
*/
static const char usage[] =
"[-?\n@(#)$Id: dirname (ksh 93u+m) 2024-12-25 $\n]"
"[--catalog?" ERROR_CATALOG "]"
"[+NAME?dirname - return directory portion of file name]"
"[+DESCRIPTION?\bdirname\b treats each \astring\a as a file name and outputs "
"the name of the directory containing the file name by deleting "
"the last component from \astring\a.]"
"[+?If \astring\a consists solely of \b/\b characters the output will "
"be a single \b/\b unless \bPATH_LEADING_SLASHES\b returned by "
"\bgetconf\b(1) is \b1\b and \astring\a consists of multiple "
"\b/\b characters in which case \b//\b will be output. "
"Otherwise, trailing \b/\b characters are removed, and if "
"there are no remaining \b/\b characters in \astring\a, "
"the string \b.\b will be written to standard output. "
"Otherwise, all characters following the last \b/\b are removed. "
"If the remaining string consists solely of \b/\b characters, "
"the output will be as if the original string had consisted solely "
"as \b/\b characters as described above. Otherwise, all "
"trailing slashes are removed and the output will be this string "
"unless this string is empty. If empty the output will be \b.\b.]"
"[f:file?Print the \b$PATH\b relative regular file path for \astring\a.]"
"[r:relative?Print the \b$PATH\b relative readable file path for \astring\a.]"
"[x:executable?Print the \b$PATH\b relative executable file path for \astring\a.]"
"[z:zero?Each line of output is terminated with a NUL character instead "
"of a newline.]"
"\n"
"\nstring ...\n"
"\n"
"[+EXIT STATUS?]{"
"[+0?Successful completion.]"
"[+>0?An error occurred.]"
"}"
"[+SEE ALSO?\bbasename\b(1), \bgetconf\b(1), \bdirname\b(3), \bpathname\b(3)]"
;
#include <cmd.h>
static void l_dirname(Sfio_t *outfile, const char *pathname, char termch)
{
const char *last;
/* go to end of path */
for(last=pathname; *last; last++);
/* back over trailing '/' */
while(last>pathname && *--last=='/');
/* back over non-slash chars */
for(;last>pathname && *last!='/';last--);
if(last==pathname)
{
/* all '/' or "" */
if(*pathname!='/')
last = pathname = ".";
}
else
{
/* back over trailing '/' */
for(;*last=='/' && last > pathname; last--);
}
/* preserve leading '//' */
if(last==pathname && pathname[0]=='/')
while(last[1]=='/')
last++;
if(last!=pathname && pathname[0]=='/' && pathname[1]=='/')
{
/* skip any '/' until last two */
while(pathname[2]=='/' && pathname<last)
pathname++;
/* skip first '/' if PATH_LEADING_SLASHES not set */
if(last!=pathname && pathname[0]=='/' && pathname[1]=='/' && *astconf("PATH_LEADING_SLASHES",NULL,NULL)!='1')
pathname++;
}
sfwrite(outfile,pathname,last+1-pathname);
sfputc(outfile,termch);
}
int
b_dirname(int argc, char** argv, Shbltin_t* context)
{
int mode = 0;
char buf[PATH_MAX];
char termch = '\n';
cmdinit(argc, argv, context, ERROR_CATALOG, 0);
for (;;)
{
switch (optget(argv, usage))
{
case 'f':
mode |= PATH_REGULAR;
continue;
case 'r':
mode &= ~PATH_REGULAR;
mode |= PATH_READ;
continue;
case 'x':
mode |= PATH_EXECUTE;
continue;
case 'z':
termch = '\0';
continue;
case ':':
error(2, "%s", opt_info.arg);
break;
case '?':
error(ERROR_usage(2), "%s", opt_info.arg);
UNREACHABLE();
}
break;
}
argv += opt_info.index;
argc -= opt_info.index;
if(error_info.errors || argc < 1)
{
error(ERROR_usage(2),"%s", optusage(NULL));
UNREACHABLE();
}
for(; argv[0]; argv++)
{
if(!mode)
l_dirname(sfstdout,argv[0],termch);
else if(pathpath(argv[0], "", mode, buf, sizeof(buf)))
sfputr(sfstdout, buf, termch);
else
error(1|ERROR_WARNING, "%s: relative path not found", argv[0]);
}
return 0;
}