1*10898Sroland.mainz@nrubsig.org#!/usr/bin/ksh93 2*10898Sroland.mainz@nrubsig.org 3*10898Sroland.mainz@nrubsig.org# 4*10898Sroland.mainz@nrubsig.org# CDDL HEADER START 5*10898Sroland.mainz@nrubsig.org# 6*10898Sroland.mainz@nrubsig.org# The contents of this file are subject to the terms of the 7*10898Sroland.mainz@nrubsig.org# Common Development and Distribution License (the "License"). 8*10898Sroland.mainz@nrubsig.org# You may not use this file except in compliance with the License. 9*10898Sroland.mainz@nrubsig.org# 10*10898Sroland.mainz@nrubsig.org# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 11*10898Sroland.mainz@nrubsig.org# or http://www.opensolaris.org/os/licensing. 12*10898Sroland.mainz@nrubsig.org# See the License for the specific language governing permissions 13*10898Sroland.mainz@nrubsig.org# and limitations under the License. 14*10898Sroland.mainz@nrubsig.org# 15*10898Sroland.mainz@nrubsig.org# When distributing Covered Code, include this CDDL HEADER in each 16*10898Sroland.mainz@nrubsig.org# file and include the License file at usr/src/OPENSOLARIS.LICENSE. 17*10898Sroland.mainz@nrubsig.org# If applicable, add the following below this CDDL HEADER, with the 18*10898Sroland.mainz@nrubsig.org# fields enclosed by brackets "[]" replaced with your own identifying 19*10898Sroland.mainz@nrubsig.org# information: Portions Copyright [yyyy] [name of copyright owner] 20*10898Sroland.mainz@nrubsig.org# 21*10898Sroland.mainz@nrubsig.org# CDDL HEADER END 22*10898Sroland.mainz@nrubsig.org# 23*10898Sroland.mainz@nrubsig.org 24*10898Sroland.mainz@nrubsig.org# 25*10898Sroland.mainz@nrubsig.org# Copyright 2009 Sun Microsystems, Inc. All rights reserved. 26*10898Sroland.mainz@nrubsig.org# Use is subject to license terms. 27*10898Sroland.mainz@nrubsig.org# 28*10898Sroland.mainz@nrubsig.org 29*10898Sroland.mainz@nrubsig.org# 30*10898Sroland.mainz@nrubsig.org# simplefiletree1 - build a simple file tree 31*10898Sroland.mainz@nrubsig.org# 32*10898Sroland.mainz@nrubsig.org 33*10898Sroland.mainz@nrubsig.org# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant 34*10898Sroland.mainz@nrubsig.orgexport PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin 35*10898Sroland.mainz@nrubsig.org 36*10898Sroland.mainz@nrubsig.org# Make sure all math stuff runs in the "C" locale to avoid problems 37*10898Sroland.mainz@nrubsig.org# with alternative # radix point representations (e.g. ',' instead of 38*10898Sroland.mainz@nrubsig.org# '.' in de_DE.*-locales). This needs to be set _before_ any 39*10898Sroland.mainz@nrubsig.org# floating-point constants are defined in this script). 40*10898Sroland.mainz@nrubsig.orgif [[ "${LC_ALL}" != "" ]] ; then 41*10898Sroland.mainz@nrubsig.org export \ 42*10898Sroland.mainz@nrubsig.org LC_MONETARY="${LC_ALL}" \ 43*10898Sroland.mainz@nrubsig.org LC_MESSAGES="${LC_ALL}" \ 44*10898Sroland.mainz@nrubsig.org LC_COLLATE="${LC_ALL}" \ 45*10898Sroland.mainz@nrubsig.org LC_CTYPE="${LC_ALL}" 46*10898Sroland.mainz@nrubsig.org unset LC_ALL 47*10898Sroland.mainz@nrubsig.orgfi 48*10898Sroland.mainz@nrubsig.orgexport LC_NUMERIC=C 49*10898Sroland.mainz@nrubsig.org 50*10898Sroland.mainz@nrubsig.org 51*10898Sroland.mainz@nrubsig.orgfunction add_file_to_tree 52*10898Sroland.mainz@nrubsig.org{ 53*10898Sroland.mainz@nrubsig.org typeset treename=$1 54*10898Sroland.mainz@nrubsig.org typeset filename=$2 55*10898Sroland.mainz@nrubsig.org integer i 56*10898Sroland.mainz@nrubsig.org typeset nodepath # full name of compound variable 57*10898Sroland.mainz@nrubsig.org typeset -a pe # path elements 58*10898Sroland.mainz@nrubsig.org 59*10898Sroland.mainz@nrubsig.org # first built an array containing the names of each path element 60*10898Sroland.mainz@nrubsig.org # (e.g. "foo/var/baz"" results in an array containing "( 'foo' 'bar' 'baz' )") 61*10898Sroland.mainz@nrubsig.org typeset IFS='/' 62*10898Sroland.mainz@nrubsig.org pe+=( ${filename} ) 63*10898Sroland.mainz@nrubsig.org 64*10898Sroland.mainz@nrubsig.org [[ ${pe[0]} == '' ]] && pe[0]='/' 65*10898Sroland.mainz@nrubsig.org 66*10898Sroland.mainz@nrubsig.org # walk path described via the "pe" array and build nodes if 67*10898Sroland.mainz@nrubsig.org # there aren't any nodes yet 68*10898Sroland.mainz@nrubsig.org nodepath="${treename}" 69*10898Sroland.mainz@nrubsig.org for (( i=0 ; i < (${#pe[@]}-1) ; i++ )) ; do 70*10898Sroland.mainz@nrubsig.org nameref x="${nodepath}" 71*10898Sroland.mainz@nrubsig.org [[ ! -v x.node ]] && compound -A x.nodes 72*10898Sroland.mainz@nrubsig.org 73*10898Sroland.mainz@nrubsig.org nodepath+=".nodes[${pe[i]}]" 74*10898Sroland.mainz@nrubsig.org done 75*10898Sroland.mainz@nrubsig.org 76*10898Sroland.mainz@nrubsig.org # insert element 77*10898Sroland.mainz@nrubsig.org nameref node="${nodepath}" 78*10898Sroland.mainz@nrubsig.org [[ ! -v node.elements ]] && typeset -a node.elements 79*10898Sroland.mainz@nrubsig.org node.elements+=( "${pe[i]}" ) 80*10898Sroland.mainz@nrubsig.org 81*10898Sroland.mainz@nrubsig.org return 0 82*10898Sroland.mainz@nrubsig.org} 83*10898Sroland.mainz@nrubsig.org 84*10898Sroland.mainz@nrubsig.org# main 85*10898Sroland.mainz@nrubsig.orgbuiltin rev 86*10898Sroland.mainz@nrubsig.org 87*10898Sroland.mainz@nrubsig.org# tree base 88*10898Sroland.mainz@nrubsig.orgcompound filetree 89*10898Sroland.mainz@nrubsig.org 90*10898Sroland.mainz@nrubsig.org# benchmark data 91*10898Sroland.mainz@nrubsig.orgcompound bench=( 92*10898Sroland.mainz@nrubsig.org float start 93*10898Sroland.mainz@nrubsig.org float stop 94*10898Sroland.mainz@nrubsig.org) 95*10898Sroland.mainz@nrubsig.org 96*10898Sroland.mainz@nrubsig.orgtypeset i 97*10898Sroland.mainz@nrubsig.org 98*10898Sroland.mainz@nrubsig.org# argument prechecks 99*10898Sroland.mainz@nrubsig.orgif (( $# == 0 )) ; then 100*10898Sroland.mainz@nrubsig.org print -u2 -f "%s: Missing <path> argument." "$0" 101*10898Sroland.mainz@nrubsig.org exit 1 102*10898Sroland.mainz@nrubsig.orgfi 103*10898Sroland.mainz@nrubsig.org 104*10898Sroland.mainz@nrubsig.orgprint -u2 "# reading file names" 105*10898Sroland.mainz@nrubsig.orgwhile (( $# > 0 )) ; do 106*10898Sroland.mainz@nrubsig.org IFS=$'\n' ; typeset -a filenames=( $(find "$1" -type f) ) ; IFS=$' \t\n' 107*10898Sroland.mainz@nrubsig.org shift 108*10898Sroland.mainz@nrubsig.orgdone 109*10898Sroland.mainz@nrubsig.orgprint -u2 "# building tree..." 110*10898Sroland.mainz@nrubsig.org 111*10898Sroland.mainz@nrubsig.org(( bench.start=SECONDS )) 112*10898Sroland.mainz@nrubsig.org 113*10898Sroland.mainz@nrubsig.orgfor ((i=0 ; i < ${#filenames[@]} ; i++ )) ; do 114*10898Sroland.mainz@nrubsig.org add_file_to_tree filetree "${filenames[i]}" 115*10898Sroland.mainz@nrubsig.orgdone 116*10898Sroland.mainz@nrubsig.org 117*10898Sroland.mainz@nrubsig.org(( bench.stop=SECONDS )) 118*10898Sroland.mainz@nrubsig.org 119*10898Sroland.mainz@nrubsig.org# print benchmark data 120*10898Sroland.mainz@nrubsig.orgprint -u2 -f "# time used: %f\n" $((bench.stop - bench.start)) 121*10898Sroland.mainz@nrubsig.org 122*10898Sroland.mainz@nrubsig.org# print tree 123*10898Sroland.mainz@nrubsig.orgprint -v filetree 124*10898Sroland.mainz@nrubsig.org 125*10898Sroland.mainz@nrubsig.orgexit 0 126*10898Sroland.mainz@nrubsig.org# EOF. 127