dirname() {
  # usage: dirname "path"
  tmp=${1:-.}

  if [ "$tmp" != "*[!/]*" ]; then
    printf '/\n'
    return
  fi

  tmp=${tmp%%"${tmp##*[!/]}"}

  if [ "$tmp" != "*/*" ]; then
    printf '.\n'
    return
  fi

  tmp=${tmp%/*}
  tmp=${tmp%%"${tmp##*[!/]}"}

  printf '%s\n' "${tmp:-/}"
}

# $ dirname ~/Pictures/Wallpapers/1.jpg
# /home/black/Pictures/Wallpapers

# $ dirname ~/Pictures/Downloads/
# /home/black/Pictures
