FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
fastfetch/src/common/impl/path.c at dev · fastfetch-cli/fastfetch · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
fastfetch-cli
/
fastfetch
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
867
Star
24.4k
Code
Issues
57
Pull requests
20
Discussions
Actions
Wiki
Security and quality
1
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
fastfetch
/
src
/
common
/
impl
/
path.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
277 lines (241 loc) · 7.96 KB
Breadcrumbs
fastfetch
/
src
/
common
/
impl
/
path.c
Copy path
File metadata and controls
277 lines (241 loc) · 7.96 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include
"common/path.h"
#include
"common/io.h"
#include
"common/arrutil.h"
#if
!
_WIN32
const
char
*
ffFindExecutableInPath
(
const
char
*
name
,
FFstrbuf
*
result
) {
char
*
path
=
getenv
(
"PATH"
);
if
(!
path
) {
return
"$PATH not set"
;
}
#ifdef
_WIN32
const
bool
appendExe
=
!
ffStrEndsWithIgnCase
(
name
,
".exe"
);
#endif
for
(
char
*
token
=
path
;
*
token
;
path
=
token
+
1
) {
token
=
strchr
(
path
,
#ifdef
_WIN32
';'
#else
':'
#endif
);
if
(!
token
) {
token
=
path
+
strlen
(
path
);
}
ffStrbufSetNS
(
result
, (
uint32_t
) (
token
-
path
),
path
);
ffStrbufEnsureEndsWithC
(
result
,
#ifdef
_WIN32
'\\'
#else
'/'
#endif
);
ffStrbufAppendS
(
result
,
name
);
#ifdef
_WIN32
if
(
appendExe
) {
ffStrbufAppendS
(
result
,
".exe"
);
}
if
(!
ffPathExists
(
result
->
chars
,
FF_PATHTYPE_FILE
)) {
continue
;
}
#else
if
(
access
(
result
->
chars
,
X_OK
)
!=
0
) {
continue
;
}
#endif
return
nullptr
;
}
ffStrbufClear
(
result
);
return
"Executable not found"
;
}
#else
#include
<windows.h>
#include
<winioctl.h>
#include
<errno.h>
#include
<stdalign.h>
const
char
*
ffFindExecutableInPath
(
const
char
*
name
,
FFstrbuf
*
result
) {
char
buffer
[
MAX_PATH
+
1
];
DWORD
length
=
SearchPathA
(
nullptr
,
name
,
".exe"
,
sizeof
(
buffer
),
buffer
,
nullptr
);
if
(
length
==
0
) {
ffStrbufClear
(
result
);
return
"Executable not found"
;
}
ffStrbufSetS
(
result
,
buffer
);
return
nullptr
;
}
static
inline
int
winerr2Errno
(
DWORD
err
) {
switch
(
err
) {
case
ERROR_FILE_NOT_FOUND
:
case
ERROR_PATH_NOT_FOUND
:
case
ERROR_INVALID_NAME
:
return
ENOENT
;
case
ERROR_ACCESS_DENIED
:
case
ERROR_SHARING_VIOLATION
:
case
ERROR_LOCK_VIOLATION
:
return
EACCES
;
case
ERROR_BUFFER_OVERFLOW
:
case
ERROR_INSUFFICIENT_BUFFER
:
return
ENAMETOOLONG
;
case
ERROR_INVALID_PARAMETER
:
case
ERROR_NOT_A_REPARSE_POINT
:
return
EINVAL
;
default
:
return
EIO
;
}
}
char
*
frealpath
(
HANDLE
hFile
,
char
*
resolved_name
) {
if
(
__builtin_expect
(
hFile
==
INVALID_HANDLE_VALUE
||
!
hFile
, false)) {
errno
=
EINVAL
;
return
nullptr
;
}
wchar_t
resolvedNameW
[
MAX_PATH
+
4
];
/* +4 for "\\\\?\\" prefix */
DWORD
lenW
=
GetFinalPathNameByHandleW
(
hFile
,
resolvedNameW
, (
DWORD
)
ARRAY_SIZE
(
resolvedNameW
),
FILE_NAME_NORMALIZED
);
if
(
lenW
==
0
) {
errno
=
winerr2Errno
(
GetLastError
());
return
nullptr
;
}
if
(
lenW
>=
ARRAY_SIZE
(
resolvedNameW
)) {
errno
=
E2BIG
;
return
nullptr
;
}
lenW
++
;
// Include null terminator
wchar_t
*
srcW
=
resolvedNameW
;
DWORD
srcLenW
=
lenW
;
if
(
srcLenW
>=
8
&&
wcsncmp
(
resolvedNameW
,
L"\\\\?\\UNC\\"
,
8
)
==
0
) {
/* Convert "\\?\UNC\server\share" to "\\server\share" */
srcW
+=
6
;
srcLenW
-=
6
;
*
srcW
=
L'\\'
;
}
else
if
(
srcLenW
>=
4
&&
wcsncmp
(
resolvedNameW
,
L"\\\\?\\"
,
4
)
==
0
) {
srcW
+=
4
;
srcLenW
-=
4
;
}
if
(
resolved_name
) {
ULONG
outBytes
=
0
;
if
(!
NT_SUCCESS
(
RtlUnicodeToUTF8N
(
resolved_name
,
MAX_PATH
,
&
outBytes
,
srcW
, (
ULONG
) (
srcLenW
*
sizeof
(
wchar_t
))))) {
errno
=
E2BIG
;
return
nullptr
;
}
}
else
{
/* UTF-8 worst-case: up to 4 bytes per UTF-16 code unit */
char
tmp
[(
MAX_PATH
+
4
)
*
4
];
ULONG
outBytes
=
0
;
if
(!
NT_SUCCESS
(
RtlUnicodeToUTF8N
(
tmp
, (
ULONG
)
sizeof
(
tmp
),
&
outBytes
,
srcW
, (
ULONG
) (
srcLenW
*
sizeof
(
wchar_t
))))) {
errno
=
E2BIG
;
return
nullptr
;
}
resolved_name
=
(
char
*
)
malloc
(
outBytes
);
if
(!
resolved_name
) {
errno
=
ENOMEM
;
return
nullptr
;
}
memcpy
(
resolved_name
,
tmp
,
outBytes
);
}
return
resolved_name
;
}
char
*
realpath
(
const
char
*
__restrict
file_name
,
char
*
__restrict
resolved_name
) {
if
(!
file_name
) {
errno
=
EINVAL
;
return
nullptr
;
}
wchar_t
fileNameW
[
MAX_PATH
];
ULONG
lenBytes
=
0
;
if
(!
NT_SUCCESS
(
RtlUTF8ToUnicodeN
(
fileNameW
, (
ULONG
)
sizeof
(
fileNameW
),
&
lenBytes
,
file_name
, (
ULONG
)
strlen
(
file_name
)
+
1
))) {
errno
=
EINVAL
;
return
nullptr
;
}
FF_AUTO_CLOSE_FD
HANDLE
hFile
=
CreateFileW
(
fileNameW
,
0
,
FILE_SHARE_READ
|
FILE_SHARE_WRITE
|
FILE_SHARE_DELETE
,
nullptr
,
OPEN_EXISTING
,
FILE_FLAG_BACKUP_SEMANTICS
,
nullptr
);
if
(
hFile
==
INVALID_HANDLE_VALUE
) {
errno
=
winerr2Errno
(
GetLastError
());
return
nullptr
;
}
return
frealpath
(
hFile
,
resolved_name
);
}
ssize_t
freadlink
(
HANDLE
hFile
,
char
*
buf
,
size_t
bufsiz
) {
if
(
__builtin_expect
(
hFile
==
INVALID_HANDLE_VALUE
||
!
buf
||
bufsiz
==
0
, false)) {
errno
=
EINVAL
;
return
-1
;
}
alignas(
REPARSE_DATA_BUFFER
)
BYTE
reparseBuf
[
MAXIMUM_REPARSE_DATA_BUFFER_SIZE
];
DWORD
bytesReturned
=
0
;
if
(!
DeviceIoControl
(
hFile
,
FSCTL_GET_REPARSE_POINT
,
nullptr
,
0
,
reparseBuf
, (
DWORD
)
sizeof
(
reparseBuf
),
&
bytesReturned
,
nullptr
)) {
errno
=
winerr2Errno
(
GetLastError
());
return
-1
;
}
REPARSE_DATA_BUFFER
*
rp
=
(
REPARSE_DATA_BUFFER
*
)
reparseBuf
;
const
wchar_t
*
targetW
=
nullptr
;
USHORT
targetBytes
=
0
;
if
(
rp
->
ReparseTag
==
IO_REPARSE_TAG_SYMLINK
) {
if
(
rp
->
SymbolicLinkReparseBuffer
.
PrintNameLength
>
0
) {
targetW
=
rp
->
SymbolicLinkReparseBuffer
.
PathBuffer
+
(
rp
->
SymbolicLinkReparseBuffer
.
PrintNameOffset
/
sizeof
(
wchar_t
));
targetBytes
=
rp
->
SymbolicLinkReparseBuffer
.
PrintNameLength
;
}
else
{
targetW
=
rp
->
SymbolicLinkReparseBuffer
.
PathBuffer
+
(
rp
->
SymbolicLinkReparseBuffer
.
SubstituteNameOffset
/
sizeof
(
wchar_t
));
targetBytes
=
rp
->
SymbolicLinkReparseBuffer
.
SubstituteNameLength
;
if
(
targetBytes
>=
8
&&
wcsncmp
(
targetW
,
L"\\??\\"
,
4
)
==
0
) {
targetW
+=
4
;
targetBytes
-=
8
;
}
}
}
else
if
(
rp
->
ReparseTag
==
IO_REPARSE_TAG_MOUNT_POINT
) {
if
(
rp
->
MountPointReparseBuffer
.
PrintNameLength
>
0
) {
targetW
=
rp
->
MountPointReparseBuffer
.
PathBuffer
+
(
rp
->
MountPointReparseBuffer
.
PrintNameOffset
/
sizeof
(
wchar_t
));
targetBytes
=
rp
->
MountPointReparseBuffer
.
PrintNameLength
;
}
else
{
targetW
=
rp
->
MountPointReparseBuffer
.
PathBuffer
+
(
rp
->
MountPointReparseBuffer
.
SubstituteNameOffset
/
sizeof
(
wchar_t
));
targetBytes
=
rp
->
MountPointReparseBuffer
.
SubstituteNameLength
;
if
(
targetBytes
>=
8
&&
wcsncmp
(
targetW
,
L"\\??\\"
,
4
)
==
0
) {
targetW
+=
4
;
targetBytes
-=
8
;
}
}
}
else
{
errno
=
EINVAL
;
return
-1
;
}
ULONG
outBytes
=
0
;
if
(!
NT_SUCCESS
(
RtlUnicodeToUTF8N
(
buf
, (
ULONG
)
bufsiz
,
&
outBytes
,
targetW
,
targetBytes
))) {
errno
=
E2BIG
;
return
-1
;
}
// Not null-terminated
return
(
ssize_t
)
outBytes
;
}
ssize_t
readlink
(
const
char
*
path
,
char
*
buf
,
size_t
bufsiz
) {
if
(!
path
||
!
buf
||
bufsiz
==
0
) {
errno
=
EINVAL
;
return
-1
;
}
wchar_t
pathW
[
MAX_PATH
];
ULONG
pathWBytes
=
0
;
if
(!
NT_SUCCESS
(
RtlUTF8ToUnicodeN
(
pathW
, (
ULONG
)
sizeof
(
pathW
),
&
pathWBytes
,
path
, (
ULONG
)
strlen
(
path
)
+
1
))) {
errno
=
EINVAL
;
return
-1
;
}
FF_AUTO_CLOSE_FD
HANDLE
hFile
=
CreateFileW
(
pathW
,
0
,
FILE_SHARE_READ
|
FILE_SHARE_WRITE
|
FILE_SHARE_DELETE
,
nullptr
,
OPEN_EXISTING
,
FILE_FLAG_BACKUP_SEMANTICS
|
FILE_FLAG_OPEN_REPARSE_POINT
,
nullptr
);
if
(
hFile
==
INVALID_HANDLE_VALUE
) {
errno
=
winerr2Errno
(
GetLastError
());
return
-1
;
}
return
freadlink
(
hFile
,
buf
,
bufsiz
);
}
#endif
Back
|
FazBrowse Home
|
New Git URL