Skip to content

Commit 7f75eec

Browse files
Version 1.5.3
- Properly quote directories for the system command when tree is relaunched using the -R option. - Fixed possible indentation problem if dirs[*] is not properly zeroed (Martin Nagy). - Use strcoll() instead of strcmp() to sort files based on locale if set. - Change "const static" to "static const" to remove some compiler warnings for Solaris (Kamaraju Kusumanchi). - Actually use TREE_CHARSET if it's defined. - Automatically select UTF-8 charset if TREE_CHARSET is not set, and the locale is set to *UTF-8 (overridden with --charset option.)
1 parent d9622fa commit 7f75eec

File tree

8 files changed

+339
-74
lines changed

8 files changed

+339
-74
lines changed

CHANGES

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
Version 1.5.3
2+
- Properly quote directories for the system command when tree is relaunched
3+
using the -R option.
4+
- Fixed possible indentation problem if dirs[*] is not properly zeroed
5+
(Martin Nagy).
6+
- Use strcoll() instead of strcmp() to sort files based on locale if set.
7+
- Change "const static" to "static const" to remove some compiler warnings
8+
for Solaris (Kamaraju Kusumanchi).
9+
- Actually use TREE_CHARSET if it's defined.
10+
- Automatically select UTF-8 charset if TREE_CHARSET is not set, and the
11+
locale is set to *UTF-8 (overridden with --charset option.)
12+
13+
Version 1.5.2.2
14+
- Set locale before checking MB_CUR_MAX.
15+
- Added HP-NonStop platform support (Craig McDaniel <craigmcd@gmail.com>)
16+
- Fixed to support 32 bit UID/GIDs.
17+
- Added Solaris build options to Makefile (edit and uncomment to use).
18+
Provided by Wang Quanhong
19+
20+
Version 1.5.2.1
21+
- Added strverscmp.c file for os's without strverscmp. Source file is
22+
attributed to: Jean-Franois Bignolles <bignolle@ecoledoc.ibp.fr>
23+
- Try different approach to MB_CUR_MAX problem.
24+
- Changed the argument to printit() to be signed char to avoid warnings.
25+
126
Version 1.5.2
227
- Added --filelimit X option to not descend directories that have more than
328
X number of files in them.

INSTALL

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Installation instructions:
2+
3+
1. Edit the Makefile for your OS. Comment out the linux options and uncomment
4+
the options for your OS.
5+
2. Type: make
6+
3. Type: make install
7+
8+
9+
I cannot test on non-linux machines, so please feel free to contribute
10+
porting information, bug reports, compile options, patches, etc for porting to
11+
other OS's to ice@mama.indstate.edu.

Makefile

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,88 @@
11
# $Copyright: $
2-
# Copyright (c) 1996 - 2008 by Steve Baker
2+
# Copyright (c) 1996 - 2009 by Steve Baker
33
# All Rights reserved
44
#
55
# This software is provided as is without any express or implied
66
# warranties, including, without limitation, the implied warranties
77
# of merchant-ability and fitness for a particular purpose.
88

9+
prefix = /usr
10+
911
CC=gcc
10-
#CFLAGS=-ggdb -Wall -DLINUX -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
11-
CFLAGS=-O2 -Wall -fomit-frame-pointer -DLINUX -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
12-
LDFLAGS=-s
12+
13+
VERSION=1.5.3
14+
TREE_DEST=tree
15+
BINDIR=${prefix}/bin
16+
MAN=tree.1
17+
MANDIR=${prefix}/man/man1
18+
19+
# Uncomment options below for your particular OS:
20+
21+
# Linux defaults:
22+
CFLAGS=-ggdb -Wall -DLINUX -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
23+
#CFLAGS=-O2 -Wall -fomit-frame-pointer -DLINUX -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
24+
#LDFLAGS=-s
1325

1426
# Uncomment for FreeBSD:
15-
#CC=gcc
1627
#CFLAGS=-O2 -Wall -fomit-frame-pointer
1728
#LDFLAGS=-s
29+
#XOBJS=strverscmp.o
30+
31+
# Uncomment for Solaris:
32+
#CC=cc
33+
#CFLAGS=-xO0 -v
34+
#LDFLAGS=
35+
#XOBJS=strverscmp.o
36+
#MANDIR=${prefix}/share/man/man1
1837

1938
# Uncomment for Cygwin:
20-
#CC=gcc
2139
#CFLAGS=-O2 -Wall -fomit-frame-pointer -DCYGWIN
2240
#LDFLAGS=-s
2341
#TREE_DEST=tree.exe
24-
## Comment out TREE_DEST definition below as well for Cygwin
42+
#XOBJS=strverscmp.o
2543

2644
# Uncomment for OS X:
2745
#CC=cc
2846
#CFLAGS=-O2 -Wall -fomit-frame-pointer -no-cpp-precomp
2947
#LDFLAGS=
48+
#XOBJS=strverscmp.o
3049

3150
# Uncomment for HP/UX:
3251
#CC=cc
3352
#CFLAGS=-Ae +O2 +DAportable -Wall
3453
#LDFLAGS=
54+
#XOBJS=strverscmp.o
3555

3656
# Uncomment for OS/2:
37-
#CC=gcc
3857
#CFLAGS=-02 -Wall -fomit-frame-pointer -Zomf -Zsmall-conv
3958
#LDFLAGS=-s -Zomf -Zsmall-conv
59+
#XOBJS=strverscmp.o
4060

41-
prefix = /usr
61+
# Uncomment for HP NonStop:
62+
#prefix = /opt
63+
#CC=c89
64+
#CFLAGS=-Wextensions -WIEEE_float -g -Wnowarn=1506 -D_XOPEN_SOURCE_EXTENDED=1 \
65+
# -Wallow_cplusplus_comments
66+
#LDFLAGS=
4267

43-
VERSION=1.5.2
44-
TREE_DEST=tree
45-
BINDIR=${prefix}/bin
46-
MAN=tree.1
47-
MANDIR=${prefix}/man/man1
68+
69+
#------------------------------------------------------------
4870

4971
all: tree
5072

51-
tree: tree.o
52-
$(CC) $(LDFLAGS) -o $(TREE_DEST) tree.o
73+
tree: tree.o $(XOBJS)
74+
$(CC) $(LDFLAGS) -o $(TREE_DEST) tree.o $(XOBJS)
5375

5476
tree.o: tree.c
5577

78+
strverscmp.o: strverscmp.c
79+
5680
clean:
5781
if [ -x $(TREE_DEST) ]; then rm $(TREE_DEST); fi
5882
if [ -f tree.o ]; then rm *.o; fi
5983
rm -f *~
6084

61-
install:
85+
install: tree
6286
install -d $(BINDIR)
6387
install -d $(MANDIR)
6488
if [ -e $(TREE_DEST) ]; then \

README

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
Please read the INSTALL file for installation instructions, particularly if
2+
you are installing on a non-linux machine.
3+
14
This is a handy little utility to display a tree view of directories that
25
I wrote some time ago and just added color support to. I've decided that
36
since no one else has done something simular I would go ahead and release
@@ -103,6 +106,26 @@ Mark Braker
103106
Michael Vogt
104107
- Suggested -v option (version sort).
105108

109+
Wang Quanhong
110+
- Provided build options for Solaris.
111+
112+
Craig McDaniel
113+
- Provided build options and source mods for HP NonStop support.
114+
115+
Christian Grigis
116+
- Noted that setlocale() should come before MB_CUR_MAX check.
117+
118+
Kamaraju Kusumanchi
119+
- Submitted patch to remove compiler warnings for Solaris.
120+
121+
Martin Nagy
122+
- Provided patch which fixes issue where indent may output more than it
123+
should when dirs[*] is not properly cleared before use.
124+
125+
William C. Lathan III
126+
- Showed that tree was not properly quoting arguments to recursively called
127+
tree instances when using -R.
128+
106129
And many others whom I've failed to keep track of. I should have started
107130
this list years ago.
108131

man/tree.1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.\" $Copyright: $
2-
.\" Copyright (c) 1996 - 2007 by Steve Baker
2+
.\" Copyright (c) 1996 - 2009 by Steve Baker
33
.\" All Rights reserved
44
.\"
55
.\" This program is free software; you can redistribute it and/or modify
@@ -17,7 +17,7 @@
1717
.\" Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1818
.\"
1919
...
20-
.TH TREE 1 "\*(V)" "Tree 1.5.2"
20+
.TH TREE 1 "\*(V)" "Tree 1.5.3"
2121
.SH NAME
2222
tree \- list contents of directories in a tree-like format.
2323
.SH SYNOPSIS
@@ -255,7 +255,7 @@ on files and directories beneath the directory it is printing.
255255

256256
The -h option rounds to the nearest whole number unlike the ls implementation
257257
of -h which rounds up always. The IEC standard names for powers of 2
258-
cooresponding to metric powers of 10 (KiBi, et al.) are gay.
258+
cooresponding to metric powers of 10 (KiBi, et al.) are silly.
259259

260260
Pruning files and directories with the -I, -P and --filelimit options will
261261
lead to incorrect file/directory count reports.

strverscmp.c

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/* Compare strings while treating digits characters numerically.
2+
Copyright (C) 1997, 2002, 2005 Free Software Foundation, Inc.
3+
This file is part of the libiberty library.
4+
Contributed by Jean-François Bignolles <bignolle@ecoledoc.ibp.fr>, 1997.
5+
6+
Libiberty is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
Libiberty is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with the GNU C Library; if not, write to the Free
18+
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19+
02110-1301 USA. */
20+
21+
/* #include "libiberty.h" */
22+
/* #include "safe-ctype.h" */
23+
#include <ctype.h>
24+
25+
/*
26+
@deftypefun int strverscmp (const char *@var{s1}, const char *@var{s2})
27+
The @code{strverscmp} function compares the string @var{s1} against
28+
@var{s2}, considering them as holding indices/version numbers. Return
29+
value follows the same conventions as found in the @code{strverscmp}
30+
function. In fact, if @var{s1} and @var{s2} contain no digits,
31+
@code{strverscmp} behaves like @code{strcmp}.
32+
33+
Basically, we compare strings normally (character by character), until
34+
we find a digit in each string - then we enter a special comparison
35+
mode, where each sequence of digits is taken as a whole. If we reach the
36+
end of these two parts without noticing a difference, we return to the
37+
standard comparison mode. There are two types of numeric parts:
38+
"integral" and "fractional" (those begin with a '0'). The types
39+
of the numeric parts affect the way we sort them:
40+
41+
@itemize @bullet
42+
@item
43+
integral/integral: we compare values as you would expect.
44+
45+
@item
46+
fractional/integral: the fractional part is less than the integral one.
47+
Again, no surprise.
48+
49+
@item
50+
fractional/fractional: the things become a bit more complex.
51+
If the common prefix contains only leading zeroes, the longest part is less
52+
than the other one; else the comparison behaves normally.
53+
@end itemize
54+
55+
@smallexample
56+
strverscmp ("no digit", "no digit")
57+
@result{} 0 // @r{same behavior as strcmp.}
58+
strverscmp ("item#99", "item#100")
59+
@result{} <0 // @r{same prefix, but 99 < 100.}
60+
strverscmp ("alpha1", "alpha001")
61+
@result{} >0 // @r{fractional part inferior to integral one.}
62+
strverscmp ("part1_f012", "part1_f01")
63+
@result{} >0 // @r{two fractional parts.}
64+
strverscmp ("foo.009", "foo.0")
65+
@result{} <0 // @r{idem, but with leading zeroes only.}
66+
@end smallexample
67+
68+
This function is especially useful when dealing with filename sorting,
69+
because filenames frequently hold indices/version numbers.
70+
@end deftypefun
71+
72+
*/
73+
74+
/* states: S_N: normal, S_I: comparing integral part, S_F: comparing
75+
fractional parts, S_Z: idem but with leading Zeroes only */
76+
#define S_N 0x0
77+
#define S_I 0x4
78+
#define S_F 0x8
79+
#define S_Z 0xC
80+
81+
/* result_type: CMP: return diff; LEN: compare using len_diff/diff */
82+
#define CMP 2
83+
#define LEN 3
84+
85+
86+
/* Compare S1 and S2 as strings holding indices/version numbers,
87+
returning less than, equal to or greater than zero if S1 is less than,
88+
equal to or greater than S2 (for more info, see the Glibc texinfo doc). */
89+
90+
int
91+
strverscmp (const char *s1, const char *s2)
92+
{
93+
const unsigned char *p1 = (const unsigned char *) s1;
94+
const unsigned char *p2 = (const unsigned char *) s2;
95+
unsigned char c1, c2;
96+
int state;
97+
int diff;
98+
99+
/* Symbol(s) 0 [1-9] others (padding)
100+
Transition (10) 0 (01) d (00) x (11) - */
101+
static const unsigned int next_state[] =
102+
{
103+
/* state x d 0 - */
104+
/* S_N */ S_N, S_I, S_Z, S_N,
105+
/* S_I */ S_N, S_I, S_I, S_I,
106+
/* S_F */ S_N, S_F, S_F, S_F,
107+
/* S_Z */ S_N, S_F, S_Z, S_Z
108+
};
109+
110+
static const int result_type[] =
111+
{
112+
/* state x/x x/d x/0 x/- d/x d/d d/0 d/-
113+
0/x 0/d 0/0 0/- -/x -/d -/0 -/- */
114+
115+
/* S_N */ CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
116+
CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
117+
/* S_I */ CMP, -1, -1, CMP, +1, LEN, LEN, CMP,
118+
+1, LEN, LEN, CMP, CMP, CMP, CMP, CMP,
119+
/* S_F */ CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
120+
CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
121+
/* S_Z */ CMP, +1, +1, CMP, -1, CMP, CMP, CMP,
122+
-1, CMP, CMP, CMP
123+
};
124+
125+
if (p1 == p2)
126+
return 0;
127+
128+
c1 = *p1++;
129+
c2 = *p2++;
130+
/* Hint: '0' is a digit too. */
131+
state = S_N | ((c1 == '0') + (isdigit (c1) != 0));
132+
133+
while ((diff = c1 - c2) == 0 && c1 != '\0')
134+
{
135+
state = next_state[state];
136+
c1 = *p1++;
137+
c2 = *p2++;
138+
state |= (c1 == '0') + (isdigit (c1) != 0);
139+
}
140+
141+
state = result_type[state << 2 | (((c2 == '0') + (isdigit (c2) != 0)))];
142+
143+
switch (state)
144+
{
145+
case CMP:
146+
return diff;
147+
148+
case LEN:
149+
while (isdigit (*p1++))
150+
if (!isdigit (*p2++))
151+
return 1;
152+
153+
return isdigit (*p2) ? -1 : diff;
154+
155+
default:
156+
return state;
157+
}
158+
}

0 commit comments

Comments
 (0)