xref: /titanic_44/usr/src/lib/libshell/common/scripts/simplefiletree1.sh (revision de6732348404877079e00a1631babd13d8a5ad14)
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 2009 Sun Microsystems, Inc.  All rights reserved.
26# Use is subject to license terms.
27#
28
29#
30# simplefiletree1 - build a simple file tree
31#
32
33# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant
34export PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin
35
36# Make sure all math stuff runs in the "C" locale to avoid problems
37# with alternative # radix point representations (e.g. ',' instead of
38# '.' in de_DE.*-locales). This needs to be set _before_ any
39# floating-point constants are defined in this script).
40if [[ "${LC_ALL}" != "" ]] ; then
41    export \
42        LC_MONETARY="${LC_ALL}" \
43        LC_MESSAGES="${LC_ALL}" \
44        LC_COLLATE="${LC_ALL}" \
45        LC_CTYPE="${LC_ALL}"
46        unset LC_ALL
47fi
48export LC_NUMERIC=C
49
50
51function add_file_to_tree
52{
53	typeset treename=$1
54	typeset filename=$2
55	integer i
56	typeset nodepath # full name of compound variable
57	typeset -a pe # path elements
58
59	# first built an array containing the names of each path element
60	# (e.g. "foo/var/baz"" results in an array containing "( 'foo' 'bar' 'baz' )")
61	typeset IFS='/'
62	pe+=( ${filename} )
63
64	[[ ${pe[0]} == '' ]] && pe[0]='/'
65
66	# walk path described via the "pe" array and build nodes if
67	# there aren't any nodes yet
68	nodepath="${treename}"
69	for (( i=0 ; i < (${#pe[@]}-1) ; i++ )) ; do
70		nameref x="${nodepath}"
71		[[ ! -v x.node ]] && compound -A x.nodes
72
73		nodepath+=".nodes[${pe[i]}]"
74	done
75
76	# insert element
77	nameref node="${nodepath}"
78	[[ ! -v node.elements ]] && typeset -a node.elements
79	node.elements+=( "${pe[i]}" )
80
81	return 0
82}
83
84# main
85builtin rev
86
87# tree base
88compound filetree
89
90# benchmark data
91compound bench=(
92	float start
93	float stop
94)
95
96typeset i
97
98# argument prechecks
99if (( $# == 0 )) ; then
100	print -u2 -f "%s: Missing <path> argument." "$0"
101	exit 1
102fi
103
104print -u2 "# reading file names"
105while (( $# > 0 )) ; do
106	IFS=$'\n' ; typeset -a filenames=( $(find "$1" -type f) ) ; IFS=$' \t\n'
107	shift
108done
109print -u2 "# building tree..."
110
111(( bench.start=SECONDS ))
112
113for ((i=0 ; i < ${#filenames[@]} ; i++ )) ; do
114	add_file_to_tree filetree "${filenames[i]}"
115done
116
117(( bench.stop=SECONDS ))
118
119# print benchmark data
120print -u2 -f "# time used: %f\n" $((bench.stop - bench.start))
121
122# print tree
123print -v filetree
124
125exit 0
126# EOF.
127