Skip to content

Commit df714c3

Browse files
committed
- add S_ISREG and friends
Signed-off-by: SSE4 <[email protected]>
1 parent 6c19d88 commit df714c3

File tree

2 files changed

+129
-1
lines changed

2 files changed

+129
-1
lines changed

example.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,43 @@
44

55
#include <unistd.h>
66
#include <assert.h>
7+
#include <stdio.h>
78

89
int main()
910
{
11+
struct stat s;
12+
1013
assert(access("dummy", R_OK) == -1);
1114
assert(access("dummy", W_OK) == -1);
1215
assert(access("dummy", X_OK) == -1);
1316
assert(access("dummy", F_OK) == -1);
1417

18+
FILE* f = fopen("dummy", "wb");
19+
assert(f);
20+
fclose(f);
21+
22+
stat("dummy", &s);
23+
24+
assert(S_ISREG(s.st_mode));
25+
assert(!S_ISDIR(s.st_mode));
26+
27+
assert(access("dummy", R_OK) == 0);
28+
assert(access("dummy", W_OK) == 0);
29+
assert(access("dummy", X_OK) == 0);
30+
assert(access("dummy", F_OK) == 0);
31+
32+
unlink("dummy");
33+
1534
int fd2 = dup(STDIN_FILENO);
1635
assert(fd2 != STDIN_FILENO);
1736
close(fd2);
18-
_mkdir("temp");
37+
38+
assert(rmdir("temp") == -1);
39+
assert(_mkdir("temp") == 0);
40+
41+
stat("temp", &s);
42+
assert(!S_ISREG(s.st_mode));
43+
assert(S_ISDIR(s.st_mode));
44+
1945
assert(rmdir("temp") == 0);
2046
}

unistd.h

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ extern "C" {
4545
#include <process.h> /* _execl() */
4646
#endif /* _INC_PROCESS */
4747

48+
#include <sys/stat.h> /* */
49+
4850
#ifndef access
4951
#define access _access
5052
#endif /* access */
@@ -121,6 +123,10 @@ extern "C" {
121123
#define rmdir _rmdir
122124
#endif /* rmdir */
123125

126+
#ifndef unlink
127+
#define unlink _unlink
128+
#endif /* unlink */
129+
124130
/* permission bits below must be defined in sys/stat.h, but MSVC lacks them */
125131

126132
#ifndef S_IRWXU
@@ -203,6 +209,102 @@ extern "C" {
203209
#define S_IXUGO 0111
204210
#endif /* S_IXUGO */
205211

212+
#ifndef _S_IFMT
213+
#define _S_IFMT 0xF000
214+
#endif /* _S_IFMT */
215+
216+
#ifndef _S_IFIFO
217+
#define _S_IFIFO 0x1000
218+
#endif /* _S_IFIFO */
219+
220+
#ifndef _S_IFCHR
221+
#define _S_IFCHR 0x2000
222+
#endif /* _S_IFCHR */
223+
224+
#ifndef _S_IFDIR
225+
#define _S_IFDIR 0x4000
226+
#endif /* _S_IFDIR */
227+
228+
#ifndef _S_IFBLK
229+
#define _S_IFBLK 0x6000
230+
#endif /* _S_IFBLK */
231+
232+
#ifndef _S_IFREG
233+
#define _S_IFREG 0x8000
234+
#endif /* _S_IFREG */
235+
236+
#ifndef _S_IFLNK
237+
#define _S_IFLNK 0xA000
238+
#endif /* _S_IFLNK */
239+
240+
#ifndef _S_IFSOCK
241+
#define _S_IFSOCK 0xC000
242+
#endif /* _S_IFSOCK */
243+
244+
#ifndef S_IFMT
245+
#define S_IFMT _S_IFMT
246+
#endif /* S_IFMT */
247+
248+
#ifndef S_IFIFO
249+
#define S_IFIFO _S_IFIFO
250+
#endif /* S_IFIFO */
251+
252+
#ifndef S_IFCHR
253+
#define S_IFCHR _S_IFCHR
254+
#endif /* S_IFCHR */
255+
256+
#ifndef S_IFDIR
257+
#define S_IFDIR _S_IFDIR
258+
#endif /* S_IFDIR */
259+
260+
#ifndef S_IFBLK
261+
#define S_IFBLK _S_IFBLK
262+
#endif /* S_IFBLK */
263+
264+
#ifndef S_IFREG
265+
#define S_IFREG _S_IFREG
266+
#endif /* S_IFREG */
267+
268+
#ifndef S_IFLNK
269+
#define S_IFLNK _S_IFLNK
270+
#endif /* S_IFLNK */
271+
272+
#ifndef S_IFSOCK
273+
#define S_IFSOCK _S_IFSOCK
274+
#endif /* S_IFSOCK */
275+
276+
#ifndef S_ISTYPE
277+
#define S_ISTYPE(mode, mask) (((mode) & S_IFMT) == (mask))
278+
#endif /* S_ISTYPE */
279+
280+
#ifndef S_ISFIFO
281+
#define S_ISFIFO(mode) S_ISTYPE(mode, S_IFIFO)
282+
#endif /* S_ISFIFO */
283+
284+
#ifndef S_ISCHR
285+
#define S_ISCHR(mode) S_ISTYPE(mode, S_IFCHR)
286+
#endif /* S_ISCHR */
287+
288+
#ifndef S_ISDIR
289+
#define S_ISDIR(mode) S_ISTYPE(mode, S_IFDIR)
290+
#endif /* S_ISDIR */
291+
292+
#ifndef S_ISBLK
293+
#define S_ISBLK(mode) S_ISTYPE(mode, S_IFBLK)
294+
#endif /* S_ISBLK */
295+
296+
#ifndef S_ISREG
297+
#define S_ISREG(mode) S_ISTYPE(mode, S_IFREG)
298+
#endif /* S_ISREG */
299+
300+
#ifndef S_ISLNK
301+
#define S_ISLNK(mode) S_ISTYPE(mode, S_IFLNK)
302+
#endif /* S_ISLNK */
303+
304+
#ifndef S_ISSOCK
305+
#define S_ISSOCK(mode) S_ISTYPE(mode, S_IFSOCK)
306+
#endif /* S_ISSOCK */
307+
206308
#ifdef __cplusplus
207309
}
208310
#endif /* __cplusplus */

0 commit comments

Comments
 (0)