From 6837690733ecd0110e9a7c3c9f5b5fd3a99dc33e Mon Sep 17 00:00:00 2001 From: Dennis Eriksen Date: Thu, 7 Sep 2023 09:43:45 +0200 Subject: More fixes! Things are popping up with the zsh-version as I'm rewriting the perl-version. - Rename help-function - I wanted zero-padding of PRINTLEN. Added it now. - Change the way we set first and last character in randstring. --- makepass.zsh | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/makepass.zsh b/makepass.zsh index e76edc9..668daee 100755 --- a/makepass.zsh +++ b/makepass.zsh @@ -56,7 +56,7 @@ function main() { while getopts 'hl:n:p' opt; do case $opt in h) - _makepass_help && return 0;; + help && return 0;; l) [[ $OPTARG = <0-> && $OPTARG -le $MAX ]] || die "-l takes a number between 0 and $MAX" LENGTH=$OPTARG;; @@ -99,15 +99,16 @@ function main() { local b1=$r4[1] b2=$r4[2] b3=$r4[3] b4=$r4[4] RANDOM=$(( #b1 << 24 | #b2 << 16 | #b3 << 8 | #b4 )) - # We zero-pad printlength, so we need three extra chars if LENGTH < 100 (four - # if LENGTH >= 100). Two for printlength, one for the space. - (( PRINTLEN )) && { ((LENGTH < 100)) && PRINTLEN=3 || PRINTLEN=4 } + # We zero-pad printlength, so we need two extra chars if LENGTH < 100 (three + # if LENGTH >= 100). + (( PRINTLEN )) && { ((LENGTH < 100)) && PRINTLEN=2 || PRINTLEN=3 } # Calculate width of columns and number of columns to use # add two for spacing between columns - COL_WIDTH=$(( ( LENGTH ? LENGTH : RANGE_MAX ) + PRINTLEN + 2 )) + COL_WIDTH=$(( ( LENGTH ? LENGTH : RANGE_MAX ) + 2 )) # $COLUMNS is a builtin variable for width of terminal - COL_NUM=$(( COLUMNS / COL_WIDTH )) + # If $PRINTLEN is set, we have to add one for the space as well + COL_NUM=$(( COLUMNS / ( COL_WIDTH + ( PRINTLEN ? PRINTLEN + 1 : 0 )) )) # Just in case COL_NUM=0 because of a small terminal or something (( COL_NUM )) || COL_NUM=1 @@ -146,8 +147,8 @@ function print_columns() { for s in $strings; do let i++ - (( PRINTLEN )) && s="$#s $s" - printf "%-${COL_WIDTH}s" $s; + (( PRINTLEN )) && printf "%0${PRINTLEN}i " $#s + printf "%-${COL_WIDTH}s" $s ((i % COL_NUM == 0 || (i == num && i % COL_NUM > 0))) && print done } @@ -160,14 +161,9 @@ function randstring() { ? LENGTH \ : RANDOM % (RANGE_MAX - RANGE_MIN + 1) + RANGE_MIN )) - repeat $len string+=$chars[$((RANDOM % $#chars + 1))] - - if ((len >= 2)); then - # add new character to start of line and remove 2 from old string - string=$ALPHA[$((RANDOM % $#ALPHA + 1))]$string[2,-2] - # add new character to end of string - string+=$ALPHA[$((RANDOM % $#ALPHA + 1))] - fi + string+=$ALPHA[$((RANDOM % $#ALPHA + 1))] + repeat $((len - 2)) string+=$chars[$((RANDOM % $#chars + 1))] + (( len >= 2 )) && string+=$ALNUM[$((RANDOM % $#ALNUM + 1))] print -- $string; return } @@ -204,7 +200,7 @@ function die() { } # Help-function -function _makepass_help() { +function help() { print -- $'NAME makepass - create several random passwords @@ -226,7 +222,8 @@ SYNOPSIS from the dictionary, separated by dashes. The number of words can not be changed, but you do not have to use all of them. Use as mane as you want. - The first and last letter will always be a letter. + The first character will always be alphabetic, and the last will always be + alphanumeric. DESCRIPTION makepass has the following options: -- cgit v1.2.3