1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
/* $Copyright: $
* Copyright (c) 1996 - 2024 by Steve Baker (steve.baker.llc@gmail.com)
*
* 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, aflag, pruneflag, gitignore, showinfo;
extern bool matchdirs, fflinks;
extern int pattern, ipattern;
extern int (*topsort)(const struct _info **, const struct _info **);
extern FILE *outfile;
extern char *file_comment, *file_pathsep;
/* 64K paths maximum */
#define MAXPATH 64*1024
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(const char *name) {
struct _info *n = xmalloc(sizeof(struct _info));
memset(n,0,sizeof(struct _info));
n->name = scopy(name);
n->child = NULL;
n->tchild = n->next = NULL;
return n;
}
/* Don't insertion sort, let fprune() do the sort if necessary */
struct _info *search(struct _info **dir, const 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;
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, const char *path, bool matched, bool root)
{
struct _info **dir, *new = NULL, *end = NULL, *ent, *t;
struct comment *com;
struct ignorefile *ig = NULL;
struct infofile *inf = NULL;
char *cur, *fpath = xmalloc(sizeof(char) * MAXPATH);
size_t i, count = 0;
bool show;
strcpy(fpath, path);
cur = fpath + strlen(fpath);
*(cur++) = '/';
push_files(path, &ig, &inf, root);
for(ent = head; ent != NULL;) {
strcpy(cur, ent->name);
if (ent->tchild) ent->isdir = 1;
show = true;
if (dflag && !ent->isdir) show = false;
if (!aflag && !root && ent->name[0] == '.') show = false;
if (show && !matched) {
if (!ent->isdir) {
if (pattern && !patinclude(ent->name, 0)) show = false;
if (ipattern && patignore(ent->name, 0)) show = false;
}
if (ent->isdir && show && matchdirs && pattern) {
if (patinclude(ent->name, 1)) matched = true;
}
}
if (pruneflag && !matched && ent->isdir && ent->tchild == NULL) show = false;
if (gitignore && filtercheck(path, ent->name, ent->isdir)) show = false;
if (show && showinfo && (com = infocheck(path, ent->name, inf != NULL, ent->isdir))) {
for(i = 0; com->desc[i] != NULL; i++);
ent->comment = xmalloc(sizeof(char *) * (i+1));
for(i = 0; com->desc[i] != NULL; i++) ent->comment[i] = scopy(com->desc[i]);
ent->comment[i] = NULL;
}
if (show && ent->tchild != NULL) ent->child = fprune(ent->tchild, fpath, 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;
if (topsort) qsort(dir,count,sizeof(struct _info *), (int (*)(const void *, const void *))topsort);
if (ig != NULL) ig = pop_filterstack();
if (inf != NULL) inf = pop_infostack();
free(fpath);
return dir;
}
struct _info **file_getfulltree(char *d, u_long lev, dev_t dev, off_t *size, char **err)
{
UNUSED(lev);UNUSED(dev);UNUSED(err);
FILE *fp = (strcmp(d,".")? fopen(d,"r") : stdin);
char *path, *spath, *s, *link;
struct _info *root = NULL, **cwd, *ent;
int tok;
size_t l;
*size = 0;
if (fp == NULL) {
fprintf(stderr,"tree: Error opening %s for reading.\n", d);
return NULL;
}
path = xmalloc(sizeof(char) * MAXPATH);
while(fgets(path, MAXPATH, fp) != NULL) {
if (file_comment != NULL && strncmp(path,file_comment,strlen(file_comment)) == 0) continue;
l = strlen(path);
while(l && (path[l-1] == '\n' || path[l-1] == '\r')) path[--l] = '\0';
if (l == 0) continue;
spath = path;
cwd = &root;
link = fflinks? strstr(path, " -> ") : NULL;
if (link) {
*link = '\0';
link += 4;
}
ent = NULL;
do {
s = nextpc(&spath, &tok);
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 (ent && link) {
ent->isdir = 0;
ent->mode = S_IFLNK;
ent->lnk = scopy(link);
}
}
if (fp != stdin) fclose(fp);
free(path);
/* Prune accumulated directory tree: */
return fprune(root, "", false, true);
}
struct _info **tabedfile_getfulltree(char *d, u_long lev, dev_t dev, off_t *size, char **err)
{
UNUSED(lev);UNUSED(dev);UNUSED(err);
FILE *fp = (strcmp(d,".")? fopen(d,"r") : stdin);
char *path, *spath, *link;
struct _info *root = NULL, **istack, *ent;
size_t line = 0, l, tabs, top = 0, maxstack = 2048;
*size = 0;
if (fp == NULL) {
fprintf(stderr,"tree: Error opening %s for reading.\n", d);
return NULL;
}
path = xmalloc(sizeof(char) * MAXPATH);
istack = xmalloc(sizeof(struct _info *) * maxstack);
memset(istack, 0, sizeof(struct _info *) * maxstack);
while(fgets(path, MAXPATH, fp) != NULL) {
line++;
if (file_comment != NULL && strncmp(path,file_comment,strlen(file_comment)) == 0) continue;
l = strlen(path);
while(l && (path[l-1] == '\n' || path[l-1] == '\r')) path[--l] = '\0';
if (l == 0) continue;
for(tabs=0; path[tabs] == '\t'; tabs++);
if (tabs >= maxstack) {
fprintf(stderr, "tree: Tab depth exceeds maximum path depth (%ld >= %ld) on line %ld\n", tabs, maxstack, line);
continue;
}
spath = path+tabs;
link = fflinks? strstr(spath, " -> ") : NULL;
if (link) {
*link = '\0';
link += 4;
}
if (tabs > 0 && ((tabs-1 > top) || (istack[tabs-1] == NULL))) {
fprintf(stderr, "tree: Orphaned file [%s] on line %ld, check tab depth in file.\n", spath, line);
continue;
}
ent = istack[tabs] = search(tabs? &(istack[tabs-1]->tchild) : &root, spath);
ent->mode = S_IFREG;
if (tabs) {
istack[tabs-1]->isdir = 1;
istack[tabs-1]->mode = S_IFDIR;
}
if (link) {
ent->isdir = 0;
ent->mode = S_IFLNK;
ent->lnk = scopy(link);
}
top = tabs;
}
if (fp != stdin) fclose(fp);
free(path);
free(istack);
/* Prune accumulated directory tree: */
return fprune(root, "", false, true);
}
|