File: xhfs.tcl

package info (click to toggle)
hfsutils 3.2.6-16
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,680 kB
  • sloc: ansic: 12,858; tcl: 1,937; makefile: 566; sh: 156; perl: 29
file content (1926 lines) | stat: -rw-r--r-- 44,011 bytes parent folder | download | duplicates (7)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
#
# hfsutils - tools for reading and writing Macintosh HFS volumes
# Copyright (C) 1996-1998 Robert Leslie
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# $Id: xhfs.tcl,v 1.8 1998/04/11 08:27:01 rob Exp $
#

set tk_strictMotif 1

set gNormalFont	{-adobe-helvetica-bold-r-normal-*-*-120-*-*-p-*-iso8859-1}
set gThinFont	{-adobe-helvetica-medium-r-normal-*-*-120-*-*-p-*-iso8859-1}
set gSmallFont	{-adobe-helvetica-medium-r-normal-*-*-100-*-*-p-*-iso8859-1}
set gTypeFont	{-*-lucidatypewriter-medium-r-normal-*-*-120-*-*-m-*-iso8859-1}
set gTitleFont	{-*-lucida-bold-i-normal-*-*-250-*-*-p-*-iso8859-1}

set gDefaultDevice	/dev/fd0
set gLowFormatCmd	[list fdformat $gDefaultDevice]
set gMountSide		.l
set gFlushInterval	30000
set gDfCmd		[list df]

option add *Font $gNormalFont

###############################################################################

# Set architecture defaults

if {[catch {exec uname} gUname] == 0} {
    switch $gUname {
	SunOS {
	    if {[catch {exec uname -r} gUrelease] == 0} {
		switch -glob $gUrelease {
		    4.* {
			set gLowFormatCmd [list fdformat -f /dev/rfd0]
		    }
		    5.* {
			set gDefaultDevice /vol/dev/aliases/floppy0
			set gLowFormatCmd [list fdformat -f $gDefaultDevice]
			set gDfCmd [list df -k]
		    }
		}
	    }
	}

	FreeBSD {
	    set gDefaultDevice /dev/rfd0
	    set gLowFormatCmd [list fdformat $gDefaultDevice]
	}
    }
}

###############################################################################

set gUnique 0

proc unique {} {
    global gUnique

    return [incr gUnique]
}

proc window {title iconname} {
    set w ".w[unique]"

    toplevel $w -cursor left_ptr -relief raised -bd 1
    wm title $w $title
    wm iconname $w $iconname
    wm transient $w .

    wm withdraw $w

    return $w
}

proc showwindow {w} {
    update idletasks

    set x [expr [winfo x .] + [winfo width  .]/2 - [winfo reqwidth  $w]/2]
    set y [expr [winfo y .] + [winfo height .]/3 - [winfo reqheight $w]/3]

    wm geom $w +$x+$y
    wm deiconify $w
}

proc dialog {title cancel {ok ""} {default 0}} {
    set w ".d[unique]"

    toplevel $w -class Dialog -cursor left_ptr
    wm title $w $title
    wm transient $w .

    wm withdraw $w

    frame $w.top -relief raised -bd 1
    frame $w.bot -relief raised -bd 1
    pack $w.top -side top -expand 1 -fill both -ipadx 4m -ipady 4m
    pack $w.bot -side bottom -fill both -ipadx 2m -ipady 2m

    frame $w.bot.default -relief sunken -bd 1

    button $w.bot.cancel -width 6 -text $cancel  \
	    -command "set gAnswer 0; [list destroy $w]"
    if {[string length $ok] > 0} {
	button $w.bot.ok -width 6 -text $ok  \
		-command "set gAnswer 1; [list destroy $w]"
    }

    if {$default} {
	pack $w.bot.cancel -side left -expand 1;# -ipadx 2m -ipady 1m
	pack $w.bot.default -side left -expand 1 -ipadx 1m -ipady 1m
	pack $w.bot.ok -in $w.bot.default -expand 1;# -ipadx 2m -ipady 1m
	bind $w <Key-Return> [list $w.bot.ok invoke]
    } else {
	pack $w.bot.default -side left -expand 1 -ipadx 1m -ipady 1m
	pack $w.bot.cancel -in $w.bot.default -expand 1;# -ipadx 2m -ipady 1m
	if {[string length $ok] > 0} {
	    pack $w.bot.ok -side left -expand 1;# -ipadx 2m -ipady 1m
	}
	bind $w <Key-Return> [list $w.bot.cancel invoke]
    }

    return $w
}

proc showdialog {w} {
    showwindow $w
    grab set $w
}

proc alert {kind title text {aspect 100}} {
    global gThinFont

    set w [dialog $title "OK"]

    frame $w.top.x
    label $w.top.x.icon -bitmap $kind -anchor n
    frame $w.top.x.space -width 2m
    message $w.top.x.msg -aspect $aspect -font $gThinFont -text $text
    pack $w.top.x.icon $w.top.x.space $w.top.x.msg -side left -fill y

    pack $w.top.x -expand 1

    showdialog $w
    tkwait window $w
}

proc bgerror {error} {
    alert stop "Error" "Sorry, $error." 1000
    return -code break
}

proc fixtext {text} {
    regsub -all "(\[^\n])\n\[ \t]*(\[^\n])" $text {\1 \2} text
    regsub -all "(^|\n)\[ \t]+" $text {\1} text
    regsub "\n*\$" $text "" text

    return $text
}

proc copy_ufs_to_hfs {srcvol name mode dstvol} {
    $dstvol copyin $mode "[$srcvol cwd]/$name" :
}

proc copy_hfs_to_ufs {srcvol name mode dstvol} {
    $srcvol copyout $mode $name "[$dstvol cwd]/."
}

proc copy_hfs_to_hfs {srcvol name mode dstvol} {
    $srcvol copy $name $dstvol :
}

proc copy_ufs_to_ufs {srcvol name mode dstvol} {
    $srcvol copy $name "[$dstvol cwd]/."
}

proc copy_directory {srcvol name dstvol copyproc modeproc} {
    global gXMode

    set srccwd [abspath [$srcvol sepchar] [$srcvol path]]
    set dstcwd [abspath [$dstvol sepchar] [$dstvol path]]

    $dstvol mkdir $name
    $srcvol chdir $name
    $dstvol chdir $name

    foreach ind [$srcvol dir] {
	array set item $ind

	if {$item(kind) == "directory"} {
	    copy_directory $srcvol $item(name) $dstvol $copyproc $modeproc
	} else {
	    if {$gXMode == "auto"} {
		set mode [$modeproc $srcvol item]
	    } else {
		set mode $gXMode
	    }

	    $copyproc $srcvol $item(name) $mode $dstvol
	}
    }

    $srcvol chdir $srccwd
    $dstvol chdir $dstcwd
}

proc delete_directory {vol name} {
    set cwd [abspath [$vol sepchar] [$vol path]]

    $vol chdir $name

    foreach ind [$vol dir] {
	array set item $ind

	if {$item(kind) == "directory"} {
	    delete_directory $vol $item(name)
	} else {
	    $vol delete $item(name)
	}
    }

    $vol chdir $cwd
    $vol rmdir $name
}

proc do_open {side} {
    set box $side.mid.lb
    set sel [$box curselection]
    set ind [lindex $sel 0]

    global vol

    set name [$box get $ind]
    if {[regexp "(.*)[$vol($side) sepchar]\$" $name ignore name]} {
	mchdir $side $name
    } else {
	show_file $side $name
    }
}

proc do_copy {side other} {
    set box $side.mid.lb
    set sel [$box curselection]

    global gXMode gAskDelete gAnswer
    global fstype dir vol

    if {$side == ".l"} {
	set other ".r"
    } else {
	set other ".l"
    }

    set needupdate 0

    foreach ind $sel {
	array set item [lindex $dir($side) $ind]

	$box see $ind

	# if {$gAskDelete && [catch {$vol($other) stat $item(name)}] == 0} {
	    # set w [dialog "Overwrite" "Cancel" "OK"]

	    # frame $w.top.x
	    # label $w.top.x.icon -bitmap caution -anchor n
	    # frame $w.top.x.space -width 2m
	    # message $w.top.x.msg -font $gThinFont -aspect 500  \
		    # -text "Overwrite \"$item(name)\"?"
	    # pack $w.top.x.icon $w.top.x.space $w.top.x.msg -side left -fill y

	    # pack $w.top.x -expand 1

	    # showdialog $w
	    # tkwait window $w

	    # if {$gAnswer == 0} {
		# break
	    # }
	# }

	watch_cursor

	if {$item(kind) == "directory"} {
	    set update [after idle  \
		    "[list updatelist $side]; [list updatelist $other]"]

	    copy_directory $vol($side) $item(name) $vol($other)  \
		    copy_$fstype($side)_to_$fstype($other)  \
		    $fstype($side)_automode
	} else {
	    set update [after idle [list updatelist $other]]

	    if {$gXMode == "auto"} {
		set mode [$fstype($side)_automode $vol($side) item]
	    } else {
		set mode $gXMode
	    }

	    copy_$fstype($side)_to_$fstype($other)  \
		    $vol($side) $item(name) $mode $vol($other)
	}

	after cancel $update
	set needupdate 1

	$box selection clear $ind
    }

    if {$needupdate} {
	updatelist $other; clearlists
    }
}

proc do_info {side} {
    set box $side.mid.lb
    set sel [$box curselection]

    global vol dir

    foreach ind $sel {
	show_info $vol($side) [lindex $dir($side) $ind]
    }
}

proc do_rename {side} {
    global dir
    global gTypeFont

    set box $side.mid.lb
    set sel [$box curselection]
    array set item [lindex $dir($side) [lindex $sel 0]]

    set w [dialog "Rename" "Cancel" "Rename" 1]
    $w.bot.ok config -command [list rename2 $w $side]

    frame $w.top.x

    label $w.top.x.lbl -text "Rename \"$item(name)\" to:"
    label $w.top.x.srcname -text $item(name)
    entry $w.top.x.dstname -width 25 -font $gTypeFont -exportsel 0

    pack $w.top.x.lbl $w.top.x.dstname -side top -pady 0.5m
    pack $w.top.x -expand 1

    $w.top.x.dstname insert end $item(name)
    $w.top.x.dstname selection range 0 end

    focus $w.top.x.dstname
    showdialog $w
}

proc rename2 {w side} {
    global vol

    set srcname [$w.top.x.srcname cget -text]
    set dstname [$w.top.x.dstname get]

    watch_cursor $w
    destroy $w

    if {[string length $dstname] == 0} {
	error "cannot rename to zero-length name"
    }

    if {[string compare $srcname $dstname] != 0} {
	$vol($side) rename $srcname $dstname
	updatelist $side; clearlists
    }
}

proc do_delete {side} {
    set box $side.mid.lb
    set sel [$box curselection]

    global gThinFont gAskDelete gAnswer
    global vol dir

    set sepchar [$vol($side) sepchar]
    set needupdate 0

    foreach ind $sel {
	array set item [lindex $dir($side) $ind]

	$box see $ind

	set recursive [expr {$item(kind) == "directory" && $item(size) > 0}]

	if {$recursive || $gAskDelete} {
	    set w [dialog "Delete" "Cancel" "Delete"]

	    if {$recursive} {
		set p1 "Recursively delete \"$item(name)\""
		set p2 "containing $item(size) item[plural $item(size) "" "s"]"
		set prompt "$p1 $p2?"
	    } else {
		set prompt "Really delete \"$item(name)\"?"
	    }

	    frame $w.top.x
	    label $w.top.x.icon -bitmap caution -anchor n
	    frame $w.top.x.space -width 2m
	    message $w.top.x.msg -font $gThinFont -aspect 500 -text $prompt
	    pack $w.top.x.icon $w.top.x.space $w.top.x.msg -side left -fill y

	    pack $w.top.x -expand 1

	    showdialog $w
	    tkwait window $w

	    if {$gAnswer == 0} {
		break
	    }
	}

	watch_cursor
	set update [after idle "[list updatelist $side]; clearlists"]

	if {$item(kind) == "directory"} {
	    delete_directory $vol($side) $item(name)
	} else {
	    $vol($side) delete $item(name)
	}

	after cancel $update
	set needupdate 1

	$box selection clear $ind
    }

    if {$needupdate} {
	updatelist $side; clearlists
    }
}

proc format_disk {} {
    global gTypeFont gThinFont gLowFormatCmd gDefaultDevice

    set w [dialog "Format Disk" "Cancel" "Format"]
    $w.bot.ok config -command [list format2 $w]

    frame $w.top.x

    set low [frame $w.top.x.low]
    label $low.icon -bitmap floppy

    frame $low.info
    checkbutton $low.info.cb -var gLowFormat  \
	    -text "Low-level Format" -anchor w
    entry $low.info.cmd -width 26 -font $gTypeFont
    pack $low.info.cb $low.info.cmd -side top -fill x -pady 0.5m

    pack $low.icon $low.info -side left -padx 2m

    frame $w.top.x.space1 -height 2m

    label $w.top.x.hfs -text "Initialize as HFS Volume..." -anchor w

    set dev [frame $w.top.x.dev]
    label $dev.lbl -width 6 -text "Device:" -font $gThinFont -anchor w
    entry $dev.name -width 15 -font $gTypeFont
    label $dev.parlbl -text "Partition:" -font $gThinFont
    entry $dev.par -width 2 -font $gTypeFont
    pack $dev.lbl $dev.name $dev.parlbl $dev.par -side left -padx 0.5m

    set vol [frame $w.top.x.vol]
    label $vol.lbl -width 6 -text "Name:" -font $gThinFont -anchor w
    entry $vol.name -width 27 -font $gTypeFont
    pack $vol.lbl $vol.name -side left -padx 0.5m

    frame $w.top.x.space2 -height 2m

    set mount [frame $w.top.x.mount]
    label $mount.lbl -text "Open:" -font $gThinFont
    radiobutton $mount.l -var gMountSide -value ".l"  \
	    -text "On Left" -font $gThinFont
    radiobutton $mount.r -var gMountSide -value ".r"  \
	    -text "On Right" -font $gThinFont
    pack $mount.lbl $mount.l $mount.r -side left

    pack $low $w.top.x.space1 $w.top.x.hfs $dev $vol  \
	    -side top -fill x -pady 0.5m
    pack $w.top.x.space2 $mount -side top -pady 0.5m

    pack $w.top.x -expand 1

    $w.top.x.low.info.cmd insert end $gLowFormatCmd
    $w.top.x.dev.name insert end $gDefaultDevice
    $w.top.x.vol.name insert end "Untitled"

    # focus $w.top.x.dev.name
    showdialog $w
}

proc format2 {w} {
    global gAppCWD gLowFormat gLowFormatCmd gMountSide

    set gLowFormatCmd [$w.top.x.low.info.cmd get]

    set path [$w.top.x.dev.name get]
    set partno [$w.top.x.dev.par get]
    set vname [$w.top.x.vol.name get]

    if {[string length $partno] == 0} {
	set partno 0
    }

    watch_cursor $w
    destroy $w

    cd $gAppCWD

    if {$gLowFormat && [string length $gLowFormatCmd] > 0} {
	exec /bin/sh -c $gLowFormatCmd </dev/null >/dev/null
    }

    if {[string length $path] > 0} {
	hfs format $path $partno $vname
	mountdev $gMountSide $path
    }
}

proc help_about {} {
    global gThinFont gSmallFont gTitleFont

    set w [dialog "About" "OK"]

    set f [frame $w.top.t]

    regsub {\(C\)} [hfs copyright] "\251" copyright

    frame $f.banner
    label $f.banner.icon -bitmap macdaemon
    label $f.banner.title -text "xhfs" -font $gTitleFont
    pack $f.banner.icon $f.banner.title -side left -padx 2m

    frame $f.space -height 2m
    label $f.vers -text [hfs version] -font $gThinFont
    label $f.copy -text $copyright -font $gSmallFont
    label $f.auth -text [hfs author] -font $gSmallFont

    pack $f.banner $f.space $f.vers $f.copy $f.auth -side top
    pack $f -expand 1

    showdialog $w
}

proc help_license {} {
    alert note "Software License" [fixtext [hfs license]] 190
}

proc help_options {} {
    set text "\
	    Show All Files: Some files are normally hidden. To unhide\n\
	    invisible files on HFS volumes, or to unhide UNIX files which\n\
	    begin with a period, select this option.\n\n\
	    \
	    Ask Before Delete: To disable verification before deleting files\n\
	    and empty directories, deselect this option.\n\n\
	    \
	    Auto Mode Select: The transfer mode is normally selected\n\
	    automatically. To use manual selection, deselect this option.\n\n\
	    "

    alert note "Options" [fixtext $text] 150
}

proc help_modes {} {
    set text "\
	    MacBinary II: A popular format for binary file transfer. Both\n\
	    forks of the Macintosh file are preserved. This is the\n\
	    recommended mode for transferring arbitrary Macintosh files.\n\n\
	    \
	    BinHex: An alternative format for ASCII file transfer. Both\n\
	    forks of the Macintosh file are preserved.\n\n\
	    \
	    Text: Performs end-of-line translation. Only the data fork of\n\
	    the Macintosh file is copied.\n\n\
	    \
	    Raw Data: Performs no translation. Only the data fork of the\n\
	    Macintosh file is copied.\n\n\
	    \
	    Automatic: A mode will be chosen automatically for each\n\
	    file based on a set of predefined heuristics.\n\n\
	    "

    alert note "Transfer Modes" [fixtext $text]
}

proc hfs_automode {vol array} {
    upvar $array item

    if {$item(kind) == "directory"} {
	return auto
    } elseif {[regexp {^TEXT|ttro$} $item(type)]} {
	return text
    } elseif {$item(rsize) == 0} {
	return raw
    } else {
	return macb
    }
}

proc ufs_automode {vol array} {
    upvar $array item

    set name $item(name)

    if {$item(kind) == "directory"} {
	return auto
    } elseif {[regexp -nocase {\.bin$} $name]} {
	return macb
    } elseif {[regexp -nocase {\.hqx$} $name]} {
	return binh
    } elseif {[regexp -nocase {\.(txt|c|h|html?|rtf)$} $name]} {
	return text
    } elseif {[regexp -nocase {\.(sit|sea|cpt|tar|gz|Z|gif|jpg)$} $name]} {
	return raw
    } else {
	cd [$vol cwd]
	if {([catch {exec file -L $name} type] == 0 ||  \
		[catch {exec file $name} type] == 0) &&  \
		[regexp {text|commands} $type]} {
	    return text
	}

	return raw
    }
} 

proc watch_cursor {{w "."} {default "left_ptr"}} {
    $w config -cursor watch
    if {[string compare $w "."] != 0} {
	. config -cursor watch
    }
    update idletasks

    $w config -cursor $default
    if {[string compare $w "."] != 0} {
	. config -cursor left_ptr
    }
}

proc updatelist {side} {
    global gShowAllFiles
    global dir vol fstype

    set box $side.mid.lb
    $box delete 0 end

    if {! [info exists vol($side)]} {
	return
    }

    if {$fstype($side) == "hfs"} {
	set chartrans {nulltrans}	;# {hfs charlocal}
    } else {
	set chartrans {nulltrans}
    }

    set list [$vol($side) dir]
    set dir($side) {}

    set sepchar [$vol($side) sepchar]

    foreach ent $list {
	array set item $ent

	set name [eval $chartrans [list $item(name)]]

	if {$item(kind) == "directory"} {
	    set name "$name$sepchar"
	}

	if {$gShowAllFiles || [lsearch $item(flags) "invis"] < 0} {
	    $box insert end $name
	    lappend dir($side) $ent
	}
    }

    set m $side.mb.pop.m
    $m delete 0 end

    set path [$vol($side) path]
    set last 0

    foreach elt $path {
	set partial [abspath [$vol($side) sepchar] [lrange $path 0 $last]]
	incr last

	$m insert 0 command -label $elt -command [list mchdir $side $partial]
    }

    if {$fstype($side) == "hfs"} {
	set what "Folder"
    } else {
	set what "Directory"
    }

    foreach menuitem {
	{separator}
	{command -label "New $what..." -command [list mmkdir $side]}
    } {eval $m insert end $menuitem}

    global cwd
    set cwd($side) [lindex $path [expr [llength $path] - 1]]

    set avail [lindex [$vol($side) size] 1]

    $side.info.x.msg config -text "[size $avail] available"
}

proc clearlists {} {
    .l.mid.lb selection clear 0 end
    .r.mid.lb selection clear 0 end

    .m.1.ops.view config -state disabled -text "View"
    .m.1.ops.copy config -state disabled -text "Copy"

    .m.2.ops.info config -state disabled
    .m.2.ops.rena config -state disabled
    .m.2.ops.dele config -state disabled
}

proc mchdir {side path} {
    global vol

    watch_cursor

    $vol($side) chdir $path

    updatelist $side; clearlists
}

proc open_volume {side} {
    global gDefaultDevice gMountSide gTypeFont gThinFont

    set gMountSide $side

    set w [dialog "Open" "Cancel" "Open" 1]
    $w.bot.ok config -command [list open2 $w]

    frame $w.top.x

    set lbl1 [frame $w.top.x.lbl1]
    label $lbl1.path -text "Path to HFS Device"
    pack $lbl1.path

    set lbl2 [frame $w.top.x.lbl2]
    # label $lbl2.or -text "or"
    # label $lbl2.icon -bitmap sm_floppy
    label $lbl2.dev -text "or UNIX Directory:"
    pack $lbl2.dev -side left -padx 0.5m

    entry $w.top.x.entry -width 25 -font $gTypeFont

    frame $w.top.x.space -height 2m

    set mount [frame $w.top.x.mount]
    label $mount.lbl -text "Open:" -font $gThinFont
    radiobutton $mount.l -var gMountSide -value ".l"  \
	    -text "On Left" -font $gThinFont
    radiobutton $mount.r -var gMountSide -value ".r"  \
	    -text "On Right" -font $gThinFont
    pack $mount.lbl $mount.l $mount.r -side left

    pack $lbl1 $lbl2 $w.top.x.entry $w.top.x.space $mount  \
	    -side top -pady 0.5m

    pack $w.top.x -expand 1

    $w.top.x.entry insert end $gDefaultDevice
    $w.top.x.entry selection range 0 end

    focus $w.top.x.entry
    showdialog $w
}

proc open2 {w} {
    global gMountSide
    global vol

    set path [$w.top.x.entry get]

    watch_cursor $w
    destroy $w

    if {[string length $path] == 0} {
	error "missing path"
    }

    mountdev $gMountSide $path
}

proc close_volume {side} {
    global vol

    watch_cursor

    $vol($side) umount
    unset vol($side)

    pack forget $side.mb.pop $side.info.x.icon $side.info.x.lock
    $side.info.x.msg config -text ""

    $side.sub.open config -text "Open..." -command [list open_volume $side]
    $side.sub.info config -state disabled

    updatelist $side; clearlists
}

proc ctime {secs} {
    return [clock format $secs -format "%c"]
}

proc volume_info {side} {
    global gThinFont
    global vol dev fstype media

    set v $vol($side)

    if {$side == ".l"} {
	set eside "Left"
    } else {
	set eside "Right"
    }

    set w [window "$eside Info" "Info"]

    set locked [$v islocked]
    set icon $media($side)
    set vname [$v vname]

    if {$fstype($side) == "ufs"} {
	set ishfs 0
	set where [file dirname $vname]
	set vname [file tail $vname]
	set kind "UNIX Directory"
    } else {
	set ishfs 1
	set kind "HFS Volume"
	set where $dev($side)
	set crdate [$v crdate]
    }

    wm iconbitmap $w $icon
    wm iconmask $w ${icon}_mask

    frame $w.x
    pack $w.x -expand 1 -fill both -ipadx 4m -ipady 4m

    set f [frame $w.x.f]

    frame $f.name
    frame $f.name.space -width 5
    label $f.name.icon -bitmap $icon
    label $f.name.text -text $vname -font $gThinFont
    pack $f.name.space $f.name.icon $f.name.text -side left -padx 2m

    if {$locked} {
	label $f.name.lock -bitmap padlock
	pack $f.name.lock -side left
    }

    frame $f.space1 -height 4m

    frame $f.kind
    label $f.kind.lbl -width 9 -text "Kind: " -anchor e
    label $f.kind.val -text $kind -font $gThinFont
    pack $f.kind.lbl $f.kind.val -side left

    frame $f.size
    label $f.size.lbl -width 9 -text "Size: " -anchor e
    label $f.size.val -text [size [lindex [$v size] 0]] -font $gThinFont
    pack $f.size.lbl $f.size.val -side left

    frame $f.space2 -height 4m

    frame $f.where
    label $f.where.lbl -width 9 -text "Where: " -anchor e
    label $f.where.val -text $where -font $gThinFont
    pack $f.where.lbl $f.where.val -side left

    pack $f.name $f.space1 $f.kind $f.size $f.space2 $f.where -side top -fill x

    if {$ishfs} {
	frame $f.space3 -height 4m

	frame $f.cr
	label $f.cr.lbl -width 9 -text "Created: " -anchor e
	label $f.cr.val -text [ctime $crdate] -font $gThinFont
	pack $f.cr.lbl $f.cr.val -side left

	pack $f.space3 $f.cr -side top -fill x
    }

    frame $f.md
    label $f.md.lbl -width 9 -text "Modified: " -anchor e
    label $f.md.val -text [ctime [$v mddate]] -font $gThinFont
    pack $f.md.lbl $f.md.val -side left

    pack $f.md -side top -fill x

    pack $f -expand 1

    showwindow $w
}

proc mmkdir {side} {
    global gTypeFont
    global fstype

    if {$fstype($side) == "hfs"} {
	set what "folder"
	set title "New Folder"
    } else {
	set what "directory"
	set title "New Directory"
    }

    set w [dialog $title "Cancel" "Create" 1]
    $w.bot.ok config -command [list mmkdir2 $w $side $what]

    frame $w.top.x

    frame $w.top.x.lbl
    label $w.top.x.lbl.icon -bitmap sm_folder
    label $w.top.x.lbl.text -text "Name for new $what:"
    pack $w.top.x.lbl.icon $w.top.x.lbl.text -side left -padx 0.5m

    entry $w.top.x.entry -width 25 -font $gTypeFont -exportsel 0

    pack $w.top.x.lbl $w.top.x.entry -side top -pady 0.5m
    pack $w.top.x -expand 1

    $w.top.x.entry insert end "untitled $what"
    $w.top.x.entry selection range 0 end

    focus $w.top.x.entry
    showdialog $w
}

proc mmkdir2 {w side what} {
    global vol

    set name [$w.top.x.entry get]

    watch_cursor $w
    destroy $w

    if {[string length $name] == 0} {
	error "cannot create $what with empty name"
    }

    $vol($side) mkdir $name
    $vol($side) chdir $name

    updatelist $side; clearlists
}

proc plural {count single plural} {
    if {$count == 1} {
	return $single
    } else {
	return $plural
    }
}

proc nulltrans {str} {
    return $str
}

proc show_file {side fname} {
    global gTypeFont
    global vol fstype

    watch_cursor

    if {$fstype($side) == "hfs"} {
	set charset macroman
    } else {
	set charset latin1
    }

    # Make sure we can open the file before building the interface
    set fh [$vol($side) open $fname]

    set path [abspath [$vol($side) sepchar]  \
	    [concat [$vol($side) path] [list $fname]] 1]
    set w [window $path $fname]

    frame $w.x
    pack $w.x -expand 1 -fill both -padx 1m -pady 1m

    set f [frame $w.x.f]

    text $f.text -yscroll [list $f.scroll set]  \
	    -height 30 -width 80 -wrap word -font $gTypeFont
    scrollbar $f.scroll -orient vert -command [list $f.text yview]
    pack $f.scroll -side right -fill y
    pack $f.text -fill both -expand 1

    pack $f -fill both -expand 1

    watch_cursor $f.text xterm
    after idle "[list $f.text config -state disabled]; [list showwindow $w]"

    while {1} {
	set buf [$fh read 512]
	if {[string length $buf] == 0} {
	    $fh close
	    break
	}

	regsub -all "\r\n?" $buf "\n" buf

	$f.text insert end [hfs chartrans $charset latin1 $buf]
    }
}

proc commas {number} {
    while {[regexp  \
	    {([^,]+)(([0-9][0-9][0-9])(,[0-9][0-9][0-9])*([^0-9].*|$))}  \
	    $number ignore pre rest]} {
	set number "$pre,$rest"
    }

    return $number
}

proc size {bytes} {
    if {$bytes == -1} {
	return "unknown K"
    }

    set k 1024
    set mb [expr 1024 * $k]

    if {$bytes > $mb} {
	set q $mb
	set u "M"
    } else {
	set q $k
	set u "K"
    }

    set amount [commas [format "%.1f" [expr ${bytes}.0 / $q]]]

    regsub {\.0$} $amount "" amount
    regsub {^0$} $amount "zero " amount

    return "$amount$u"
}

proc show_info {vol list} {
    global gTypeFont gThinFont

    array set info $list

    set isdir [expr [string compare $info(kind) "directory"] == 0]
    set ishfs [info exists info(crdate)]

    if {$ishfs} {
	set chartrans {nulltrans}	;# {hfs charlocal}
    } else {
	set chartrans {nulltrans}
    }

    set name [eval $chartrans [list $info(name)]]
    set w [window "$name Info" $name]

    set icon "document"

    if {$isdir} {
	set icon "folder"

	if {$ishfs} {
	    set info(kind) "folder"
	}
    } else {
	if {$ishfs} {
	    if {[string compare $info(type) "APPL"] == 0} {
		set icon "application"
		set info(kind) "application"
	    } else {
		set info(kind) "document"
	    }
	} else {
	    cd [$vol cwd]
	    if {[file executable $info(name)]} {
		set icon "application"
	    }
	}
    }

    wm iconbitmap $w $icon
    wm iconmask $w ${icon}_mask

    if {$ishfs} {
	set where "[join [$vol path] ":"]:"
    } else {
	set where [$vol cwd]

	cd $where
	if {[catch {exec file $info(name)} type] == 0} {
	    if {[string first "$info(name):" $type] == 0} {
		set type [string range $type  \
			[expr [string length $info(name)] + 1] end]
	    }

	    regsub "^\[ \t]*" $type "" info(kind)
	}
    }

    frame $w.x
    pack $w.x -expand 1 -fill both -ipadx 4m -ipady 4m

    set f [frame $w.x.f]

    frame $f.name
    frame $f.name.space -width 5
    label $f.name.icon -bitmap $icon
    label $f.name.text -text $name -font $gThinFont
    pack $f.name.space $f.name.icon $f.name.text -side left -padx 2m

    if {[lsearch $info(flags) "locked"] >= 0} {
	label $f.name.lock -bitmap padlock
	pack $f.name.lock -side left
    }

    frame $f.space1 -height 4m

    frame $f.kind
    label $f.kind.lbl -width 9 -text "Kind: " -anchor e
    label $f.kind.val -text $info(kind) -font $gThinFont
    pack $f.kind.lbl $f.kind.val -side left

    set label "Size: "
    if {$isdir} {
	set label "Contains: "
	set size $info(size)
	set what [plural $size item items]
    } elseif {$ishfs} {
	set size [list $info(rsize) $info(dsize)]
	set what [list  \
		[plural $info(rsize) byte bytes]  \
		[plural $info(dsize) byte bytes]]
    } else {
	set size $info(size)
	set what [plural $size byte bytes]
    }

    frame $f.size

    if {[llength $size] == 1} {
	label $f.size.lbl -width 9 -text $label -anchor e
	label $f.size.val -text "[commas $size] $what" -font $gThinFont
	pack $f.size.lbl $f.size.val -side left
    } else {
	set rsize [lindex $size 0]
	set rwhat [lindex $what 0]
	set dsize [lindex $size 1]
	set dwhat [lindex $what 1]

	frame $f.size.r
	label $f.size.r.lbl -width 9 -text "Resource: " -anchor e
	label $f.size.r.val -text "[commas $rsize] $rwhat" -font $gThinFont
	pack $f.size.r.lbl $f.size.r.val -side left

	frame $f.size.d
	label $f.size.d.lbl -width 9 -text "Data: " -anchor e
	label $f.size.d.val -text "[commas $dsize] $dwhat" -font $gThinFont
	pack $f.size.d.lbl $f.size.d.val -side left

	pack $f.size.r $f.size.d -side top -fill x
    }

    frame $f.space2 -height 4m

    frame $f.where
    label $f.where.lbl -width 9 -text "Where: " -anchor e
    label $f.where.val -text $where -font $gThinFont
    pack $f.where.lbl $f.where.val -side left

    pack $f.name $f.space1 $f.kind $f.size $f.space2 $f.where -side top -fill x

    if {$ishfs && ! $isdir} {
	frame $f.space3 -height 4m

	frame $f.type
	label $f.type.lbl -width 9 -text "Type: " -anchor e
	label $f.type.val -text $info(type) -font $gTypeFont
	pack $f.type.lbl $f.type.val -side left

	frame $f.crea
	label $f.crea.lbl -width 9 -text "Creator: " -anchor e
	label $f.crea.val -text $info(creator) -font $gTypeFont
	pack $f.crea.lbl $f.crea.val -side left

	pack $f.space3 $f.type $f.crea -side top -fill x
    }

    if {$ishfs} {
	frame $f.space4 -height 4m

	frame $f.cr
	label $f.cr.lbl -width 9 -text "Created: " -anchor e
	label $f.cr.val -text [ctime $info(crdate)] -font $gThinFont
	pack $f.cr.lbl $f.cr.val -side left

	pack $f.space4 $f.cr -side top -fill x
    }

    frame $f.md
    label $f.md.lbl -width 9 -text "Modified: " -anchor e
    label $f.md.val -text [ctime $info(mddate)] -font $gThinFont
    pack $f.md.lbl $f.md.val -side left

    pack $f.md -side top -fill x

    pack $f -expand 1

    showwindow $w
}

proc abspath {sepchar path {asfile 0}} {
    switch $sepchar {
	":" {
	    if {$asfile} {
		return [join $path ":"]
	    } else {
		return "[join $path ":"]:"
	    }
	}

	"/" {
	    if {[string compare [lindex $path 0] "/"] == 0} {
		set path [lreplace $path 0 0 ""]
	    }

	    if {[llength $path] == 1 &&  \
		    [string length [lindex $path 0]] == 0} {
		return "/"
	    } else {
		return [join $path "/"]
	    }
	}
    }
}

proc ufs_handle {dir} {
    set handle "ufsvol[unique]"

    global ufsdir

    cd $dir
    set ufsdir($handle) [pwd]

    proc $handle {verb args} {
	set handle [lindex [info level [info level]] 0]
	global gAppCWD gDfCmd
	global ufsdir

	set cwd $ufsdir($handle)

	switch -glob $verb {
	    vname {
		return $cwd
	    }

	    umount {
		cd $gAppCWD

		unset ufsdir($handle)
		rename $handle ""
	    }

	    path {
		set path [split $cwd "/"]

		if {[string compare [lindex $path 0] ""] == 0} {
		    set path [lreplace $path 0 0 "/"]
		}

		if {[llength $path] == 2 &&  \
			[string length [lindex $path 1]] == 0} {
		    set path [lreplace $path 1 1]
		}

		return $path
	    }

	    dir {
		cd $cwd

		set list {}
		set files [lsort [glob .* *]]

		foreach name $files {
		    if {[string compare $name "."] == 0 ||  \
			    [string compare $name ".."] == 0} {
			continue
		    }

		    if {[catch {file stat $name stat}]} {
			continue
		    }

		    set kind [file type $name]
		    set mddate $stat(mtime)

		    if {[file isdirectory $name]} {
			set kind "directory"
			set size "?"
			catch {
			    set size [expr [llength  \
				    [glob $name/.* $name/*]] - 2]
			}
		    } elseif {$kind == "file"} {
			set size $stat(size)
		    } else {
			continue
		    }

		    set flags {}

		    if {! [file writable $name]} {
			lappend flags "locked"
		    }

		    if {[regexp {^\.} $name]} {
			lappend flags "invis"
		    }

		    lappend list [list  \
			    name $name  \
			    kind $kind  \
			    size $size  \
			    flags $flags  \
			    mddate $mddate]
		}

		return $list
	    }

	    chdir {
		set path [lindex $args 0]

		if {! [regexp {^/} $path]} {
		    cd $cwd
		}

		cd $path
		set ufsdir($handle) [pwd]
	    }

	    cwd {
		return $cwd
	    }

	    sepchar {
		return "/"
	    }

	    mkdir {
		set path [lindex $args 0]

		cd $cwd
		exec mkdir $path
	    }

	    rmdir {
		set path [lindex $args 0]

		cd $cwd
		exec rmdir $path
	    }

	    open {
		set path [lindex $args 0]

		cd $cwd
		set fh [open $path]

		proc $fh {verb args} {
		    set fh [lindex [info level [info level]] 0]

		    switch -glob $verb {
			read {
			    set len [lindex $args 0]

			    return [read $fh $len]
			}

			close {
			    close $fh
			    rename $fh ""
			}

			* {
			    error "unknown call to $fh $verb $args"
			}
		    }
		}

		return $fh
	    }

	    copy {
		set path [lindex $args 0]
		set dest [lindex $args 1]

		cd $cwd
		exec cp -f $path $dest
	    }

	    rename {
		set path [lindex $args 0]
		set dest [lindex $args 1]

		cd $cwd
		exec mv -f $path $dest
	    }

	    delete {
		set path [lindex $args 0]

		cd $cwd
		exec rm -f $path
	    }

	    size {
		if {[catch {eval exec [join $gDfCmd " "] [list $cwd]} info]} {
		    return [list -1 -1]
		}

		set s "\[ \t]+"
		set d "\[0-9]+"

		if {[catch {
		    regexp "(.*)\n(\[^\n]*)\$" $info ignore head data
		    regexp "${s}($d)$s$d${s}($d)" $data ignore size avail
		}]} {
		    return [list -1 -1]
		}

		set block 512
		if {[regexp -nocase {kbyte|1024} $head]} {
		    set block 1024
		}

		return [list [expr $size * $block] [expr $avail * $block]]
	    }

	    mddate {
		return [file mtime $cwd]
	    }

	    islocked {
		return 0
	    }

	    * {
		error "unknown call to $handle $verb $args"
	    }
	}
    }

    return $handle
}

proc ufs {verb args} {
    switch $verb {
	mount {
	    set dir [lindex $args 0]

	    return [ufs_handle $dir]
	}

	* {
	    error "bad arg 1 to ufs"
	}
    }
}

proc mountdev {side device} {
    global gAppCWD gXMode
    global vol dev fstype media

    if {[info exists vol($side)]} {
	$vol($side) umount
    }

    cd $gAppCWD

    if {! [file exists $device]} {
	error "can't open $device (no such file or directory)"
    }

    if {[file isdirectory $device]} {
	set type ufs
    } else {
	set type hfs
    }

    set v [$type mount $device]
    set fstype($side) $type
    set locked [$v islocked]

    if {$type == "ufs"} {
	set icon "folder"
    } else {
	set size [lindex [$v size] 0]

	if {$size <= [expr 1440 * 1024]} {
	    set icon "floppy"
	} elseif {$locked &&  \
		$size >= [expr 100 * 1024 * 1024] &&  \
		$size <= [expr 800 * 1024 * 1024]} {
	    set icon "cdrom"
	} else {
	    set icon "harddisk"
	}
    }

    $side.info.x.icon config -bitmap sm_$icon
    pack $side.info.x.icon -before $side.info.x.msg -side left -padx 0.5m

    if {$locked} {
	pack $side.info.x.lock -side left -padx 0.5m
    } else {
	pack forget $side.info.x.lock
    }

    pack $side.mb.pop

    $side.sub.open config -text "Close" -command [list close_volume $side]
    $side.sub.info config -state normal

    set vol($side) $v
    set dev($side) $device
    set media($side) $icon

    updatelist $side; clearlists

    if {$fstype(.l) == $fstype(.r)} {
	set state disabled
	set gXMode auto
    } else {
	set state normal
    }

    foreach mode {auto macb binh text raw} {
	.m.3.mode.$mode config -state $state
    }
}

proc flushtask {} {
    global gFlushInterval

    after $gFlushInterval flushtask

    watch_cursor
    hfs flushall
}

###############################################################################

# Apparently some versions of Tk don't grok this
if {$argc > 0 && [string compare [lindex $argv 0] "--"] == 0} {
    incr argc -1
    set argv [lreplace $argv 0 0]
}

if {[string compare [lindex $argv 0] "--license"] == 0} {
    puts -nonewline stdout "\n[hfs license]"
    exit
}

if {[string compare [lindex $argv 0] "--version"] == 0} {
    puts stdout "[hfs version] - [hfs copyright]"
    puts stdout "`$argv0 --license' for licensing information."
    exit
}

###############################################################################

# Build the user interface

proc makelist {w click other} {
    global gSmallFont

    frame $w

    frame $w.mb -height 30
    menubutton $w.mb.pop -textvariable cwd($w)  \
	    -indicatoron 1 -menu $w.mb.pop.m  \
	    -relief raised -bd 2 -padx 4p -pady 4p  \
	    -highlightthickness 2 -anchor c
    menu $w.mb.pop.m -tearoff 0

    frame $w.info -height 16
    pack propagate $w.info 0

    frame $w.info.x
    pack $w.info.x -expand 1

    label $w.info.x.icon
    label $w.info.x.msg -font $gSmallFont -text ""
    label $w.info.x.lock -bitmap padlock

    pack $w.info.x.msg -side left -padx 0.5m

    frame $w.mid -bd 0

    listbox $w.mid.lb -xscroll "$w.bot.sb set" -yscroll "$w.mid.sb set"  \
	    -exportselection 0 -height 16 -width 25 -selectmode extended
    scrollbar $w.mid.sb -orient vert -command [list $w.mid.lb yview]

    bind $w.mid.lb <Button-1> [list $other selection clear 0 end]
    bind $w.mid.lb <KeyPress> [list $other selection clear 0 end]
    bind $w.mid.lb <ButtonRelease-1> [list wclick $w $click]
    bind $w.mid.lb <KeyRelease> [list wclick $w $click]
    bind $w.mid.lb <Double-ButtonRelease-1> ".m.1.ops.view invoke"
    bind $w.mid.lb <Key-Return> ".m.1.ops.view invoke"

    pack $w.mid.sb -side right -fill y
    pack $w.mid.lb -side right -fill both -expand 1

    frame $w.bot

    scrollbar $w.bot.sb -orient horiz -command [list $w.mid.lb xview]
    frame $w.bot.c -height 24 -width 24

    # label $w.bot.c.icon
    # pack $w.bot.c.icon -expand 1
    # pack propagate $w.bot.c 0

    pack $w.bot.c -side right
    pack $w.bot.sb -side bottom -fill x

    frame $w.sub -relief sunken -bd 1

    frame $w.sub.space1 -width 1m
    button $w.sub.open -width 8 -text "Open..."  \
	    -command [list open_volume $w]
    button $w.sub.info -width 8 -text "Volume Info" -state disabled  \
	    -command [list volume_info $w]

    frame $w.sub.space2 -width 1m
    pack $w.sub.space1 -side left
    pack $w.sub.open $w.sub.info  \
	    -side left -pady 1m -padx 0.5m -fill x -expand 1
    pack $w.sub.space2 -side left

    frame $w.space -height 2m

    pack $w.space -side bottom
    pack $w.mb -side top
    pack $w.sub -side bottom -fill x -pady 1m -ipadx 2m
    pack $w.info $w.bot -side bottom -fill x
    pack $w.mid -side top -fill both -expand 1
}

proc wclick {side copytext} {
    set box $side.mid.lb
    set sel [$box curselection]

    global gXMode gAutoModeSelect
    global vol dir fstype

    if {$side == ".l"} {
	set other ".r"
    } else {
	set other ".l"
    }

    set cstate normal
    if {! [info exists vol($other)]} {
	set cstate disabled
    }

    if {[llength $sel] == 0} {
	.m.1.ops.view config -state disabled -text "View"
	.m.1.ops.copy config -state disabled -text "Copy"

	.m.2.ops.info config -state disabled
	.m.2.ops.rena config -state disabled
	.m.2.ops.dele config -state disabled
    } elseif {[llength $sel] == 1} {
	array set item [lindex $dir($side) [lindex $sel 0]]

	switch -glob $item(kind) {
	    directory {
		set viewtext "Open"
		set vstate normal
	    }

	    file {
		set viewtext "View"
		set mode [$fstype($side)_automode $vol($side) item]

		if {$mode == "text" || $mode == "binh"} {
		    set vstate normal
		} else {
		    set vstate disabled
		}
	    }
	}

	.m.1.ops.view config -state $vstate -text $viewtext  \
		-command [list do_open $side]
	.m.1.ops.copy config -state $cstate -text $copytext  \
		-command [list do_copy $side $other]

	.m.2.ops.info config -state normal -command [list do_info $side]
	.m.2.ops.rena config -state normal -command [list do_rename $side]
	.m.2.ops.dele config -state normal -command [list do_delete $side]

	if {$gAutoModeSelect &&  \
		[.m.3.mode.auto cget -state] != "disabled"} {
	    if {[info exists mode]} {
		set gXMode $mode
	    } else {
		set gXMode [$fstype($side)_automode $vol($side) item]
	    }
	}
    } else {
	.m.1.ops.view config -state disabled -text "View"
	.m.1.ops.copy config -state $cstate -text $copytext  \
		-command [list do_copy $side $other]

	.m.2.ops.info config -state normal -command [list do_info $side]
	.m.2.ops.rena config -state disabled
	.m.2.ops.dele config -state normal -command [list do_delete $side]

	if {$gAutoModeSelect &&  \
		[.m.3.mode.auto cget -state] != "disabled"} {
	    foreach ind $sel {
		array set item [lindex $dir($side) $ind]
		set auto [$fstype($side)_automode $vol($side) item]

		if {[info exists mode]} {
		    if {$mode != $auto} {
			set mode auto
			break
		    }
		} else {
		    set mode $auto
		}
	    }

	    set gXMode $mode
	}
    }
}


# Middle controls

frame .m

frame .m.1
frame .m.1.ops -relief sunken -bd 1
frame .m.1.ops.space1 -height 0.5m
button .m.1.ops.view -state disabled -text "View"
button .m.1.ops.copy -state disabled -text "Copy"
frame .m.1.ops.space2 -height 0.5m

pack  \
	.m.1.ops.space1  \
	.m.1.ops.view .m.1.ops.copy  \
	.m.1.ops.space2  \
	-side top -fill x -padx 1m -pady 0.5m
pack .m.1.ops -side top -fill x

frame .m.2
frame .m.2.ops -relief sunken -bd 1
frame .m.2.ops.space1 -height 0.5m
button .m.2.ops.info -state disabled -text "Get Info"
button .m.2.ops.rena -state disabled -text "Rename..."
button .m.2.ops.dele -state disabled -text "Delete"
frame .m.2.ops.space2 -height 0.5m

pack  \
	.m.2.ops.space1  \
	.m.2.ops.info .m.2.ops.rena .m.2.ops.dele  \
	.m.2.ops.space2  \
	-side top -fill x -padx 1m -pady 0.5m
pack .m.2.ops -side top -fill x

frame .m.3
label .m.3.l -text "Mode"
frame .m.3.mode -relief groove -bd 2

frame .m.3.mode.space1 -height 1m
radiobutton .m.3.mode.macb -font $gThinFont -text "MacBinary II"  \
	-var gXMode -value macb -anchor w
radiobutton .m.3.mode.binh -font $gThinFont -text "BinHex"  \
	-var gXMode -value binh -anchor w
radiobutton .m.3.mode.text -font $gThinFont -text "Text"  \
	-var gXMode -value text -anchor w
radiobutton .m.3.mode.raw -font $gThinFont -text "Raw Data"  \
	-var gXMode -value raw -anchor w
radiobutton .m.3.mode.auto -font $gThinFont -text "Automatic"  \
	-var gXMode -value auto -anchor w
frame .m.3.mode.space2 -height 1m

set gXMode auto

pack  \
	.m.3.mode.space1  \
	.m.3.mode.macb  \
	.m.3.mode.binh  \
	.m.3.mode.text  \
	.m.3.mode.raw  \
	.m.3.mode.auto  \
	.m.3.mode.space2  \
	-side top -fill x -padx 1m
pack .m.3.l .m.3.mode -side top -fill x

pack .m.1 .m.2 .m.3 -side top -fill x -padx 2m -pady 2m


# Menu bar

frame .mb -relief raised -bd 2
frame .mbpad -height 3m
menubutton .mb.file -text "File" -menu .mb.file.m
menubutton .mb.opts -text "Options" -menu .mb.opts.m
menubutton .mb.help -bitmap help -menu .mb.help.m

pack .mb.file .mb.opts -side left
pack .mb.help -side right -fill y -ipadx 2m

menu .mb.file.m -tearoff 0
foreach menuitem {
    {command -label "Open..." -command {open_volume $gMountSide}}
    {separator}
    {command -label "Format Disk..." -command "format_disk"}
    {command -label "Eject Disk" -command "hfs flushall; exec eject"}
    {separator}
    {command -label "Quit" -command "destroy ."}
} {eval .mb.file.m add $menuitem}

menu .mb.opts.m -tearoff 0
foreach menuitem {
    {checkbutton -label "Show All Files" -variable gShowAllFiles  \
	    -command "watch_cursor; updatelist .l; updatelist .r; clearlists"}
    {separator}
    {checkbutton -label "Ask Before Delete" -variable gAskDelete}
    {checkbutton -label "Auto Mode Select" -variable gAutoModeSelect}
} {eval .mb.opts.m add $menuitem}

set gAutoModeSelect 1
set gAskDelete 1

menu .mb.help.m -tearoff 0
foreach menuitem {
    {command -label "About" -command "help_about"}
    {command -label "Software License" -command "help_license"}
    {separator}
    {command -label "Options" -command "help_options"}
    {command -label "Transfer Modes" -command "help_modes"}
} {eval .mb.help.m add $menuitem}


# Put it together

makelist .l ">> Copy >>" .r.mid.lb
makelist .r "<< Copy <<" .l.mid.lb

rename makelist ""

pack .mb .mbpad -side top -fill x
pack .l -side left -fill both -expand 1 -padx 3m
pack .r -side right -fill both -expand 1 -padx 3m
pack .m -side left

wm title . "Macintosh HFS Volume Browser"

wm iconbitmap . macdaemon
wm iconmask . macdaemon_mask
wm iconname . "HFS Browser"

. config -cursor left_ptr

###############################################################################

tk appname xhfs

set gAppCWD [pwd]

set fstype(.l) none
set fstype(.r) none

if {$argc > 2} {
    puts stderr "Usage: $argv0 \[left-vol \[right-vol]]"
    exit 1
}

if {$argc > 0} {
    mountdev .l [lindex $argv 0]

    if {$argc > 1} {
	mountdev .r [lindex $argv 1]
    }
}

if {$argc < 2} {
    mountdev .r .
}

after $gFlushInterval flushtask