Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4973 | right-hear | 1 | /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */ |
2 | /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */ |
||
3 | #include |
||
4 | #include |
||
5 | #include |
||
6 | #include |
||
7 | #include |
||
8 | #include |
||
9 | #include |
||
10 | |||
11 | int access(const char *fn, int flags) |
||
12 | { |
||
13 | unsigned attr = _chmod(fn, 0); |
||
14 | |||
15 | if (attr == -1) { |
||
16 | char fixed_path[FILENAME_MAX]; |
||
17 | const char* p; |
||
18 | int nums = 0; |
||
19 | DIR* d; |
||
20 | |||
21 | /* Root directories on some non-local drives (e.g. CD-ROM) |
||
22 | might fail `_chmod'. `findfirst' to the rescue. */ |
||
23 | _fixpath(fn, fixed_path); |
||
24 | for (p=fixed_path;*p;p++) if (*p == '/') ++nums; |
||
25 | if (nums <= 2) |
||
26 | { |
||
27 | d = opendir(fn); |
||
28 | if (d) {closedir(d);return 0;} |
||
29 | } |
||
30 | |||
31 | errno = ENOENT; |
||
32 | return -1; |
||
33 | } |
||
34 | |||
35 | if (attr & 0x10) /* directory? */ |
||
36 | return 0; /* directories always OK */ |
||
37 | if (flags & D_OK) |
||
38 | { |
||
39 | errno = EACCES; |
||
40 | return -1; /* not a directory */ |
||
41 | } |
||
42 | |||
43 | if ((flags & W_OK) && (attr & 1)) |
||
44 | { |
||
45 | errno = EACCES; |
||
46 | return -1; /* not writable */ |
||
47 | } |
||
48 | |||
49 | if (flags & X_OK) |
||
50 | { |
||
51 | if (!_is_executable(fn, 0, 0)) |
||
52 | { |
||
53 | errno = EACCES; |
||
54 | return -1; |
||
55 | } |
||
56 | } |
||
57 | |||
58 | return 0; /* everything else is OK */ |
||
59 | }=> |