summaryrefslogtreecommitdiff
path: root/test/fts.awk
blob: 70af560f18d165e086926e2ae4c2c4385594ef61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
@load "filefuncs"

BEGIN {
	Level = 0

	os = ""
	if (ENVIRON["AWKLIBPATH"] == "sys$disk:[-]") {
		os = "VMS"
		system("create/dir/prot=o:rwed [.d1]")
		system("create/dir/prot=o:rwed [.d2]")
		system("copy fts.awk [.d1]f1")
		system("copy fts.awk [.d1]f2")
		system("copy fts.awk [.d2]f1")
		system("copy fts.awk [.d2]f2")
	} else {
		system("rm -fr d1 d2")
		system("mkdir d1 d2 ; touch d1/f1 d1/f2 d2/f1 d2/f2")
	}
	pathlist[1] = "d1"
	pathlist[2] = "d2"
	flags = FTS_PHYSICAL
	fts(pathlist, flags, data)

	output = "fts.ok"
	traverse(data)
	close(output)

	ftswalk(pathlist, data2)
	output = "_fts"
	traverse(data2)
	close(output)

	if (os == "VMS") {
		system("delete [.d1]*.*;*")
		system("delete [.d2]*.*;*")
		system("delete d1.dir;*")
		system("delete d2.dir;*")
	} else {
		system("rm -fr d1 d2")
	}
}

function indent(        i)
{
	for (i = 1; i <= Level; i++)
		printf("\t") > output
}

function sort_traverse(data,	sorted, i)
{
	asorti(data, sorted)
	for (i = 1; i in sorted; i++) {
		indent()
		printf("%s --> %s\n", sorted[i], data[sorted[i]]) > output
	}
}

function traverse(data,         i)
{
	for (i in data) {
		if (isarray(data[i])) {
			indent()
			printf("%s:\n", i) > output

			Level++
			if (("mtime" in data[i]) && ! isarray(data[i][mtime])) {
				sort_traverse(data[i])
			} else {
				traverse(data[i])
			}
			Level--
		} else {
			indent()
			printf("%s --> %s\n", i, data[i]) > output
		}
	}
}


function ftswalk(pathlist, data,	i, toppath)
{
	delete data
	for (i = 1; i in pathlist; i++) {
		toppath = pathlist[i]
		data[toppath]["junk"]++	# create array
		delete data[toppath]["junk"]
		process(pathlist[i], data)
	}
}

# enter process with pathname, array for that path already created but
# empty

function process(pathname, data_array,
		stat_data, i, direntry, command, shortname)	# locals
{
	stat(pathname, stat_data)
	if (stat_data["type"] == "file") {
		shortname = strrstr(pathname, "/")
		data_array["path"] = pathname
		for (i in stat_data) {
			if (i == "name")
				data_array["stat"][i] = shortname
			else
				data_array["stat"][i] = stat_data[i]
		}

		return
	}

	# stuff for a directory

	data_array[pathname]["."]["path"] = pathname
	for (i in stat_data)
		data_array[pathname]["."]["stat"][i] = stat_data[i]

	os = ""
	if (ENVIRON["AWKLIBPATH"] == "sys$disk:[-]") {
		os = "VMS"
		# Command in next section not valid on VMS.
		return
	}

	command = ("ls -f " pathname)
	while ((command | getline direntry) > 0) {
		if (direntry == "." || direntry == "..")
			continue
		data_array[pathname][direntry]["junk"]++
		delete data_array[pathname][direntry]["junk"]
		process(pathname "/" direntry,
			data_array[pathname][direntry])
	}
	close(command)
}

function strrstr(string, delim,		ind)
{
	if ((ind = index(string, delim)) == 0)
		return string

	do {
		string = substr(string, ind + 1)
	} while ((ind = index(string, delim)) > 0)

	return string
}