summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2023-07-10 19:18:48 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2023-07-10 19:21:47 +0900
commitc8d0470bb0888bcb6719ba536e5f3f6a8b6551bb (patch)
tree98536f216cd90b7987629c95b8d2f03f2ec9a3e6
parent092c9b266ab3175c40a78e8af99bb82a0f3d2f4b (diff)
Use `File::NULL` instead of hard coded null device names
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/8050
-rwxr-xr-xext/extmk.rb6
-rw-r--r--file.c4
-rw-r--r--io.c6
-rw-r--r--lib/mkmf.rb2
-rw-r--r--process.c14
-rw-r--r--test/ruby/test_file_exhaustive.rb2
-rw-r--r--tool/lib/vcs.rb3
7 files changed, 17 insertions, 20 deletions
diff --git a/ext/extmk.rb b/ext/extmk.rb
index d6a4b80bf7..428ffc91a6 100755
--- a/ext/extmk.rb
+++ b/ext/extmk.rb
@@ -43,11 +43,7 @@ $" << "mkmf.rb"
load File.expand_path("lib/mkmf.rb", srcdir)
require 'optparse/shellwords'
-if defined?(File::NULL)
- @null = File::NULL
-elsif !File.chardev?(@null = "/dev/null")
- @null = "nul"
-end
+@null = File::NULL
def verbose?
$mflags.defined?("V") == "1"
diff --git a/file.c b/file.c
index d9446e6dcf..0966d5bb23 100644
--- a/file.c
+++ b/file.c
@@ -4971,7 +4971,7 @@ rb_file_s_extname(VALUE klass, VALUE fname)
*
* Returns the string representation of the path
*
- * File.path("/dev/null") #=> "/dev/null"
+ * File.path(File::NULL) #=> "/dev/null"
* File.path(Pathname.new("/tmp")) #=> "/tmp"
*
*/
@@ -6086,7 +6086,7 @@ rb_stat_z(VALUE obj)
* the file otherwise.
*
* File.stat("testfile").size? #=> 66
- * File.stat("/dev/null").size? #=> nil
+ * File.stat(File::NULL).size? #=> nil
*
*/
diff --git a/io.c b/io.c
index c3661a90a7..0eb8da3f4f 100644
--- a/io.c
+++ b/io.c
@@ -5256,7 +5256,7 @@ rb_io_close_on_exec_p(VALUE io)
*
* Sets a close-on-exec flag.
*
- * f = open("/dev/null")
+ * f = File.open(File::NULL)
* f.close_on_exec = true
* system("cat", "/proc/self/fd/#{f.fileno}") # cat: /proc/self/fd/3: No such file or directory
* f.closed? #=> false
@@ -9634,11 +9634,11 @@ rb_io_autoclose_p(VALUE io)
*
* Sets auto-close flag.
*
- * f = open("/dev/null")
+ * f = File.open(File::NULL)
* IO.for_fd(f.fileno).close
* f.gets # raises Errno::EBADF
*
- * f = open("/dev/null")
+ * f = File.open(File::NULL)
* g = IO.for_fd(f.fileno)
* g.autoclose = false
* g.close
diff --git a/lib/mkmf.rb b/lib/mkmf.rb
index 20389ed705..cd67645a5d 100644
--- a/lib/mkmf.rb
+++ b/lib/mkmf.rb
@@ -2699,7 +2699,7 @@ MESSAGE
when $mswin
$nmake = ?m if /nmake/i =~ make
end
- $ignore_error = $nmake ? '' : ' 2> /dev/null || true'
+ $ignore_error = $nmake ? '' : " 2> #{File::NULL} || true"
RbConfig::CONFIG["srcdir"] = CONFIG["srcdir"] =
$srcdir = arg_config("--srcdir", File.dirname($0))
diff --git a/process.c b/process.c
index b4435446f0..fe9c0a31d7 100644
--- a/process.c
+++ b/process.c
@@ -4804,11 +4804,11 @@ rb_f_system(int argc, VALUE *argv, VALUE _)
*
* A filename can be specified as a hash value.
*
- * pid = spawn(command, :in=>"/dev/null") # read mode
- * pid = spawn(command, :out=>"/dev/null") # write mode
+ * pid = spawn(command, :in=>File::NULL) # read mode
+ * pid = spawn(command, :out=>File::NULL) # write mode
* pid = spawn(command, :err=>"log") # write mode
- * pid = spawn(command, [:out, :err]=>"/dev/null") # write mode
- * pid = spawn(command, 3=>"/dev/null") # read mode
+ * pid = spawn(command, [:out, :err]=>File::NULL) # write mode
+ * pid = spawn(command, 3=>File::NULL) # read mode
*
* For stdout and stderr (and combination of them),
* it is opened in write mode.
@@ -6834,7 +6834,7 @@ static int rb_daemon(int nochdir, int noclose);
* nochdir is true (i.e. non false), it changes the current
* working directory to the root ("/"). Unless the argument
* noclose is true, daemon() will redirect standard input,
- * standard output and standard error to /dev/null.
+ * standard output and standard error to null device.
* Return zero on success, or raise one of Errno::*.
*/
@@ -6854,6 +6854,8 @@ proc_daemon(int argc, VALUE *argv, VALUE _)
return INT2FIX(n);
}
+extern const char ruby_null_device[];
+
static int
rb_daemon(int nochdir, int noclose)
{
@@ -6877,7 +6879,7 @@ rb_daemon(int nochdir, int noclose)
if (!nochdir)
err = chdir("/");
- if (!noclose && (n = rb_cloexec_open("/dev/null", O_RDWR, 0)) != -1) {
+ if (!noclose && (n = rb_cloexec_open(ruby_null_device, O_RDWR, 0)) != -1) {
rb_update_max_fd(n);
(void)dup2(n, 0);
(void)dup2(n, 1);
diff --git a/test/ruby/test_file_exhaustive.rb b/test/ruby/test_file_exhaustive.rb
index be6e1f2326..fbb18f07f9 100644
--- a/test/ruby/test_file_exhaustive.rb
+++ b/test/ruby/test_file_exhaustive.rb
@@ -1518,7 +1518,7 @@ class TestFileExhaustive < Test::Unit::TestCase
stat = File.stat(f)
unless stat.chardev?
- # /dev/null may be accessed by other processes
+ # null device may be accessed by other processes
assert_equal(stat.atime, File.atime(f), f)
assert_equal(stat.ctime, File.ctime(f), f)
assert_equal(stat.mtime, File.mtime(f), f)
diff --git a/tool/lib/vcs.rb b/tool/lib/vcs.rb
index 650f275250..8566d72349 100644
--- a/tool/lib/vcs.rb
+++ b/tool/lib/vcs.rb
@@ -154,8 +154,7 @@ class VCS
alias dryrun? dryrun
alias debug? debug
- NullDevice = defined?(IO::NULL) ? IO::NULL :
- %w[/dev/null NUL NIL: NL:].find {|dev| File.exist?(dev)}
+ NullDevice = IO::NULL
# returns
# * the last revision of the current branch