From 472461c2e2f92d3070e030b34342d824fa57d608 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sun, 1 Aug 2021 01:35:00 +1200 Subject: Apply shebang for doomsh with make recipe --- doomsh.bash | 135 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 doomsh.bash (limited to 'doomsh.bash') diff --git a/doomsh.bash b/doomsh.bash new file mode 100644 index 0000000..8603e70 --- /dev/null +++ b/doomsh.bash @@ -0,0 +1,135 @@ +#!bash +# +# doomsh: Play with low ulimits in Bash +# +# Author: Tom Ryder +# Copyright: 2016 +# License: MIT (see LICENSE.markdown) +# +self=doomsh + +# Don't try to play doomsh if already playing doomsh +if [[ $DOOMSH ]] ; then + printf '%s: Already playing doomsh!\n' \ + "$self" >&2 + exit 2 +fi + +# Show the coloured logo; thanks to Frans P. de Vries for the classic ASCII art +# +printf >&2 %s "$( { + tput AF 3 || tput setaf 3 + tput md || tput bold +} 2>/dev/null )" +while IFS= read -r line ; do + printf >&2 '%s\n' "$line" +done <&2 %s "$( { + tput me || tput sgr0 +} 2>/dev/null )" + +# Need to figure out a skill level +declare -i skill + +# If we have an argument, that must be the skill level +if (($#)) ; then + skill=$1 + shift + +# Otherwise, we'll give the user a menu to choose from +else + while IFS= read -r line ; do + printf >&2 '%s\n' "$line" + done <&2 + done <&2 + exit 2 +esac + +# Form a red prompt (hopefully) using terminal escapes +ps1=$(printf '%sdoomsh[%u]\$%s ' \ + "$( { + tput AF 1 || tput setaf 1 + } 2>/dev/null )" \ + "$skill" \ + "$( { + tput me || tput sgr0 + } 2>/dev/null )" +) + +# Set the limits based on the skill level +declare -i filesize files stack memory +filesize=$((1 << (8 - skill))) +files=$((11 - skill)) +stack=$((1 << (11 - skill))) +memory=$((1 << (19 - skill))) + +# Display the limits +printf >&2 '\n' +printf >&2 'File size writing limit: %u blocks\n' \ + "$filesize" +printf >&2 'Open files limit: %u\n' \ + "$files" +printf >&2 'Stack size: %u kbytes\n' \ + "$stack" +printf >&2 'Memory size: %u kbytes\n' \ + "$memory" +printf >&2 '\n' + +# Record the current seconds since shell start, so we can tell how long the +# user lasts before giving up or segfaulting +declare -i start +start=$SECONDS + +# Open a subshell so that the ulimit settings don't persist after the fork +( + # We only proceed if the ulimit call succeeds + ulimit \ + -f "$filesize" \ + -n "$files" \ + -s "$stack" \ + -v "$memory" \ + || exit 2 + DOOMSH=$skill PS1=$ps1 bash --norc +) + +# Give the user some statistics about their performance +printf >&2 '%s: You lasted %u seconds at skill level %u!\n' \ + "$self" "$((SECONDS - start))" "$skill" -- cgit v1.2.3 From d4855fe175f034659242853e062e1187fe6fd008 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sun, 1 Aug 2021 01:45:40 +1200 Subject: Relicense to GPLv3+ --- doomsh.bash | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'doomsh.bash') diff --git a/doomsh.bash b/doomsh.bash index 8603e70..5c48aed 100644 --- a/doomsh.bash +++ b/doomsh.bash @@ -1,11 +1,22 @@ #!bash # -# doomsh: Play with low ulimits in Bash +# Copyright (C) 2016, 2018, 2021 Tom Ryder +# +# This file is part of doomsh. +# +# doomsh is free software: you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation, either version 3 of the License, or (at your option) any later +# version. # -# Author: Tom Ryder -# Copyright: 2016 -# License: MIT (see LICENSE.markdown) +# doomsh is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. # +# You should have received a copy of the GNU General Public License along with +# doomsh. If not, see . + +# doomsh: Play with low ulimits in Bash self=doomsh # Don't try to play doomsh if already playing doomsh -- cgit v1.2.3 From 7a2628cb2e6e81cded2da702467c7b68b953f685 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sun, 1 Aug 2021 01:49:50 +1200 Subject: Use tabs for indentation --- doomsh.bash | 98 ++++++++++++++++++++++++++++++------------------------------- 1 file changed, 49 insertions(+), 49 deletions(-) (limited to 'doomsh.bash') diff --git a/doomsh.bash b/doomsh.bash index 5c48aed..13d0dc9 100644 --- a/doomsh.bash +++ b/doomsh.bash @@ -21,19 +21,19 @@ self=doomsh # Don't try to play doomsh if already playing doomsh if [[ $DOOMSH ]] ; then - printf '%s: Already playing doomsh!\n' \ - "$self" >&2 - exit 2 + printf '%s: Already playing doomsh!\n' \ + "$self" >&2 + exit 2 fi # Show the coloured logo; thanks to Frans P. de Vries for the classic ASCII art # printf >&2 %s "$( { - tput AF 3 || tput setaf 3 - tput md || tput bold + tput AF 3 || tput setaf 3 + tput md || tput bold } 2>/dev/null )" while IFS= read -r line ; do - printf >&2 '%s\n' "$line" + printf >&2 '%s\n' "$line" done <&2 %s "$( { - tput me || tput sgr0 + tput me || tput sgr0 } 2>/dev/null )" # Need to figure out a skill level @@ -52,14 +52,14 @@ declare -i skill # If we have an argument, that must be the skill level if (($#)) ; then - skill=$1 - shift + skill=$1 + shift # Otherwise, we'll give the user a menu to choose from else - while IFS= read -r line ; do - printf >&2 '%s\n' "$line" - done <&2 '%s\n' "$line" + done <&2 - done <&2 + done <&2 - exit 2 + 1|2|3|4|5) + ;; + *) + printf '%s: Unknown skill level\n' \ + "$self" >&2 + exit 2 esac # Form a red prompt (hopefully) using terminal escapes ps1=$(printf '%sdoomsh[%u]\$%s ' \ - "$( { - tput AF 1 || tput setaf 1 - } 2>/dev/null )" \ - "$skill" \ - "$( { - tput me || tput sgr0 - } 2>/dev/null )" + "$( { + tput AF 1 || tput setaf 1 + } 2>/dev/null )" \ + "$skill" \ + "$( { + tput me || tput sgr0 + } 2>/dev/null )" ) # Set the limits based on the skill level @@ -115,13 +115,13 @@ memory=$((1 << (19 - skill))) # Display the limits printf >&2 '\n' printf >&2 'File size writing limit: %u blocks\n' \ - "$filesize" + "$filesize" printf >&2 'Open files limit: %u\n' \ - "$files" + "$files" printf >&2 'Stack size: %u kbytes\n' \ - "$stack" + "$stack" printf >&2 'Memory size: %u kbytes\n' \ - "$memory" + "$memory" printf >&2 '\n' # Record the current seconds since shell start, so we can tell how long the @@ -131,16 +131,16 @@ start=$SECONDS # Open a subshell so that the ulimit settings don't persist after the fork ( - # We only proceed if the ulimit call succeeds - ulimit \ - -f "$filesize" \ - -n "$files" \ - -s "$stack" \ - -v "$memory" \ - || exit 2 - DOOMSH=$skill PS1=$ps1 bash --norc + # We only proceed if the ulimit call succeeds + ulimit \ + -f "$filesize" \ + -n "$files" \ + -s "$stack" \ + -v "$memory" \ + || exit 2 + DOOMSH=$skill PS1=$ps1 bash --norc ) # Give the user some statistics about their performance printf >&2 '%s: You lasted %u seconds at skill level %u!\n' \ - "$self" "$((SECONDS - start))" "$skill" + "$self" "$((SECONDS - start))" "$skill" -- cgit v1.2.3