ls.c revision 1.51 1 1.51 jschauma /* $NetBSD: ls.c,v 1.51 2003/09/14 19:16:06 jschauma Exp $ */
2 1.14 cgd
3 1.1 cgd /*
4 1.12 mycroft * Copyright (c) 1989, 1993, 1994
5 1.12 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Michael Fischbein.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.50 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.19 christos #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.19 christos __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\
38 1.19 christos The Regents of the University of California. All rights reserved.\n");
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.1 cgd #ifndef lint
42 1.14 cgd #if 0
43 1.14 cgd static char sccsid[] = "@(#)ls.c 8.7 (Berkeley) 8/5/94";
44 1.14 cgd #else
45 1.51 jschauma __RCSID("$NetBSD: ls.c,v 1.51 2003/09/14 19:16:06 jschauma Exp $");
46 1.14 cgd #endif
47 1.1 cgd #endif /* not lint */
48 1.1 cgd
49 1.7 mycroft #include <sys/types.h>
50 1.1 cgd #include <sys/stat.h>
51 1.1 cgd #include <sys/ioctl.h>
52 1.12 mycroft
53 1.1 cgd #include <dirent.h>
54 1.12 mycroft #include <err.h>
55 1.12 mycroft #include <errno.h>
56 1.7 mycroft #include <fts.h>
57 1.39 tron #include <locale.h>
58 1.12 mycroft #include <stdio.h>
59 1.7 mycroft #include <stdlib.h>
60 1.1 cgd #include <string.h>
61 1.12 mycroft #include <unistd.h>
62 1.33 christos #include <termios.h>
63 1.21 christos #include <pwd.h>
64 1.21 christos #include <grp.h>
65 1.12 mycroft
66 1.1 cgd #include "ls.h"
67 1.7 mycroft #include "extern.h"
68 1.1 cgd
69 1.43 lukem static void display(FTSENT *, FTSENT *);
70 1.43 lukem static int mastercmp(const FTSENT **, const FTSENT **);
71 1.43 lukem static void traverse(int, char **, int);
72 1.7 mycroft
73 1.43 lukem static void (*printfcn)(DISPLAY *);
74 1.43 lukem static int (*sortfcn)(const FTSENT *, const FTSENT *);
75 1.7 mycroft
76 1.12 mycroft #define BY_NAME 0
77 1.12 mycroft #define BY_SIZE 1
78 1.8 mycroft #define BY_TIME 2
79 1.8 mycroft
80 1.7 mycroft long blocksize; /* block size units */
81 1.1 cgd int termwidth = 80; /* default terminal width */
82 1.8 mycroft int sortkey = BY_NAME;
83 1.37 simonb int rval = EXIT_SUCCESS; /* exit value - set if error encountered */
84 1.1 cgd
85 1.1 cgd /* flags */
86 1.1 cgd int f_accesstime; /* use time of last access */
87 1.1 cgd int f_column; /* columnated format */
88 1.24 lukem int f_columnacross; /* columnated format, sorted across */
89 1.51 jschauma int f_escape; /* print octal escapes for nongraphic characters */
90 1.7 mycroft int f_flags; /* show flags associated with a file */
91 1.46 grant int f_grouponly; /* long listing without owner */
92 1.1 cgd int f_inode; /* print inode */
93 1.1 cgd int f_listdir; /* list actual directory, not contents */
94 1.1 cgd int f_listdot; /* list files beginning with . */
95 1.1 cgd int f_longform; /* long listing format */
96 1.1 cgd int f_nonprint; /* show unprintables as ? */
97 1.1 cgd int f_nosort; /* don't sort output */
98 1.24 lukem int f_numericonly; /* don't convert uid/gid to name */
99 1.1 cgd int f_recursive; /* ls subdirectories also */
100 1.1 cgd int f_reversesort; /* reverse whatever sort is used */
101 1.1 cgd int f_sectime; /* print the real time for all files */
102 1.1 cgd int f_singlecol; /* use single column output */
103 1.1 cgd int f_size; /* list size in short listing */
104 1.1 cgd int f_statustime; /* use time of last mode change */
105 1.35 kleink int f_stream; /* stream format */
106 1.1 cgd int f_type; /* add type character for non-regular files */
107 1.36 kleink int f_typedir; /* add type character for directories */
108 1.13 mycroft int f_whiteout; /* show whiteout entries */
109 1.1 cgd
110 1.7 mycroft int
111 1.43 lukem ls_main(int argc, char *argv[])
112 1.1 cgd {
113 1.7 mycroft static char dot[] = ".", *dotav[] = { dot, NULL };
114 1.1 cgd struct winsize win;
115 1.49 simonb int ch, fts_options;
116 1.7 mycroft int kflag = 0;
117 1.27 mycroft const char *p;
118 1.39 tron
119 1.39 tron setlocale(LC_ALL, "");
120 1.7 mycroft
121 1.7 mycroft /* Terminal defaults to -Cq, non-terminal defaults to -1. */
122 1.7 mycroft if (isatty(STDOUT_FILENO)) {
123 1.16 jtc if ((p = getenv("COLUMNS")) != NULL)
124 1.16 jtc termwidth = atoi(p);
125 1.16 jtc else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 &&
126 1.16 jtc win.ws_col > 0)
127 1.1 cgd termwidth = win.ws_col;
128 1.7 mycroft f_column = f_nonprint = 1;
129 1.1 cgd } else
130 1.1 cgd f_singlecol = 1;
131 1.1 cgd
132 1.7 mycroft /* Root is -A automatically. */
133 1.1 cgd if (!getuid())
134 1.1 cgd f_listdot = 1;
135 1.1 cgd
136 1.7 mycroft fts_options = FTS_PHYSICAL;
137 1.51 jschauma while ((ch = getopt(argc, argv, "1ACFLRSTWabcdfgiklmnopqrstux")) != -1) {
138 1.1 cgd switch (ch) {
139 1.1 cgd /*
140 1.35 kleink * The -1, -C, -l, -m and -x options all override each other so
141 1.24 lukem * shell aliasing works correctly.
142 1.1 cgd */
143 1.1 cgd case '1':
144 1.1 cgd f_singlecol = 1;
145 1.35 kleink f_column = f_columnacross = f_longform = f_stream = 0;
146 1.1 cgd break;
147 1.1 cgd case 'C':
148 1.1 cgd f_column = 1;
149 1.35 kleink f_columnacross = f_longform = f_singlecol = f_stream =
150 1.35 kleink 0;
151 1.1 cgd break;
152 1.46 grant case 'g':
153 1.48 kleink if (f_grouponly != -1)
154 1.48 kleink f_grouponly = 1;
155 1.48 kleink f_longform = 1;
156 1.48 kleink f_column = f_columnacross = f_singlecol = f_stream = 0;
157 1.48 kleink break;
158 1.1 cgd case 'l':
159 1.1 cgd f_longform = 1;
160 1.35 kleink f_column = f_columnacross = f_singlecol = f_stream = 0;
161 1.48 kleink /* Never let -g take precedence over -l. */
162 1.48 kleink f_grouponly = -1;
163 1.35 kleink break;
164 1.35 kleink case 'm':
165 1.35 kleink f_stream = 1;
166 1.35 kleink f_column = f_columnacross = f_longform = f_singlecol =
167 1.35 kleink 0;
168 1.24 lukem break;
169 1.24 lukem case 'x':
170 1.24 lukem f_columnacross = 1;
171 1.35 kleink f_column = f_longform = f_singlecol = f_stream = 0;
172 1.1 cgd break;
173 1.7 mycroft /* The -c and -u options override each other. */
174 1.1 cgd case 'c':
175 1.1 cgd f_statustime = 1;
176 1.1 cgd f_accesstime = 0;
177 1.1 cgd break;
178 1.1 cgd case 'u':
179 1.1 cgd f_accesstime = 1;
180 1.1 cgd f_statustime = 0;
181 1.1 cgd break;
182 1.1 cgd case 'F':
183 1.1 cgd f_type = 1;
184 1.1 cgd break;
185 1.1 cgd case 'L':
186 1.7 mycroft fts_options &= ~FTS_PHYSICAL;
187 1.7 mycroft fts_options |= FTS_LOGICAL;
188 1.1 cgd break;
189 1.1 cgd case 'R':
190 1.1 cgd f_recursive = 1;
191 1.1 cgd break;
192 1.1 cgd case 'a':
193 1.7 mycroft fts_options |= FTS_SEEDOT;
194 1.1 cgd /* FALLTHROUGH */
195 1.1 cgd case 'A':
196 1.1 cgd f_listdot = 1;
197 1.1 cgd break;
198 1.51 jschauma /* the -b option turns off the -q option. */
199 1.51 jschauma case 'b':
200 1.51 jschauma f_escape = 1;
201 1.51 jschauma f_nonprint = 0;
202 1.51 jschauma break;
203 1.7 mycroft /* The -d option turns off the -R option. */
204 1.1 cgd case 'd':
205 1.1 cgd f_listdir = 1;
206 1.7 mycroft f_recursive = 0;
207 1.1 cgd break;
208 1.1 cgd case 'f':
209 1.1 cgd f_nosort = 1;
210 1.1 cgd break;
211 1.1 cgd case 'i':
212 1.1 cgd f_inode = 1;
213 1.1 cgd break;
214 1.1 cgd case 'k':
215 1.7 mycroft blocksize = 1024;
216 1.7 mycroft kflag = 1;
217 1.1 cgd break;
218 1.24 lukem case 'n':
219 1.24 lukem f_numericonly = 1;
220 1.24 lukem break;
221 1.7 mycroft case 'o':
222 1.7 mycroft f_flags = 1;
223 1.7 mycroft break;
224 1.36 kleink case 'p':
225 1.36 kleink f_typedir = 1;
226 1.36 kleink break;
227 1.51 jschauma /* the -q option turns off the -b option. */
228 1.1 cgd case 'q':
229 1.1 cgd f_nonprint = 1;
230 1.51 jschauma f_escape = 0;
231 1.1 cgd break;
232 1.1 cgd case 'r':
233 1.1 cgd f_reversesort = 1;
234 1.1 cgd break;
235 1.8 mycroft case 'S':
236 1.8 mycroft sortkey = BY_SIZE;
237 1.8 mycroft break;
238 1.1 cgd case 's':
239 1.1 cgd f_size = 1;
240 1.1 cgd break;
241 1.1 cgd case 'T':
242 1.1 cgd f_sectime = 1;
243 1.1 cgd break;
244 1.1 cgd case 't':
245 1.8 mycroft sortkey = BY_TIME;
246 1.1 cgd break;
247 1.13 mycroft case 'W':
248 1.13 mycroft f_whiteout = 1;
249 1.13 mycroft break;
250 1.1 cgd default:
251 1.1 cgd case '?':
252 1.1 cgd usage();
253 1.1 cgd }
254 1.1 cgd }
255 1.1 cgd argc -= optind;
256 1.1 cgd argv += optind;
257 1.48 kleink
258 1.48 kleink /*
259 1.48 kleink * If both -g and -l options, let -l take precedence.
260 1.48 kleink */
261 1.48 kleink if (f_grouponly == -1)
262 1.48 kleink f_grouponly = 0;
263 1.1 cgd
264 1.7 mycroft /*
265 1.36 kleink * If not -F, -i, -l, -p, -S, -s or -t options, don't require stat
266 1.7 mycroft * information.
267 1.7 mycroft */
268 1.36 kleink if (!f_inode && !f_longform && !f_size && !f_type && !f_typedir &&
269 1.8 mycroft sortkey == BY_NAME)
270 1.7 mycroft fts_options |= FTS_NOSTAT;
271 1.7 mycroft
272 1.7 mycroft /*
273 1.7 mycroft * If not -F, -d or -l options, follow any symbolic links listed on
274 1.7 mycroft * the command line.
275 1.7 mycroft */
276 1.7 mycroft if (!f_longform && !f_listdir && !f_type)
277 1.7 mycroft fts_options |= FTS_COMFOLLOW;
278 1.13 mycroft
279 1.13 mycroft /*
280 1.15 jtc * If -W, show whiteout entries
281 1.13 mycroft */
282 1.13 mycroft #ifdef FTS_WHITEOUT
283 1.13 mycroft if (f_whiteout)
284 1.13 mycroft fts_options |= FTS_WHITEOUT;
285 1.13 mycroft #endif
286 1.1 cgd
287 1.7 mycroft /* If -l or -s, figure out block size. */
288 1.45 simonb if (f_inode || f_longform || f_size) {
289 1.9 cgd if (!kflag)
290 1.49 simonb (void)getbsize(NULL, &blocksize);
291 1.7 mycroft blocksize /= 512;
292 1.7 mycroft }
293 1.1 cgd
294 1.7 mycroft /* Select a sort function. */
295 1.1 cgd if (f_reversesort) {
296 1.8 mycroft switch (sortkey) {
297 1.8 mycroft case BY_NAME:
298 1.1 cgd sortfcn = revnamecmp;
299 1.8 mycroft break;
300 1.8 mycroft case BY_SIZE:
301 1.8 mycroft sortfcn = revsizecmp;
302 1.8 mycroft break;
303 1.8 mycroft case BY_TIME:
304 1.8 mycroft if (f_accesstime)
305 1.8 mycroft sortfcn = revacccmp;
306 1.8 mycroft else if (f_statustime)
307 1.8 mycroft sortfcn = revstatcmp;
308 1.12 mycroft else /* Use modification time. */
309 1.8 mycroft sortfcn = revmodcmp;
310 1.8 mycroft break;
311 1.8 mycroft }
312 1.1 cgd } else {
313 1.8 mycroft switch (sortkey) {
314 1.8 mycroft case BY_NAME:
315 1.1 cgd sortfcn = namecmp;
316 1.8 mycroft break;
317 1.8 mycroft case BY_SIZE:
318 1.8 mycroft sortfcn = sizecmp;
319 1.8 mycroft break;
320 1.8 mycroft case BY_TIME:
321 1.8 mycroft if (f_accesstime)
322 1.8 mycroft sortfcn = acccmp;
323 1.8 mycroft else if (f_statustime)
324 1.8 mycroft sortfcn = statcmp;
325 1.12 mycroft else /* Use modification time. */
326 1.8 mycroft sortfcn = modcmp;
327 1.8 mycroft break;
328 1.8 mycroft }
329 1.1 cgd }
330 1.1 cgd
331 1.7 mycroft /* Select a print function. */
332 1.1 cgd if (f_singlecol)
333 1.1 cgd printfcn = printscol;
334 1.24 lukem else if (f_columnacross)
335 1.24 lukem printfcn = printacol;
336 1.1 cgd else if (f_longform)
337 1.1 cgd printfcn = printlong;
338 1.35 kleink else if (f_stream)
339 1.35 kleink printfcn = printstream;
340 1.1 cgd else
341 1.1 cgd printfcn = printcol;
342 1.1 cgd
343 1.7 mycroft if (argc)
344 1.7 mycroft traverse(argc, argv, fts_options);
345 1.7 mycroft else
346 1.7 mycroft traverse(1, dotav, fts_options);
347 1.37 simonb exit(rval);
348 1.30 mycroft /* NOTREACHED */
349 1.1 cgd }
350 1.1 cgd
351 1.7 mycroft static int output; /* If anything output. */
352 1.1 cgd
353 1.7 mycroft /*
354 1.7 mycroft * Traverse() walks the logical directory structure specified by the argv list
355 1.7 mycroft * in the order specified by the mastercmp() comparison function. During the
356 1.7 mycroft * traversal it passes linked lists of structures to display() which represent
357 1.7 mycroft * a superset (may be exact set) of the files to be displayed.
358 1.7 mycroft */
359 1.7 mycroft static void
360 1.43 lukem traverse(int argc, char *argv[], int options)
361 1.1 cgd {
362 1.12 mycroft FTS *ftsp;
363 1.12 mycroft FTSENT *p, *chp;
364 1.7 mycroft int ch_options;
365 1.7 mycroft
366 1.7 mycroft if ((ftsp =
367 1.7 mycroft fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL)
368 1.40 drochner err(EXIT_FAILURE, NULL);
369 1.7 mycroft
370 1.7 mycroft display(NULL, fts_children(ftsp, 0));
371 1.7 mycroft if (f_listdir)
372 1.7 mycroft return;
373 1.1 cgd
374 1.1 cgd /*
375 1.7 mycroft * If not recursing down this tree and don't need stat info, just get
376 1.7 mycroft * the names.
377 1.1 cgd */
378 1.7 mycroft ch_options = !f_recursive && options & FTS_NOSTAT ? FTS_NAMEONLY : 0;
379 1.1 cgd
380 1.12 mycroft while ((p = fts_read(ftsp)) != NULL)
381 1.12 mycroft switch (p->fts_info) {
382 1.7 mycroft case FTS_DC:
383 1.9 cgd warnx("%s: directory causes a cycle", p->fts_name);
384 1.7 mycroft break;
385 1.12 mycroft case FTS_DNR:
386 1.12 mycroft case FTS_ERR:
387 1.12 mycroft warnx("%s: %s", p->fts_name, strerror(p->fts_errno));
388 1.37 simonb rval = EXIT_FAILURE;
389 1.12 mycroft break;
390 1.7 mycroft case FTS_D:
391 1.7 mycroft if (p->fts_level != FTS_ROOTLEVEL &&
392 1.7 mycroft p->fts_name[0] == '.' && !f_listdot)
393 1.7 mycroft break;
394 1.1 cgd
395 1.7 mycroft /*
396 1.7 mycroft * If already output something, put out a newline as
397 1.7 mycroft * a separator. If multiple arguments, precede each
398 1.7 mycroft * directory with its name.
399 1.7 mycroft */
400 1.7 mycroft if (output)
401 1.7 mycroft (void)printf("\n%s:\n", p->fts_path);
402 1.7 mycroft else if (argc > 1) {
403 1.7 mycroft (void)printf("%s:\n", p->fts_path);
404 1.7 mycroft output = 1;
405 1.7 mycroft }
406 1.1 cgd
407 1.12 mycroft chp = fts_children(ftsp, ch_options);
408 1.12 mycroft display(p, chp);
409 1.1 cgd
410 1.12 mycroft if (!f_recursive && chp != NULL)
411 1.7 mycroft (void)fts_set(ftsp, p, FTS_SKIP);
412 1.7 mycroft break;
413 1.1 cgd }
414 1.12 mycroft if (errno)
415 1.35 kleink err(EXIT_FAILURE, "fts_read");
416 1.1 cgd }
417 1.1 cgd
418 1.7 mycroft /*
419 1.7 mycroft * Display() takes a linked list of FTSENT structures and passes the list
420 1.7 mycroft * along with any other necessary information to the print function. P
421 1.7 mycroft * points to the parent directory of the display list.
422 1.7 mycroft */
423 1.7 mycroft static void
424 1.43 lukem display(FTSENT *p, FTSENT *list)
425 1.1 cgd {
426 1.7 mycroft struct stat *sp;
427 1.7 mycroft DISPLAY d;
428 1.12 mycroft FTSENT *cur;
429 1.7 mycroft NAMES *np;
430 1.26 lukem u_int64_t btotal, maxblock, maxsize;
431 1.26 lukem int maxinode, maxnlink, maxmajor, maxminor;
432 1.26 lukem int bcfile, entries, flen, glen, ulen, maxflags, maxgroup, maxlen;
433 1.26 lukem int maxuser, needstats;
434 1.27 mycroft const char *user, *group;
435 1.41 enami char buf[21]; /* 64 bits == 20 digits, +1 for NUL */
436 1.24 lukem char nuser[12], ngroup[12];
437 1.26 lukem char *flags = NULL;
438 1.25 mycroft
439 1.25 mycroft #ifdef __GNUC__
440 1.25 mycroft /* This outrageous construct just to shut up a GCC warning. */
441 1.25 mycroft (void) &maxsize;
442 1.25 mycroft #endif
443 1.1 cgd
444 1.7 mycroft /*
445 1.7 mycroft * If list is NULL there are two possibilities: that the parent
446 1.7 mycroft * directory p has no children, or that fts_children() returned an
447 1.12 mycroft * error. We ignore the error case since it will be replicated
448 1.12 mycroft * on the next call to fts_read() on the post-order visit to the
449 1.12 mycroft * directory p, and will be signalled in traverse().
450 1.7 mycroft */
451 1.12 mycroft if (list == NULL)
452 1.7 mycroft return;
453 1.1 cgd
454 1.7 mycroft needstats = f_inode || f_longform || f_size;
455 1.7 mycroft flen = 0;
456 1.26 lukem maxinode = maxnlink = 0;
457 1.7 mycroft bcfile = 0;
458 1.26 lukem maxuser = maxgroup = maxflags = maxlen = 0;
459 1.26 lukem btotal = maxblock = maxsize = 0;
460 1.23 mycroft maxmajor = maxminor = 0;
461 1.7 mycroft for (cur = list, entries = 0; cur; cur = cur->fts_link) {
462 1.7 mycroft if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) {
463 1.12 mycroft warnx("%s: %s",
464 1.12 mycroft cur->fts_name, strerror(cur->fts_errno));
465 1.7 mycroft cur->fts_number = NO_PRINT;
466 1.37 simonb rval = EXIT_FAILURE;
467 1.7 mycroft continue;
468 1.7 mycroft }
469 1.1 cgd
470 1.7 mycroft /*
471 1.7 mycroft * P is NULL if list is the argv list, to which different rules
472 1.7 mycroft * apply.
473 1.7 mycroft */
474 1.7 mycroft if (p == NULL) {
475 1.7 mycroft /* Directories will be displayed later. */
476 1.7 mycroft if (cur->fts_info == FTS_D && !f_listdir) {
477 1.7 mycroft cur->fts_number = NO_PRINT;
478 1.1 cgd continue;
479 1.7 mycroft }
480 1.7 mycroft } else {
481 1.7 mycroft /* Only display dot file if -a/-A set. */
482 1.7 mycroft if (cur->fts_name[0] == '.' && !f_listdot) {
483 1.7 mycroft cur->fts_number = NO_PRINT;
484 1.1 cgd continue;
485 1.7 mycroft }
486 1.7 mycroft }
487 1.7 mycroft if (cur->fts_namelen > maxlen)
488 1.7 mycroft maxlen = cur->fts_namelen;
489 1.7 mycroft if (needstats) {
490 1.7 mycroft sp = cur->fts_statp;
491 1.7 mycroft if (sp->st_blocks > maxblock)
492 1.7 mycroft maxblock = sp->st_blocks;
493 1.7 mycroft if (sp->st_ino > maxinode)
494 1.7 mycroft maxinode = sp->st_ino;
495 1.7 mycroft if (sp->st_nlink > maxnlink)
496 1.7 mycroft maxnlink = sp->st_nlink;
497 1.7 mycroft if (sp->st_size > maxsize)
498 1.7 mycroft maxsize = sp->st_size;
499 1.23 mycroft if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode)) {
500 1.23 mycroft bcfile = 1;
501 1.23 mycroft if (major(sp->st_rdev) > maxmajor)
502 1.23 mycroft maxmajor = major(sp->st_rdev);
503 1.23 mycroft if (minor(sp->st_rdev) > maxminor)
504 1.23 mycroft maxminor = minor(sp->st_rdev);
505 1.23 mycroft }
506 1.7 mycroft
507 1.7 mycroft btotal += sp->st_blocks;
508 1.7 mycroft if (f_longform) {
509 1.41 enami if (f_numericonly ||
510 1.41 enami (user = user_from_uid(sp->st_uid, 0)) ==
511 1.41 enami NULL) {
512 1.29 mycroft (void)snprintf(nuser, sizeof(nuser),
513 1.29 mycroft "%u", sp->st_uid);
514 1.41 enami user = nuser;
515 1.41 enami }
516 1.41 enami if (f_numericonly ||
517 1.41 enami (group = group_from_gid(sp->st_gid, 0)) ==
518 1.41 enami NULL) {
519 1.29 mycroft (void)snprintf(ngroup, sizeof(ngroup),
520 1.29 mycroft "%u", sp->st_gid);
521 1.24 lukem group = ngroup;
522 1.24 lukem }
523 1.7 mycroft if ((ulen = strlen(user)) > maxuser)
524 1.7 mycroft maxuser = ulen;
525 1.7 mycroft if ((glen = strlen(group)) > maxgroup)
526 1.7 mycroft maxgroup = glen;
527 1.7 mycroft if (f_flags) {
528 1.12 mycroft flags =
529 1.12 mycroft flags_to_string(sp->st_flags, "-");
530 1.7 mycroft if ((flen = strlen(flags)) > maxflags)
531 1.7 mycroft maxflags = flen;
532 1.7 mycroft } else
533 1.7 mycroft flen = 0;
534 1.7 mycroft
535 1.7 mycroft if ((np = malloc(sizeof(NAMES) +
536 1.7 mycroft ulen + glen + flen + 3)) == NULL)
537 1.40 drochner err(EXIT_FAILURE, NULL);
538 1.7 mycroft
539 1.7 mycroft np->user = &np->data[0];
540 1.7 mycroft (void)strcpy(np->user, user);
541 1.7 mycroft np->group = &np->data[ulen + 1];
542 1.7 mycroft (void)strcpy(np->group, group);
543 1.7 mycroft
544 1.7 mycroft if (f_flags) {
545 1.7 mycroft np->flags = &np->data[ulen + glen + 2];
546 1.7 mycroft (void)strcpy(np->flags, flags);
547 1.7 mycroft }
548 1.7 mycroft cur->fts_pointer = np;
549 1.7 mycroft }
550 1.1 cgd }
551 1.7 mycroft ++entries;
552 1.1 cgd }
553 1.1 cgd
554 1.7 mycroft if (!entries)
555 1.1 cgd return;
556 1.7 mycroft
557 1.7 mycroft d.list = list;
558 1.7 mycroft d.entries = entries;
559 1.7 mycroft d.maxlen = maxlen;
560 1.7 mycroft if (needstats) {
561 1.7 mycroft d.btotal = btotal;
562 1.44 sommerfe (void)snprintf(buf, sizeof(buf), "%llu",
563 1.44 sommerfe (long long)howmany(maxblock, blocksize));
564 1.7 mycroft d.s_block = strlen(buf);
565 1.7 mycroft d.s_flags = maxflags;
566 1.7 mycroft d.s_group = maxgroup;
567 1.26 lukem (void)snprintf(buf, sizeof(buf), "%u", maxinode);
568 1.7 mycroft d.s_inode = strlen(buf);
569 1.26 lukem (void)snprintf(buf, sizeof(buf), "%u", maxnlink);
570 1.7 mycroft d.s_nlink = strlen(buf);
571 1.34 christos (void)snprintf(buf, sizeof(buf), "%llu", (long long)maxsize);
572 1.7 mycroft d.s_size = strlen(buf);
573 1.7 mycroft d.s_user = maxuser;
574 1.23 mycroft if (bcfile) {
575 1.26 lukem (void)snprintf(buf, sizeof(buf), "%u", maxmajor);
576 1.23 mycroft d.s_major = strlen(buf);
577 1.26 lukem (void)snprintf(buf, sizeof(buf), "%u", maxminor);
578 1.23 mycroft d.s_minor = strlen(buf);
579 1.23 mycroft if (d.s_major + d.s_minor + 2 > d.s_size)
580 1.23 mycroft d.s_size = d.s_major + d.s_minor + 2;
581 1.23 mycroft else if (d.s_size - d.s_minor - 2 > d.s_major)
582 1.23 mycroft d.s_major = d.s_size - d.s_minor - 2;
583 1.23 mycroft } else {
584 1.23 mycroft d.s_major = 0;
585 1.23 mycroft d.s_minor = 0;
586 1.23 mycroft }
587 1.7 mycroft }
588 1.7 mycroft
589 1.7 mycroft printfcn(&d);
590 1.7 mycroft output = 1;
591 1.7 mycroft
592 1.7 mycroft if (f_longform)
593 1.7 mycroft for (cur = list; cur; cur = cur->fts_link)
594 1.7 mycroft free(cur->fts_pointer);
595 1.1 cgd }
596 1.1 cgd
597 1.7 mycroft /*
598 1.7 mycroft * Ordering for mastercmp:
599 1.7 mycroft * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories
600 1.7 mycroft * as larger than directories. Within either group, use the sort function.
601 1.7 mycroft * All other levels use the sort function. Error entries remain unsorted.
602 1.7 mycroft */
603 1.7 mycroft static int
604 1.43 lukem mastercmp(const FTSENT **a, const FTSENT **b)
605 1.1 cgd {
606 1.12 mycroft int a_info, b_info;
607 1.1 cgd
608 1.7 mycroft a_info = (*a)->fts_info;
609 1.7 mycroft if (a_info == FTS_ERR)
610 1.7 mycroft return (0);
611 1.7 mycroft b_info = (*b)->fts_info;
612 1.7 mycroft if (b_info == FTS_ERR)
613 1.7 mycroft return (0);
614 1.7 mycroft
615 1.31 thorpej if (a_info == FTS_NS || b_info == FTS_NS) {
616 1.17 mycroft if (b_info != FTS_NS)
617 1.17 mycroft return (1);
618 1.17 mycroft else if (a_info != FTS_NS)
619 1.17 mycroft return (-1);
620 1.17 mycroft else
621 1.18 mycroft return (namecmp(*a, *b));
622 1.31 thorpej }
623 1.7 mycroft
624 1.24 lukem if (a_info != b_info && !f_listdir &&
625 1.24 lukem (*a)->fts_level == FTS_ROOTLEVEL) {
626 1.7 mycroft if (a_info == FTS_D)
627 1.7 mycroft return (1);
628 1.7 mycroft else if (b_info == FTS_D)
629 1.7 mycroft return (-1);
630 1.24 lukem }
631 1.24 lukem return (sortfcn(*a, *b));
632 1.1 cgd }
633