#!/bin/sh # # $Id: dtree,v 1.5 2009/04/10 21:36:41 vogelke Exp $ # # NAME: # dtree # # SYNOPSIS: # dtree [-ahl] [--all] [--help] [--list] dir or list # dtree [-v] [--version] # # DESCRIPTION: # program to ... # # OPTIONS: # -a, --all prints all files, not just directories. # -h, --help prints usage information. # -l, --list reads a list of files from stdin. # -v, --version print the version and exit. # # "dir" is the top-level directory to use. If not supplied, # the current directory is used. # # The "-l" argument reads a list of files, sorts them, # and prints them in tree form. "-l" implies "-a". # # EXAMPLE: # If a directory looks like this: # # ./faq: # -rw-r--r-- 1 bin bin 317534 Sep 24 16:13 port-numbers # -rw-r--r-- 1 bin bin 11656 Jun 1 1997 socket-tutorial # drwxr-xr-x 2 bin bin 512 Oct 17 17:34 solaris2/ # # ./faq/solaris2: # -rw-r--r-- 1 bin bin 153927 Jul 31 1997 nis+tips # -rw-r--r-- 1 bin bin 118555 Nov 18 18:15 sun-managers-faq # # Then output from dtree looks something like this: # # /path/to/faq # +-----port-numbers # +-----socket-tutorial # +-----solaris2 # | +-----nis+tips # | +-----sun-managers-faq # # AUTHOR: # Based on Free Software Foundation configure scripts. # # Karl Vogel # Sumaria Systems, Inc. PATH=/bin:/usr/sbin:/usr/bin:/usr/local/bin export PATH umask 022 tag=`basename $0` # ======================== FUNCTIONS ============================= # # die: prints an optional argument to stderr and exits. # warn: prints an optional argument to stderr. # A common use for "die" is with a test: # test -f /etc/passwd || die "no passwd file" # This works in subshells and loops, but may not exit with # a code other than 0. die () { echo "$tag: error: $*" 1>&2 exit 1 } # usage: prints an optional string plus part of the comment # header (if any) to stderr, and exits with code 1. usage () { ( case "$#" in 0) ;; *) echo "usage error: $*"; echo ;; esac awk '/^# NAME/,/^# AUTHOR/ { if ($2 == "AUTHOR:") exit print substr($0, 2) }' $0 ) 1>&2 exit 1 } # version: prints the current version to stdout. version () { awk '{c=index($2,","); n=substr($2,1,c-1); v=" v"$5" "; print n, v, $8, $9}' <<' EOV' $RCSfile: dtree,v $ $Revision: 1.5 $ $Date: 2009/04/10 21:36:41 $ EOV exit 0 } # mktree: sort the file information properly. mktree () { scr=' s,^.$,, /^$/d s,[^/]*/\([^/]*\)$,+-----\1, s,[^/]*/,| ,g' tr '/' '\001' | sort -f | tr '\001' '/' | sed -e "$scr" } # ======================== MAIN PROGRAM ========================== # Defaults: ac_help= ac_prev= ac_invalid="invalid option; use --help to show usage" argv= # Initialize some variables set by options. all=no list=no fopt="-type d" for ac_option do # If the previous option needs an argument, assign it. case "$ac_prev" in "") ;; *) eval "$ac_prev=\$ac_option"; ac_prev=; continue ;; esac case "$ac_option" in -*=*) ac_optarg=`echo "$ac_option" | sed 's/[-_a-zA-Z0-9]*=//'` ;; *) ac_optarg= ;; esac # main switch case "$ac_option" in -a | -all | --all | --al | --a) all=yes; fopt="" ;; -h | -help | --help | --hel | --he) usage ;; -l | -list | --list | --lis | --li | --l) list=yes ;; -v | -version | --version | --versio |\ --versi | --vers) version ;; -*) die "$ac_option: $ac_invalid" ;; *) argv="$argv $ac_option" ;; esac done case "$ac_prev" in "") ;; *) die "missing arg to --`echo $ac_prev | sed 's/_/-/g'`" ;; esac # # Real work starts here. Test for specific features. # argv=`echo $argv` case "$argv" in "") case "$list" in "yes") top="" ;; # sort reads stdin. *) top="." ;; esac ;; *) top=$argv ;; esac # # Print the directory tree. # case "$list" in "no") find $top $fopt -print | mktree ;; "yes") mktree ;; esac exit 0