From 6608d2e8c99f76a03bec682fcbc36819b0722f2c Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Wed, 22 Nov 2017 13:13:02 +0100 Subject: [PATCH] Remove tree() from libite, moved to troglobit/toolbox.git instead Signed-off-by: Joachim Nilsson --- README.md | 6 --- src/Makefile.am | 1 - src/lite.h | 2 - src/tree.c | 126 ---------------------------------------------- tests/Makefile.am | 1 - tests/rsync.c | 4 +- tests/tree.c | 19 ------- 7 files changed, 2 insertions(+), 157 deletions(-) delete mode 100644 src/tree.c delete mode 100644 tests/tree.c diff --git a/README.md b/README.md index 4b11d5d..3516115 100644 --- a/README.md +++ b/README.md @@ -278,12 +278,6 @@ Generic Functions **Note:** Requires Linux v3.11, or later, will fall back to the old and unsafe `tmpfile()` on older systems. -- `tree(path, show_perms)` - - Very simple `/bin/tree` replacement. Draw ASCII tree of `path`, with - optional listing of file and directory permissions if `show_perms` is - set. The `path` argument should be a directory. - - `which(cmd)`, `whichp(cmd)` C implementation of UNIX which(1). Returns a malloc'ed string with the diff --git a/src/Makefile.am b/src/Makefile.am index 9882a3e..c991997 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -15,7 +15,6 @@ libite_la_SOURCES = chomp.c copyfile.c \ strlcpy.c strlcat.c strtonum.c \ strdupa.h strndupa.h strnlen.h \ tempfile.c which.c \ - tree.c \ lite.h strlite.h \ queue.h tree.h diff --git a/src/lite.h b/src/lite.h index 8389c7b..843a574 100644 --- a/src/lite.h +++ b/src/lite.h @@ -86,8 +86,6 @@ pid_t pidfile_poll (const char *pidfile); void *reallocarray(void *optr, size_t nmemb, size_t size); -int tree(char *path, int show_perms); - void progress (int percent, int max_width); void progress_simple(int percent); diff --git a/src/tree.c b/src/tree.c deleted file mode 100644 index 6416841..0000000 --- a/src/tree.c +++ /dev/null @@ -1,126 +0,0 @@ -/* Simple /bin/tree replacement - * - * Copyright (c) 2015 Joachim Nilsson - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -static int all = 0; - -static int filter(const struct dirent *entry) -{ - /* Skip current dir ".", and prev dir "..", from list of files */ - if ((1 == strlen(entry->d_name) && entry->d_name[0] == '.') || - (2 == strlen(entry->d_name) && !strcmp(entry->d_name, ".."))) - return 0; - - if (!all && entry->d_name[0] == '.') - return 0; - - return 1; -} - -static void get_perms(struct stat *st, char *buf, size_t len) -{ - mode_t m = st->st_mode; - - snprintf(buf, len, "[%c%c%c%c%c%c%c%c%c%c] ", - S_ISCHR(m) ? 'c' : S_ISBLK(m) ? 'b' : S_ISFIFO(m) ? 'p' : S_ISLNK(m) ? 'l' : S_ISSOCK(m) ? 's' : '-', - (m & S_IRUSR) ? 'r' : '-', - (m & S_IWUSR) ? 'w' : '-', - (m & S_ISUID ) ? 's' : (m & S_IXUSR) ? 'x' : '-', - (m & S_IRGRP) ? 'r' : '-', - (m & S_IWGRP) ? 'w' : '-', - (m & S_ISGID) ? 's' : (m & S_IXGRP) ? 'x' : '-', - (m & S_IROTH) ? 'r' : '-', - (m & S_IWOTH) ? 'w' : '-', - (m & S_IXOTH) ? 'x' : '-' - ); -} - -static int descend(char *path, int show_perms, char *pfx) -{ - int i, n, result = 0; - struct stat st; - struct dirent **namelist = NULL; - - if (-1 == lstat(path, &st)) - return 1; - - if ((st.st_mode & S_IFMT) != S_IFDIR) { - errno = ENOTDIR; - return -1; - } - - n = scandir(path, &namelist, filter, alphasort); - if (n) { - for (i = 0; i < n; i++) { - char t = ' ', p[14] = "", s[256] = ""; - char buf[512]; - char dir[80]; - - if (i + 1 == n) { - printf("%s `- ", pfx); - snprintf(dir, sizeof(dir), "%s ", pfx); - } else { - printf("%s|-- ", pfx); - snprintf(dir, sizeof(dir), "%s| ", pfx); - } - - snprintf(buf, sizeof(buf), "%s/%s", path, namelist[i]->d_name); - if (!lstat(buf, &st)) { - if (show_perms) - get_perms(&st, p, sizeof(p)); - if ((st.st_mode & S_IFMT) == S_IFDIR) - t = '/'; - if (S_ISLNK(st.st_mode)) { - snprintf(s, sizeof(s), "-> "); - if (-1 == readlink(buf, &s[3], sizeof(s) - 3)) - s[0] = 0; - } - } - - printf("%s%s%c%s\n", p, namelist[i]->d_name, t, s); - if (t == '/') - result += descend(buf, show_perms, dir); - - free(namelist[i]); - } - - free(namelist); - } - - return result; -} - -int tree(char *path, int show_perms) -{ - printf("[%s]\n", path); - return descend(path, show_perms, ""); -} - -/** - * Local Variables: - * indent-tabs-mode: t - * c-file-style: "linux" - * End: - */ diff --git a/tests/Makefile.am b/tests/Makefile.am index 3e77c74..3453272 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -15,7 +15,6 @@ TESTS += touch TESTS += progress TESTS += rsync TESTS += tempfile -TESTS += tree TESTS += which check_PROGRAMS = $(TESTS) diff --git a/tests/rsync.c b/tests/rsync.c index 789619e..d80f83a 100644 --- a/tests/rsync.c +++ b/tests/rsync.c @@ -53,9 +53,9 @@ static void check_tree(char *heading, char *dir) if (verbose) { if (heading) puts(heading); - - tree(dir, 1); } + + // XXX: Fixme, add code to list tree, nftw() possibly } int run_test(void) diff --git a/tests/tree.c b/tests/tree.c deleted file mode 100644 index 6c03716..0000000 --- a/tests/tree.c +++ /dev/null @@ -1,19 +0,0 @@ -#include "check.h" - -int main(void) -{ - int result = 0; - size_t i; - struct { char *path; int fail; } arr[] = { - { "/etc/passwd", -1 }, - { "/dev", 0 }, - { "/usr/sbin", 0 }, - }; - - for (i = 0; i < NELEMS(arr); i++) { - if (tree(arr[i].path, 1) != arr[i].fail) - result++; - } - - return result; -}