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