Merge pull request #6535 from mruby/add-mempool-comments
[mruby.git] / mrbgems / mruby-io / src / file.c
blobed9d7a4de4dc8665d8f57da409d43b2899a8b19d
1 /*
2 ** file.c - File class
3 */
5 #include <mruby.h>
6 #include <mruby/array.h>
7 #include <mruby/class.h>
8 #include <mruby/data.h>
9 #include <mruby/string.h>
10 #include <mruby/ext/io.h>
11 #include <mruby/error.h>
12 #include <mruby/presym.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
17 #include <fcntl.h>
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #if defined(_WIN32)
23 #include <windows.h>
24 #include <io.h>
25 #define NULL_FILE "NUL"
26 #define UNLINK _unlink
27 #define GETCWD _getcwd
28 #define CHMOD(a, b) 0
29 #define MAXPATHLEN 1024
30 #if !defined(PATH_MAX)
31 #define PATH_MAX _MAX_PATH
32 #endif
33 #define realpath(N,R) _fullpath((R),(N),_MAX_PATH)
34 #include <direct.h>
35 #else
36 #define NULL_FILE "/dev/null"
37 #include <unistd.h>
38 #define UNLINK unlink
39 #define GETCWD getcwd
40 #define CHMOD(a, b) chmod(a,b)
41 #include <sys/file.h>
42 #ifndef __DJGPP__
43 #include <libgen.h>
44 #endif
45 #include <sys/param.h>
46 #include <pwd.h>
47 #endif
49 #define FILE_SEPARATOR "/"
51 #if defined(_WIN32)
52 #define PATH_SEPARATOR ";"
53 #define FILE_ALT_SEPARATOR "\\"
54 #define VOLUME_SEPARATOR ":"
55 #define DIRSEP_P(ch) (((ch) == '/') | ((ch) == '\\'))
56 #define VOLSEP_P(ch) ((ch) == ':')
57 #define UNC_PATH_P(path) (DIRSEP_P((path)[0]) && DIRSEP_P((path)[1]))
58 #define DRIVE_LETTER_P(path) (((size_t)(((path)[0]) | 0x20) - 'a' <= (size_t)'z' - 'a') && (path)[1] == ':')
59 #define DRIVE_EQUAL_P(x, y) (((x)[0] | 0x20) == ((y)[0] | 0x20))
60 #else
61 #define PATH_SEPARATOR ":"
62 #define DIRSEP_P(ch) ((ch) == '/')
63 #endif
65 #ifndef LOCK_SH
66 #define LOCK_SH 1
67 #endif
68 #ifndef LOCK_EX
69 #define LOCK_EX 2
70 #endif
71 #ifndef LOCK_NB
72 #define LOCK_NB 4
73 #endif
74 #ifndef LOCK_UN
75 #define LOCK_UN 8
76 #endif
78 #if !defined(_WIN32) || defined(MRB_MINGW32_LEGACY)
79 typedef struct stat mrb_stat;
80 # define mrb_stat(path, sb) stat(path, sb)
81 # define mrb_fstat(fd, sb) fstat(fd, sb)
82 #elif defined MRB_INT32
83 typedef struct _stat32 mrb_stat;
84 # define mrb_stat(path, sb) _stat32(path, sb)
85 # define mrb_fstat(fd, sb) _fstat32(fd, sb)
86 #else
87 typedef struct _stat64 mrb_stat;
88 # define mrb_stat(path, sb) _stat64(path, sb)
89 # define mrb_fstat(fd, sb) _fstat64(fd, sb)
90 #endif
92 #ifdef _WIN32
93 static int
94 flock(int fd, int operation)
96 HANDLE h = (HANDLE)_get_osfhandle(fd);
97 DWORD flags;
98 flags = ((operation & LOCK_NB) ? LOCKFILE_FAIL_IMMEDIATELY : 0)
99 | ((operation & LOCK_SH) ? LOCKFILE_EXCLUSIVE_LOCK : 0);
100 static const OVERLAPPED zero_ov = {0};
101 OVERLAPPED ov = zero_ov;
102 return LockFileEx(h, flags, 0, 0xffffffff, 0xffffffff, &ov) ? 0 : -1;
104 #endif
106 static mrb_value
107 mrb_file_s_umask(mrb_state *mrb, mrb_value klass)
109 #if defined(_WIN32)
110 /* nothing to do on windows */
111 return mrb_fixnum_value(0);
113 #else
114 mrb_int mask, omask;
115 if (mrb_get_args(mrb, "|i", &mask) == 0) {
116 omask = umask(0);