如何编译和运行这个1989年编写的C程序?

时间:2021-03-04 17:31:02

I found this amazing piece of work by Arthur Whitney - http://www.jsoftware.com/jwiki/Essays/Incunabulum

我找到了亚瑟·惠特尼这件神奇的作品 - http://www.jsoftware.com/jwiki/Essays/Incunabulum

It compiled with a few warnings

它汇编了一些警告

$ gcc-4.7 incuna.c -o incuna.o
incuna.c: In function 'ma':
incuna.c:8:15: warning: incompatible implicit declaration of built-in function 'malloc' [enabled by default]
incuna.c: In function 'pi':
incuna.c:26:7: warning: incompatible implicit declaration of built-in function 'printf' [enabled by default]
incuna.c: In function 'nl':
incuna.c:26:24: warning: incompatible implicit declaration of built-in function 'printf' [enabled by default]
incuna.c: In function 'pr':
incuna.c:28:10: warning: incompatible implicit declaration of built-in function 'printf' [enabled by default]
incuna.c: In function 'ex':
incuna.c:35:36: warning: assignment makes integer from pointer without a cast [enabled by default]
incuna.c:35:25: warning: return makes pointer from integer without a cast [enabled by default]
incuna.c: In function 'noun':
incuna.c:37:57: warning: return makes integer from pointer without a cast [enabled by default]
incuna.c: In function 'wd':
incuna.c:39:21: warning: incompatible implicit declaration of built-in function 'strlen' [enabled by default]

But it segfaulted on entering a basic input 1 + 1.

但是在输入基本输入1 + 1时它会分段。

./incuna.o
warning: this program uses gets(), which is unsafe.
1 + 1
[1]    11525 segmentation fault  ./incuna.o

I'm guessing this has something to do with the difference in C compiler since 1989.

我猜这与自1989年以来C编译器的差异有关。

How would I be able to run this? Can I get this working on recent Linux/Mac? or on a VirtualBox VM? or anything else?

我怎么能运行这个?我可以在最近的Linux / Mac上使用它吗?或在VirtualBox VM上?或其他什么?

My Google searches turned up nothing related.

我的谷歌搜索没有任何关联。

4 个解决方案

#1


9  

It converts pointers to int and long and vice-versa. This breaks with 64-bit architectures in which they have different sizes.

它将指针转换为int和long,反之亦然。这打破了64位架构,它们具有不同的大小。

Compile it for a 32-bit target. E.g., with “-arch i386” using clang/LLVM on Mac OS X.

将其编译为32位目标。例如,在Mac OS X上使用clang / LLVM的“-arch i386”。

#2


4  

I would wager a guess that it segfaulted because of this:

我猜赌它因为这个而被分割出来:

incuna.c:8:15: warning: incompatible implicit declaration of built-in function 'malloc' [enabled by default]

incuna.c:8:15:警告:内置函数'malloc'的不兼容隐式声明[默认启用]

If malloc is not declared, then it's not going to allocate you memory and you'll end up dereferencing a null and that could lead to a seg fault.

如果没有声明malloc,那么它不会分配你的内存,你最终会取消引用null,这可能会导致seg错误。

After including:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

The warnings for printf(), malloc(), and strlen() are gone. The code runs and works if you input:

printf(),malloc()和strlen()的警告消失了。如果输入,代码将运行并运行:

1+1

Note the spacing is important here:

注意间距在这里很重要:

1 + 1 

will segfault.

#3


4  

Run it through the preprocessor only:

仅通过预处理器运行:

gcc -E interp.c > interp-pp.c

gcc -E interp.c> interp-pp.c

Then prettify it in an editor, then use a debugger to watch what it does.

然后在编辑器中对它进行美化,然后使用调试器来观察它的作用。

#4


1  

On my system (AMD64 Win 8), it appears that pointer values often have the top bit set, so treating a pointer as an integer (which this program does) will misbehave and crash.

在我的系统(AMD64 Win 8)上,似乎指针值通常设置了最高位,因此将指针视为整数(此程序所做的)将会出现异常和崩溃。

Changing the qv(a) function ("query verb") allows the program to run:

更改qv(a)函数(“查询动词”)允许程序运行:

qv(a){R a<'a';}

should be

qv(a){R a<'a'&&a>0;}

or

qv(a)unsigned a;{R a<'a';}

Here's a link to a minimally-modified version that should compile without warnings (with gcc, default options) and execute (with correct input).

这是一个最小修改版本的链接,应该在没有警告的情况下编译(使用gcc,默认选项)和执行(使用正确的输入)。

#1


9  

It converts pointers to int and long and vice-versa. This breaks with 64-bit architectures in which they have different sizes.

它将指针转换为int和long,反之亦然。这打破了64位架构,它们具有不同的大小。

Compile it for a 32-bit target. E.g., with “-arch i386” using clang/LLVM on Mac OS X.

将其编译为32位目标。例如,在Mac OS X上使用clang / LLVM的“-arch i386”。

#2


4  

I would wager a guess that it segfaulted because of this:

我猜赌它因为这个而被分割出来:

incuna.c:8:15: warning: incompatible implicit declaration of built-in function 'malloc' [enabled by default]

incuna.c:8:15:警告:内置函数'malloc'的不兼容隐式声明[默认启用]

If malloc is not declared, then it's not going to allocate you memory and you'll end up dereferencing a null and that could lead to a seg fault.

如果没有声明malloc,那么它不会分配你的内存,你最终会取消引用null,这可能会导致seg错误。

After including:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

The warnings for printf(), malloc(), and strlen() are gone. The code runs and works if you input:

printf(),malloc()和strlen()的警告消失了。如果输入,代码将运行并运行:

1+1

Note the spacing is important here:

注意间距在这里很重要:

1 + 1 

will segfault.

#3


4  

Run it through the preprocessor only:

仅通过预处理器运行:

gcc -E interp.c > interp-pp.c

gcc -E interp.c> interp-pp.c

Then prettify it in an editor, then use a debugger to watch what it does.

然后在编辑器中对它进行美化,然后使用调试器来观察它的作用。

#4


1  

On my system (AMD64 Win 8), it appears that pointer values often have the top bit set, so treating a pointer as an integer (which this program does) will misbehave and crash.

在我的系统(AMD64 Win 8)上,似乎指针值通常设置了最高位,因此将指针视为整数(此程序所做的)将会出现异常和崩溃。

Changing the qv(a) function ("query verb") allows the program to run:

更改qv(a)函数(“查询动词”)允许程序运行:

qv(a){R a<'a';}

should be

qv(a){R a<'a'&&a>0;}

or

qv(a)unsigned a;{R a<'a';}

Here's a link to a minimally-modified version that should compile without warnings (with gcc, default options) and execute (with correct input).

这是一个最小修改版本的链接,应该在没有警告的情况下编译(使用gcc,默认选项)和执行(使用正确的输入)。