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