TONT 42073 关于预留文件名NUL和CON的故事

过去与现在,当下与将来,都是一样的。

原文链接:https://blogs.msdn.microsoft.com/oldnewthing/20031022-00/?p=42073

Set the wayback machine to DOS 1.0.

让我们把时间拨回DOS 1.0的时代。

DOS 1.0 didn’t support subdirectories, lowercase, or filenames longer than 8.3.

DOS 1.0不支持子目录,小写文件名,或者比8.3格式(译注:即文件名最长为8个字符、扩展名最长为3个字符的文件名格式)更长的文件名。

When you ran the assembler (or compiler if you were really fancy) the conversation went something like this:

当你运行某个汇编器(或者很有想象力的话,编译器),人机对话大概是下面这样的:

A>asm foo the “.asm” extension on “foo” is implied(文件foo的扩展名.asm为隐式传递)
Assembler version blah blah blah 汇编器 ver x.xx
Source file: FOO.ASM 输入文件名:FOO.ASM
Listing file [FOO.LST]: just hit Enter to accept the default 列表文件名 [FOO.LST]:按回车键使用默认值FOO.LST
Object file [FOO.OBJ]: just hit Enter to accept the default 对象文件名 [FOO.OBJ]:按回车键使用默认值FOO.OBJ
Assembler cranks away 汇编器跑去干活了

You only had to type the base name of the file; the “.LST” and “.OBJ” extensions were appended automatically. In fact, I don’t think you could disable the extensions; they were always added.

你只需要输入文件的主名就可以了,扩展名.LST和.OBJ是自动加上去的。实际上,想不加上也不行,不管怎么做都是会加上去的。

But what if you didn’t want a listing file? The assembler demanded a filename, and if you didn’t type any filename at all, it created one with the same basename as your source file.

但是,如果不想要列表文件该怎么办呢?汇编器无论如何都需要一个文件名,而如果不输入任何文件名的话,汇编器会创建一个与你的源代码文件主名一样的(带有.LST后缀的)列表文件。

That’s where the magic filenames come in. Suppose you wanted the listing file to go straight to the printer. You didn’t want to create a file on your floppy drive because there might not be enough space to hold it, or just because you didn’t want to waste the time creating a file just to delete it anyway. So you typed “PRN” as the filename.

到这一步『神奇文件名』就体现出它的作用了。假设你想让列表文件直接被送到打印机(打印出来),又不想在软盘上创建这么一个文件,因为可能没有足够的空间,也可能只是不想浪费时间删掉这个眼下并没有什么用的文件,那么你可以用PRN作为文件名。

Now, the assembler doesn’t know about these magic filenames. So the assembler will try to create the file “PRN.LST” and then start writing to it. Little does the assembler realize that the output is actually going to the printer.

此时,汇编器并不知道这个『神奇文件名』的底细,只是照常尝试创建文件PRN.LST然后开始向里面写入数据,全然不知输出直接都被导向了打印机。

If you wanted to discard the output entirely, you would type “NUL”, of course. And if you wanted it to go to the screen, you would type “CON”.

如果干脆不想要任何输出,当然也可以可以使用NUL作为文件名。或者想直接将输出打印在屏幕上,可以输入文件名CON。

Now, if you followed closely, you can see that the above story explains two things already:

到此为止,如果看得足够仔细的话,你会发现以上例子已经额外解释了两件事:

  • Why are the magic filenames magical even if I add an extension?
    Answer: If an extension removed the magic, then when the assembler added “.LST” to the filename, it would no longer be recognized as magical, thereby defeating the purpose of the magic.
  • 为什么这些『神奇文件名』加了扩展名依旧有效?
    答:如果加了扩展名就失效了,那么当汇编器为文件名添加.LST后缀时,系统就无法将其辨识为『神奇』的文件名,也就失去了称其为『神奇』的意义。
  • Why do these magic files exist in every directory?
    Answer: Because DOS 1.0 didn’t have subdirectories. There was only one directory, which today we would call the root directory, but back then, since there was no such thing as a subdirectory, there was no need to talk about directories in the first place, much less give the only one you have a name. It was just called “the files on your disk”. If magic files didn’t work in subdirectories, then when you tried to, for example, chdir into a subdirectory and then run the assembler, you wouldn’t be able to type “NUL” as the filename and get the magic.
  • 为什么这些『神奇文件』在每个目录中都存在?
    答:因为DOS 1.0不支持子目录。整个磁盘只有一个目录,也就是我们如今所知的『根目录』,但那个时候,因为根本没有『子目录』这种东西,『目录』一说也就无从谈起,而这些『神奇文件』也只存在一份而已。那个时候,只有『磁盘上的文件』一说。如果『神奇文件』在子目录里就不好使了,那么,比如说你改变目录到某个子目录里,然后运行汇编器的话,就没有办法使用NUL作为文件名来体会其神奇之处了。

But why do we carry these magic filenames forward even today?

说得好,但这些『神奇文件』为什么又会一路活到今天呢?

Because everybody still relies on them. Just look at all the batch files that do things like redirect to >NUL or test if a directory exists by asking “if exist directoryname\nul”, or all the documentation that says to create a file with “copy CON …”.

因为所有人仍然依赖它们(来做一些特定的事情)。随便找个批处理文件,里面大概都会有将输出重定向的 >NUL,或者用于判断某个目录是否存在的IF EXIST 目录名\NUL,还有各种各样的文档中会教你用『COPY CON …』来创建文件之类的事情。

Comments

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

 剩余字数 ( Characters available )

Your comment will be available after auditing.
您的评论将在通过审核后显示。

Please DO NOT add any links in your comment, otherwise it would be identified as SPAM automatically and never be audited.
请不要在评论中插入任何链接,否则将被自动归类为垃圾评论,且永远不会被提交给博主进行复审。

*