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 | |
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
-rw-r--r-- | coreutils/dd.c | 7 | ||||
-rw-r--r-- | include/mingw.h | 6 | ||||
-rw-r--r-- | win32/mingw.c | 70 |
3 files changed, 40 insertions, 43 deletions
diff --git a/coreutils/dd.c b/coreutils/dd.c index 10066575e..d29357dae 100644 --- a/coreutils/dd.c +++ b/coreutils/dd.c | |||
@@ -433,12 +433,7 @@ int dd_main(int argc UNUSED_PARAM, char **argv) | |||
433 | xmove_fd(xopen(infile, O_RDONLY), ifd); | 433 | xmove_fd(xopen(infile, O_RDONLY), ifd); |
434 | #else | 434 | #else |
435 | xmove_fd(mingw_xopen(infile, O_RDONLY), ifd); | 435 | xmove_fd(mingw_xopen(infile, O_RDONLY), ifd); |
436 | if (!strcmp(infile, "/dev/zero")) { | 436 | update_dev_fd(get_dev_type(infile), ifd); |
437 | mingw_read_zero(ifd); | ||
438 | } | ||
439 | else if (!strcmp(infile, "/dev/urandom")) { | ||
440 | mingw_read_random(ifd); | ||
441 | } | ||
442 | #endif | 437 | #endif |
443 | } else { | 438 | } else { |
444 | infile = bb_msg_standard_input; | 439 | infile = bb_msg_standard_input; |
diff --git a/include/mingw.h b/include/mingw.h index a54a6ac85..bc0772574 100644 --- a/include/mingw.h +++ b/include/mingw.h | |||
@@ -367,10 +367,12 @@ int fsync(int fd); | |||
367 | int kill(pid_t pid, int sig); | 367 | int kill(pid_t pid, int sig); |
368 | int link(const char *oldpath, const char *newpath); | 368 | int link(const char *oldpath, const char *newpath); |
369 | NOIMPL(mknod,const char *name UNUSED_PARAM, mode_t mode UNUSED_PARAM, dev_t device UNUSED_PARAM); | 369 | NOIMPL(mknod,const char *name UNUSED_PARAM, mode_t mode UNUSED_PARAM, dev_t device UNUSED_PARAM); |
370 | /* order of devices must match that in get_dev_type */ | ||
371 | enum {DEV_NULL, DEV_ZERO, DEV_URANDOM, NOT_DEVICE}; | ||
372 | int get_dev_type(const char *filename); | ||
373 | void update_dev_fd(int dev, int fd); | ||
370 | int mingw_open (const char *filename, int oflags, ...); | 374 | int mingw_open (const char *filename, int oflags, ...); |
371 | int mingw_xopen(const char *filename, int oflags); | 375 | int mingw_xopen(const char *filename, int oflags); |
372 | void mingw_read_zero(int fd); | ||
373 | void mingw_read_random(int fd); | ||
374 | ssize_t mingw_read(int fd, void *buf, size_t count); | 376 | ssize_t mingw_read(int fd, void *buf, size_t count); |
375 | int mingw_close(int fd); | 377 | int mingw_close(int fd); |
376 | int pipe(int filedes[2]); | 378 | int pipe(int filedes[2]); |
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 | ||