Address the fixed / unfixed 1996/westley situation

When I first fixed this it was because the scripts segfaulted and at
least one showed an environmental variable both of which caused some
usability issues. But I started to wonder if maybe the fixed code should
be the alt version. To do this I made a change to the scripts so one can
tell the script which version to use. I was having a problem in figuring
out how to explain it (owing to the fact I've been awake once again
since stupid o'clock and without good sleep) but before I answered that
I decided to test out the new feature to the script and the output made
me decide to at least for now (discussion can be had) keep the fix the
westley.c and the unfixed original code the alt code. This particularly
shows up in the third script where the output is hard to tell what is
part of the program and what is not. The unfixed version looks like:

    $ WESTLEY=./westley.alt ./clock3
    TERM=xterm-256color
    ./clock3: line 12: 15498 Segmentation fault: 11  ${WESTLEY} "123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_\`abcdefghijkl" "/\$%&'()*+,-./\$%&'()*+,-." " ||" "
    ..3..6..9..12 Hours

     ----.----10---.----20---.----30---.----40---.----50---.---- Minutes
    "

which has a " by itself on the last line that should not be there. Then
there's the other problematic output. It should just look like:

    ..3..6..9..12 Hours

     ----.----10---.----20---.----30---.----40---.----50---.---- Minutes
This commit is contained in:
Cody Boone Ferguson committed 2023-04-18 10:20:23 -07:00
1 parent fa87b511bf
commit c9ce688238
4 files changed
+57 -41

No files matched your search

+51 -38
View File
@@ -1,53 +1,55 @@
# Best One Liner
Brian Westley (Merlyn LeRoy on Usenet)
1906 James Ave.
St. Paul, MN 55105
USA
http://www.westley.org
Brian Westley (Merlyn LeRoy on Usenet)
1906 James Ave.
St. Paul, MN 55105
USA
<http://www.westley.org>
## To build:
make all
```sh
make all
```
[Cody Boone Ferguson](/winners.html#Cody_Boone_Ferguson) fixed a segfault in
this entry as well as it displaying environmental variables. Although the
scripts showed correct output these also segfaulted but with the fix they no
longer do. Now if argc < 5 (argv[4] is referenced) it will not do anything and
it will not segfault either (this was caused by the body of the for() loop which
is now empty (it doesn't appear to be needed at all). Note that this does make
some of the author's explanations / portability notes no longer valid but we
have kept them there for the interested. Thank you Cody for your assistance!
scripts showed correct output, it somewhat lessened the usability because they
segfaulted. With the fix they no longer do. If argc < 5 (argv[4] is referenced)
it will not do anything and it will not segfault either - this was caused by the
body of the for() loop which is now empty (it doesn't appear to be needed at
all). Note that you should check the [westley.alt.c](westley.alt.c) file when
reading the author's comments. To see how to use the original see below. Thank
you Cody for your assistance!
## To run:
sh ./clock1
```sh
sh ./clock1
```
## Try:
sh ./clock2
Also try:
sh ./clock2
```sh
sh ./clock2
```
And for a good time, try:
sh ./clock3
```sh
sh ./clock3
```
### Alternate code:
An alternate version of this entry, `westley.alt.c`, is provided.
The original is in [westley.alt.c](westley.alt.c) does not contain
the above mentioned fixes.
An alternate version of this entry, [westley.alt.c](westley.alt.c), is provided.
This version is the original code [westley.alt.c](westley.alt.c) and does not
contain the above mentioned fixes.
To compile this alternate version:
@@ -55,6 +57,15 @@ To compile this alternate version:
make alt
```
To use it with the scripts try:
```sh
WESTLEY=./westley.alt ./clock1
WESTLEY=./westley.alt ./clock2
WESTLEY=./westley.alt ./clock3
```
## Judges' remarks:
Time and time again, Brian Westley seems to come up with a winner!
@@ -74,37 +85,39 @@ clock that runs backwards, and a linear-style clock.
The program makes the following (possibly non-portable) assumptions:
1. Command line strings can be written to
1. Command line strings can be written to.
2. `argv[0]` can be written to as if it were a `long *`
2. `argv[0]` can be written to as if it were a `long *`.
3. The first three elements of the struct * returned by localtime() are seconds,
minutes, hours, and all are ints
minutes, hours, and all are ints.
4. My sample scripts assume ASCII, though this is not inherent in the code, just
my sample scripts
my sample scripts.
These assumptions are pretty reasonable. 2) can be eliminated
by makes the code larger.
These assumptions are pretty reasonable. 2) can be eliminated but makes the code
larger.
The code is simple; the arguments passed are:
prog minute_map hour_map hand_symbol clock_image
```sh
./westley minute_map hour_map hand_symbol clock_image
```
The program merely takes the minute (0-59) and the hour (0-23)
values, looks up the offsets in `minute_map` and `hour_map`
and writes the `hand_symbol` for each into the `clock_image`
string. This string is then printed.
The for() loop starts by calling time() with argv[0], so
The for() loop starts by calling `time()` with `argv[0]`, so
the time value (a long) is written into the program name.
The for() control value is argc, which starts at 5, but
the /=2 statement is executed before the test, so the for()
loop is executed twice, with h=2, then h=1 (for hour & minute).
The for() control value is `argc`, which starts at `5`, but
the `/=2` statement is executed before the test, so the for()
loop is executed twice, with `h=2`, then `h=1` (for hour & minute).
The statement of the for() loop takes the hour or minute
field of the struct returned by localtime() by casting it
to an int* and dereferencing element [h]. It is slightly
field of the struct returned by `localtime()` by casting it
to an `int *` and dereferencing element `[h]`. It is slightly
smaller to use the trick `h[(int*)localtime(*m)]` instead of
`((int*)localtime(*m))[h]`.
+2 -1
View File
@@ -1,5 +1,6 @@
#!/usr/bin/env sh
./westley \
${WESTLEY:=./westley}
${WESTLEY} \
"%%%&&&'''666EEESSSaaannn{{{zzzyyyxxxwwwhhhYYYKKK===000###\$\$\$" \
"34CQ_lkj[M?234CQ_lkj[M?2" \
" .*" \
+2 -1
View File
@@ -1,5 +1,6 @@
#!/usr/bin/env sh
./westley \
${WESTLEY:=./westley}
${WESTLEY} \
"%%%\$\$\$###000===KKKYYYhhhwwwxxxyyyzzz{{{nnnaaaSSSEEE666'''&&&" \
"32?M[jkl_QC432?M[jkl_QC4" \
" xX" \
+2 -1
View File
@@ -1,5 +1,6 @@
#!/usr/bin/env sh
./westley \
${WESTLEY:=./westley}
${WESTLEY} \
"123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_\`abcdefghijkl" \
"/\$%&'()*+,-./\$%&'()*+,-." \
" ||" \