From aee071cdf853f6922a77245d60eca15bea55de02 Mon Sep 17 00:00:00 2001 From: Cody Boone Ferguson <53008573+xexyl@users.noreply.github.com> Date: Sat, 11 Nov 2023 04:17:31 -0800 Subject: [PATCH] Add 1996/gandalf/try.sh and format README.md The script is rather elaborate in that it runs the programs in the different ways the judges previously suggested as well as some additional runs and shows the difference (or if no difference says they are identical) of the different commands, using temporary files that are deleted if the script is sent (some) signals, the script is quit (when prompted to press a key pressing 'q' will quit the script) or if the script runs to completion. The README.md file was format/typo checked. BTW: it is absolutely perilous to try the patience of Gandalf (https://www.glyphweb.com/arda/g/gandalf.html). Go ahead, try it! :-) The Makefile has had -Wno-implicit-int added to CSILENCE. --- 1996/gandalf/Makefile | 2 +- 1996/gandalf/README.md | 20 +++----- 1996/gandalf/try.sh | 104 +++++++++++++++++++++++++++++++++++++++++ thanks-for-fixes.md | 6 +++ 4 files changed, 118 insertions(+), 14 deletions(-) create mode 100755 1996/gandalf/try.sh diff --git a/1996/gandalf/Makefile b/1996/gandalf/Makefile index 9a937843b..6ad2e080b 100644 --- a/1996/gandalf/Makefile +++ b/1996/gandalf/Makefile @@ -39,7 +39,7 @@ include ../../var.mk # Common C compiler warnings to silence # CSILENCE= -Wno-error -Wno-implicit-function-declaration -Wno-keyword-macro \ - -Wno-shift-op-parentheses -Wno-unused-parameter \ + -Wno-shift-op-parentheses -Wno-unused-parameter -Wno-implicit-int \ -Wno-deprecated-non-prototype -Wno-strict-prototypes -Wno-unsequenced # Common C compiler warning flags diff --git a/1996/gandalf/README.md b/1996/gandalf/README.md index a813fc266..64c9c472c 100644 --- a/1996/gandalf/README.md +++ b/1996/gandalf/README.md @@ -26,21 +26,12 @@ For more detailed information see [1996 gandalf in bugs.md](/bugs.md#1996-gandal ### Try: ```sh -PATH=.:$PATH -./hatcat -./cathat +./try.sh ``` -And next try: - -```sh -./cathat -`pwd`/cathat -``` - -Care to guess why these execute differently? Only the -compiler knows ... ok maybe other things like the author, -the judges and maybe you too? +As the script asks, care to guess why these execute differently? Only the +compiler knows ... ok maybe other things like the author, the judges and perhaps +you too? ## Judges' remarks: @@ -51,6 +42,9 @@ For misleading C pre-processor output, try: cc -E gandalf.c ``` +BTW: it is perilous to try the patience of +[Gandalf](https://www.glyphweb.com/arda/g/gandalf.html). Go ahead, try it! :-) + ## Author's remarks: diff --git a/1996/gandalf/try.sh b/1996/gandalf/try.sh new file mode 100755 index 000000000..03128d772 --- /dev/null +++ b/1996/gandalf/try.sh @@ -0,0 +1,104 @@ +#!/usr/bin/env bash + +make all 1>/dev/null || exit 1 + +# remove temporary files to show later +rm -f hatcat.txt cathat.txt hatcat2.txt cathat2.txt hatcat3.txt cathat3.txt + +trap 'rm -f hatcat.txt cathat.txt hatcat2.txt cathat2.txt hatcat3.txt cathat3.txt; exit' 0 1 2 3 15 + +delete_tmp_files() +{ + rm -f hatcat.txt cathat.txt hatcat2.txt cathat2.txt hatcat3.txt cathat3.txt + return 0 +} +press_any_key() +{ + echo 1>&2 + read -r -n 1 -p "Press any key to continue (q = quit): " 1>&2 + test "$REPLY" = "q" && delete_tmp_files && echo 1>&2 && exit +} + +show_diff() +{ + if [[ "$#" -ne 2 ]]; then + echo "$0: show_diff expects two args, got: $#" 1>&2 + exit 1 + fi + echo "$ diff $1 $2" 1>&2 + diff "$1" "$2" && echo "$1 and $2 are identical" +} + +echo "Notice how the programs are the same:" 1>&2 +diff -s hatcat cathat +press_any_key + +echo "Now notice how running both cathat and hatcat in different ways" 1>&2 +echo "shows different output." 1>&2 +press_any_key + +echo "Running hatcat without a path:" 1>&2 +echo "$ PATH=.:\$PATH" 1>&2 +PATH=.:$PATH +echo "$ hatcat | tee hatcat.txt" 1>&2 +hatcat | tee hatcat.txt +press_any_key + +echo "Running cathat without a path:" 1>&2 +echo "$ cathat | tee cathat.txt" 1>&2 +cathat | tee cathat.txt +press_any_key + +echo "Showing diff between the above two commands, 'hatcat' and './cathat':" 1>&2 +show_diff hatcat.txt cathat.txt +press_any_key + +echo "Now watch ./hatcat:" 1>&2 +./hatcat | tee hatcat2.txt +press_any_key + +echo "Now watch ./cathat:" 1>&2 +./cathat | tee cathat2.txt +press_any_key + +echo "Showing diff between running 'hatcat' and './hatcat':" 1>&2 +show_diff hatcat.txt hatcat2.txt +press_any_key + +echo "Now watch \$(pwd)/cathat" 1>&2 +"$(pwd)"/cathat | tee cathat3.txt +press_any_key + +echo "Finally, watch \$(pwd)/hatcat" 1>&2 +"$(pwd)"/hatcat | tee hatcat3.txt +press_any_key + +echo "Showing diff between running './hatcat' and './cathat':" 1>&2 +show_diff hatcat2.txt cathat2.txt +press_any_key + +echo "Showing diff between running '\$(pwd)/hatcat' and '\$(pwd)/cathat':" 1>&2 +show_diff hatcat3.txt cathat3.txt +press_any_key + +echo "Showing diff between running '\$(pwd)/hatcat' and 'hatcat':" 1>&2 +show_diff hatcat3.txt hatcat.txt +press_any_key + +echo "Showing diff between running '\$(pwd)/hatcat' and './hatcat':" 1>&2 +show_diff hatcat3.txt hatcat2.txt +press_any_key + +echo "Showing diff between running '\$(pwd)/cathat' and 'cathat':" 1>&2 +show_diff cathat3.txt cathat.txt +press_any_key + +echo "Showing diff between running '\$(pwd)/cathat' and './cathat':" 1>&2 +show_diff cathat3.txt cathat2.txt +press_any_key + +# remove temporary files +rm -f hatcat.txt cathat.txt hatcat2.txt cathat2.txt hatcat3.txt cathat3.txt + +echo 1>&2 +echo "Now answer: why did some of the same programs execute differently?" 1>&2 diff --git a/thanks-for-fixes.md b/thanks-for-fixes.md index cac9c27b5..fe9604438 100644 --- a/thanks-for-fixes.md +++ b/thanks-for-fixes.md @@ -1951,6 +1951,12 @@ Cody fixed this to compile and work with modern systems. As he loved the references in the code that could not compile he just commented out as little as possible to get this to compile. +Cody also added the rather useful [try.sh](1996/gandalf/try.sh) script to +really demonstrate the different ways of running the program ends up showing +either different output or the same output that we briefly pointed out. + +BTW: it is perilous to try the patience of +[Gandalf](https://www.glyphweb.com/arda/g/gandalf.html). Go ahead, try it! :-) ## [1996/jonth](1996/jonth/jonth.c) ([README.md](1996/jonth/README.md]))