Fix 1998/schweikh1 and add alt code for macOS

This was a fun one to figure out and fix!

The original code did not work; there were several things wrong with it
as below. The macOS version was even more complicated but also fun and
also described below.

The problem with the original was a bad call to freopen() where the arg
parameter was actually 5+__FILE__ when it should be "r" (though of
course it used to work fine). A call to fopen() had a similar mode:
44+__FILE__ though this actually did not seem to affect the output. But
it didn't work with macOS at all. The following is some of what I put in
the README.md so that one can see here too what was wrong. Spoilers for
the original entry (at least some spoilers) are here!

For this alternative version you will need the command line tools which
you can install like:

```sh
sudo xcode-select --install
```

This how it works: if you look at line 55 you see a funny command.
This goes for both versions. Now the key are two magic numbers, one on a
different line shortly below the string. In the original the numbers are,
respectively, 44 and 46.  But how does this work? Well the string is:

	"0gcc -ansi -E -dM -undef %s /usr/include/%s>r\0 ("

The first number means: the length starting from 0 up through the `>`. The
second number is the same starting point but up through the `\0` which is why
it's +2. Somehow it was basic intuition that made me figure out these
two numbers and a quick test confirmed it. But what happens if only the
first number is updated? Most of the output will be just `#define` by
itself; in the cases where there was text after that it was macros that
certainly were not defined. There might have been other errors as well.

This allows for opening the right files. The problem with the macOS is
/usr/include does not exist: instead it's
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include which is
why the different command line and the different numbers.
This commit is contained in:
Cody Boone Ferguson committed 2023-03-26 06:29:52 -07:00
1 parent fa02fba4e3
commit cb02b4da5f
5 files changed
+282 -158

No files matched your search

+2 -2
View File
@@ -142,8 +142,8 @@ OBJ= ${PROG}.o
DATA= info
TARGET= ${PROG}
#
ALT_OBJ=
ALT_TARGET=
ALT_OBJ= ${PROG}.alt.o
ALT_TARGET= ${PROG}.alt
#################