为什么 OS X 上的 `cat` 在附加时无法识别输入文件是否与输出文件匹配?
Why does `cat` on OS X not recognize if the input file matches the output file when appending?
我正在为一个项目编写一个 shell 并且 运行 在 OS X 上编写命令 cat out >> out
,当它挂起时我感到很惊讶。在 Linux 中,当您 运行 cat file >> file
时,它将识别输入文件与输出文件匹配并引发错误。
在 OS X 上,行为是文件将无限次附加到自身,直到您 运行 超出 space。 OS X 上的 cat
没有与 Linux 相同的错误检查是有原因的吗?
OS X 传统上使用 BSD 版本的 cat,而在 Linux 上 user-land 程序套件往往是 GNU 版本。
在 GNU 版本的 cat 中,input/output 文件是 checked and an error is shown if they are the same
if (out_isreg
&& stat_buf.st_dev == out_dev && stat_buf.st_ino == out_ino
&& lseek (input_desc, 0, SEEK_CUR) < stat_buf.st_size)
{
error (0, 0, _("%s: input file is output file"), quotef (infile));
ok = false;
goto contin;
}
cat的版本included in OS X does not include a redirection check,好像是基于Net BSD版本的
我正在为一个项目编写一个 shell 并且 运行 在 OS X 上编写命令 cat out >> out
,当它挂起时我感到很惊讶。在 Linux 中,当您 运行 cat file >> file
时,它将识别输入文件与输出文件匹配并引发错误。
在 OS X 上,行为是文件将无限次附加到自身,直到您 运行 超出 space。 OS X 上的 cat
没有与 Linux 相同的错误检查是有原因的吗?
OS X 传统上使用 BSD 版本的 cat,而在 Linux 上 user-land 程序套件往往是 GNU 版本。
在 GNU 版本的 cat 中,input/output 文件是 checked and an error is shown if they are the same
if (out_isreg
&& stat_buf.st_dev == out_dev && stat_buf.st_ino == out_ino
&& lseek (input_desc, 0, SEEK_CUR) < stat_buf.st_size)
{
error (0, 0, _("%s: input file is output file"), quotef (infile));
ok = false;
goto contin;
}
cat的版本included in OS X does not include a redirection check,好像是基于Net BSD版本的