forked from nodakai/tree-command
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunix.c
260 lines (225 loc) · 7.38 KB
/
unix.c
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
/* $Copyright: $
* Copyright (c) 1996 - 2014 by Steve Baker ([email protected])
* 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 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);
}