diff options
author | Ron Yorston <rmy@pobox.com> | 2018-03-23 16:42:54 +0000 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2018-03-23 16:42:54 +0000 |
commit | 36065f083a7eba3e0c6a9047c835b7efd1c61e9d (patch) | |
tree | 7e65c0c56977a4f21e2503636d52f7fcee48fdbe /win32 | |
parent | 6cff8357023adb4d5c9daa027522f883f3905f4c (diff) | |
download | busybox-w32-36065f083a7eba3e0c6a9047c835b7efd1c61e9d.tar.gz busybox-w32-36065f083a7eba3e0c6a9047c835b7efd1c61e9d.tar.bz2 busybox-w32-36065f083a7eba3e0c6a9047c835b7efd1c61e9d.zip |
win32: save a few bytes in device file support
Diffstat (limited to 'win32')
-rw-r--r-- | win32/mingw.c | 70 |
1 files changed, 35 insertions, 35 deletions
diff --git a/win32/mingw.c b/win32/mingw.c index d73a4d96e..8141e45a5 100644 --- a/win32/mingw.c +++ b/win32/mingw.c | |||
@@ -141,14 +141,33 @@ int err_win_to_posix(DWORD winerr) | |||
141 | static int zero_fd = -1; | 141 | static int zero_fd = -1; |
142 | static int rand_fd = -1; | 142 | static int rand_fd = -1; |
143 | 143 | ||
144 | void mingw_read_zero(int fd) | 144 | /* |
145 | * Determine if 'filename' corresponds to one of the supported | ||
146 | * device files. Constants for these are defined as an enum | ||
147 | * in mingw.h. | ||
148 | */ | ||
149 | int get_dev_type(const char *filename) | ||
145 | { | 150 | { |
146 | zero_fd = fd; | 151 | int i; |
152 | const char *devname[NOT_DEVICE] = { "null", "zero", "urandom" }; | ||
153 | |||
154 | if (filename && !strncmp(filename, "/dev/", 5)) { | ||
155 | for (i=0; i<NOT_DEVICE; ++i ) { | ||
156 | if (!strcmp(filename+5, devname[i])) { | ||
157 | return i; | ||
158 | } | ||
159 | } | ||
160 | } | ||
161 | |||
162 | return NOT_DEVICE; | ||
147 | } | 163 | } |
148 | 164 | ||
149 | void mingw_read_random(int fd) | 165 | void update_dev_fd(int dev, int fd) |
150 | { | 166 | { |
151 | rand_fd = fd; | 167 | if (dev == DEV_ZERO) |
168 | zero_fd = fd; | ||
169 | else if (dev == DEV_URANDOM) | ||
170 | rand_fd = fd; | ||
152 | } | 171 | } |
153 | 172 | ||
154 | #undef open | 173 | #undef open |
@@ -157,47 +176,28 @@ int mingw_open (const char *filename, int oflags, ...) | |||
157 | va_list args; | 176 | va_list args; |
158 | unsigned mode; | 177 | unsigned mode; |
159 | int fd; | 178 | int fd; |
160 | int special = 0; | 179 | int special = (oflags & O_SPECIAL); |
161 | int devnull = 0; | 180 | int dev = get_dev_type(filename); |
162 | int devzero = 0; | 181 | |
163 | int devrand = 0; | 182 | /* /dev/null is always allowed, others only if O_SPECIAL is set */ |
183 | if (dev != NOT_DEVICE && (dev == DEV_NULL || special)) { | ||
184 | filename = "nul"; | ||
185 | oflags = O_RDWR; | ||
186 | } | ||
164 | 187 | ||
165 | va_start(args, oflags); | 188 | va_start(args, oflags); |
166 | mode = va_arg(args, int); | 189 | mode = va_arg(args, int); |
167 | va_end(args); | 190 | va_end(args); |
168 | 191 | ||
169 | if (oflags & O_SPECIAL) { | 192 | fd = open(filename, oflags&~O_SPECIAL, mode); |
170 | oflags &= ~O_SPECIAL; | 193 | if (fd >= 0) { |
171 | special = 1; | 194 | update_dev_fd(dev, fd); |
172 | } | 195 | } |
173 | if (filename && !strncmp(filename, "/dev/", 5)) { | 196 | else if ((oflags & O_ACCMODE) != O_RDONLY && errno == EACCES) { |
174 | if (!strcmp(filename+5, "null")) { | ||
175 | devnull = 1; | ||
176 | } | ||
177 | else if (special) { | ||
178 | if (!strcmp(filename+5, "zero")) | ||
179 | devzero = 1; | ||
180 | else if (!strcmp(filename+5, "urandom")) | ||
181 | devrand = 1; | ||
182 | } | ||
183 | |||
184 | if (devnull || devzero || devrand ) { | ||
185 | filename = "nul"; | ||
186 | oflags = O_RDWR; | ||
187 | } | ||
188 | } | ||
189 | fd = open(filename, oflags, mode); | ||
190 | if (fd < 0 && (oflags & O_ACCMODE) != O_RDONLY && errno == EACCES) { | ||
191 | DWORD attrs = GetFileAttributes(filename); | 197 | DWORD attrs = GetFileAttributes(filename); |
192 | if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY)) | 198 | if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY)) |
193 | errno = EISDIR; | 199 | errno = EISDIR; |
194 | } | 200 | } |
195 | if (fd >= 0 ) { | ||
196 | if (devzero) | ||
197 | zero_fd = fd; | ||
198 | else if (devrand) | ||
199 | rand_fd = fd; | ||
200 | } | ||
201 | return fd; | 201 | return fd; |
202 | } | 202 | } |
203 | 203 | ||