tree-1.8.0/CHANGES0000644001052500001440000002737013373557122012661 0ustar sbakerusersVersion 1.8.0 (11/16/2018) - Added an experimental --fromfile option (suggested by several people.) This may eventually be replaced or supplimented by a --fromjson option. - Added support for BSD's CLICOLOR and CLICOLOR_FORCE environment variables. (Suggested by Alyssa Ross) - Use strftime() exclusively when formatting date/time to respect locale. - Some man page fixes and cleanups curtsey of Kirill Kolyshkin - Update BINDIR in Makefile for MacOS X -- It is not allowed to install programs to /usr/bin on MacOS X any longer due to System Integrity Protection (SIP) (Shawn Mehan) - Misc patches from Jacob Wahlgren: - Improved command line switch error reporting. - Symbolic links not displayed if a -P pattern is active - Missing argument error reporting fixes on long format switches. - Fixed JSON output hanging commas (John Lane, Tad, others) - JSON size output ignored -h/--si flags (Wagner Camarao) - Fixed issue with malformed multibyte string handling. (Mantas Mikulėnas) - Fixed issue where mbstowcs() fails to null terminate the string due to improper UTF-8 encoding leading to garbage being printed. (Nick Craig-Wood) - Found a bug where the wrong inode (and device) information would be printed for symbolic links. (Stephan Gabert) Version 1.7.0 (04/23/2014) - Allow user/group names up to 32 characters before clipping. - Made -i compress XML and JSON output as much as possible by eliminating extraneous whitespace. - Added --caseinsensitive (renamed --ignore-case ala grep) flag so patterns match without regard to case, courtesy of Jason A Donenfeld. - Added --matchdirs option courtesy of Brian Mattern & Jason A. Donenfeld . - Fixed possible buffer overflow on large uid/gids w/o user names/group names (Alexandre Wendling ) - Added JSON support courtesy of Florian Sesser . - Fixed formatting error with HTML output when -L 1 specified. (Sascha Zorn ) - Added file size sorting (Philipp M?ller ) - Added '--sort[=]' option, ala ls. - Fixed OS X makefile problems (Ryan Hollis ) - Fixed possible memory overflow in read_dir (path/lbuf not equal in size to pathsize/lbufsize.) (Han Hui ) - Fix S_ISDOOR/S_IFDOOR spelling mistake for Solaris. (Tim Mooney ) - Make tree more reliably detect UTF-8 locales. (Mantas Mikulnas and others.) - Return non-zero exit status on option errors, print usage to stdout when not an error, add the posix '--' option terminator, Change -S description to mean CP437 (console) output codes, not ASCII. (Ivan Shmakov ) Version 1.6.0 (05/24/11) - Re-org of code into multiple files, split HTML and Unix listdir() into separate functions, various code cleanups and optimizations. - Fixed a memory leak in listdir() when memory was allocated early and not freed before function exit. - Fixed possible buffer overflow where symbolic links are followed. - Fixed links printing "argetm" before the name of the link when the LINK setting for DIR_COLORS is set to target (Markus Schnalke ) - More fully support dir colors -- added support for su, sg, tw, ow, & st options (and "do" in theory). - Use the environment variable "TREE_COLORS" instead of "LS_COLORS" for color information if it exists. - Added --si flag to print filesizes in SI (powers of 1000) units (Ulrich Eckhardt) - Added -Q to quote filenames in double quotes. Does not override -N or -q. - Control characters are no longer printed in carrot notation, but as backslashed octal, ala ls, except for codes 7-13 which are printed as \a, \b, \t, \n, \v, \f and \r respectively. Spaces and backslashes are also now backslashed as per ls, for better input to scripts unless -Q is in use (where "'s are backslashed.) (Ujjwal Kumar) - Added -U for unsorted listings (directory order). - Added -c for sorting by last status change (ala ls -c). - --dirsfirst is now a meta-sort and does not override -c, -v, -r or -t, but is disabled by -U. - After many requests, added the ability to process the entire tree before emitting output. Used for the new options --du, which works like the du command: sums the amount of space under each directory and prints a total amount used in the report and the --prune option which will prune all empty directories from the output (makes the -P option output much more readable.) It should be noted that this will be slow to output when processing large directory trees and can consume copious amounts of memory, use at your own peril. - Added -X option to emit the directory tree in XML format (turns colorization off always.) - Added --timefmt option to specify the format of time display (implies -D). Uses the strftime format. Version 1.5.3 (11/24/09) - 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.) Version 1.5.2.2 (01/22/09) - Set locale before checking MB_CUR_MAX. - Added HP-NonStop platform support (Craig McDaniel ) - Fixed to support 32 bit UID/GIDs. - Added Solaris build options to Makefile (edit and uncomment to use). Provided by Wang Quanhong Version 1.5.2.1 (08/29/08) - Added strverscmp.c file for os's without strverscmp. Source file is attributed to: Jean-Franois Bignolles - Try different approach to MB_CUR_MAX problem. - Changed the argument to printit() to be signed char to avoid warnings. Version 1.5.2 (06/06/08) - Added --filelimit X option to not descend directories that have more than X number of files in them. - Added -v option for version sorting (also called natural sorting) ala ls. Version 1.5.1.2 (06/04/08) - Fixed compile issues related to MB_CUR_MAX on non-linux machines. - Removed unecessary features.h Version 1.5.1.1 (06/11/07) - Regression in HTML output, fixed formatting issues. Version 1.5.1 (?) - Remove extraneous / at end of user input directory names when using -f option (Zurd) - List available charsets if --charset option is missing charset argument. - Fixed --charset option processing bug. - Fixed missing
's when -i option used with -H. - Added -h option for human readable output. - Colorization bugfix for special files and directories (make tree behave as ls does) Version 1.5.0 (08/15/04) - Added -T option to change title and H1 header in HTML output. - Added -r option to reverse alpha sort output, ala. 'ls -r'. - '|' wildcard support added by David MacMahon . - Remove extraneous '/' at the end of dirs and dir-symlinks in HTML output. - Removed several possible overflow problems by dynamically allocating arrays in several places. - Better support for Locales and printing utf-8 encoded characters in filenames (still hackish). - Fixed -t to alphasort files with same time-stamps. - Fixed encoding of filenames in HTML output, Kyosuke and others. - Patches by Kyosuke Tokoro : - Now, runs OS/2 systems. + Print the file attributes in 'adhrs' format for each file, instead of the protections in 'drwxrwxrwx' format, when running the tree on OS/2 with option -p. - Added --charset option, to specify which character set is used for output. + You can specify any IANA registered character set name. But I have tested only following character sets: Shift_JIS EUC-JP IBM850 UTF-8 ISO-8859-1 US-ASCII + Now, `-S' option is equal to `--charset=IBM437'. + When running on OS/2 systems, the default value of this option is according to current codepage. On the other systems, no default. - Change font-weight to font-size in CSS .VERSION. - Change version to standard major.minor.patch format. - Switch from artistic license to GPLv2. Version 1.4 (02/21/02 (b1), 03/24/02 (b2), 02/06/03 (b3)) - Added large file support under Linux. - Fixed crashing on missing command line arguments. - Fixed several memory leaks - Added --dirsfirst option to list directories first. - Fixed formatting error when unable to open directories. - Fixed bug in parse_dir_colors(). - Changed -I to also ignore directories. - Added --nolinks command to turn off hyperlinks with the HTML output. - Fixed several memory leaks in listdir(). - Some additional code cleanup in listdir(). - Some systems may define TRUE/FALSE, so don't create the enums for TRUE and FALSE if that's the case. - Fixed over-allocation bug in read_dir(). - Added crude beginnings of color output for HTML via CSS (Ted Tiberio ttiberio@rochester.rr.com). - Fixed buffer overflow problem in dircolors parsing. - Fixed recursive symlink detection. - Added --inodes and --device options. - Added --noreport option. Version 1.3 (02/15/99) - Fixed long pathname problem by dynamically allocating the path. - Added recursive symlink detection. - Added --help and --version options. - When -C is used and LS_COLORS is undefined, tree uses a default color scheme (thus -C always forces color output now). - Added -S to show ASCII tree lines (Gerald Scheidl) - Made tree more portable (Guido Socher and others) Following options courtesy of Francesc Rocher: - Added -o to redirect the output. - Added -H to print the tree in HTML format. - Added -L to set the maximum level of directories to print. - Added -R to recursively restart the search at the level given by `-L' option (adding as well `-o 00Tree.html'). Version 1.2 (01/05/97) - Added -D to print the date of the last modification. - Added -t option to sort by last modification time (ala ls -t). - Added -I , similar to the -P option except tree does not print those files which match the pattern. - Made tree print non-printable characters in filenames in standard unix carrot notation. - Added -N option to make tree print filenames without any processing. - Added -q option to make tree print non-printable characters in filenames as question marks. - Added `|' to -F output and made it only print character type after the link on sym-links, not on the symlink name itself. - Added -u option to display username/uid, and -g option to display group name/gid. - Fully (pass the salt) implemented dircolors support. Version 1.1 (07/09/96) - Made some changes to the Makefile to insure proper installation and for multi-architecture support and a bug-fix. - Made root directory colorized if dircolors is enabled. - Put []'s around permission and size info, 'cause I think it looks better. - Added -A option to enable ANSI-lines hack. - Added some sanity checks for dircolors support. - Added -P to list only those files that match the wildcard given in . - Fixed error where relative symbolic links to directories would not be followed when the -l option was used. - Made uid 0 the same as anyone else (-a was default for uid 0) - Added -x directive to stay on one filesystem (ala find -xdev). Version 1.0 (??/??/90?) - The original, a model of perfection... tree-1.8.0/INSTALL0000644001052500001440000000112513223305110012663 0ustar sbakerusersInstallation instructions: 1. Edit the Makefile for your OS. Comment out the Linux options and un-comment the options for your OS. 2. Type: make 3. Type: make install 4. Enjoy colorful directory trees. I cannot test on non-Linux machines, so please feel free to contribute porting information, bug reports, compile options, patches, etc for porting to other OS's to ice@mama.indstate.edu. I would also welcome any localization efforts, particularly translating the man page to other languages. And of course feel free to suggest options and improvements you would like to see in tree. tree-1.8.0/LICENSE0000644001052500001440000004313110106752467012664 0ustar sbakerusers GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Library General Public License instead of this License. tree-1.8.0/Makefile0000644001052500001440000000563513230176514013320 0ustar sbakerusers# $Copyright: $ # Copyright (c) 1996 - 2018 by Steve Baker # All Rights reserved # # 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA prefix = /usr CC=gcc VERSION=1.8.0 TREE_DEST=tree BINDIR=${prefix}/bin MAN=tree.1 MANDIR=${prefix}/man/man1 OBJS=tree.o unix.o html.o xml.o json.o hash.o color.o file.o # Uncomment options below for your particular OS: # Linux defaults: CFLAGS=-ggdb -pedantic -Wall -DLINUX -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 #CFLAGS=-O4 -Wall -DLINUX -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 #LDFLAGS=-s # Uncomment for FreeBSD: #CFLAGS=-O2 -Wall -fomit-frame-pointer #LDFLAGS=-s #OBJS+=strverscmp.o # Uncomment for OpenBSD: #TREE_DEST=colortree #MAN=colortree.1 #CFLAGS=-O2 -Wall -fomit-frame-pointer #LDFLAGS=-s #OBJS+=strverscmp.o # Uncomment for Solaris: #CC=cc #CFLAGS=-xO0 -v #LDFLAGS= #OBJS+=strverscmp.o #MANDIR=${prefix}/share/man/man1 # Uncomment for Cygwin: #CFLAGS=-O2 -Wall -fomit-frame-pointer -DCYGWIN #LDFLAGS=-s #TREE_DEST=tree.exe #OBJS+=strverscmp.o # Uncomment for OS X: # It is not allowed to install to /usr/bin on OS X any longer (SIP): #prefix = /usr/local #CC=cc #CFLAGS=-O2 -Wall -fomit-frame-pointer -no-cpp-precomp #LDFLAGS= #MANDIR=/usr/share/man/man1 #OBJS+=strverscmp.o # Uncomment for HP/UX: #CC=cc #CFLAGS=-O2 -DAportable -Wall #LDFLAGS= #OBJS+=strverscmp.o # Uncomment for OS/2: #CFLAGS=-02 -Wall -fomit-frame-pointer -Zomf -Zsmall-conv #LDFLAGS=-s -Zomf -Zsmall-conv #OBJS+=strverscmp.o # Uncomment for HP NonStop: #prefix = /opt #CC=c89 #CFLAGS=-Wextensions -WIEEE_float -g -Wnowarn=1506 -D_XOPEN_SOURCE_EXTENDED=1 \ # -Wallow_cplusplus_comments #LDFLAGS= #OBJS+=strverscmp.o # AIX #CC=cc_r -q64 #LD=ld -d64 #LDFLAGS=-lc #OBJS+=strverscmp.o #------------------------------------------------------------ all: tree tree: $(OBJS) $(CC) $(LDFLAGS) -o $(TREE_DEST) $(OBJS) $(OBJS): %.o: %.c tree.h $(CC) $(CFLAGS) -c -o $@ $< clean: if [ -x $(TREE_DEST) ]; then rm $(TREE_DEST); fi if [ -f tree.o ]; then rm *.o; fi rm -f *~ install: tree install -d $(BINDIR) install -d $(MANDIR) if [ -e $(TREE_DEST) ]; then \ install $(TREE_DEST) $(BINDIR)/$(TREE_DEST); \ fi install doc/$(MAN) $(MANDIR)/$(MAN) distclean: if [ -f tree.o ]; then rm *.o; fi rm -f *~ dist: distclean tar zcf ../tree-$(VERSION).tgz -C .. `cat .tarball` tree-1.8.0/README0000644001052500001440000001315412542066521012534 0ustar sbakerusers Please read the INSTALL file for installation instructions, particularly if you are installing on a non-Linux machine. This is a handy little utility to display a tree view of directories that I wrote some time ago and just added color support to. I've decided that since no one else has done something similar I would go ahead and release it, even though it's barely a 1st year CS student hack. I've found it damn handy to peruse a directory tree though, especially when someone is trying to hide something from you. The main distribution site for tree is here: http://mama.indstate.edu/users/ice/tree/ ftp://mama.indstate.edu/linux/tree/ If you don't like the way it looks let me know how you think it should be formatted. Feel free to suggest modifications and additions. Thanks go out so the following people who have helped bring tree to the pinnacle of perfection that it is: ;) Francesc Rocher - Added HTML output (-H). - Added options -o, -L and -R. Gerald Scheidl - Added -S option to print ASCII graphics lines for use under linux console when an alternate console font has been selected (might also work under DOS telnet). Guido Socher (and others) - Made tree more portable. Should compile under solaris. Mitja Lacen - Discovered bug where tree will segmentation fault on long pathnames. - Discovered in -L argument processing. Nathaniel Delage - Discovered problem with recursive symlink detection A. Karthik - Suggested option to remove file and directory report at end of tree listing. Roger Luethi - Spotted memory over-allocation bug in read_dir(). - Submitted several patches to fix various memory leaks. Daniel Lee - Reported that Tru64 defines TRUE/FALSE in sys/types.h (OSF1 standard?) Paolo Violini - Found bug in tree that caused it to seg-fault if 50 file arguments where given and directory coloring was turned on. Mitsuaki Masuhara - Discovered tree crashed on missing arguments. - Discovered that tree did not properly encode characters in filenames when used as URLs when using the -H option. - Fixed issue with --charset option processing. Johan Fredrik - Pointed out that tree did not list large files. Ted Tiberio - Submitted patch which fixed a compiler issue and cleaned up HTML and CSS code, applied CSS to all output, and fixed up HTML to 4.01 strict standards. David MacMahon - Added '|' support to the pattern matching routines. Dan Jacobson - Pointed out that -t did not sort properly for files with the same timestamp. - Suggested option to change HTML title and H1 string. - Suggested -r option for reversed alphanumeric sort ala 'ls -r'. Kyosuke Tokoro - Provided patch to support OS/2, fix HTML encoding, provide charset support. Added to authors list. Florian Ernst - Debian maintainer who pointed out problems and applied fire to feet to fix stuff. Jack Cuyler - Suggested -h option for human readable output for -s, ala ls -lh. Jonathon Cranford - Supplied patch to make tree under cygwin. Richard Houser - Provided patch to fix a colorization bug when dealing with special files and directories that seem to have an extension. Zurd (?) - Suggested removing trailing slash on user supplied directory names if -f option is used. John Nintendud - Pointed out broken HTML output in 1.5.1. Mark Braker - Suggested --filelimit option. Michael Vogt - Suggested -v option (version sort). Wang Quanhong - Provided build options for Solaris. Craig McDaniel - Provided build options and source mods for HP NonStop support. Christian Grigis - Noted that setlocale() should come before MB_CUR_MAX check. Kamaraju Kusumanchi - Submitted patch to remove compiler warnings for Solaris. Martin Nagy - Provided patch which fixes issue where indent may output more than it should when dirs[*] is not properly cleared before use. William C. Lathan III - Showed that tree was not properly quoting arguments to recursively called tree instances when using -R. Ulrich Eckhardt - Submitted patch for --si option. Tim Waugh (redhat) - Pointed out a potential memory leak in listdir(). Markus Schnalke - Tracked down bug where tree would print "argetm" before the filename of a symbolic link when the "target" option was specified for LINK in dircolors. Ujjwal Kumar - Suggested that tree backslash spaces like ls does for script use. Made output more like ls. Ivan Shmakov - Pointed out multiple CLI defenciencies (via Debian) Mantas Mikulnas - Provided patch to make tree more reliably detect the UTF-8 locale. Tim Mooney - Noticed S_ISDOOR/S_IFDOOR spelling mistake for under Solaris. Han Hui - Pointed out possible memory overflow in read_dir (path/lbuf not equal in size to pathsize/lbufsize.) Ryan Hollis - Pointed out problems with the Makefile w/ respect to OSX. Philipp M?ller - Provided patch for filesize sorting. Sascha Zorn - Pointed out that the HTML output was broken when -L 1 option was used. Alexandre Wendling - Pointed out that modern systems may use 32 bit uid/gids which could lead to a potential buffer overflow in the uid/gid to name mapping functions. Florian Sesser - Provided patch to add JSON support. Brian Mattern & Jason A. Donenfeld - Provided patch to add --matchdirs functionality. Jason A. Donenfeld - Added --caseinsentive, renamed --ignore-case option. - Bugged me a lot. Stephan Gabert - Found a bug where the wrong inode (and device) information would be printed for symbolic links. And many others whom I've failed to keep track of. I should have started this list years ago. - Steve Baker ice@mama.indstate.edu tree-1.8.0/TODO0000644001052500001440000000454312466741036012354 0ustar sbakerusersShould do: - Use stdint.h and inttypes.h to standardize the int sizes and format strings. - Add --DU option to fully report disk usage, taking into account files that are not displayed in the output. - Should be a better way code the various tree formats (XML/HTML/Unix) to share more code. Probably make a generic tree crawling routine that calls various function pointers to emit the file information based on the kind of tree we want. - Make wide character support less of a hack. - Fully support HTML colorization properly. - Go over the -R option for HTML output and make it cleaner & less of a hack. Should do -R internally so we don't have to rebuild a command line or use something awful like system(). Should be doable by saving some state and recursing and also separating out parts of main. - Should probably make it so that options like -p, -u, etc print out info for the top directory, if people think that would be desirable. - Might be nice to prune files by things like type, mode, access/modify/change time, uid/gid, etc. ala find. - Just incorporate the stat structure into _info, since we now need most of the structure anyway. Maybe do: - With the addition of TREE_COLORS, add some custom options to perhaps colorize metadata like the permissions, date, username, etc, and change the color of the tree lines and so on. - Make tree look for a .comments file in a directory and append the text from that file to the output of the files and directories, so that the output looks like Slackware's README file, like: > tree -F --comments ... |-- kernels/ Precompiled Linux 2.6.37.6 kernel images. | | | |-- hugesmp.s The default standard install kernel for Slackware. | | This supports pretty much everything in the | | 2.6.37.6 kernel, and includes support for Speakup. | | This kernel requires at least a Pentium-Pro processor. | | | `-- huge.s A single-processor version of huge.s that will | function with older hardware such as a 486 with | 128MB (64MB _might_ work) or more of RAM. | This kernel also supports Speakup. ... Might be particularly nice for the HTML output, where the comments could be made as mouse-over popups or the like. tree-1.8.0/color.c0000644001052500001440000004003013373552117013133 0ustar sbakerusers/* $Copyright: $ * Copyright (c) 1996 - 2018 by Steve Baker (ice@mama.indstate.edu) * All Rights reserved * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "tree.h" /* * Hacked in DIR_COLORS support for linux. ------------------------------ */ /* * If someone asked me, I'd extend dircolors, to provide more generic * color support so that more programs could take advantage of it. This * is really just hacked in support. The dircolors program should: * 1) Put the valid terms in a environment var, like: * COLOR_TERMS=linux:console:xterm:vt100... * 2) Put the COLOR and OPTIONS directives in a env var too. * 3) Have an option to dircolors to silently ignore directives that it * doesn't understand (directives that other programs would * understand). * 4) Perhaps even make those unknown directives environment variables. * * The environment is the place for cryptic crap no one looks at, but * programs. No one is going to care if it takes 30 variables to do * something. */ enum { CMD_COLOR, CMD_OPTIONS, CMD_TERM, CMD_EIGHTBIT, COL_NORMAL, COL_FILE, COL_DIR, COL_LINK, COL_FIFO, COL_DOOR, COL_BLK, COL_CHR, COL_ORPHAN, COL_SOCK, COL_SETUID, COL_SETGID, COL_STICKY_OTHER_WRITABLE, COL_OTHER_WRITABLE, COL_STICKY, COL_EXEC, COL_MISSING, COL_LEFTCODE, COL_RIGHTCODE, COL_ENDCODE, DOT_EXTENSION, ERROR }; bool colorize = FALSE, ansilines = FALSE, linktargetcolor = FALSE; char *term, termmatch = FALSE, istty; char *leftcode = NULL, *rightcode = NULL, *endcode = NULL; char *norm_flgs = NULL, *file_flgs = NULL, *dir_flgs = NULL, *link_flgs = NULL; char *fifo_flgs = NULL, *door_flgs = NULL, *block_flgs = NULL, *char_flgs = NULL; char *orphan_flgs = NULL, *sock_flgs = NULL, *suid_flgs = NULL, *sgid_flgs = NULL; char *stickyow_flgs = NULL, *otherwr_flgs = NULL, *sticky_flgs = NULL; char *exec_flgs = NULL, *missing_flgs = NULL; char *vgacolor[] = { "black", "red", "green", "yellow", "blue", "fuchsia", "aqua", "white", NULL, NULL, "transparent", "red", "green", "yellow", "blue", "fuchsia", "aqua", "black" }; struct colortable colortable[11]; struct extensions *ext = NULL; const struct linedraw *linedraw; char **split(char *str, char *delim, int *nwrds); int cmd(char *s); extern FILE *outfile; extern bool Hflag, force_color, nocolor; extern const char *charset; void parse_dir_colors() { char buf[1025], **arg, **c, *colors, *s, *cc; int i, n; struct extensions *e; if (Hflag) return; if (getenv("TERM") == NULL) { colorize = FALSE; return; } s = getenv("TREE_COLORS"); if (s == NULL) s = getenv("LS_COLORS"); cc = getenv("CLICOLOR"); if (getenv("CLICOLOR_FORCE") != NULL) force_color=TRUE; if ((s == NULL || strlen(s) == 0) && (force_color || cc != NULL)) s = ":no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.bat=01;32:*.BAT=01;32:*.btm=01;32:*.BTM=01;32:*.cmd=01;32:*.CMD=01;32:*.com=01;32:*.COM=01;32:*.dll=01;32:*.DLL=01;32:*.exe=01;32:*.EXE=01;32:*.arj=01;31:*.bz2=01;31:*.deb=01;31:*.gz=01;31:*.lzh=01;31:*.rpm=01;31:*.tar=01;31:*.taz=01;31:*.tb2=01;31:*.tbz2=01;31:*.tbz=01;31:*.tgz=01;31:*.tz2=01;31:*.z=01;31:*.Z=01;31:*.zip=01;31:*.ZIP=01;31:*.zoo=01;31:*.asf=01;35:*.ASF=01;35:*.avi=01;35:*.AVI=01;35:*.bmp=01;35:*.BMP=01;35:*.flac=01;35:*.FLAC=01;35:*.gif=01;35:*.GIF=01;35:*.jpg=01;35:*.JPG=01;35:*.jpeg=01;35:*.JPEG=01;35:*.m2a=01;35:*.M2a=01;35:*.m2v=01;35:*.M2V=01;35:*.mov=01;35:*.MOV=01;35:*.mp3=01;35:*.MP3=01;35:*.mpeg=01;35:*.MPEG=01;35:*.mpg=01;35:*.MPG=01;35:*.ogg=01;35:*.OGG=01;35:*.ppm=01;35:*.rm=01;35:*.RM=01;35:*.tga=01;35:*.TGA=01;35:*.tif=01;35:*.TIF=01;35:*.wav=01;35:*.WAV=01;35:*.wmv=01;35:*.WMV=01;35:*.xbm=01;35:*.xpm=01;35:"; if (s == NULL || (!force_color && (nocolor || !isatty(1)))) { colorize = FALSE; return; } else { colorize = TRUE; /* You can uncomment the below line and tree will always try to ANSI-fy the indentation lines */ /* ansilines = TRUE; */ } colors = scopy(s); arg = split(colors,":",&n); for(i=0;arg[i];i++) { c = split(arg[i],"=",&n); switch(cmd(c[0])) { case COL_NORMAL: if (c[1]) norm_flgs = scopy(c[1]); break; case COL_FILE: if (c[1]) file_flgs = scopy(c[1]); break; case COL_DIR: if (c[1]) dir_flgs = scopy(c[1]); break; case COL_LINK: if (c[1]) { if (strcasecmp("target",c[1]) == 0) { linktargetcolor = TRUE; link_flgs = "01;36"; /* Should never actually be used */ } else link_flgs = scopy(c[1]); } break; case COL_FIFO: if (c[1]) fifo_flgs = scopy(c[1]); break; case COL_DOOR: if (c[1]) door_flgs = scopy(c[1]); break; case COL_BLK: if (c[1]) block_flgs = scopy(c[1]); break; case COL_CHR: if (c[1]) char_flgs = scopy(c[1]); break; case COL_ORPHAN: if (c[1]) orphan_flgs = scopy(c[1]); break; case COL_SOCK: if (c[1]) sock_flgs = scopy(c[1]); break; case COL_SETUID: if (c[1]) suid_flgs = scopy(c[1]); break; case COL_SETGID: if (c[1]) sgid_flgs = scopy(c[1]); break; case COL_STICKY_OTHER_WRITABLE: if (c[1]) stickyow_flgs = scopy(c[1]); break; case COL_OTHER_WRITABLE: if (c[1]) otherwr_flgs = scopy(c[1]); break; case COL_STICKY: if (c[1]) sticky_flgs = scopy(c[1]); break; case COL_EXEC: if (c[1]) exec_flgs = scopy(c[1]); break; case COL_MISSING: if (c[1]) missing_flgs = scopy(c[1]); break; case COL_LEFTCODE: if (c[1]) leftcode = scopy(c[1]); break; case COL_RIGHTCODE: if (c[1]) rightcode = scopy(c[1]); break; case COL_ENDCODE: if (c[1]) endcode = scopy(c[1]); break; case DOT_EXTENSION: if (c[1]) { e = xmalloc(sizeof(struct extensions)); e->ext = scopy(c[0]+1); e->term_flg = scopy(c[1]); e->nxt = ext; ext = e; } } free(c); } free(arg); /* make sure at least norm_flgs is defined. We're going to assume vt100 support */ if (!leftcode) leftcode = scopy("\033["); if (!rightcode) rightcode = scopy("m"); if (!norm_flgs) norm_flgs = scopy("00"); if (!endcode) { sprintf(buf,"%s%s%s",leftcode,norm_flgs,rightcode); endcode = scopy(buf); } free(colors); /* if (!termmatch) colorize = FALSE; */ } /* * You must free the pointer that is allocated by split() after you * are done using the array. */ char **split(char *str, char *delim, int *nwrds) { int n = 128; char **w = xmalloc(sizeof(char *) * n); w[*nwrds = 0] = strtok(str,delim); while (w[*nwrds]) { if (*nwrds == (n-2)) w = xrealloc(w,sizeof(char *) * (n+=256)); w[++(*nwrds)] = strtok(NULL,delim); } w[*nwrds] = NULL; return w; } int cmd(char *s) { static struct { char *cmd; char cmdnum; } cmds[] = { {"no", COL_NORMAL}, {"fi", COL_FILE}, {"di", COL_DIR}, {"ln", COL_LINK}, {"pi", COL_FIFO}, {"do", COL_DOOR}, {"bd", COL_BLK}, {"cd", COL_CHR}, {"or", COL_ORPHAN}, {"so", COL_SOCK}, {"su", COL_SETUID}, {"sg", COL_SETGID}, {"tw", COL_STICKY_OTHER_WRITABLE}, {"ow", COL_OTHER_WRITABLE}, {"st", COL_STICKY}, {"ex", COL_EXEC}, {"mi", COL_MISSING}, {"lc", COL_LEFTCODE}, {"rc", COL_RIGHTCODE}, {"ec", COL_ENDCODE}, {NULL, 0} }; int i; if (s[0] == '*') return DOT_EXTENSION; for(i=0;cmds[i].cmdnum;i++) { if (!strcmp(cmds[i].cmd,s)) return cmds[i].cmdnum; } return ERROR; } int color(u_short mode, char *name, bool orphan, bool islink) { struct extensions *e; int l, xl; if (orphan) { if (islink) { if (missing_flgs) { fprintf(outfile,"%s%s%s",leftcode,missing_flgs,rightcode); return TRUE; } } else { if (orphan_flgs) { fprintf(outfile,"%s%s%s",leftcode,orphan_flgs,rightcode); return TRUE; } } } switch(mode & S_IFMT) { case S_IFIFO: if (!fifo_flgs) return FALSE; fprintf(outfile,"%s%s%s",leftcode,fifo_flgs,rightcode); return TRUE; case S_IFCHR: if (!char_flgs) return FALSE; fprintf(outfile,"%s%s%s",leftcode,char_flgs,rightcode); return TRUE; case S_IFDIR: if (mode & S_ISVTX) { if ((mode & S_IWOTH) && stickyow_flgs) { fprintf(outfile, "%s%s%s",leftcode,stickyow_flgs,rightcode); return TRUE; } if (!(mode & S_IWOTH) && sticky_flgs) { fprintf(outfile, "%s%s%s",leftcode,sticky_flgs,rightcode); return TRUE; } } if ((mode & S_IWOTH) && otherwr_flgs) { fprintf(outfile,"%s%s%s",leftcode,otherwr_flgs,rightcode); return TRUE; } if (!dir_flgs) return FALSE; fprintf(outfile,"%s%s%s",leftcode,dir_flgs,rightcode); return TRUE; #ifndef __EMX__ case S_IFBLK: if (!block_flgs) return FALSE; fprintf(outfile,"%s%s%s",leftcode,block_flgs,rightcode); return TRUE; case S_IFLNK: if (!link_flgs) return FALSE; fprintf(outfile,"%s%s%s",leftcode,link_flgs,rightcode); return TRUE; #ifdef S_IFDOOR case S_IFDOOR: if (!door_flgs) return FALSE; fprintf(outfile,"%s%s%s",leftcode,door_flgs,rightcode); return TRUE; #endif #endif case S_IFSOCK: if (!sock_flgs) return FALSE; fprintf(outfile,"%s%s%s",leftcode,sock_flgs,rightcode); return TRUE; case S_IFREG: if ((mode & S_ISUID) && suid_flgs) { fprintf(outfile,"%s%s%s",leftcode,suid_flgs,rightcode); return TRUE; } if ((mode & S_ISGID) && sgid_flgs) { fprintf(outfile,"%s%s%s",leftcode,sgid_flgs,rightcode); return TRUE; } if (!exec_flgs) return FALSE; if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) { fprintf(outfile,"%s%s%s",leftcode,exec_flgs,rightcode); return TRUE; } /* not a directory, link, special device, etc, so check for extension match */ l = strlen(name); for(e=ext;e;e=e->nxt) { xl = strlen(e->ext); if (!strcmp((l>xl)?name+(l-xl):name,e->ext)) { fprintf(outfile,"%s%s%s",leftcode,e->term_flg,rightcode); return TRUE; } } return FALSE; } return FALSE; } /* * Charsets provided by Kyosuke Tokoro (NBG01720@nifty.ne.jp) */ const char *getcharset(void) { #ifndef __EMX__ return getenv("TREE_CHARSET"); #else static char buffer[13]; ULONG aulCpList[3],ulListSize,codepage=0; char*charset=getenv("TREE_CHARSET"); if(charset) return charset; if(!getenv("WINDOWID")) if(!DosQueryCp(sizeof aulCpList,aulCpList,&ulListSize)) if(ulListSize>=sizeof*aulCpList) codepage=*aulCpList; switch(codepage){ case 437: case 775: case 850: case 851: case 852: case 855: case 857: case 860: case 861: case 862: case 863: case 864: case 865: case 866: case 868: case 869: case 891: case 903: case 904: sprintf(buffer,"IBM%03lu",codepage); break; case 367: return"US-ASCII"; case 813: return"ISO-8859-7"; case 819: return"ISO-8859-1"; case 881: case 882: case 883: case 884: case 885: sprintf(buffer,"ISO-8859-%lu",codepage-880); break; case 858: case 924: sprintf(buffer,"IBM%05lu",codepage); break; case 874: return"TIS-620"; case 897: case 932: case 942: case 943: return"Shift_JIS"; case 912: return"ISO-8859-2"; case 915: return"ISO-8859-5"; case 916: return"ISO-8859-8"; case 949: case 970: return"EUC-KR"; case 950: return"Big5"; case 954: return"EUC-JP"; case 1051: return"hp-roman8"; case 1089: return"ISO-8859-6"; case 1250: case 1251: case 1253: case 1254: case 1255: case 1256: case 1257: case 1258: sprintf(buffer,"windows-%lu",codepage); break; case 1252: return"ISO-8859-1-Windows-3.1-Latin-1"; default: return NULL; } #endif } void initlinedraw(int flag) { static const char*latin1_3[]={ "ISO-8859-1", "ISO-8859-1:1987", "ISO_8859-1", "latin1", "l1", "IBM819", "CP819", "csISOLatin1", "ISO-8859-3", "ISO_8859-3:1988", "ISO_8859-3", "latin3", "ls", "csISOLatin3", NULL }; static const char*iso8859_789[]={ "ISO-8859-7", "ISO_8859-7:1987", "ISO_8859-7", "ELOT_928", "ECMA-118", "greek", "greek8", "csISOLatinGreek", "ISO-8859-8", "ISO_8859-8:1988", "iso-ir-138", "ISO_8859-8", "hebrew", "csISOLatinHebrew", "ISO-8859-9", "ISO_8859-9:1989", "iso-ir-148", "ISO_8859-9", "latin5", "l5", "csISOLatin5", NULL }; static const char*shift_jis[]={ "Shift_JIS", "MS_Kanji", "csShiftJIS", NULL }; static const char*euc_jp[]={ "EUC-JP", "Extended_UNIX_Code_Packed_Format_for_Japanese", "csEUCPkdFmtJapanese", NULL }; static const char*euc_kr[]={ "EUC-KR", "csEUCKR", NULL }; static const char*iso2022jp[]={ "ISO-2022-JP", "csISO2022JP", "ISO-2022-JP-2", "csISO2022JP2", NULL }; static const char*ibm_pc[]={ "IBM437", "cp437", "437", "csPC8CodePage437", "IBM852", "cp852", "852", "csPCp852", "IBM863", "cp863", "863", "csIBM863", "IBM855", "cp855", "855", "csIBM855", "IBM865", "cp865", "865", "csIBM865", "IBM866", "cp866", "866", "csIBM866", NULL }; static const char*ibm_ps2[]={ "IBM850", "cp850", "850", "csPC850Multilingual", "IBM00858", "CCSID00858", "CP00858", "PC-Multilingual-850+euro", NULL }; static const char*ibm_gr[]={ "IBM869", "cp869", "869", "cp-gr", "csIBM869", NULL }; static const char*gb[]={ "GB2312", "csGB2312", NULL }; static const char*utf8[]={ "UTF-8", "utf8", NULL }; static const char*big5[]={ "Big5", "csBig5", NULL }; static const char*viscii[]={ "VISCII", "csVISCII", NULL }; static const char*koi8ru[]={ "KOI8-R", "csKOI8R", "KOI8-U", NULL }; static const char*windows[]={ "ISO-8859-1-Windows-3.1-Latin-1", "csWindows31Latin1", "ISO-8859-2-Windows-Latin-2", "csWindows31Latin2", "windows-1250", "windows-1251", "windows-1253", "windows-1254", "windows-1255", "windows-1256", "windows-1256", "windows-1257", NULL }; static const struct linedraw cstable[]={ { latin1_3, "| ", "|--", "·--", "©" }, { iso8859_789, "| ", "|--", "·--", "(c)" }, { shift_jis, "\204\240 ", "\204\245", "\204\244", "(c)" }, { euc_jp, "\250\242 ", "\250\247", "\250\246", "(c)" }, { euc_kr, "\246\242 ", "\246\247", "\246\246", "(c)" }, { iso2022jp, "\033$B(\"\033(B ", "\033$B('\033(B", "\033$B(&\033(B", "(c)" }, { ibm_pc, "\263 ", "\303\304\304", "\300\304\304", "(c)" }, { ibm_ps2, "\263 ", "\303\304\304", "\300\304\304", "\227" }, { ibm_gr, "\263 ", "\303\304\304", "\300\304\304", "\270" }, { gb, "\251\246 ", "\251\300", "\251\270", "(c)" }, { utf8, "\342\224\202\302\240\302\240", "\342\224\234\342\224\200\342\224\200", "\342\224\224\342\224\200\342\224\200", "\302\251" }, { big5, "\242x ", "\242u", "\242|", "(c)" }, { viscii, "| ", "|--", "`--", "\371" }, { koi8ru, "\201 ", "\206\200\200", "\204\200\200", "\277" }, { windows, "| ", "|--", "`--", "\251" }, { NULL, "| ", "|--", "`--", "(c)" } }; const char**s; if (flag) { fprintf(stderr,"tree: missing argument to --charset, valid charsets include:\n"); for(linedraw=cstable;linedraw->name;++linedraw) { for(s=linedraw->name;*s;++s) { fprintf(stderr," %s\n",*s); } } return; } if (charset) { for(linedraw=cstable;linedraw->name;++linedraw) for(s=linedraw->name;*s;++s) if(!strcasecmp(charset,*s)) return; } linedraw=cstable+sizeof cstable/sizeof*cstable-1; } tree-1.8.0/file.c0000644001052500001440000001651113225171351012734 0ustar sbakerusers/* $Copyright: $ * Copyright (c) 1996 - 2018 by Steve Baker (ice@mama.indstate.edu) * All Rights reserved * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "tree.h" extern bool dflag, Fflag, aflag, fflag, pruneflag; extern bool noindent, force_color, flimit, matchdirs; extern bool reverse; extern char *pattern, *ipattern; extern int (*cmpfunc)(); extern FILE *outfile; extern int Level, *dirs, maxdirs; extern bool colorize; extern char *endcode; extern char *file_comment, *file_pathsep; enum ftok { T_PATHSEP, T_DIR, T_FILE, T_EOP }; char *nextpc(char **p, int *tok) { static char prev = 0; char *s = *p; if (!**p) { *tok = T_EOP; // Shouldn't happen. return NULL; } if (prev) { prev = 0; *tok = T_PATHSEP; return NULL; } if (strchr(file_pathsep, **p) != NULL) { (*p)++; *tok = T_PATHSEP; return NULL; } while(**p && strchr(file_pathsep,**p) == NULL) (*p)++; if (**p) { *tok = T_DIR; prev = **p; *(*p)++ = '\0'; } else *tok = T_FILE; return s; } struct _info *newent(char *name) { struct _info *n = xmalloc(sizeof(struct _info)); memset(n,0,sizeof(struct _info)); n->name = strdup(name); n->child = NULL; n->tchild = n->next = NULL; return n; } // Should replace this with a Red-Black tree implementation or the like struct _info *search(struct _info **dir, char *name) { struct _info *ptr, *prev, *n; int cmp; if (*dir == NULL) return (*dir = newent(name)); for(prev = ptr = *dir; ptr != NULL; ptr=ptr->next) { cmp = strcmp(ptr->name,name); if (cmp == 0) return ptr; if (cmp > 0) break; prev = ptr; } n = newent(name); n->next = ptr; if (prev == ptr) *dir = n; else prev->next = n; return n; } void freefiletree(struct _info *ent) { struct _info *ptr = ent, *t; while (ptr != NULL) { if (ptr->tchild) freefiletree(ptr->tchild); t = ptr; ptr = ptr->next; free(t); } } /** * Recursively prune (unset show flag) files/directories of matches/ignored * patterns: */ struct _info **fprune(struct _info *head, bool matched, bool root) { struct _info **dir, *new = NULL, *end = NULL, *ent, *t; int show, count = 0; for(ent = head; ent != NULL;) { if (ent->tchild) ent->isdir = 1; show = 1; if (dflag && !ent->isdir) show = 0; if (!aflag && !root && ent->name[0] == '.') show = 0; if (show && !matched) { if (!ent->isdir) { if (pattern && patmatch(ent->name, pattern) == 0) show = 0; if (ipattern && patmatch(ent->name, pattern) == 1) show = 0; } if (ent->isdir && show && matchdirs && pattern) { if (patmatch(ent->name, pattern) == 1) matched = TRUE; } } if (pruneflag && !matched && ent->isdir && ent->tchild == NULL) show = 0; if (show && ent->tchild != NULL) ent->child = fprune(ent->tchild, matched, FALSE); t = ent; ent = ent->next; if (show) { if (end) end = end->next = t; else new = end = t; count++; } else { t->next = NULL; freefiletree(t); } } if (end) end->next = NULL; dir = xmalloc(sizeof(struct _info *) * (count+1)); for(count = 0, ent = new; ent != NULL; ent = ent->next, count++) { dir[count] = ent; } dir[count] = NULL; return dir; } struct _info **file_getfulltree(char *d, u_long lev, dev_t dev, off_t *size, char **err) { FILE *fp = (strcmp(d,".")? fopen(d,"r") : stdin); char *path, *spath, *s; long pathsize; struct _info *root = NULL, **cwd, *ent; int l, tok; size = 0; if (fp == NULL) { fprintf(stderr,"Error opening %s for reading.\n", d); return NULL; } // 64K paths maximum path = xmalloc(sizeof(char *) * (pathsize = (64 * 1024))); while(fgets(path, pathsize, fp) != NULL) { if (file_comment != NULL && strcmp(path,file_comment) == 0) continue; l = strlen(path); while(l && isspace(path[l-1])) path[--l] = '\0'; if (l == 0) continue; spath = path; cwd = &root; do { s = nextpc(&spath, &tok); if (tok == T_PATHSEP) continue; switch(tok) { case T_PATHSEP: continue; case T_FILE: case T_DIR: // Should probably handle '.' and '..' entries here ent = search(cwd, s); // Might be empty, but should definitely be considered a directory: if (tok == T_DIR) { ent->isdir = 1; ent->mode = S_IFDIR; } else { ent->mode = S_IFREG; } cwd = &(ent->tchild); break; } } while (tok != T_FILE && tok != T_EOP); } if (fp != stdin) fclose(fp); // Prune accumulated directory tree: return fprune(root, FALSE, TRUE); } // void f_listdir(struct _info *dir, char *d, int *dt, int *ft, u_long lev) // { // char *path; // long pathsize = 0; // bool nlf = FALSE, colored = FALSE; // // if (dir == NULL) return; // // dirs[lev] = (dir->next? 1 : 2); // fprintf(outfile,"\n"); // // path = malloc(pathsize=4096); // // while(dir) { // if (!noindent) indent(lev); // // if (colorize) { // colored = color(dir->isdir? S_IFDIR : S_IFREG, dir->name, FALSE, FALSE); // } // // if (fflag) { // if (sizeof(char) * (strlen(d)+strlen(dir->name)+2) > pathsize) // path=xrealloc(path,pathsize=(sizeof(char) * (strlen(d)+strlen(dir->name)+1024))); // if (!strcmp(d,"/")) sprintf(path,"%s%s",d,dir->name); // else sprintf(path,"%s/%s",d,dir->name); // } else { // if (sizeof(char) * (strlen(dir->name)+1) > pathsize) // path=xrealloc(path,pathsize=(sizeof(char) * (strlen(dir->name)+1024))); // sprintf(path,"%s",dir->name); // } // // printit(path); // // if (colored) fprintf(outfile,"%s",endcode); // if (Fflag && dir->isdir) fputc(Ftype(S_IFDIR), outfile); // // if (dir->child) { // if (fflag) { // if (strlen(d)+strlen(dir->name)+2 > pathsize) path=xrealloc(path,pathsize=(strlen(d)+strlen(dir->name)+1024)); // if (!strcmp(d,"/")) sprintf(path,"%s%s",d,dir->name); // else sprintf(path,"%s/%s",d,dir->name); // } // f_listdir(dir->child, fflag? path : NULL, dt, ft, lev+1); // nlf = TRUE; // *dt += 1; // } else { // if (dir->isdir) *dt += 1; // else *ft += 1; // } // // if (dir->next && !dir->next->next) dirs[lev] = 2; // if (nlf) nlf = FALSE; // else fprintf(outfile,"\n"); // dir=dir->next; // } // dirs[lev] = 0; // free(path); // } // void file_listdir(char *d, int *dt, int *ft, u_long lev) // { // FILE *fp = (d != NULL? fopen(d,"r") : stdin); // struct _info *root; // // if (fp == NULL) { // fprintf(stderr,"Error opening %s for reading.\n", d); // return; // } // root = getfulltree(fp, lev); // if (d != NULL) fclose(fp); // // memset(dirs, 0, sizeof(int) * maxdirs); // // f_listdir(root, "/", dt, ft, lev); // // freefiletree(root); // return; // } tree-1.8.0/hash.c0000644001052500001440000000611713225441654012747 0ustar sbakerusers/* $Copyright: $ * Copyright (c) 1996 - 2018 by Steve Baker (ice@mama.indstate.edu) * All Rights reserved * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "tree.h" /* Faster uid/gid -> name lookup with hash(tm)(r)(c) tables! */ #define HASH(x) ((x)&255) struct xtable *gtable[256], *utable[256]; #define inohash(x) ((x)&255) struct inotable *itable[256]; char *uidtoname(uid_t uid) { struct xtable *o, *p, *t; struct passwd *ent; char ubuf[32]; int uent = HASH(uid); for(o = p = utable[uent]; p ; p=p->nxt) { if (uid == p->xid) return p->name; else if (uid < p->xid) break; o = p; } /* Not found, do a real lookup and add to table */ t = xmalloc(sizeof(struct xtable)); if ((ent = getpwuid(uid)) != NULL) t->name = scopy(ent->pw_name); else { snprintf(ubuf,30,"%d",uid); ubuf[31] = 0; t->name = scopy(ubuf); } t->xid = uid; t->nxt = p; if (p == utable[uent]) utable[uent] = t; else o->nxt = t; return t->name; } char *gidtoname(gid_t gid) { struct xtable *o, *p, *t; struct group *ent; char gbuf[32]; int gent = HASH(gid); for(o = p = gtable[gent]; p ; p=p->nxt) { if (gid == p->xid) return p->name; else if (gid < p->xid) break; o = p; } /* Not found, do a real lookup and add to table */ t = xmalloc(sizeof(struct xtable)); if ((ent = getgrgid(gid)) != NULL) t->name = scopy(ent->gr_name); else { snprintf(gbuf,30,"%d",gid); gbuf[31] = 0; t->name = scopy(gbuf); } t->xid = gid; t->nxt = p; if (p == gtable[gent]) gtable[gent] = t; else o->nxt = t; return t->name; } /* Record inode numbers of followed sym-links to avoid refollowing them */ void saveino(ino_t inode, dev_t device) { struct inotable *it, *ip, *pp; int hp = inohash(inode); for(pp = ip = itable[hp];ip;ip = ip->nxt) { if (ip->inode > inode) break; if (ip->inode == inode && ip->device >= device) break; pp = ip; } if (ip && ip->inode == inode && ip->device == device) return; it = xmalloc(sizeof(struct inotable)); it->inode = inode; it->device = device; it->nxt = ip; if (ip == itable[hp]) itable[hp] = it; else pp->nxt = it; } int findino(ino_t inode, dev_t device) { struct inotable *it; for(it=itable[inohash(inode)]; it; it=it->nxt) { if (it->inode > inode) break; if (it->inode == inode && it->device >= device) break; } if (it && it->inode == inode && it->device == device) return TRUE; return FALSE; } tree-1.8.0/html.c0000644001052500001440000003410413225441661012763 0ustar sbakerusers/* $Copyright: $ * Copyright (c) 1996 - 2018 by Steve Baker (ice@mama.indstate.edu) * All Rights reserved * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "tree.h" extern bool dflag, lflag, pflag, sflag, Fflag, aflag, fflag, uflag, gflag; extern bool Dflag, inodeflag, devflag, Rflag; extern bool noindent, force_color, xdev, nolinks, flimit; //extern char *title, extern char *host, *sp; extern struct _info **(*getfulltree)(char *d, u_long lev, dev_t dev, off_t *size, char **err); extern void (*listdir)(char *, int *, int *, u_long, dev_t); extern int (*cmpfunc)(); extern FILE *outfile; extern int Level, *dirs, maxdirs; extern bool colorize, linktargetcolor; extern char *endcode; void html_encode(FILE *, char *), url_encode(FILE *, char *); void url_encode(FILE *fd, char *s); void emit_html_header(const char *charset, char *title, char *version) { fprintf(outfile, "\n" "\n" "\n" " \n" " \n" " \n" " %s\n" " \n" "\n" "\n" "\t

%s

\n\t",charset ? charset : "iso-8859-1", version, title, title); } off_t html_listdir(char *d, int *dt, int *ft, u_long lev, dev_t dev) { char *path; bool nlf = FALSE, colored = FALSE; long pathsize = 0; struct _info **dir, **sav; struct stat sb; int i,n,c; char hclr[20], *hdir, *hcmd; if ((Level >= 0) && (lev > Level)) { fprintf(outfile, "
\n"); return 0; } if (xdev && lev == 0) { stat(d,&sb); dev = sb.st_dev; } sav = dir = read_dir(d,&n); if (!dir && n) { fprintf(outfile," [error opening dir]
\n"); return 0; } if (!n) { fprintf(outfile,"
\n"); free_dir(sav); return 0; } if (flimit > 0 && n > flimit) { fprintf(outfile," [%d entries exceeds filelimit, not opening dir]
\n",n); free_dir(sav); return 0; } if (cmpfunc) qsort(dir,n,sizeof(struct _info *),cmpfunc); if (lev >= maxdirs-1) { dirs = xrealloc(dirs,sizeof(int) * (maxdirs += 1024)); memset(dirs+(maxdirs-1024), 0, sizeof(int) * 1024); } dirs[lev] = 1; if (!*(dir+1)) dirs[lev] = 2; fprintf(outfile,"
\n"); path = malloc(pathsize=4096); while(*dir) { if (!noindent) indent(lev); fillinfo(path,*dir); if (path[0] == ' ') { path[0] = '['; for(i=0;path[i];i++) { if (path[i] == ' ') fprintf(outfile,"%s",sp); else fprintf(outfile,"%c", path[i]); } fprintf(outfile,"]%s%s", sp, sp); } if (colorize) { if ((*dir)->lnk && linktargetcolor) colored = color((*dir)->lnkmode,(*dir)->name,(*dir)->orphan,FALSE); else colored = color((*dir)->mode,(*dir)->name,(*dir)->orphan,FALSE); } if (fflag) { if (sizeof(char) * (strlen(d)+strlen((*dir)->name)+2) > pathsize) path=xrealloc(path,pathsize=(sizeof(char) * (strlen(d)+strlen((*dir)->name)+1024))); if (!strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->name); else sprintf(path,"%s/%s",d,(*dir)->name); } else { if (sizeof(char) * (strlen((*dir)->name)+1) > pathsize) path=xrealloc(path,pathsize=(sizeof(char) * (strlen((*dir)->name)+1024))); sprintf(path,"%s",(*dir)->name); } /* This is really hackish and should be done over. */ if (Rflag && (lev == Level) && (*dir)->isdir) { if (nolinks) fprintf(outfile,"%s",(*dir)->name); else { fprintf(outfile,"name); fprintf(outfile,"/00Tree.html\">"); html_encode(outfile,(*dir)->name); fprintf(outfile,"
\n"); } hdir = gnu_getcwd(); if (sizeof(char) * (strlen(hdir)+strlen(d)+strlen((*dir)->name)+2) > pathsize) path = xrealloc(path, pathsize = sizeof(char) * (strlen(hdir)+strlen(d)+strlen((*dir)->name) + 1024)); sprintf(path,"%s%s/%s",hdir,d+1,(*dir)->name); fprintf(stderr,"Entering directory %s\n",path); hcmd = xmalloc(sizeof(char) * (49 + strlen(host) + strlen(d) + strlen((*dir)->name)) + 10 + (2*strlen(path))); sprintf(hcmd,"tree -n -H \"%s%s/%s\" -L %d -R -o \"%s/00Tree.html\" \"%s\"\n", host,d+1,(*dir)->name,Level+1,path,path); system(hcmd); free(hdir); free(hcmd); } else { if (nolinks) { if (force_color) { /* * Note that the B element has been set to normal weight in the * style portion of the output. so using will just gives us a element * for which we can assign a color class to. */ fprintf(outfile, "%s", (*dir)->isdir ? "DIR" : (*dir)->isexe ? "EXEC" : (*dir)->isfifo ? "FIFO" : (*dir)->issok ? "SOCK" : "NORM", (*dir)->name); } else fprintf(outfile,"%s",(*dir)->name); } else { if (force_color) { sprintf(hclr, "%s", (*dir)->isdir ? "DIR" : (*dir)->isexe ? "EXEC" : (*dir)->isfifo ? "FIFO" : (*dir)->issok ? "SOCK" : "NORM"); fprintf(outfile,"%s", hclr, host,d+1,(*dir)->name, ((*dir)->isdir?"/":""),(*dir)->name); } else { fprintf(outfile,"name); fprintf(outfile,"%s\">",((*dir)->isdir?"/":"")); html_encode(outfile,(*dir)->name); fprintf(outfile,""); } } } if (colored) fprintf(outfile,"%s",endcode); if (Fflag && !(*dir)->lnk) { if ((c = Ftype((*dir)->mode))) fputc(c, outfile); } if ((*dir)->isdir) { if ((*dir)->lnk) { if (lflag && !(xdev && dev != (*dir)->dev)) { if (findino((*dir)->inode,(*dir)->dev)) { fprintf(outfile," [recursive, not followed]"); } else { saveino((*dir)->inode, (*dir)->dev); if (*(*dir)->lnk == '/') listdir((*dir)->lnk,dt,ft,lev+1,dev); else { if (strlen(d)+strlen((*dir)->lnk)+2 > pathsize) path=xrealloc(path,pathsize=(strlen(d)+strlen((*dir)->name)+1024)); if (fflag && !strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->lnk); else sprintf(path,"%s/%s",d,(*dir)->lnk); listdir(path,dt,ft,lev+1,dev); } nlf = TRUE; } } } else if (!(xdev && dev != (*dir)->dev)) { if (strlen(d)+strlen((*dir)->name)+2 > pathsize) path=xrealloc(path,pathsize=(strlen(d)+strlen((*dir)->name)+1024)); if (fflag && !strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->name); else sprintf(path,"%s/%s",d,(*dir)->name); saveino((*dir)->inode, (*dir)->dev); listdir(path,dt,ft,lev+1,dev); nlf = TRUE; } *dt += 1; } else *ft += 1; if (*(dir+1) && !*(dir+2)) dirs[lev] = 2; if (nlf) nlf = FALSE; else fprintf(outfile,"
\n"); dir++; } dirs[lev] = 0; free(path); free_dir(sav); return 0; } off_t html_rlistdir(char *d, int *dt, int *ft, u_long lev, dev_t dev) { struct _info **dir; off_t size = 0; char *err; dir = getfulltree(d, lev, dev, &size, &err); memset(dirs, 0, sizeof(int) * maxdirs); htmlr_listdir(dir, d, dt, ft, lev); return size; } void htmlr_listdir(struct _info **dir, char *d, int *dt, int *ft, u_long lev) { char *path; long pathsize = 0; bool nlf = FALSE, colored = FALSE; struct _info **sav = dir; int i,c; char hclr[20], *hdir, *hcmd; if (dir == NULL) return; dirs[lev] = 1; if (!*(dir+1)) dirs[lev] = 2; fprintf(outfile,"
\n"); path = malloc(pathsize=4096); while(*dir) { if (!noindent) indent(lev); fillinfo(path,*dir); if (path[0] == ' ') { path[0] = '['; for(i=0;path[i];i++) { if (path[i] == ' ') fprintf(outfile,"%s",sp); else fprintf(outfile,"%c", path[i]); } fprintf(outfile,"]%s%s", sp, sp); } if (colorize) { if ((*dir)->lnk && linktargetcolor) colored = color((*dir)->lnkmode,(*dir)->name,(*dir)->orphan,FALSE); else colored = color((*dir)->mode,(*dir)->name,(*dir)->orphan,FALSE); } if (fflag) { if (sizeof(char) * (strlen(d)+strlen((*dir)->name)+2) > pathsize) path=xrealloc(path,pathsize=(sizeof(char) * (strlen(d)+strlen((*dir)->name)+1024))); if (!strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->name); else sprintf(path,"%s/%s",d,(*dir)->name); } else { if (sizeof(char) * (strlen((*dir)->name)+1) > pathsize) path=xrealloc(path,pathsize=(sizeof(char) * (strlen((*dir)->name)+1024))); sprintf(path,"%s",(*dir)->name); } /* This is really hackish and should be done over. */ if (Rflag && (lev == Level) && (*dir)->isdir) { if (nolinks) fprintf(outfile,"%s",(*dir)->name); else { fprintf(outfile,"name); fprintf(outfile,"/00Tree.html\">"); html_encode(outfile,(*dir)->name); fprintf(outfile,"
\n"); } hdir = gnu_getcwd(); if (sizeof(char) * (strlen(hdir)+strlen(d)+strlen((*dir)->name)+2) > pathsize) path = xrealloc(path, pathsize = sizeof(char) * (strlen(hdir)+strlen(d)+strlen((*dir)->name) + 1024)); sprintf(path,"%s%s/%s",hdir,d+1,(*dir)->name); fprintf(stderr,"Entering directory %s\n",path); hcmd = xmalloc(sizeof(char) * (49 + strlen(host) + strlen(d) + strlen((*dir)->name)) + 10 + (2*strlen(path))); sprintf(hcmd,"tree -n -H \"%s%s/%s\" -L %d -R -o \"%s/00Tree.html\" \"%s\"\n", host,d+1,(*dir)->name,Level+1,path,path); system(hcmd); free(hdir); free(hcmd); } else { if (nolinks) { if (force_color) { /* * Note that the B element has been set to normal weight in the * style portion of the output. so using will just gives us a element * for which we can assign a color class to. */ fprintf(outfile, "%s", (*dir)->isdir ? "DIR" : (*dir)->isexe ? "EXEC" : (*dir)->isfifo ? "FIFO" : (*dir)->issok ? "SOCK" : "NORM", (*dir)->name); } else fprintf(outfile,"%s",(*dir)->name); } else { if (force_color) { sprintf(hclr, "%s", (*dir)->isdir ? "DIR" : (*dir)->isexe ? "EXEC" : (*dir)->isfifo ? "FIFO" : (*dir)->issok ? "SOCK" : "NORM"); fprintf(outfile,"%s", hclr, host,d+1,(*dir)->name, ((*dir)->isdir?"/":""),(*dir)->name); } else { fprintf(outfile,"name); fprintf(outfile,"%s\">",((*dir)->isdir?"/":"")); html_encode(outfile,(*dir)->name); fprintf(outfile,""); } } } if (colored) fprintf(outfile,"%s",endcode); if (Fflag && !(*dir)->lnk) { if ((c = Ftype((*dir)->mode))) fputc(c, outfile); } if ((*dir)->child) { if (strlen(d)+strlen((*dir)->name)+2 > pathsize) path=xrealloc(path,pathsize=(strlen(d)+strlen((*dir)->name)+1024)); if (!strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->name); else sprintf(path,"%s/%s",d,(*dir)->name); htmlr_listdir((*dir)->child, path, dt, ft, lev+1); nlf = TRUE; *dt += 1; } else { if ((*dir)->isdir) *dt += 1; else *ft += 1; } if (*(dir+1) && !*(dir+2)) dirs[lev] = 2; if (nlf) nlf = FALSE; else fprintf(outfile,"
\n"); dir++; } dirs[lev] = 0; free(path); free_dir(sav); return; } void html_encode(FILE *fd, char *s) { for(;*s;s++) { switch(*s) { case '<': fputs("<",fd); break; case '>': fputs(">",fd); break; case '&': fputs("&",fd); break; case '"': fputs(""",fd); break; default: fputc(*s,fd); // fputc(isprint(*s)?*s:'?',fd); break; } } } void url_encode(FILE *fd, char *s) { for(;*s;s++) { switch(*s) { case ' ': case '"': case '#': case '%': case '<': case '>': case '[': case ']': case '^': case '\\': case '?': case '+': fprintf(fd,"%%%02X",*s); break; case '&': fprintf(fd,"&"); break; default: fprintf(fd,isprint((u_int)*s)?"%c":"%%%02X",(u_char)*s); break; } } } tree-1.8.0/json.c0000644001052500001440000002312513225441667012777 0ustar sbakerusers/* $Copyright: $ * Copyright (c) 1996 - 2018 by Steve Baker (ice@mama.indstate.edu) * All Rights reserved * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "tree.h" extern bool dflag, lflag, pflag, sflag, Fflag, aflag, fflag, uflag, gflag; extern bool Dflag, inodeflag, devflag, Rflag, cflag, hflag, siflag; extern bool noindent, force_color, xdev, nolinks, flimit; extern const int ifmt[]; extern const char fmt[], *ftype[]; extern struct _info **(*getfulltree)(char *d, u_long lev, dev_t dev, off_t *size, char **err); extern void (*listdir)(char *, int *, int *, u_long, dev_t); extern int (*cmpfunc)(); extern FILE *outfile; extern int Level, *dirs, maxdirs; extern char *endcode; /* JSON code courtesy of Florian Sesser [ {"type": "directory", "name": "name", "mode": "0777", "user": "user", "group": "group", "inode": ###, "dev": ####, "time": "00:00 00-00-0000", "contents": [ {"type": "link", "name": "name", "target": "name", "contents": [... if link is followed, otherwise this is empty.]} {"type": "file", "name": "name", "mode": "0777", "size": ###, "group": "group", "inode": ###, "dev": ###, "time": "00:00 00-00-0000"} {"type": "socket", "name": "", "error": "some error" ...} {"type": "block", "name": "" ...}, {"type": "char", "name": "" ...}, {"type": "fifo", "name": "" ...}, {"type": "door", "name": "" ...}, {"type": "port", "name": "" ...} ]}, {"type": "report", "size": ###, "files": ###, "directories": ###} ] */ off_t json_listdir(char *d, int *dt, int *ft, u_long lev, dev_t dev) { char *path; bool nlf = FALSE; long pathsize = 0; struct _info **dir, **sav; struct stat sb; int t, n, mt; if ((Level >= 0) && (lev > Level)) { if (!noindent) fputc('\n',outfile); return 0; } if (xdev && lev == 0) { stat(d,&sb); dev = sb.st_dev; } sav = dir = read_dir(d,&n); if (!dir && n) { fprintf(outfile,"{\"error\": \"opening dir\"}%s",noindent?"":"\n"); return 0; } if (!n) { if (!noindent) fputc('\n', outfile); free_dir(sav); return 0; } if (flimit > 0 && n > flimit) { fprintf(outfile,", \"error\": \"%d entries exceeds filelimit, not opening dir\"\n",n); free_dir(sav); return 0; } if (cmpfunc) qsort(dir,n,sizeof(struct _info *),cmpfunc); if (lev >= maxdirs-1) { dirs = xrealloc(dirs,sizeof(int) * (maxdirs += 1024)); memset(dirs+(maxdirs-1024), 0, sizeof(int) * 1024); } dirs[lev] = 1; if (!*(dir+1)) dirs[lev] = 2; if (!noindent) fprintf(outfile,"\n"); path = malloc(pathsize=4096); while(*dir) { if (!noindent) json_indent(lev); if ((*dir)->lnk) mt = (*dir)->mode & S_IFMT; else mt = (*dir)->mode & S_IFMT; for(t=0;ifmt[t];t++) if (ifmt[t] == mt) break; fprintf(outfile,"{\"type\":\"%s\"", ftype[t]); if (fflag) { if (sizeof(char) * (strlen(d)+strlen((*dir)->name)+2) > pathsize) path=xrealloc(path,pathsize=(sizeof(char) * (strlen(d)+strlen((*dir)->name)+1024))); if (!strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->name); else sprintf(path,"%s/%s",d,(*dir)->name); } else { if (sizeof(char) * (strlen((*dir)->name)+1) > pathsize) path=xrealloc(path,pathsize=(sizeof(char) * (strlen((*dir)->name)+1024))); sprintf(path,"%s",(*dir)->name); } fprintf(outfile, ",\"name\":\""); html_encode(outfile,path); fputc('"',outfile); if ((*dir)->lnk) { fprintf(outfile, ",\"target\":\""); html_encode(outfile,(*dir)->lnk); fputc('"',outfile); } json_fillinfo(*dir); if (!(*dir)->isdir && !(*dir)->lnk) { fputc('}',outfile); if (*(dir+1)) fputc(',',outfile); } else fprintf(outfile, ",\"contents\":["); if ((*dir)->isdir) { if ((*dir)->lnk) { if (lflag && !(xdev && dev != (*dir)->dev)) { if (findino((*dir)->inode,(*dir)->dev)) { fprintf(outfile,"{\"error\":\"recursive, not followed\"}"); } else { saveino((*dir)->inode, (*dir)->dev); if (*(*dir)->lnk == '/') listdir((*dir)->lnk,dt,ft,lev+1,dev); else { if (strlen(d)+strlen((*dir)->lnk)+2 > pathsize) path=xrealloc(path,pathsize=(strlen(d)+strlen((*dir)->name)+1024)); if (fflag && !strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->lnk); else sprintf(path,"%s/%s",d,(*dir)->lnk); listdir(path,dt,ft,lev+1,dev); } nlf = TRUE; } } } else if (!(xdev && dev != (*dir)->dev)) { if (strlen(d)+strlen((*dir)->name)+2 > pathsize) path=xrealloc(path,pathsize=(strlen(d)+strlen((*dir)->name)+1024)); if (fflag && !strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->name); else sprintf(path,"%s/%s",d,(*dir)->name); saveino((*dir)->inode, (*dir)->dev); listdir(path,dt,ft,lev+1,dev); nlf = TRUE; } *dt += 1; } else *ft += 1; if (*(dir+1) && !*(dir+2)) dirs[lev] = 2; if (nlf) { nlf = FALSE; if (!noindent) json_indent(lev); } if ((*dir)->isdir || (*dir)->lnk) { fprintf(outfile, "]}"); if (*(dir+1)) fputc(',',outfile); } if (!noindent) fputc('\n', outfile); dir++; } dirs[lev] = 0; free(path); free_dir(sav); return 0; } off_t json_rlistdir(char *d, int *dt, int *ft, u_long lev, dev_t dev) { struct _info **dir; off_t size = 0; char *err; dir = getfulltree(d, lev, dev, &size, &err); memset(dirs, 0, sizeof(int) * maxdirs); jsonr_listdir(dir, d, dt, ft, lev); return size; } void jsonr_listdir(struct _info **dir, char *d, int *dt, int *ft, u_long lev) { char *path; long pathsize = 0; struct _info **sav = dir; bool nlf = FALSE; int mt, t; if (dir == NULL) return; dirs[lev] = 1; if (!*(dir+1)) dirs[lev] = 2; if (!noindent) fprintf(outfile,"\n"); path = malloc(pathsize=4096); while(*dir) { if (!noindent) json_indent(lev); if ((*dir)->lnk) mt = (*dir)->mode & S_IFMT; else mt = (*dir)->mode & S_IFMT; for(t=0;ifmt[t];t++) if (ifmt[t] == mt) break; fprintf(outfile,"{\"type\":\"%s\"", ftype[t]); if (fflag) { if (sizeof(char) * (strlen(d)+strlen((*dir)->name)+2) > pathsize) path=xrealloc(path,pathsize=(sizeof(char) * (strlen(d)+strlen((*dir)->name)+1024))); if (!strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->name); else sprintf(path,"%s/%s",d,(*dir)->name); } else { if (sizeof(char) * (strlen((*dir)->name)+1) > pathsize) path=xrealloc(path,pathsize=(sizeof(char) * (strlen((*dir)->name)+1024))); sprintf(path,"%s",(*dir)->name); } fprintf(outfile, ",\"name\":\""); html_encode(outfile,path); fputc('"',outfile); if ((*dir)->lnk) { fprintf(outfile, ",\"target\":\""); html_encode(outfile,(*dir)->lnk); fputc('"',outfile); } json_fillinfo(*dir); if (mt != S_IFDIR && mt != S_IFLNK && (*dir)->err == NULL) { fputc('}', outfile); if (*(dir+1)) fputc(',',outfile); } else fprintf(outfile, ",\"contents\":["); if ((*dir)->err) { fprintf(outfile,",\"error\":\"%s\"", (*dir)->err); free((*dir)->err); (*dir)->err = NULL; } if ((*dir)->child) { if (fflag) { if (strlen(d)+strlen((*dir)->name)+2 > pathsize) path=xrealloc(path,pathsize=(strlen(d)+strlen((*dir)->name)+1024)); if (!strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->name); else sprintf(path,"%s/%s",d,(*dir)->name); } jsonr_listdir((*dir)->child, fflag? path : NULL, dt, ft, lev+1); nlf = TRUE; *dt += 1; } else { if ((*dir)->isdir) *dt += 1; else *ft += 1; } if (*(dir+1) && !*(dir+2)) dirs[lev] = 2; if (nlf) { nlf = FALSE; if (!noindent) json_indent(lev); } if (mt == S_IFDIR || mt == S_IFLNK || (*dir)->err != NULL) { fprintf(outfile,"]}"); if (*(dir+1)) fputc(',',outfile); } if (!noindent) putc('\n',outfile); dir++; } dirs[lev] = 0; free(path); free_dir(sav); } void json_indent(int maxlevel) { int i; fprintf(outfile, " "); for(i=0; iinode); #else if (inodeflag) fprintf(outfile,",\"inode\":%ld",(long int)ent->inode); #endif if (devflag) fprintf(outfile, ",\"dev\":%d", (int)ent->dev); #ifdef __EMX__ if (pflag) fprintf(outfile, ",\"mode\":\"%04o\",\"prot\":\"%s\"",ent->attr, prot(ent->attr)); #else if (pflag) fprintf(outfile, ",\"mode\":\"%04o\",\"prot\":\"%s\"", ent->mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX), prot(ent->mode)); #endif if (uflag) fprintf(outfile, ",\"user\":\"%s\"", uidtoname(ent->uid)); if (gflag) fprintf(outfile, ",\"group\":\"%s\"", gidtoname(ent->gid)); if (sflag) { if (hflag || siflag) { char nbuf[64]; int i; psize(nbuf,ent->size); for(i=0; isspace(nbuf[i]); i++); // trim() hack fprintf(outfile, ",\"size\":\"%s\"", nbuf+i); } else fprintf(outfile, ",\"size\":%lld", (long long int)ent->size); } if (Dflag) fprintf(outfile, ",\"time\":\"%s\"", do_date(cflag? ent->ctime : ent->mtime)); } tree-1.8.0/strverscmp.c0000644001052500001440000001231211055310032014206 0ustar sbakerusers/* Compare strings while treating digits characters numerically. Copyright (C) 1997, 2002, 2005 Free Software Foundation, Inc. This file is part of the libiberty library. Contributed by Jean-François Bignolles , 1997. Libiberty is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. Libiberty 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with the GNU C Library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ /* #include "libiberty.h" */ /* #include "safe-ctype.h" */ #include /* @deftypefun int strverscmp (const char *@var{s1}, const char *@var{s2}) The @code{strverscmp} function compares the string @var{s1} against @var{s2}, considering them as holding indices/version numbers. Return value follows the same conventions as found in the @code{strverscmp} function. In fact, if @var{s1} and @var{s2} contain no digits, @code{strverscmp} behaves like @code{strcmp}. Basically, we compare strings normally (character by character), until we find a digit in each string - then we enter a special comparison mode, where each sequence of digits is taken as a whole. If we reach the end of these two parts without noticing a difference, we return to the standard comparison mode. There are two types of numeric parts: "integral" and "fractional" (those begin with a '0'). The types of the numeric parts affect the way we sort them: @itemize @bullet @item integral/integral: we compare values as you would expect. @item fractional/integral: the fractional part is less than the integral one. Again, no surprise. @item fractional/fractional: the things become a bit more complex. If the common prefix contains only leading zeroes, the longest part is less than the other one; else the comparison behaves normally. @end itemize @smallexample strverscmp ("no digit", "no digit") @result{} 0 // @r{same behavior as strcmp.} strverscmp ("item#99", "item#100") @result{} <0 // @r{same prefix, but 99 < 100.} strverscmp ("alpha1", "alpha001") @result{} >0 // @r{fractional part inferior to integral one.} strverscmp ("part1_f012", "part1_f01") @result{} >0 // @r{two fractional parts.} strverscmp ("foo.009", "foo.0") @result{} <0 // @r{idem, but with leading zeroes only.} @end smallexample This function is especially useful when dealing with filename sorting, because filenames frequently hold indices/version numbers. @end deftypefun */ /* states: S_N: normal, S_I: comparing integral part, S_F: comparing fractional parts, S_Z: idem but with leading Zeroes only */ #define S_N 0x0 #define S_I 0x4 #define S_F 0x8 #define S_Z 0xC /* result_type: CMP: return diff; LEN: compare using len_diff/diff */ #define CMP 2 #define LEN 3 /* Compare S1 and S2 as strings holding indices/version numbers, returning less than, equal to or greater than zero if S1 is less than, equal to or greater than S2 (for more info, see the Glibc texinfo doc). */ int strverscmp (const char *s1, const char *s2) { const unsigned char *p1 = (const unsigned char *) s1; const unsigned char *p2 = (const unsigned char *) s2; unsigned char c1, c2; int state; int diff; /* Symbol(s) 0 [1-9] others (padding) Transition (10) 0 (01) d (00) x (11) - */ static const unsigned int next_state[] = { /* state x d 0 - */ /* S_N */ S_N, S_I, S_Z, S_N, /* S_I */ S_N, S_I, S_I, S_I, /* S_F */ S_N, S_F, S_F, S_F, /* S_Z */ S_N, S_F, S_Z, S_Z }; static const int result_type[] = { /* state x/x x/d x/0 x/- d/x d/d d/0 d/- 0/x 0/d 0/0 0/- -/x -/d -/0 -/- */ /* S_N */ CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP, /* S_I */ CMP, -1, -1, CMP, +1, LEN, LEN, CMP, +1, LEN, LEN, CMP, CMP, CMP, CMP, CMP, /* S_F */ CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP, /* S_Z */ CMP, +1, +1, CMP, -1, CMP, CMP, CMP, -1, CMP, CMP, CMP }; if (p1 == p2) return 0; c1 = *p1++; c2 = *p2++; /* Hint: '0' is a digit too. */ state = S_N | ((c1 == '0') + (isdigit (c1) != 0)); while ((diff = c1 - c2) == 0 && c1 != '\0') { state = next_state[state]; c1 = *p1++; c2 = *p2++; state |= (c1 == '0') + (isdigit (c1) != 0); } state = result_type[state << 2 | (((c2 == '0') + (isdigit (c2) != 0)))]; switch (state) { case CMP: return diff; case LEN: while (isdigit (*p1++)) if (!isdigit (*p2++)) return 1; return isdigit (*p2) ? -1 : diff; default: return state; } } tree-1.8.0/tree.c0000644001052500001440000010774713225441622012771 0ustar sbakerusers/* $Copyright: $ * Copyright (c) 1996 - 2018 by Steve Baker (ice@mama.indstate.edu) * All Rights reserved * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "tree.h" static char *version ="$Version: $ tree v1.8.0 (c) 1996 - 2018 by Steve Baker, Thomas Moore, Francesc Rocher, Florian Sesser, Kyosuke Tokoro $"; static char *hversion="\t\t tree v1.8.0 %s 1996 - 2018 by Steve Baker and Thomas Moore
\n" "\t\t HTML output hacked and copyleft %s 1998 by Francesc Rocher
\n" "\t\t JSON output hacked and copyleft %s 2014 by Florian Sesser
\n" "\t\t Charsets / OS/2 support %s 2001 by Kyosuke Tokoro\n"; /* Globals */ bool dflag, lflag, pflag, sflag, Fflag, aflag, fflag, uflag, gflag; bool qflag, Nflag, Qflag, Dflag, inodeflag, devflag, hflag, Rflag; bool Hflag, siflag, cflag, Xflag, Jflag, duflag, pruneflag; bool noindent, force_color, nocolor, xdev, noreport, nolinks, flimit, dirsfirst; bool ignorecase, matchdirs, fromfile; bool reverse; char *pattern = NULL, *ipattern = NULL, *host = NULL, *title = "Directory Tree", *sp = " ", *_nl = "\n"; char *file_comment = "#", *file_pathsep = "/"; char *timefmt = NULL; const char *charset = NULL; struct _info **(*getfulltree)(char *d, u_long lev, dev_t dev, off_t *size, char **err) = unix_getfulltree; off_t (*listdir)(char *, int *, int *, u_long, dev_t) = unix_listdir; int (*cmpfunc)() = alnumsort; char *sLevel, *curdir, *outfilename = NULL; FILE *outfile; int Level, *dirs, maxdirs; int mb_cur_max; #ifdef __EMX__ const u_short ifmt[]={ FILE_ARCHIVED, FILE_DIRECTORY, FILE_SYSTEM, FILE_HIDDEN, FILE_READONLY, 0}; #else #ifdef S_IFPORT const u_int ifmt[] = {S_IFREG, S_IFDIR, S_IFLNK, S_IFCHR, S_IFBLK, S_IFSOCK, S_IFIFO, S_IFDOOR, S_IFPORT, 0}; const char fmt[] = "-dlcbspDP?"; const char *ftype[] = {"file", "directory", "link", "char", "block", "socket", "fifo", "door", "port", "unknown", NULL}; #else const u_int ifmt[] = {S_IFREG, S_IFDIR, S_IFLNK, S_IFCHR, S_IFBLK, S_IFSOCK, S_IFIFO, 0}; const char fmt[] = "-dlcbsp?"; const char *ftype[] = {"file", "directory", "link", "char", "block", "socket", "fifo", "unknown", NULL}; #endif #endif struct sorts { char *name; int (*cmpfunc)(); } sorts[] = { {"name", alnumsort}, {"version", versort}, {"size", fsizesort}, {"mtime", mtimesort}, {"ctime", ctimesort}, {NULL, NULL} }; /* Externs */ /* hash.c */ extern struct xtable *gtable[256], *utable[256]; extern struct inotable *itable[256]; /* color.c */ extern bool colorize, ansilines, linktargetcolor; extern char *leftcode, *rightcode, *endcode; extern const struct linedraw *linedraw; int main(int argc, char **argv) { char **dirname = NULL; int i,j=0,k,n,optf,p,q,dtotal,ftotal,colored = FALSE; struct stat st; char sizebuf[64], *stmp; off_t size = 0; mode_t mt; bool needfulltree; q = p = dtotal = ftotal = 0; aflag = dflag = fflag = lflag = pflag = sflag = Fflag = uflag = gflag = FALSE; Dflag = qflag = Nflag = Qflag = Rflag = hflag = Hflag = siflag = cflag = FALSE; noindent = force_color = nocolor = xdev = noreport = nolinks = reverse = FALSE; ignorecase = matchdirs = dirsfirst = inodeflag = devflag = Xflag = Jflag = FALSE; duflag = pruneflag = FALSE; flimit = 0; dirs = xmalloc(sizeof(int) * (maxdirs=4096)); memset(dirs, 0, sizeof(int) * maxdirs); dirs[0] = 0; Level = -1; setlocale(LC_CTYPE, ""); setlocale(LC_COLLATE, ""); charset = getcharset(); if (charset == NULL && strcmp(nl_langinfo(CODESET), "UTF-8") == 0) { charset = "UTF-8"; } /* Until I get rid of this hack, make it linux/cygwin/HP nonstop only: */ #if defined (LINUX) || defined (CYGWIN) || defined (__TANDEM) mb_cur_max = (int)MB_CUR_MAX; #else mb_cur_max = 1; #endif memset(utable,0,sizeof(utable)); memset(gtable,0,sizeof(gtable)); memset(itable,0,sizeof(itable)); optf = TRUE; for(n=i=1;i%s
",host); else fprintf(outfile,"%s",host); } else { if (force_color) fprintf(outfile,"%s",host,host); else fprintf(outfile,"%s",host,host); } curdir = gnu_getcwd(); } else if (Xflag) { fprintf(outfile,"%s%s",_nl,_nl); } else if (Jflag) fputc('[',outfile); if (dirname) { for(colored=i=0;dirname[i];i++,colored=0) { if (fflag) { do { j=strlen(dirname[i]); if (j > 1 && dirname[i][j-1] == '/') dirname[i][--j] = 0; } while (j > 1 && dirname[i][j-1] == '/'); } if ((n = lstat(dirname[i],&st)) >= 0) { saveino(st.st_ino, st.st_dev); if (colorize) colored = color(st.st_mode,dirname[i],n<0,FALSE); size += st.st_size; } if (Xflag || Jflag) { mt = st.st_mode & S_IFMT; for(j=0;ifmt[j];j++) if (ifmt[j] == mt) break; if (Xflag) fprintf(outfile,"%s<%s name=\"%s\">", noindent?"":" ", ftype[j], dirname[i]); else if (Jflag) { if (i) fprintf(outfile, ","); fprintf(outfile,"%s{\"type\":\"%s\",\"name\":\"%s\",\"contents\":[", noindent?"":"\n ", ftype[j], dirname[i]); } } else if (!Hflag) printit(dirname[i]); if (colored) fprintf(outfile,"%s",endcode); if (!Hflag) size += listdir(dirname[i],&dtotal,&ftotal,0,0); else { if (chdir(dirname[i])) { fprintf(outfile,"%s [error opening dir]\n",dirname[i]); exit(1); } else { size += listdir(".",&dtotal,&ftotal,0,0); chdir(curdir); } } if (Xflag) fprintf(outfile,"%s\n",noindent?"":" ", ftype[j]); if (Jflag) fprintf(outfile,"%s]}",noindent?"":" "); } } else { if ((n = lstat(".",&st)) >= 0) { saveino(st.st_ino, st.st_dev); if (colorize) colored = color(st.st_mode,".",n<0,FALSE); size = st.st_size; } if (Xflag) fprintf(outfile,"%s",noindent?"":" "); else if (Jflag) fprintf(outfile, "{\"type\":\"directory\",\"name\": \".\",\"contents\":["); else if (!Hflag) fprintf(outfile,"."); if (colored) fprintf(outfile,"%s",endcode); size += listdir(".",&dtotal,&ftotal,0,0); if (Xflag) fprintf(outfile,"%s%s",noindent?"":" ", _nl); if (Jflag) fprintf(outfile,"%s]}",noindent?"":" "); } if (Hflag) fprintf(outfile,"\t

\n\t

\n\t

\n"); if (!noreport) { if (Xflag) { fprintf(outfile,"%s%s",noindent?"":" ", _nl); if (duflag) fprintf(outfile,"%s%lld%s", noindent?"":" ", (long long int)size, _nl); fprintf(outfile,"%s%d%s", noindent?"":" ", dtotal, _nl); if (!dflag) fprintf(outfile,"%s%d%s", noindent?"":" ", ftotal, _nl); fprintf(outfile,"%s%s",noindent?"":" ", _nl); } else if (Jflag) { fprintf(outfile, ",%s{\"type\":\"report\"",noindent?"":"\n "); if (duflag) fprintf(outfile,",\"size\":%lld", (long long int)size); fprintf(outfile,",\"directories\":%d", dtotal); if (!dflag) fprintf(outfile,",\"files\":%d", ftotal); fprintf(outfile, "}"); } else { if (duflag) { psize(sizebuf, size); fprintf(outfile,"\n%s%s used in ", sizebuf, hflag || siflag? "" : " bytes"); } else fputc('\n', outfile); if (dflag) fprintf(outfile,"%d director%s\n",dtotal,(dtotal==1? "y":"ies")); else fprintf(outfile,"%d director%s, %d file%s\n",dtotal,(dtotal==1? "y":"ies"),ftotal,(ftotal==1? "":"s")); } } if (Hflag) { fprintf(outfile,"\t

\n\t

\n"); fprintf(outfile,"\t
\n"); fprintf(outfile,"\t

\n"); fprintf(outfile,hversion,linedraw->copy, linedraw->copy, linedraw->copy, linedraw->copy); fprintf(outfile,"\t

\n"); fprintf(outfile,"\n"); fprintf(outfile,"\n"); } else if (Xflag) { fprintf(outfile,"\n"); } else if (Jflag) { fprintf(outfile, "%s]\n",_nl); } if (outfilename != NULL) fclose(outfile); return 0; } void usage(int n) { /* 123456789!123456789!123456789!123456789!123456789!123456789!123456789!123456789! */ /* \t9!123456789!123456789!123456789!123456789!123456789!123456789!123456789! */ fprintf(n < 2? stderr: stdout, "usage: tree [-acdfghilnpqrstuvxACDFJQNSUX] [-H baseHREF] [-T title ]\n" "\t[-L level [-R]] [-P pattern] [-I pattern] [-o filename] [--version]\n" "\t[--help] [--inodes] [--device] [--noreport] [--nolinks] [--dirsfirst]\n" "\t[--charset charset] [--filelimit[=]#] [--si] [--timefmt[=]]\n" "\t[--sort[=]] [--matchdirs] [--ignore-case] [--fromfile] [--]\n" "\t[]\n"); if (n < 2) return; fprintf(stdout, " ------- Listing options -------\n" " -a All files are listed.\n" " -d List directories only.\n" " -l Follow symbolic links like directories.\n" " -f Print the full path prefix for each file.\n" " -x Stay on current filesystem only.\n" " -L level Descend only level directories deep.\n" " -R Rerun tree when max dir level reached.\n" " -P pattern List only those files that match the pattern given.\n" " -I pattern Do not list files that match the given pattern.\n" " --ignore-case Ignore case when pattern matching.\n" " --matchdirs Include directory names in -P pattern matching.\n" " --noreport Turn off file/directory count at end of tree listing.\n" " --charset X Use charset X for terminal/HTML and indentation line output.\n" " --filelimit # Do not descend dirs with more than # files in them.\n" " --timefmt Print and format time according to the format .\n" " -o filename Output to file instead of stdout.\n" " ------- File options -------\n" " -q Print non-printable characters as '?'.\n" " -N Print non-printable characters as is.\n" " -Q Quote filenames with double quotes.\n" " -p Print the protections for each file.\n" " -u Displays file owner or UID number.\n" " -g Displays file group owner or GID number.\n" " -s Print the size in bytes of each file.\n" " -h Print the size in a more human readable way.\n" " --si Like -h, but use in SI units (powers of 1000).\n" " -D Print the date of last modification or (-c) status change.\n" " -F Appends '/', '=', '*', '@', '|' or '>' as per ls -F.\n" " --inodes Print inode number of each file.\n" " --device Print device ID number to which each file belongs.\n" " ------- Sorting options -------\n" " -v Sort files alphanumerically by version.\n" " -t Sort files by last modification time.\n" " -c Sort files by last status change time.\n" " -U Leave files unsorted.\n" " -r Reverse the order of the sort.\n" " --dirsfirst List directories before files (-U disables).\n" " --sort X Select sort: name,version,size,mtime,ctime.\n" " ------- Graphics options -------\n" " -i Don't print indentation lines.\n" " -A Print ANSI lines graphic indentation lines.\n" " -S Print with CP437 (console) graphics indentation lines.\n" " -n Turn colorization off always (-C overrides).\n" " -C Turn colorization on always.\n" " ------- XML/HTML/JSON options -------\n" " -X Prints out an XML representation of the tree.\n" " -J Prints out an JSON representation of the tree.\n" " -H baseHREF Prints out HTML format with baseHREF as top directory.\n" " -T string Replace the default HTML title and H1 header with string.\n" " --nolinks Turn off hyperlinks in HTML output.\n" " ------- Input options -------\n" " --fromfile Reads paths from files (.=stdin)\n" " ------- Miscellaneous options -------\n" " --version Print version and exit.\n" " --help Print usage and this help message and exit.\n" " -- Options processing terminator.\n"); exit(0); } struct _info **read_dir(char *dir, int *n) { static char *path = NULL, *lbuf = NULL; static long pathsize, lbufsize; struct _info **dl; struct dirent *ent; struct stat lst,st; DIR *d; int ne, p = 0, len, rs; if (path == NULL) { pathsize = lbufsize = strlen(dir)+4096; path=xmalloc(pathsize); lbuf=xmalloc(lbufsize); } *n = 1; if ((d=opendir(dir)) == NULL) return NULL; dl = (struct _info **)xmalloc(sizeof(struct _info *) * (ne = MINIT)); while((ent = (struct dirent *)readdir(d))) { if (!strcmp("..",ent->d_name) || !strcmp(".",ent->d_name)) continue; if (Hflag && !strcmp(ent->d_name,"00Tree.html")) continue; if (!aflag && ent->d_name[0] == '.') continue; if (strlen(dir)+strlen(ent->d_name)+2 > pathsize) path = xrealloc(path,pathsize=(strlen(dir)+strlen(ent->d_name)+4096)); sprintf(path,"%s/%s",dir,ent->d_name); if (lstat(path,&lst) < 0) continue; if ((lst.st_mode & S_IFMT) == S_IFLNK) { if ((rs = stat(path,&st)) < 0) memset(&st, 0, sizeof(st)); } else { rs = 0; st.st_mode = lst.st_mode; st.st_dev = lst.st_dev; st.st_ino = lst.st_ino; } #ifndef __EMX__ if ((lst.st_mode & S_IFMT) != S_IFDIR && !(lflag && ((st.st_mode & S_IFMT) == S_IFDIR))) { if (pattern && patmatch(ent->d_name,pattern) != 1) continue; } if (ipattern && patmatch(ent->d_name,ipattern) == 1) continue; #endif if (dflag && ((st.st_mode & S_IFMT) != S_IFDIR)) continue; #ifndef __EMX__ // if (pattern && ((lst.st_mode & S_IFMT) == S_IFLNK) && !lflag) continue; #endif if (p == (ne-1)) dl = (struct _info **)xrealloc(dl,sizeof(struct _info *) * (ne += MINC)); dl[p] = (struct _info *)xmalloc(sizeof(struct _info)); dl[p]->name = scopy(ent->d_name); /* We should just incorporate struct stat into _info, and elminate this unecessary copying. * Made sense long ago when we had fewer options and didn't need half of stat. */ dl[p]->mode = lst.st_mode; dl[p]->uid = lst.st_uid; dl[p]->gid = lst.st_gid; dl[p]->size = lst.st_size; dl[p]->dev = st.st_dev; dl[p]->inode = st.st_ino; dl[p]->ldev = lst.st_dev; dl[p]->linode = lst.st_ino; dl[p]->lnk = NULL; dl[p]->orphan = FALSE; dl[p]->err = NULL; dl[p]->child = NULL; dl[p]->atime = lst.st_atime; dl[p]->ctime = lst.st_ctime; dl[p]->mtime = lst.st_mtime; #ifdef __EMX__ dl[p]->attr = lst.st_attr; #else if ((lst.st_mode & S_IFMT) == S_IFLNK) { if (lst.st_size+1 > lbufsize) lbuf = xrealloc(lbuf,lbufsize=(lst.st_size+8192)); if ((len=readlink(path,lbuf,lbufsize-1)) < 0) { dl[p]->lnk = scopy("[Error reading symbolic link information]"); dl[p]->isdir = FALSE; dl[p++]->lnkmode = st.st_mode; continue; } else { lbuf[len] = 0; dl[p]->lnk = scopy(lbuf); if (rs < 0) dl[p]->orphan = TRUE; dl[p]->lnkmode = st.st_mode; } } #endif /* These should be elminiated, as they're barely used */ dl[p]->isdir = ((st.st_mode & S_IFMT) == S_IFDIR); dl[p]->issok = ((st.st_mode & S_IFMT) == S_IFSOCK); dl[p]->isfifo = ((st.st_mode & S_IFMT) == S_IFIFO); dl[p++]->isexe = (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) ? 1 : 0; } closedir(d); *n = p; dl[p] = NULL; return dl; } /* This is for all the impossible things people wanted the old tree to do. * This can and will use a large amount of memory for large directory trees * and also take some time. */ struct _info **unix_getfulltree(char *d, u_long lev, dev_t dev, off_t *size, char **err) { char *path; long pathsize = 0; struct _info **dir, **sav, **p, *sp; struct stat sb; int n; u_long lev_tmp; char *tmp_pattern = NULL, *start_rel_path; *err = NULL; if (Level >= 0 && lev > Level) return NULL; if (xdev && lev == 0) { stat(d,&sb); dev = sb.st_dev; } // if the directory name matches, turn off pattern matching for contents if (matchdirs && pattern) { lev_tmp = lev; start_rel_path = d + strlen(d); for (start_rel_path = d + strlen(d); start_rel_path != d; --start_rel_path) { if (*start_rel_path == '/') --lev_tmp; if (lev_tmp <= 0) { if (*start_rel_path) ++start_rel_path; break; } } if (*start_rel_path && patmatch(start_rel_path,pattern) == 1) { tmp_pattern = pattern; pattern = NULL; } } sav = dir = read_dir(d,&n); if (tmp_pattern) { pattern = tmp_pattern; tmp_pattern = NULL; } if (dir == NULL) { *err = scopy("error opening dir"); return NULL; } if (n == 0) { free_dir(sav); return NULL; } path = malloc(pathsize=4096); if (flimit > 0 && n > flimit) { sprintf(path,"%d entries exceeds filelimit, not opening dir",n); *err = scopy(path); free_dir(sav); free(path); return NULL; } if (cmpfunc) qsort(dir,n,sizeof(struct _info *),cmpfunc); if (lev >= maxdirs-1) { dirs = xrealloc(dirs,sizeof(int) * (maxdirs += 1024)); } while (*dir) { if ((*dir)->isdir && !(xdev && dev != (*dir)->dev)) { if ((*dir)->lnk) { if (lflag) { if (findino((*dir)->inode,(*dir)->dev)) { (*dir)->err = scopy("recursive, not followed"); } else { saveino((*dir)->inode, (*dir)->dev); if (*(*dir)->lnk == '/') (*dir)->child = unix_getfulltree((*dir)->lnk,lev+1,dev,&((*dir)->size),&((*dir)->err)); else { if (strlen(d)+strlen((*dir)->lnk)+2 > pathsize) path=xrealloc(path,pathsize=(strlen(d)+strlen((*dir)->name)+1024)); if (fflag && !strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->lnk); else sprintf(path,"%s/%s",d,(*dir)->lnk); (*dir)->child = unix_getfulltree(path,lev+1,dev,&((*dir)->size),&((*dir)->err)); } } } } else { if (strlen(d)+strlen((*dir)->name)+2 > pathsize) path=xrealloc(path,pathsize=(strlen(d)+strlen((*dir)->name)+1024)); if (fflag && !strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->name); else sprintf(path,"%s/%s",d,(*dir)->name); saveino((*dir)->inode, (*dir)->dev); (*dir)->child = unix_getfulltree(path,lev+1,dev,&((*dir)->size),&((*dir)->err)); } // prune empty folders, unless they match the requested pattern if (pruneflag && (*dir)->child == NULL && !(matchdirs && pattern && patmatch((*dir)->name,pattern) == 1)) { sp = *dir; for(p=dir;*p;p++) *p = *(p+1); n--; free(sp->name); if (sp->lnk) free(sp->lnk); free(sp); continue; } } if (duflag) *size += (*dir)->size; dir++; } free(path); if (n == 0) { free_dir(sav); return NULL; } return sav; } /* Sorting functions */ int alnumsort(struct _info **a, struct _info **b) { int v; if (dirsfirst && ((*a)->isdir != (*b)->isdir)) { return (*a)->isdir ? -1 : 1; } v = strcoll((*a)->name,(*b)->name); return reverse? -v : v; } int versort(struct _info **a, struct _info **b) { int v; if (dirsfirst && ((*a)->isdir != (*b)->isdir)) { return (*a)->isdir ? -1 : 1; } v = strverscmp((*a)->name,(*b)->name); return reverse? -v : v; } int mtimesort(struct _info **a, struct _info **b) { int v; if (dirsfirst && ((*a)->isdir != (*b)->isdir)) { return (*a)->isdir ? -1 : 1; } if ((*a)->mtime == (*b)->mtime) { v = strcoll((*a)->name,(*b)->name); return reverse? -v : v; } v = (*a)->mtime == (*b)->mtime? 0 : ((*a)->mtime < (*b)->mtime ? -1 : 1); return reverse? -v : v; } int ctimesort(struct _info **a, struct _info **b) { int v; if (dirsfirst && ((*a)->isdir != (*b)->isdir)) { return (*a)->isdir ? -1 : 1; } if ((*a)->ctime == (*b)->ctime) { v = strcoll((*a)->name,(*b)->name); return reverse? -v : v; } v = (*a)->ctime == (*b)->ctime? 0 : ((*a)->ctime < (*b)->ctime? -1 : 1); return reverse? -v : v; } int sizecmp(off_t a, off_t b) { return (a == b)? 0 : ((a < b)? 1 : -1); } int fsizesort(struct _info **a, struct _info **b) { int v; if (dirsfirst && ((*a)->isdir != (*b)->isdir)) { return (*a)->isdir ? -1 : 1; } v = sizecmp((*a)->size, (*b)->size); if (v == 0) v = strcoll((*a)->name,(*b)->name); return reverse? -v : v; } void *xmalloc (size_t size) { register void *value = malloc (size); if (value == 0) { fprintf(stderr,"tree: virtual memory exhausted.\n"); exit(1); } return value; } void *xrealloc (void *ptr, size_t size) { register void *value = realloc (ptr,size); if (value == 0) { fprintf(stderr,"tree: virtual memory exhausted.\n"); exit(1); } return value; } void free_dir(struct _info **d) { int i; for(i=0;d[i];i++) { free(d[i]->name); if (d[i]->lnk) free(d[i]->lnk); free(d[i]); } free(d); } char *gnu_getcwd() { int size = 100; char *buffer = (char *) xmalloc (size); while (1) { char *value = getcwd (buffer, size); if (value != 0) return buffer; size *= 2; free (buffer); buffer = (char *) xmalloc (size); } } static inline char cond_lower(char c) { return ignorecase ? tolower(c) : c; } /* * Patmatch() code courtesy of Thomas Moore (dark@mama.indstate.edu) * '|' support added by David MacMahon (davidm@astron.Berkeley.EDU) * Case insensitive support added by Jason A. Donenfeld (Jason@zx2c4.com) * returns: * 1 on a match * 0 on a mismatch * -1 on a syntax error in the pattern */ int patmatch(char *buf, char *pat) { int match = 1,m,n; char *bar = strchr(pat, '|'); /* If a bar is found, call patmatch recursively on the two sub-patterns */ if (bar) { /* If the bar is the first or last character, it's a syntax error */ if (bar == pat || !bar[1]) { return -1; } /* Break pattern into two sub-patterns */ *bar = '\0'; match = patmatch(buf, pat); if (!match) { match = patmatch(buf,bar+1); } /* Join sub-patterns back into one pattern */ *bar = '|'; return match; } while(*pat && match) { switch(*pat) { case '[': pat++; if(*pat != '^') { n = 1; match = 0; } else { pat++; n = 0; } while(*pat != ']'){ if(*pat == '\\') pat++; if(!*pat /* || *pat == '/' */ ) return -1; if(pat[1] == '-'){ m = *pat; pat += 2; if(*pat == '\\' && *pat) pat++; if(cond_lower(*buf) >= cond_lower(m) && cond_lower(*buf) <= cond_lower(*pat)) match = n; if(!*pat) pat--; } else if(cond_lower(*buf) == cond_lower(*pat)) match = n; pat++; } buf++; break; case '*': pat++; if(!*pat) return 1; while(*buf && !(match = patmatch(buf++,pat))); return match; case '?': if(!*buf) return 0; buf++; break; case '\\': if(*pat) pat++; default: match = (cond_lower(*buf++) == cond_lower(*pat)); break; } pat++; if(match<1) return match; } if(!*buf) return match; return 0; } /** * They cried out for ANSI-lines (not really), but here they are, as an option * for the xterm and console capable among you, as a run-time option. */ void indent(int maxlevel) { int i; if (ansilines) { if (dirs[0]) fprintf(outfile,"\033(0"); for(i=0; (i <= maxlevel) && dirs[i]; i++) { if (dirs[i+1]) { if (dirs[i] == 1) fprintf(outfile,"\170 "); else printf(" "); } else { if (dirs[i] == 1) fprintf(outfile,"\164\161\161 "); else fprintf(outfile,"\155\161\161 "); } } if (dirs[0]) fprintf(outfile,"\033(B"); } else { if (Hflag) fprintf(outfile,"\t"); for(i=0; (i <= maxlevel) && dirs[i]; i++) { fprintf(outfile,"%s ", dirs[i+1] ? (dirs[i]==1 ? linedraw->vert : (Hflag? "   " : " ") ) : (dirs[i]==1 ? linedraw->vert_left:linedraw->corner)); } } } #ifdef __EMX__ char *prot(long m) #else char *prot(mode_t m) #endif { #ifdef __EMX__ const u_short *p; static char buf[6]; char*cp; for(p=ifmt,cp=strcpy(buf,"adshr");*cp;++p,++cp) if(!(m&*p)) *cp='-'; #else static char buf[11], perms[] = "rwxrwxrwx"; int i, b; for(i=0;ifmt[i] && (m&S_IFMT) != ifmt[i];i++); buf[0] = fmt[i]; /** * Nice, but maybe not so portable, it is should be no less portable than the * old code. */ for(b=S_IRUSR,i=0; i<9; b>>=1,i++) buf[i+1] = (m & (b)) ? perms[i] : '-'; if (m & S_ISUID) buf[3] = (buf[3]=='-')? 'S' : 's'; if (m & S_ISGID) buf[6] = (buf[6]=='-')? 'S' : 's'; if (m & S_ISVTX) buf[9] = (buf[9]=='-')? 'T' : 't'; buf[10] = 0; #endif return buf; } #define SIXMONTHS (6*31*24*60*60) char *do_date(time_t t) { static char buf[256]; struct tm *tm; tm = localtime(&t); if (timefmt) { strftime(buf,255,timefmt,tm); buf[255] = 0; } else { time_t c = time(0); /* Use strftime() so that locale is respected: */ if (t > c || (t+SIXMONTHS) < c) strftime(buf,255,"%b %e %Y",tm); else strftime(buf,255,"%b %e %R", tm); } return buf; } /** * Must fix this someday */ void printit(char *s) { int c; if (Nflag) { if (Qflag) fprintf(outfile, "\"%s\"",s); else fprintf(outfile,"%s",s); return; } if (mb_cur_max > 1) { wchar_t *ws, *tp; ws = xmalloc(sizeof(wchar_t)* (c=(strlen(s)+1))); if (mbstowcs(ws,s,c) != (size_t)-1) { if (Qflag) putc('"',outfile); for(tp=ws;*tp && c > 1;tp++, c--) { if (iswprint(*tp)) fprintf(outfile,"%lc",(wint_t)*tp); else { if (qflag) putc('?',outfile); else fprintf(outfile,"\\%03o",(unsigned int)*tp); } } if (Qflag) putc('"',outfile); free(ws); return; } free(ws); } if (Qflag) putc('"',outfile); for(;*s;s++) { c = (unsigned char)*s; #ifdef __EMX__ if(_nls_is_dbcs_lead(*(unsigned char*)s)){ putc(*s,outfile); putc(*++s,outfile); continue; } #endif if((c >= 7 && c <= 13) || c == '\\' || (c == '"' && Qflag) || (c == ' ' && !Qflag)) { putc('\\',outfile); if (c > 13) putc(c, outfile); else putc("abtnvfr"[c-7], outfile); } else if (isprint(c)) putc(c,outfile); else { if (qflag) { if (mb_cur_max > 1 && c > 127) putc(c,outfile); else putc('?',outfile); } else fprintf(outfile,"\\%03o",c); } } if (Qflag) putc('"',outfile); } int psize(char *buf, off_t size) { static char *iec_unit="BKMGTPEZY", *si_unit = "dkMGTPEZY"; char *unit = siflag ? si_unit : iec_unit; int idx, usize = siflag ? 1000 : 1024; if (hflag || siflag) { for (idx=size= (usize*usize); idx++,size/=usize); if (!idx) return sprintf(buf, " %4d", (int)size); else return sprintf(buf, ((size/usize) >= 10)? " %3.0f%c" : " %3.1f%c" , (float)size/(float)usize,unit[idx]); } else return sprintf(buf, sizeof(off_t) == sizeof(long long)? " %11lld" : " %9lld", (long long int)size); } char Ftype(mode_t mode) { int m = mode & S_IFMT; if (!dflag && m == S_IFDIR) return '/'; else if (m == S_IFSOCK) return '='; else if (m == S_IFIFO) return '|'; else if (m == S_IFLNK) return '@'; /* Here, but never actually used though. */ #ifdef S_IFDOOR else if (m == S_IFDOOR) return '>'; #endif else if ((m == S_IFREG) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH))) return '*'; return 0; } char *fillinfo(char *buf, struct _info *ent) { int n; buf[n=0] = 0; #ifdef __USE_FILE_OFFSET64 if (inodeflag) n += sprintf(buf," %7lld",(long long)ent->linode); #else if (inodeflag) n += sprintf(buf," %7ld",(long int)ent->linode); #endif if (devflag) n += sprintf(buf+n, " %3d", (int)ent->ldev); #ifdef __EMX__ if (pflag) n += sprintf(buf+n, " %s",prot(ent->attr)); #else if (pflag) n += sprintf(buf+n, " %s", prot(ent->mode)); #endif if (uflag) n += sprintf(buf+n, " %-8.32s", uidtoname(ent->uid)); if (gflag) n += sprintf(buf+n, " %-8.32s", gidtoname(ent->gid)); if (sflag) n += psize(buf+n,ent->size); if (Dflag) n += sprintf(buf+n, " %s", do_date(cflag? ent->ctime : ent->mtime)); return buf; } tree-1.8.0/tree.h0000644001052500001440000001273113225442157012766 0ustar sbakerusers/* $Copyright: $ * Copyright (c) 1996 - 2018 by Steve Baker (ice@mama.indstate.edu) * All Rights reserved * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef __EMX__ /* for OS/2 systems */ # define INCL_DOSFILEMGR # define INCL_DOSNLS # include # include # include /* On many systems stat() function is idential to lstat() function. * But the OS/2 does not support symbolic links and doesn't have lstat() function. */ # define lstat stat # define strcasecmp stricmp /* Following two functions, getcwd() and chdir() don't support for drive letters. * To implement support them, use _getcwd2() and _chdir2(). */ # define getcwd _getcwd2 # define chdir _chdir2 #endif #include #include #include #include #ifdef __ANDROID #define mbstowcs(w,m,x) mbsrtowcs(w,(const char**)(& #m),x,NULL) #endif /* Should probably use strdup(), but we like our xmalloc() */ #define scopy(x) strcpy(xmalloc(strlen(x)+1),(x)) #define MINIT 30 /* number of dir entries to initially allocate */ #define MINC 20 /* allocation increment */ #ifndef TRUE typedef enum {FALSE=0, TRUE} bool; #else typedef int bool; #endif struct _info { char *name; char *lnk; bool isdir; bool issok; bool isfifo; bool isexe; bool orphan; mode_t mode, lnkmode; uid_t uid; gid_t gid; off_t size; time_t atime, ctime, mtime; dev_t dev, ldev; ino_t inode, linode; #ifdef __EMX__ long attr; #endif char *err; struct _info **child, *next, *tchild; }; /* hash.c */ struct xtable { unsigned int xid; char *name; struct xtable *nxt; }; struct inotable { ino_t inode; dev_t device; struct inotable *nxt; }; /* color.c */ struct colortable { char *term_flg, *CSS_name, *font_fg, *font_bg; }; struct extensions { char *ext; char *term_flg, *CSS_name, *web_fg, *web_bg, *web_extattr; struct extensions *nxt; }; struct linedraw { const char **name, *vert, *vert_left, *corner, *copy; }; /* Function prototypes: */ /* tree.c */ void usage(int); struct _info **unix_getfulltree(char *d, u_long lev, dev_t dev, off_t *size, char **err); struct _info **read_dir(char *, int *); int alnumsort(struct _info **, struct _info **); int versort(struct _info **a, struct _info **b); int reversealnumsort(struct _info **, struct _info **); int mtimesort(struct _info **, struct _info **); int ctimesort(struct _info **, struct _info **); int sizecmp(off_t a, off_t b); int fsizesort(struct _info **a, struct _info **b); void *xmalloc(size_t), *xrealloc(void *, size_t); char *gnu_getcwd(); int patmatch(char *, char *); void indent(int maxlevel); void free_dir(struct _info **); #ifdef __EMX__ char *prot(long); #else char *prot(mode_t); #endif char *do_date(time_t); void printit(char *); int psize(char *buf, off_t size); char Ftype(mode_t mode); char *fillinfo(char *buf, struct _info *ent); /* unix.c */ off_t unix_listdir(char *d, int *dt, int *ft, u_long lev, dev_t dev); off_t unix_rlistdir(char *d, int *dt, int *ft, u_long lev, dev_t dev); void r_listdir(struct _info **dir, char *d, int *dt, int *ft, u_long lev); /* html.c */ void emit_html_header(const char *charset, char *title, char *version); off_t html_listdir(char *d, int *dt, int *ft, u_long lev, dev_t dev); off_t html_rlistdir(char *d, int *dt, int *ft, u_long lev, dev_t dev); void htmlr_listdir(struct _info **dir, char *d, int *dt, int *ft, u_long lev); void html_encode(FILE *fd, char *s); /* xml.c */ off_t xml_listdir(char *d, int *dt, int *ft, u_long lev, dev_t dev); off_t xml_rlistdir(char *d, int *dt, int *ft, u_long lev, dev_t dev); void xmlr_listdir(struct _info **dir, char *d, int *dt, int *ft, u_long lev); void xml_indent(int maxlevel); void xml_fillinfo(struct _info *ent); /* json.c */ off_t json_listdir(char *d, int *dt, int *ft, u_long lev, dev_t dev); off_t json_rlistdir(char *d, int *dt, int *ft, u_long lev, dev_t dev); void jsonr_listdir(struct _info **dir, char *d, int *dt, int *ft, u_long lev); void json_indent(int maxlevel); void json_fillinfo(struct _info *ent); /* color.c */ void parse_dir_colors(); int color(u_short mode, char *name, bool orphan, bool islink); const char *getcharset(void); void initlinedraw(int); /* hash.c */ char *uidtoname(uid_t uid); char *gidtoname(gid_t gid); int findino(ino_t, dev_t); void saveino(ino_t, dev_t); /* file.c */ struct _info **file_getfulltree(char *d, u_long lev, dev_t dev, off_t *size, char **err); /* We use the strverscmp.c file if we're not linux */ #if ! defined (LINUX) int strverscmp (const char *s1, const char *s2); #endif tree-1.8.0/unix.c0000644001052500001440000001674313225442164013012 0ustar sbakerusers/* $Copyright: $ * Copyright (c) 1996 - 2018 by Steve Baker (ice@mama.indstate.edu) * All Rights reserved * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "tree.h" extern bool dflag, lflag, pflag, sflag, Fflag, aflag, fflag, uflag, gflag; extern bool Dflag, inodeflag, devflag, Rflag, duflag, pruneflag; extern bool noindent, force_color, xdev, nolinks, flimit; extern struct _info **(*getfulltree)(char *d, u_long lev, dev_t dev, off_t *size, char **err); extern void (*listdir)(char *, int *, int *, u_long, dev_t); extern int (*cmpfunc)(); extern FILE *outfile; extern int Level, *dirs, maxdirs; extern bool colorize, linktargetcolor; extern char *endcode; off_t unix_listdir(char *d, int *dt, int *ft, u_long lev, dev_t dev) { char *path; bool nlf = FALSE, colored = FALSE; long pathsize = 0; struct _info **dir, **sav; struct stat sb; int n, c; if ((Level >= 0) && (lev > Level)) { fputc('\n',outfile); return 0; } if (xdev && lev == 0) { stat(d,&sb); dev = sb.st_dev; } sav = dir = read_dir(d,&n); if (!dir && n) { fprintf(outfile," [error opening dir]\n"); return 0; } if (!n) { fputc('\n', outfile); free_dir(sav); return 0; } if (flimit > 0 && n > flimit) { fprintf(outfile," [%d entries exceeds filelimit, not opening dir]\n",n); free_dir(sav); return 0; } if (cmpfunc) qsort(dir,n,sizeof(struct _info *), cmpfunc); if (lev >= maxdirs-1) { dirs = xrealloc(dirs,sizeof(int) * (maxdirs += 1024)); memset(dirs+(maxdirs-1024), 0, sizeof(int) * 1024); } dirs[lev] = 1; if (!*(dir+1)) dirs[lev] = 2; fprintf(outfile,"\n"); path = malloc(pathsize=4096); while(*dir) { if (!noindent) indent(lev); fillinfo(path,*dir); if (path[0] == ' ') { path[0] = '['; fprintf(outfile, "%s] ",path); } if (colorize) { if ((*dir)->lnk && linktargetcolor) colored = color((*dir)->lnkmode,(*dir)->name,(*dir)->orphan,FALSE); else colored = color((*dir)->mode,(*dir)->name,(*dir)->orphan,FALSE); } if (fflag) { if (sizeof(char) * (strlen(d)+strlen((*dir)->name)+2) > pathsize) path=xrealloc(path,pathsize=(sizeof(char) * (strlen(d)+strlen((*dir)->name)+1024))); if (!strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->name); else sprintf(path,"%s/%s",d,(*dir)->name); } else { if (sizeof(char) * (strlen((*dir)->name)+1) > pathsize) path=xrealloc(path,pathsize=(sizeof(char) * (strlen((*dir)->name)+1024))); sprintf(path,"%s",(*dir)->name); } printit(path); if (colored) fprintf(outfile,"%s",endcode); if (Fflag && !(*dir)->lnk) { if ((c = Ftype((*dir)->mode))) fputc(c, outfile); } if ((*dir)->lnk) { fprintf(outfile," -> "); if (colorize) colored = color((*dir)->lnkmode,(*dir)->lnk,(*dir)->orphan,TRUE); printit((*dir)->lnk); if (colored) fprintf(outfile,"%s",endcode); if (Fflag) { if ((c = Ftype((*dir)->lnkmode))) fputc(c, outfile); } } if ((*dir)->isdir) { if ((*dir)->lnk) { if (lflag && !(xdev && dev != (*dir)->dev)) { if (findino((*dir)->inode,(*dir)->dev)) { fprintf(outfile," [recursive, not followed]"); } else { saveino((*dir)->inode, (*dir)->dev); if (*(*dir)->lnk == '/') listdir((*dir)->lnk,dt,ft,lev+1,dev); else { if (strlen(d)+strlen((*dir)->lnk)+2 > pathsize) path=xrealloc(path,pathsize=(strlen(d)+strlen((*dir)->name)+1024)); if (fflag && !strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->lnk); else sprintf(path,"%s/%s",d,(*dir)->lnk); listdir(path,dt,ft,lev+1,dev); } nlf = TRUE; } } } else if (!(xdev && dev != (*dir)->dev)) { if (strlen(d)+strlen((*dir)->name)+2 > pathsize) path=xrealloc(path,pathsize=(strlen(d)+strlen((*dir)->name)+1024)); if (fflag && !strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->name); else sprintf(path,"%s/%s",d,(*dir)->name); saveino((*dir)->inode, (*dir)->dev); listdir(path,dt,ft,lev+1,dev); nlf = TRUE; } *dt += 1; } else *ft += 1; if (*(dir+1) && !*(dir+2)) dirs[lev] = 2; if (nlf) nlf = FALSE; else fprintf(outfile,"\n"); dir++; } dirs[lev] = 0; free(path); free_dir(sav); return 0; } off_t unix_rlistdir(char *d, int *dt, int *ft, u_long lev, dev_t dev) { struct _info **dir; off_t size = 0; char *err; dir = getfulltree(d, lev, dev, &size, &err); memset(dirs, 0, sizeof(int) * maxdirs); r_listdir(dir, d, dt, ft, lev); return size; } void r_listdir(struct _info **dir, char *d, int *dt, int *ft, u_long lev) { char *path; long pathsize = 0; struct _info **sav = dir; bool nlf = FALSE, colored = FALSE; int c; if (dir == NULL) return; dirs[lev] = 1; if (!*(dir+1)) dirs[lev] = 2; fprintf(outfile,"\n"); path = malloc(pathsize=4096); while(*dir) { if (!noindent) indent(lev); fillinfo(path,*dir); if (path[0] == ' ') { path[0] = '['; fprintf(outfile, "%s] ",path); } if (colorize) { if ((*dir)->lnk && linktargetcolor) colored = color((*dir)->lnkmode,(*dir)->name,(*dir)->orphan,FALSE); else colored = color((*dir)->mode,(*dir)->name,(*dir)->orphan,FALSE); } if (fflag) { if (sizeof(char) * (strlen(d)+strlen((*dir)->name)+2) > pathsize) path=xrealloc(path,pathsize=(sizeof(char) * (strlen(d)+strlen((*dir)->name)+1024))); if (!strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->name); else sprintf(path,"%s/%s",d,(*dir)->name); } else { if (sizeof(char) * (strlen((*dir)->name)+1) > pathsize) path=xrealloc(path,pathsize=(sizeof(char) * (strlen((*dir)->name)+1024))); sprintf(path,"%s",(*dir)->name); } printit(path); if (colored) fprintf(outfile,"%s",endcode); if (Fflag && !(*dir)->lnk) { if ((c = Ftype((*dir)->mode))) fputc(c, outfile); } if ((*dir)->lnk) { fprintf(outfile," -> "); if (colorize) colored = color((*dir)->lnkmode,(*dir)->lnk,(*dir)->orphan,TRUE); printit((*dir)->lnk); if (colored) fprintf(outfile,"%s",endcode); if (Fflag) { if ((c = Ftype((*dir)->lnkmode))) fputc(c, outfile); } } if ((*dir)->err) { fprintf(outfile," [%s]", (*dir)->err); free((*dir)->err); (*dir)->err = NULL; } if ((*dir)->child) { if (fflag) { if (strlen(d)+strlen((*dir)->name)+2 > pathsize) path=xrealloc(path,pathsize=(strlen(d)+strlen((*dir)->name)+1024)); if (!strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->name); else sprintf(path,"%s/%s",d,(*dir)->name); } r_listdir((*dir)->child, fflag? path : NULL, dt, ft, lev+1); nlf = TRUE; *dt += 1; } else { if ((*dir)->isdir) *dt += 1; else *ft += 1; } if (*(dir+1) && !*(dir+2)) dirs[lev] = 2; if (nlf) nlf = FALSE; else fprintf(outfile,"\n"); dir++; } dirs[lev] = 0; free(path); free_dir(sav); } tree-1.8.0/xml.c0000644001052500001440000002150513373551724012626 0ustar sbakerusers/* $Copyright: $ * Copyright (c) 1996 - 2018 by Steve Baker (ice@mama.indstate.edu) * All Rights reserved * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "tree.h" extern bool dflag, lflag, pflag, sflag, Fflag, aflag, fflag, uflag, gflag; extern bool Dflag, inodeflag, devflag, Rflag, cflag; extern bool noindent, force_color, xdev, nolinks, flimit; extern struct _info **(*getfulltree)(char *d, u_long lev, dev_t dev, off_t *size, char **err); extern const int ifmt[]; extern const char fmt[], *ftype[]; extern void (*listdir)(char *, int *, int *, u_long, dev_t); extern int (*cmpfunc)(); extern FILE *outfile; extern int Level, *dirs, maxdirs; extern char *endcode; /* ... if link is followed, otherwise this is empty. some error ... # # # */ off_t xml_listdir(char *d, int *dt, int *ft, u_long lev, dev_t dev) { char *path; bool nlf = FALSE; long pathsize = 0; struct _info **dir, **sav; struct stat sb; int t, n, mt; if ((Level >= 0) && (lev > Level)) { if (!noindent) fputc('\n',outfile); return 0; } if (xdev && lev == 0) { stat(d,&sb); dev = sb.st_dev; } sav = dir = read_dir(d,&n); if (!dir && n) { fprintf(outfile,"opening dir\n"); return 0; } if (!n) { if (!noindent) fputc('\n', outfile); free_dir(sav); return 0; } if (flimit > 0 && n > flimit) { fprintf(outfile,"%d entries exceeds filelimit, not opening dir%s",n,noindent?"":"\n"); free_dir(sav); return 0; } if (cmpfunc) qsort(dir,n,sizeof(struct _info *), cmpfunc); if (lev >= maxdirs-1) { dirs = xrealloc(dirs,sizeof(int) * (maxdirs += 1024)); memset(dirs+(maxdirs-1024), 0, sizeof(int) * 1024); } dirs[lev] = 1; if (!*(dir+1)) dirs[lev] = 2; if (!noindent) fprintf(outfile,"\n"); path = malloc(pathsize=4096); while(*dir) { if (!noindent) xml_indent(lev); if ((*dir)->lnk) mt = (*dir)->mode & S_IFMT; else mt = (*dir)->mode & S_IFMT; for(t=0;ifmt[t];t++) if (ifmt[t] == mt) break; fprintf(outfile,"<%s", ftype[t]); if (fflag) { if (sizeof(char) * (strlen(d)+strlen((*dir)->name)+2) > pathsize) path=xrealloc(path,pathsize=(sizeof(char) * (strlen(d)+strlen((*dir)->name)+1024))); if (!strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->name); else sprintf(path,"%s/%s",d,(*dir)->name); } else { if (sizeof(char) * (strlen((*dir)->name)+1) > pathsize) path=xrealloc(path,pathsize=(sizeof(char) * (strlen((*dir)->name)+1024))); sprintf(path,"%s",(*dir)->name); } fprintf(outfile, " name=\""); html_encode(outfile,path); fputc('"',outfile); if ((*dir)->lnk) { fprintf(outfile, " target=\""); html_encode(outfile,(*dir)->lnk); fputc('"',outfile); } xml_fillinfo(*dir); fputc('>',outfile); if ((*dir)->isdir) { if ((*dir)->lnk) { if (lflag && !(xdev && dev != (*dir)->dev)) { if (findino((*dir)->inode,(*dir)->dev)) { fprintf(outfile,"recursive, not followed"); } else { saveino((*dir)->inode, (*dir)->dev); if (*(*dir)->lnk == '/') listdir((*dir)->lnk,dt,ft,lev+1,dev); else { if (strlen(d)+strlen((*dir)->lnk)+2 > pathsize) path=xrealloc(path,pathsize=(strlen(d)+strlen((*dir)->name)+1024)); if (fflag && !strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->lnk); else sprintf(path,"%s/%s",d,(*dir)->lnk); listdir(path,dt,ft,lev+1,dev); } nlf = TRUE; } } } else if (!(xdev && dev != (*dir)->dev)) { if (strlen(d)+strlen((*dir)->name)+2 > pathsize) path=xrealloc(path,pathsize=(strlen(d)+strlen((*dir)->name)+1024)); if (fflag && !strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->name); else sprintf(path,"%s/%s",d,(*dir)->name); saveino((*dir)->inode, (*dir)->dev); listdir(path,dt,ft,lev+1,dev); nlf = TRUE; } *dt += 1; } else *ft += 1; if (*(dir+1) && !*(dir+2)) dirs[lev] = 2; if (nlf) { nlf = FALSE; if (!noindent) xml_indent(lev); } fprintf(outfile,"%s",ftype[t],noindent?"":"\n"); dir++; } dirs[lev] = 0; free(path); free_dir(sav); return 0; } off_t xml_rlistdir(char *d, int *dt, int *ft, u_long lev, dev_t dev) { struct _info **dir; off_t size = 0; char *err; dir = getfulltree(d, lev, dev, &size, &err); memset(dirs, 0, sizeof(int) * maxdirs); xmlr_listdir(dir, d, dt, ft, lev); return size; } void xmlr_listdir(struct _info **dir, char *d, int *dt, int *ft, u_long lev) { char *path; long pathsize = 0; struct _info **sav = dir; bool nlf = FALSE; int mt, t; if (dir == NULL) return; dirs[lev] = 1; if (!*(dir+1)) dirs[lev] = 2; fprintf(outfile,"\n"); path = malloc(pathsize=4096); while(*dir) { if (!noindent) xml_indent(lev); if ((*dir)->lnk) mt = (*dir)->mode & S_IFMT; else mt = (*dir)->mode & S_IFMT; for(t=0;ifmt[t];t++) if (ifmt[t] == mt) break; fprintf(outfile,"<%s", ftype[t]); if (fflag) { if (sizeof(char) * (strlen(d)+strlen((*dir)->name)+2) > pathsize) path=xrealloc(path,pathsize=(sizeof(char) * (strlen(d)+strlen((*dir)->name)+1024))); if (!strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->name); else sprintf(path,"%s/%s",d,(*dir)->name); } else { if (sizeof(char) * (strlen((*dir)->name)+1) > pathsize) path=xrealloc(path,pathsize=(sizeof(char) * (strlen((*dir)->name)+1024))); sprintf(path,"%s",(*dir)->name); } fprintf(outfile, " name=\""); html_encode(outfile,path); fputc('"',outfile); if ((*dir)->lnk) { fprintf(outfile, " target=\""); html_encode(outfile,(*dir)->lnk); fputc('"',outfile); } xml_fillinfo(*dir); if (mt != S_IFDIR && mt != S_IFLNK && (*dir)->err == NULL) fprintf(outfile,"/>"); else fputc('>',outfile); if ((*dir)->err) { fprintf(outfile,"%s", (*dir)->err); free((*dir)->err); (*dir)->err = NULL; } if ((*dir)->child) { if (fflag) { if (strlen(d)+strlen((*dir)->name)+2 > pathsize) path=xrealloc(path,pathsize=(strlen(d)+strlen((*dir)->name)+1024)); if (!strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->name); else sprintf(path,"%s/%s",d,(*dir)->name); } xmlr_listdir((*dir)->child, fflag? path : NULL, dt, ft, lev+1); nlf = TRUE; *dt += 1; } else { if ((*dir)->isdir) *dt += 1; else *ft += 1; } if (*(dir+1) && !*(dir+2)) dirs[lev] = 2; if (nlf) { nlf = FALSE; if (!noindent) xml_indent(lev); } if (mt == S_IFDIR || mt == S_IFLNK || (*dir)->err != NULL) fprintf(outfile,"\n",ftype[t]); else putc('\n',outfile); dir++; } dirs[lev] = 0; free(path); free_dir(sav); } void xml_indent(int maxlevel) { int i; fprintf(outfile, " "); for(i=0; iinode); #else if (inodeflag) fprintf(outfile," inode=\"%ld\"",(long int)ent->inode); #endif if (devflag) fprintf(outfile, " dev=\"%d\"", (int)ent->dev); #ifdef __EMX__ if (pflag) fprintf(outfile, " mode=\"%04o\" prot=\"%s\"",ent->attr, prot(ent->attr)); #else if (pflag) fprintf(outfile, " mode=\"%04o\" prot=\"%s\"", ent->mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX), prot(ent->mode)); #endif if (uflag) fprintf(outfile, " user=\"%s\"", uidtoname(ent->uid)); if (gflag) fprintf(outfile, " group=\"%s\"", gidtoname(ent->gid)); if (sflag) fprintf(outfile, " size=\"%lld\"", (long long int)(ent->size)); if (Dflag) fprintf(outfile, " time=\"%s\"", do_date(cflag? ent->ctime : ent->mtime)); } tree-1.8.0/doc/tree.10000644001052500001440000002657513373555225013464 0ustar sbakerusers.\" $Copyright: $ .\" Copyright (c) 1996 - 2018 by Steve Baker .\" All Rights reserved .\" .\" 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA .\" ... .TH TREE 1 "" "Tree 1.8.0" .SH NAME tree \- list contents of directories in a tree-like format. .SH SYNOPSIS \fBtree\fP [\fB-acdfghilnpqrstuvxACDFQNSUX\fP] [\fB-L\fP \fIlevel\fP [\fB-R\fP]] [\fB-H\fP \fIbaseHREF\fP] [\fB-T\fP \fItitle\fP] [\fB-o\fP \fIfilename\fP] [\fB--nolinks\fP] [\fB-P\fP \fIpattern\fP] [\fB-I\fP \fIpattern\fP] [\fB--inodes\fP] [\fB--device\fP] [\fB--noreport\fP] [\fB--dirsfirst\fP] [\fB--version\fP] [\fB--help\fP] [\fB--filelimit\fP \fI#\fP] [\fB--si\fP] [\fB--prune\fP] [\fB--du\fP] [\fB--timefmt\fP \fIformat\fP] [\fB--matchdirs\fP] [\fB--fromfile\fP] [\fB--\fP] [\fIdirectory\fP ...] .br .SH DESCRIPTION \fITree\fP is a recursive directory listing program that produces a depth indented listing of files, which is colorized ala \fIdircolors\fP if the \fBLS_COLORS\fP environment variable is set and output is to tty. With no arguments, \fItree\fP lists the files in the current directory. When directory arguments are given, \fItree\fP lists all the files and/or directories found in the given directories each in turn. Upon completion of listing all files/directories found, \fItree\fP returns the total number of files and/or directories listed. By default, when a symbolic link is encountered, the path that the symbolic link refers to is printed after the name of the link in the format: .br name -> real-path .br If the `\fB-l\fP' option is given and the symbolic link refers to an actual directory, then \fItree\fP will follow the path of the symbolic link as if it were a real directory. .br .SH OPTIONS \fITree\fP understands the following command line switches: .SH LISTING OPTIONS .TP .B -a All files are printed. By default tree does not print hidden files (those beginning with a dot `.'). In no event does tree print the file system constructs `.' (current directory) and `..' (previous directory). .PP .TP .B -d List directories only. .PP .TP .B -l Follows symbolic links if they point to directories, as if they were directories. Symbolic links that will result in recursion are avoided when detected. .PP .TP .B -f Prints the full path prefix for each file. .PP .TP .B -x Stay on the current file-system only. Ala \fBfind \fI-xdev\fP. .PP .TP .B -L \fIlevel\fP Max display depth of the directory tree. .PP .TP .B -R Recursively cross down the tree each \fIlevel\fP directories (see \fB-L\fP option), and at each of them execute \fBtree\fP again adding `-o 00Tree.html' as a new option. .PP .TP .B -P \fIpattern\fP List only those files that match the wild-card \fIpattern\fP. Note: you must use the \fI-a\fP option to also consider those files beginning with a dot `.' for matching. Valid wildcard operators are `*' (any zero or more characters), `?' (any single character), `[...]' (any single character listed between brackets (optional - (dash) for character range may be used: ex: [A-Z]), and `[^...]' (any single character not listed in brackets) and `|' separates alternate patterns. .PP .TP .B -I \fIpattern\fP Do not list those files that match the wild-card \fIpattern\fP. .PP .TP .B --ignore-case If a match pattern is specified by the \fB-P\fP or \fB-I\fP option, this will cause the pattern to match without regards to the case of each letter. .PP .TP .B --matchdirs If a match pattern is specified by the \fB-P\fP option, this will cause the pattern to be applied to directory names (in addition to filenames). In the event of a match on the directory name, matching is disabled for the directory's contents. If the \fB--prune\fP option is used, empty folders that match the pattern will not be pruned. .PP .TP .B --prune Makes tree prune empty directories from the output, useful when used in conjunction with \fB-P\fP or \fB-I\fP. See \fBBUGS AND NOTES\fP below for more information on this option. .PP .TP .B --noreport Omits printing of the file and directory report at the end of the tree listing. .PP .TP .B --charset \fIcharset\fP Set the character set to use when outputting HTML and for line drawing. .PP .TP .B --filelimit \fI#\fP Do not descend directories that contain more than \fI#\fP entries. .PP .TP .B --timefmt \fIformat\fP Prints (implies -D) and formats the date according to the format string which uses the \fBstrftime\fP(3) syntax. .PP .TP .B -o \fIfilename\fP Send output to \fIfilename\fP. .PP .SH FILE OPTIONS .TP .B -q Print non-printable characters in filenames as question marks instead of the default. .PP .TP .B -N Print non-printable characters as is instead of as escaped octal numbers. .PP .TP .B -Q Quote the names of files in double quotes. .PP .TP .B -p Print the file type and permissions for each file (as per ls -l). .PP .TP .B -u Print the username, or UID # if no username is available, of the file. .PP .TP .B -g Print the group name, or GID # if no group name is available, of the file. .PP .TP .B -s Print the size of each file in bytes along with the name. .PP .TP .B -h Print the size of each file but in a more human readable way, e.g. appending a size letter for kilobytes (K), megabytes (M), gigabytes (G), terabytes (T), petabytes (P) and exabytes (E). .PP .TP .B --si Like \fB-h\fP but use SI units (powers of 1000) instead. .PP .TP .B --du For each directory report its size as the accumulation of sizes of all its files and sub-directories (and their files, and so on). The total amount of used space is also given in the final report (like the 'du -c' command.) This option requires tree to read the entire directory tree before emitting it, see \fBBUGS AND NOTES\fP below. Implies \fB-s\fP. .PP .TP .B -D Print the date of the last modification time or if \fB-c\fP is used, the last status change time for the file listed. .PP .TP .B -F Append a `/' for directories, a `=' for socket files, a `*' for executable files, a `>' for doors (Solaris) and a `|' for FIFO's, as per ls -F .PP .TP .B --inodes Prints the inode number of the file or directory .PP .TP .B --device Prints the device number to which the file or directory belongs .PP .SH SORTING OPTIONS .TP .B -v Sort the output by version. .PP .TP .B -t Sort the output by last modification time instead of alphabetically. .PP .TP .B -c Sort the output by last status change instead of alphabetically. Modifies the \fB-D\fP option (if used) to print the last status change instead of modification time. .PP .TP .B -U Do not sort. Lists files in directory order. Disables \fB--dirsfirst\fP. .PP .TP .B -r Sort the output in reverse order. This is a meta-sort that alter the above sorts. This option is disabled when \fB-U\fP is used. .PP .TP .B --dirsfirst List directories before files. This is a meta-sort that alters the above sorts. This option is disabled when \fB-U\fP is used. .PP .TP .B --sort\fR[\fB=\fR]\fItype\fR Sort the output by \fItype\fR instead of name. Possible values are: \fBctime\fR (\fB-c\fP), \fBmtime\fR (\fB-t\fB), \fBsize\fR, or \fBversion\fR (\fB-v\fB). .SH GRAPHICS OPTIONS .TP .B -i Makes tree not print the indentation lines, useful when used in conjunction with the \fB-f\fP option. Also removes as much whitespace as possible when used with the \fB-J\fP or \fB-x\fP options. .PP .TP .B -A Turn on ANSI line graphics hack when printing the indentation lines. .PP .TP .B -S Turn on CP437 line graphics (useful when using Linux console mode fonts). This option is now equivalent to `--charset=IBM437' and may eventually be depreciated. .PP .TP .B -n Turn colorization off always, over-ridden by the \fB-C\fP option. .PP .TP .B -C Turn colorization on always, using built-in color defaults if the LS_COLORS or TREE_COLORS environment variables are not set. Useful to colorize output to a pipe. .PP .SH XML/JSON/HTML OPTIONS .TP .B -X Turn on XML output. Outputs the directory tree as an XML formatted file. .PP .TP .B -J Turn on JSON output. Outputs the directory tree as an JSON formatted array. .PP .TP .B -H \fIbaseHREF\fP Turn on HTML output, including HTTP references. Useful for ftp sites. \fIbaseHREF\fP gives the base ftp location when using HTML output. That is, the local directory may be `/local/ftp/pub', but it must be referenced as `ftp://hostname.organization.domain/pub' (\fIbaseHREF\fP should be `ftp://hostname.organization.domain'). Hint: don't use ANSI lines with this option, and don't give more than one directory in the directory list. If you wish to use colors via CSS style-sheet, use the -C option in addition to this option to force color output. .PP .TP .B -T \fItitle\fP Sets the title and H1 header string in HTML output mode. .PP .TP .B --nolinks Turns off hyperlinks in HTML output. .PP .SH INPUT OPTIONS .B --fromfile Reads a directory listing from a file rather than the file-system. Paths provided on the command line are files to read from rather than directories to search. The dot (.) directory indicates that tree should read paths from standard input. .SH MISC OPTIONS .TP .B --help Outputs a verbose usage listing. .PP .TP .B --version Outputs the version of tree. .PP .TP .B -- Option processing terminator. No further options will be processed after this. .PP .br .SH FILES /etc/DIR_COLORS System color database. .br ~/.dircolors Users color database. .br .SH ENVIRONMENT \fBLS_COLORS\fP Color information created by dircolors .br \fBTREE_COLORS\fP Uses this for color information over LS_COLORS if it is set. .br \fBTREE_CHARSET\fP Character set for tree to use in HTML mode. .br \fBCLICOLOR\fP Enables colorization even if TREE_COLORS or LS_COLORS is not set. .br \fBCLICOLOR_FORCE\fP Always enables colorization (effectively -C) .br \fBLC_CTYPE\fP Locale for filename output. .br \fBLC_TIME\fP Locale for timefmt output, see \fBstrftime\fP(3). .br \fBTZ\fP Timezone for timefmt output, see \fBstrftime\fP(3). .SH AUTHOR Steve Baker (ice@mama.indstate.edu) .br HTML output hacked by Francesc Rocher (rocher@econ.udg.es) .br Charsets and OS/2 support by Kyosuke Tokoro (NBG01720@nifty.ne.jp) .SH BUGS AND NOTES Tree does not prune "empty" directories when the -P and -I options are used by default. Use the --prune option. The -h and --si options round to the nearest whole number unlike the ls implementations which rounds up always. Pruning files and directories with the -I, -P and --filelimit options will lead to incorrect file/directory count reports. The --prune and --du options cause tree to accumulate the entire tree in memory before emitting it. For large directory trees this can cause a significant delay in output and the use of large amounts of memory. The timefmt expansion buffer is limited to a ridiculously large 255 characters. Output of time strings longer than this will be undefined, but are guaranteed to not exceed 255 characters. XML/JSON trees are not colored, which is a bit of a shame. Probably more. .SH SEE ALSO .BR dircolors (1), .BR ls (1), .BR find (1), .BR du (1), .BR strftime (3) tree-1.8.0/doc/tree.1.fr0000644001052500001440000001554110222775567014064 0ustar sbakerusers.\" $Copyright: $ .\" Copyright (c) 1996 - 2001 by Steve Baker .\" All Rights reserved .\" .\" This software is provided as is without any express or implied .\" warranties, including, without limitation, the implied warranties .\" of merchantability and fitness for a particular purpose. .\" ... .V= $Header: tree.1 1.4 1996 - 2001 $ .TH TREE 1 "\*(V)" "Manuel du Programmeur UNIX" .SH NOM tree \- liste le contenu de répertoires sous forme d'arborescence .SH SYNOPSIS \fBtree\fP [ \fB-adfgilnopqstuxACDFNS\fP ] [ \fB-L\fP \fIniveau\fP [ \fB-R\fP ]] [ \fB-H\fP \fIbaseHREF\fP ] [ \fB-P\fP \fImotif\fP ] [ \fB-I\fP \fImotif\fP ] [ \fIrepertoire\fP ... ] .br .SH DESCRIPTION \fITree\fP est un programme de listage récursif de répertoires qui produit un listage des fichiers indenté en profondeur, qui est mis en couleurs à la manière de \fIdircolors\fP si la variable d'environnement \fBLS_COLORS\fP est activée et que la sortie est un tty. Sans arguments, \fItree\fP liste les fichiers contenus dans le répertoire courant. Lorsque des répertoires sont donnés en argument, \fItree\fP liste tous les fichiers et/ou répertoires trouvés dans les répertoires donnés, chacun à son tour. Après avoir affiché tous les fichiers et répertoires trouvés, \fItree\fP renvoie le nombre total de fichiers et/ou de répertoires listés. Par défaut, quand un lien symbolique est rencontré, la chemin auquel il renvoie est affiché après le nom du lien sous la forme suivante\ : .br nom -> chemin-reel .br Si l'on donne l'option `\fB-l\fP' et que le lien symbolique renvoie à un répertoire existant, \fItree\fP suivra le chemin du lien symbolique comme s'il s'agissait d'un véritable répertoire. .br .SH OPTIONS \fITree\fP comprend les commutateurs suivants en ligne de commande\ : .TP .B --help Donne en sortie un listage verbeux des options d'utilisation .PP .TP .B --version Sort la version de tree .PP .TP .B --inodes Affiche le numéro d'inode du fichier ou du répertoire .PP .TP .B --device Affiche le numéro de périphérique auquel le fichier ou le répertoire appartient .PP .TP .B --noreport Omet d'afficher le compte des fichiers et répertoires à la fin du listage arborescent .PP .TP .B --nolinks Désactive les hyperliens en sortie HTML .PP .TP .B -a Tous les fichiers sont affichés. Par défaut tree n'affiche pas les fichiers cachés (ceux qui commencent par un point `.'). En aucun cas, tree n'affiche les constructeurs de système de fichiers `.' (répertoire courant) et `..' (répertoire parent). .PP .TP .B -d Liste uniquement les répertoires .PP .TP .B -f Affiche le chemin complet en préfixe de chaque fichier .PP .TP .B -i Fait que tree n'affiche pas les lignes d'indentation, ce qui est utile en conjonction avec l'option \fB-f\fP .PP .TP .B -l Suit les liens symboliques s'ils pointent sur des répertoires, comme s'ils étaient des répertoires. Les liens symboliques qui finiraient de façon récursive sont évités, une fois détectés .PP .TP .B -x Reste exclusivement dans le système de fichiers courant, à la manière de \fBfind \fI-xdev\fP .PP .TP \fB-P \fImotif\fP Liste exclusivement les fichiers correspondant au joker \fImotif\fP. Remarque\ : vous devez utiliser l'option \fI-a\fP pour prendre également en considération les fichiers commençant par un point `.'. Les opérateurs de joker valides sont `*' (zéro caractère ou plus), `?' (n'importe quel caractère), `[...]' (n'importe quel caractère parmi ceux qui sont listés entre crochets (un tiret `-' optionnel peut être utilisé pour une plage de caractères\ ; par exemple\ : [A-Z])), et `[^...]' (n'importe quel caractère à l'exclusion de ceux qui sont listés entre crochets). .PP .TP .B -I \fImotif\fP Ne pas lister les fichiers correspondant au joker \fImotif\fP .PP .TP .B -p Afficher les protections pour chaque fichier (comme avec ls -l) .PP .TP .B -o \fInom-de-fichier\fP Envoie la sortie vers \fInom-de-fichier\fP .PP .TP .B -s Affiche la taille de chaque fichier devant son nom .PP .TP .B -u Affiche le nom d'utilisateur du fichier, ou le numéro d'UID si aucun nom d'utilisateur n'est disponible .PP .TP .B -g Affiche le nom de groupe du fichier, ou le numéro de GID si aucun nom de groupe n'est disponible .PP .TP .B -D Affiche la date et l'heure de dernière modification du fichier .PP .TP .B -F Appose un `/' pour les répertoires, un `=' pour les fichiers de socket, un `*' pour les fichiers exécutables et un `|' pour les FIFO, comme avec ls\ -F .PP .TP .B -q Affiche les caractères non affichables dans les noms de fichiers sous forme de points d'interrogation au lieu de la notation carret par défaut .PP .TP .B -N Affiche les caractères non affichables tels quels au lieu de la notation carret par défaut .PP .TP .B -t Trie la sortie par horaire de dernière modification, au lieu de trier alphabétiquement .PP .TP .B -n Désactive toujours la mise en couleurs, forcée par l'option \fB-C\fP .PP .TP .B -C Active toujours la mise en couleurs, au moyen des couleurs intégrées par défaut si la variable d'environnement LS_COLORS n'est pas activée. Utile pour mettre en couleurs la sortie vers un tube. .PP .TP .B -A Active le graphisme ANSI de lignes pour l'affichage des lignes d'indentation .PP .TP .B -S Active le graphisme ASCII de lignes (utile pour utiliser les polices du mode console de Linux) .PP .TP .B -L \fIniveau\fP Maximum de profondeur d'affichage de l'arborescence des répertoires .PP .TP .B -R Descendre récursivement dans l'arborescence de chaque niveau de répertoires (voir l'option \fB-L\fP), et pour chacun d'eux exécuter de nouveau \fBtree\fP en y ajoutant `-o 00Tree.html' comme nouvelle option. .PP .TP .B -H \fIbaseHREF\fP Active la sortie HTML, en incluant des références HTTP. Utile pour les sites FTP. \fIbaseHREF\fP donne l'emplacement ftp de base lorsque l'on utilise la sortie HTML. C'est-à-dire que le répertoire local peut être `/local/ftp/pub', mais il doit être référencé en tant que `ftp://hostname.organization.domain/pub' (\fIbaseHREF\fP doit être `ftp://hostname.organization.domain'). Conseils\ : n'utilisez pas les lignes ANSI avec cette option, et n'indiquez pas plus d'un répertoire dans la liste des répertoires. Si vous voulez utiliser des couleurs via une feuille de style CSS, utilisez l'option -C en plus de cette option pour forcer la sortie en couleurs. .PP .br .SH FICHIERS /etc/DIR_COLORS Base de données des couleurs système .br ~/.dircolors Base de données des couleurs utilisateur .br .SH ENVIRONNEMENT \fBLS_COLORS\fP Informations sur les couleurs créées par dircolors .SH AUTEUR Steve Baker (ice@mama.indstate.edu) .br La sortie HTML a été hackée par Francesc Rocher (rocher@econ.udg.es) .SH BOGUES Aucun bogue connu. N'a pas été testé en profondeur. Pourrait utiliser quelques options supplémentaires, à la manière de ls. .SH VOIR AUSSI .BR dircolors (1L), .BR ls (1L), .BR find (1L) .SH TRADUCTEUR Baptiste Mélès N'hésitez pas à reporter respectivement à l'auteur ou au traducteur toute erreur ou omission dans cette page de manuel. tree-1.8.0/doc/xml.dtd0000644001052500001440000000506511601115311013703 0ustar sbakerusers