ls.c revision 1.68 1 1.68 erh /* $NetBSD: ls.c,v 1.68 2011/03/15 03:52:37 erh 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.64 lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\
38 1.64 lukem The Regents of the University of California. All rights reserved.");
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.68 erh __RCSID("$NetBSD: ls.c,v 1.68 2011/03/15 03:52:37 erh Exp $");
46 1.14 cgd #endif
47 1.1 cgd #endif /* not lint */
48 1.1 cgd
49 1.67 rmind #include <sys/param.h>
50 1.7 mycroft #include <sys/types.h>
51 1.1 cgd #include <sys/stat.h>
52 1.1 cgd #include <sys/ioctl.h>
53 1.12 mycroft
54 1.1 cgd #include <dirent.h>
55 1.12 mycroft #include <err.h>
56 1.12 mycroft #include <errno.h>
57 1.7 mycroft #include <fts.h>
58 1.39 tron #include <locale.h>
59 1.12 mycroft #include <stdio.h>
60 1.7 mycroft #include <stdlib.h>
61 1.1 cgd #include <string.h>
62 1.12 mycroft #include <unistd.h>
63 1.33 christos #include <termios.h>
64 1.21 christos #include <pwd.h>
65 1.21 christos #include <grp.h>
66 1.63 he #include <util.h>
67 1.12 mycroft
68 1.1 cgd #include "ls.h"
69 1.7 mycroft #include "extern.h"
70 1.1 cgd
71 1.43 lukem static void display(FTSENT *, FTSENT *);
72 1.43 lukem static int mastercmp(const FTSENT **, const FTSENT **);
73 1.43 lukem static void traverse(int, char **, int);
74 1.7 mycroft
75 1.43 lukem static void (*printfcn)(DISPLAY *);
76 1.43 lukem static int (*sortfcn)(const FTSENT *, const FTSENT *);
77 1.7 mycroft
78 1.12 mycroft #define BY_NAME 0
79 1.12 mycroft #define BY_SIZE 1
80 1.8 mycroft #define BY_TIME 2
81 1.8 mycroft
82 1.7 mycroft long blocksize; /* block size units */
83 1.1 cgd int termwidth = 80; /* default terminal width */
84 1.8 mycroft int sortkey = BY_NAME;
85 1.37 simonb int rval = EXIT_SUCCESS; /* exit value - set if error encountered */
86 1.1 cgd
87 1.1 cgd /* flags */
88 1.1 cgd int f_accesstime; /* use time of last access */
89 1.1 cgd int f_column; /* columnated format */
90 1.24 lukem int f_columnacross; /* columnated format, sorted across */
91 1.7 mycroft int f_flags; /* show flags associated with a file */
92 1.46 grant int f_grouponly; /* long listing without owner */
93 1.53 grant int f_humanize; /* humanize the size field */
94 1.68 erh int f_commas; /* separate size field with comma */
95 1.1 cgd int f_inode; /* print inode */
96 1.1 cgd int f_listdir; /* list actual directory, not contents */
97 1.1 cgd int f_listdot; /* list files beginning with . */
98 1.1 cgd int f_longform; /* long listing format */
99 1.1 cgd int f_nonprint; /* show unprintables as ? */
100 1.1 cgd int f_nosort; /* don't sort output */
101 1.24 lukem int f_numericonly; /* don't convert uid/gid to name */
102 1.52 jschauma int f_octal; /* print octal escapes for nongraphic characters */
103 1.52 jschauma int f_octal_escape; /* like f_octal but use C escapes if possible */
104 1.1 cgd int f_recursive; /* ls subdirectories also */
105 1.1 cgd int f_reversesort; /* reverse whatever sort is used */
106 1.1 cgd int f_sectime; /* print the real time for all files */
107 1.1 cgd int f_singlecol; /* use single column output */
108 1.1 cgd int f_size; /* list size in short listing */
109 1.1 cgd int f_statustime; /* use time of last mode change */
110 1.35 kleink int f_stream; /* stream format */
111 1.1 cgd int f_type; /* add type character for non-regular files */
112 1.36 kleink int f_typedir; /* add type character for directories */
113 1.13 mycroft int f_whiteout; /* show whiteout entries */
114 1.1 cgd
115 1.7 mycroft int
116 1.43 lukem ls_main(int argc, char *argv[])
117 1.1 cgd {
118 1.7 mycroft static char dot[] = ".", *dotav[] = { dot, NULL };
119 1.1 cgd struct winsize win;
120 1.49 simonb int ch, fts_options;
121 1.7 mycroft int kflag = 0;
122 1.27 mycroft const char *p;
123 1.39 tron
124 1.56 hira setprogname(argv[0]);
125 1.62 christos (void)setlocale(LC_ALL, "");
126 1.7 mycroft
127 1.7 mycroft /* Terminal defaults to -Cq, non-terminal defaults to -1. */
128 1.7 mycroft if (isatty(STDOUT_FILENO)) {
129 1.57 jschauma if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 &&
130 1.16 jtc win.ws_col > 0)
131 1.1 cgd termwidth = win.ws_col;
132 1.7 mycroft f_column = f_nonprint = 1;
133 1.1 cgd } else
134 1.1 cgd f_singlecol = 1;
135 1.1 cgd
136 1.7 mycroft /* Root is -A automatically. */
137 1.1 cgd if (!getuid())
138 1.1 cgd f_listdot = 1;
139 1.1 cgd
140 1.7 mycroft fts_options = FTS_PHYSICAL;
141 1.68 erh while ((ch = getopt(argc, argv, "1ABCFLMRSTWabcdfghiklmnopqrstuwx")) != -1) {
142 1.1 cgd switch (ch) {
143 1.1 cgd /*
144 1.35 kleink * The -1, -C, -l, -m and -x options all override each other so
145 1.24 lukem * shell aliasing works correctly.
146 1.1 cgd */
147 1.1 cgd case '1':
148 1.1 cgd f_singlecol = 1;
149 1.35 kleink f_column = f_columnacross = f_longform = f_stream = 0;
150 1.1 cgd break;
151 1.1 cgd case 'C':
152 1.1 cgd f_column = 1;
153 1.35 kleink f_columnacross = f_longform = f_singlecol = f_stream =
154 1.35 kleink 0;
155 1.1 cgd break;
156 1.46 grant case 'g':
157 1.48 kleink if (f_grouponly != -1)
158 1.48 kleink f_grouponly = 1;
159 1.48 kleink f_longform = 1;
160 1.48 kleink f_column = f_columnacross = f_singlecol = f_stream = 0;
161 1.48 kleink break;
162 1.1 cgd case 'l':
163 1.1 cgd f_longform = 1;
164 1.35 kleink f_column = f_columnacross = f_singlecol = f_stream = 0;
165 1.48 kleink /* Never let -g take precedence over -l. */
166 1.48 kleink f_grouponly = -1;
167 1.35 kleink break;
168 1.35 kleink case 'm':
169 1.35 kleink f_stream = 1;
170 1.35 kleink f_column = f_columnacross = f_longform = f_singlecol =
171 1.35 kleink 0;
172 1.24 lukem break;
173 1.24 lukem case 'x':
174 1.24 lukem f_columnacross = 1;
175 1.35 kleink f_column = f_longform = f_singlecol = f_stream = 0;
176 1.1 cgd break;
177 1.7 mycroft /* The -c and -u options override each other. */
178 1.1 cgd case 'c':
179 1.1 cgd f_statustime = 1;
180 1.1 cgd f_accesstime = 0;
181 1.1 cgd break;
182 1.1 cgd case 'u':
183 1.1 cgd f_accesstime = 1;
184 1.1 cgd f_statustime = 0;
185 1.1 cgd break;
186 1.1 cgd case 'F':
187 1.1 cgd f_type = 1;
188 1.1 cgd break;
189 1.1 cgd case 'L':
190 1.7 mycroft fts_options &= ~FTS_PHYSICAL;
191 1.7 mycroft fts_options |= FTS_LOGICAL;
192 1.1 cgd break;
193 1.1 cgd case 'R':
194 1.1 cgd f_recursive = 1;
195 1.1 cgd break;
196 1.1 cgd case 'a':
197 1.7 mycroft fts_options |= FTS_SEEDOT;
198 1.1 cgd /* FALLTHROUGH */
199 1.1 cgd case 'A':
200 1.1 cgd f_listdot = 1;
201 1.1 cgd break;
202 1.58 jschauma /* The -B option turns off the -b, -q and -w options. */
203 1.52 jschauma case 'B':
204 1.52 jschauma f_nonprint = 0;
205 1.52 jschauma f_octal = 1;
206 1.52 jschauma f_octal_escape = 0;
207 1.52 jschauma break;
208 1.58 jschauma /* The -b option turns off the -B, -q and -w options. */
209 1.51 jschauma case 'b':
210 1.51 jschauma f_nonprint = 0;
211 1.52 jschauma f_octal = 0;
212 1.52 jschauma f_octal_escape = 1;
213 1.51 jschauma break;
214 1.7 mycroft /* The -d option turns off the -R option. */
215 1.1 cgd case 'd':
216 1.1 cgd f_listdir = 1;
217 1.7 mycroft f_recursive = 0;
218 1.1 cgd break;
219 1.1 cgd case 'f':
220 1.1 cgd f_nosort = 1;
221 1.1 cgd break;
222 1.1 cgd case 'i':
223 1.1 cgd f_inode = 1;
224 1.1 cgd break;
225 1.1 cgd case 'k':
226 1.7 mycroft blocksize = 1024;
227 1.7 mycroft kflag = 1;
228 1.60 christos f_humanize = 0;
229 1.1 cgd break;
230 1.58 jschauma /* The -h option forces all sizes to be measured in bytes. */
231 1.53 grant case 'h':
232 1.53 grant f_humanize = 1;
233 1.60 christos kflag = 0;
234 1.68 erh f_commas = 0;
235 1.68 erh break;
236 1.68 erh case 'M':
237 1.68 erh f_humanize = 0;
238 1.68 erh f_commas = 1;
239 1.53 grant break;
240 1.24 lukem case 'n':
241 1.24 lukem f_numericonly = 1;
242 1.65 lukem f_longform = 1;
243 1.65 lukem f_column = f_columnacross = f_singlecol = f_stream = 0;
244 1.24 lukem break;
245 1.7 mycroft case 'o':
246 1.7 mycroft f_flags = 1;
247 1.7 mycroft break;
248 1.36 kleink case 'p':
249 1.36 kleink f_typedir = 1;
250 1.36 kleink break;
251 1.58 jschauma /* The -q option turns off the -B, -b and -w options. */
252 1.1 cgd case 'q':
253 1.1 cgd f_nonprint = 1;
254 1.52 jschauma f_octal = 0;
255 1.52 jschauma f_octal_escape = 0;
256 1.1 cgd break;
257 1.1 cgd case 'r':
258 1.1 cgd f_reversesort = 1;
259 1.1 cgd break;
260 1.8 mycroft case 'S':
261 1.8 mycroft sortkey = BY_SIZE;
262 1.8 mycroft break;
263 1.1 cgd case 's':
264 1.1 cgd f_size = 1;
265 1.1 cgd break;
266 1.1 cgd case 'T':
267 1.1 cgd f_sectime = 1;
268 1.1 cgd break;
269 1.1 cgd case 't':
270 1.8 mycroft sortkey = BY_TIME;
271 1.1 cgd break;
272 1.13 mycroft case 'W':
273 1.13 mycroft f_whiteout = 1;
274 1.52 jschauma break;
275 1.58 jschauma /* The -w option turns off the -B, -b and -q options. */
276 1.52 jschauma case 'w':
277 1.52 jschauma f_nonprint = 0;
278 1.52 jschauma f_octal = 0;
279 1.52 jschauma f_octal_escape = 0;
280 1.13 mycroft break;
281 1.1 cgd default:
282 1.1 cgd case '?':
283 1.1 cgd usage();
284 1.1 cgd }
285 1.1 cgd }
286 1.1 cgd argc -= optind;
287 1.1 cgd argv += optind;
288 1.48 kleink
289 1.57 jschauma if (f_column || f_columnacross || f_stream) {
290 1.57 jschauma if ((p = getenv("COLUMNS")) != NULL)
291 1.57 jschauma termwidth = atoi(p);
292 1.57 jschauma }
293 1.57 jschauma
294 1.48 kleink /*
295 1.48 kleink * If both -g and -l options, let -l take precedence.
296 1.48 kleink */
297 1.48 kleink if (f_grouponly == -1)
298 1.48 kleink f_grouponly = 0;
299 1.1 cgd
300 1.7 mycroft /*
301 1.36 kleink * If not -F, -i, -l, -p, -S, -s or -t options, don't require stat
302 1.7 mycroft * information.
303 1.7 mycroft */
304 1.36 kleink if (!f_inode && !f_longform && !f_size && !f_type && !f_typedir &&
305 1.8 mycroft sortkey == BY_NAME)
306 1.7 mycroft fts_options |= FTS_NOSTAT;
307 1.7 mycroft
308 1.7 mycroft /*
309 1.7 mycroft * If not -F, -d or -l options, follow any symbolic links listed on
310 1.7 mycroft * the command line.
311 1.7 mycroft */
312 1.7 mycroft if (!f_longform && !f_listdir && !f_type)
313 1.7 mycroft fts_options |= FTS_COMFOLLOW;
314 1.13 mycroft
315 1.13 mycroft /*
316 1.15 jtc * If -W, show whiteout entries
317 1.13 mycroft */
318 1.13 mycroft #ifdef FTS_WHITEOUT
319 1.13 mycroft if (f_whiteout)
320 1.13 mycroft fts_options |= FTS_WHITEOUT;
321 1.13 mycroft #endif
322 1.1 cgd
323 1.7 mycroft /* If -l or -s, figure out block size. */
324 1.45 simonb if (f_inode || f_longform || f_size) {
325 1.9 cgd if (!kflag)
326 1.49 simonb (void)getbsize(NULL, &blocksize);
327 1.7 mycroft blocksize /= 512;
328 1.7 mycroft }
329 1.1 cgd
330 1.7 mycroft /* Select a sort function. */
331 1.1 cgd if (f_reversesort) {
332 1.8 mycroft switch (sortkey) {
333 1.8 mycroft case BY_NAME:
334 1.1 cgd sortfcn = revnamecmp;
335 1.8 mycroft break;
336 1.8 mycroft case BY_SIZE:
337 1.8 mycroft sortfcn = revsizecmp;
338 1.8 mycroft break;
339 1.8 mycroft case BY_TIME:
340 1.8 mycroft if (f_accesstime)
341 1.8 mycroft sortfcn = revacccmp;
342 1.8 mycroft else if (f_statustime)
343 1.8 mycroft sortfcn = revstatcmp;
344 1.12 mycroft else /* Use modification time. */
345 1.8 mycroft sortfcn = revmodcmp;
346 1.8 mycroft break;
347 1.8 mycroft }
348 1.1 cgd } else {
349 1.8 mycroft switch (sortkey) {
350 1.8 mycroft case BY_NAME:
351 1.1 cgd sortfcn = namecmp;
352 1.8 mycroft break;
353 1.8 mycroft case BY_SIZE:
354 1.8 mycroft sortfcn = sizecmp;
355 1.8 mycroft break;
356 1.8 mycroft case BY_TIME:
357 1.8 mycroft if (f_accesstime)
358 1.8 mycroft sortfcn = acccmp;
359 1.8 mycroft else if (f_statustime)
360 1.8 mycroft sortfcn = statcmp;
361 1.12 mycroft else /* Use modification time. */
362 1.8 mycroft sortfcn = modcmp;
363 1.8 mycroft break;
364 1.8 mycroft }
365 1.1 cgd }
366 1.1 cgd
367 1.7 mycroft /* Select a print function. */
368 1.1 cgd if (f_singlecol)
369 1.1 cgd printfcn = printscol;
370 1.24 lukem else if (f_columnacross)
371 1.24 lukem printfcn = printacol;
372 1.1 cgd else if (f_longform)
373 1.1 cgd printfcn = printlong;
374 1.35 kleink else if (f_stream)
375 1.35 kleink printfcn = printstream;
376 1.1 cgd else
377 1.1 cgd printfcn = printcol;
378 1.1 cgd
379 1.7 mycroft if (argc)
380 1.7 mycroft traverse(argc, argv, fts_options);
381 1.7 mycroft else
382 1.7 mycroft traverse(1, dotav, fts_options);
383 1.62 christos return rval;
384 1.30 mycroft /* NOTREACHED */
385 1.1 cgd }
386 1.1 cgd
387 1.7 mycroft static int output; /* If anything output. */
388 1.1 cgd
389 1.7 mycroft /*
390 1.7 mycroft * Traverse() walks the logical directory structure specified by the argv list
391 1.7 mycroft * in the order specified by the mastercmp() comparison function. During the
392 1.7 mycroft * traversal it passes linked lists of structures to display() which represent
393 1.7 mycroft * a superset (may be exact set) of the files to be displayed.
394 1.7 mycroft */
395 1.7 mycroft static void
396 1.43 lukem traverse(int argc, char *argv[], int options)
397 1.1 cgd {
398 1.12 mycroft FTS *ftsp;
399 1.12 mycroft FTSENT *p, *chp;
400 1.59 christos int ch_options, error;
401 1.7 mycroft
402 1.7 mycroft if ((ftsp =
403 1.7 mycroft fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL)
404 1.40 drochner err(EXIT_FAILURE, NULL);
405 1.7 mycroft
406 1.7 mycroft display(NULL, fts_children(ftsp, 0));
407 1.59 christos if (f_listdir) {
408 1.62 christos (void)fts_close(ftsp);
409 1.7 mycroft return;
410 1.59 christos }
411 1.1 cgd
412 1.1 cgd /*
413 1.7 mycroft * If not recursing down this tree and don't need stat info, just get
414 1.7 mycroft * the names.
415 1.1 cgd */
416 1.7 mycroft ch_options = !f_recursive && options & FTS_NOSTAT ? FTS_NAMEONLY : 0;
417 1.1 cgd
418 1.12 mycroft while ((p = fts_read(ftsp)) != NULL)
419 1.12 mycroft switch (p->fts_info) {
420 1.7 mycroft case FTS_DC:
421 1.9 cgd warnx("%s: directory causes a cycle", p->fts_name);
422 1.7 mycroft break;
423 1.12 mycroft case FTS_DNR:
424 1.12 mycroft case FTS_ERR:
425 1.12 mycroft warnx("%s: %s", p->fts_name, strerror(p->fts_errno));
426 1.37 simonb rval = EXIT_FAILURE;
427 1.12 mycroft break;
428 1.7 mycroft case FTS_D:
429 1.7 mycroft if (p->fts_level != FTS_ROOTLEVEL &&
430 1.7 mycroft p->fts_name[0] == '.' && !f_listdot)
431 1.7 mycroft break;
432 1.1 cgd
433 1.7 mycroft /*
434 1.7 mycroft * If already output something, put out a newline as
435 1.7 mycroft * a separator. If multiple arguments, precede each
436 1.7 mycroft * directory with its name.
437 1.7 mycroft */
438 1.7 mycroft if (output)
439 1.7 mycroft (void)printf("\n%s:\n", p->fts_path);
440 1.7 mycroft else if (argc > 1) {
441 1.7 mycroft (void)printf("%s:\n", p->fts_path);
442 1.7 mycroft output = 1;
443 1.7 mycroft }
444 1.1 cgd
445 1.12 mycroft chp = fts_children(ftsp, ch_options);
446 1.12 mycroft display(p, chp);
447 1.1 cgd
448 1.12 mycroft if (!f_recursive && chp != NULL)
449 1.7 mycroft (void)fts_set(ftsp, p, FTS_SKIP);
450 1.7 mycroft break;
451 1.1 cgd }
452 1.59 christos error = errno;
453 1.62 christos (void)fts_close(ftsp);
454 1.59 christos errno = error;
455 1.12 mycroft if (errno)
456 1.35 kleink err(EXIT_FAILURE, "fts_read");
457 1.1 cgd }
458 1.1 cgd
459 1.7 mycroft /*
460 1.7 mycroft * Display() takes a linked list of FTSENT structures and passes the list
461 1.7 mycroft * along with any other necessary information to the print function. P
462 1.7 mycroft * points to the parent directory of the display list.
463 1.7 mycroft */
464 1.7 mycroft static void
465 1.43 lukem display(FTSENT *p, FTSENT *list)
466 1.1 cgd {
467 1.7 mycroft struct stat *sp;
468 1.7 mycroft DISPLAY d;
469 1.12 mycroft FTSENT *cur;
470 1.7 mycroft NAMES *np;
471 1.66 lukem u_int64_t btotal, stotal;
472 1.66 lukem off_t maxsize;
473 1.66 lukem blkcnt_t maxblock;
474 1.62 christos ino_t maxinode;
475 1.66 lukem int maxmajor, maxminor;
476 1.66 lukem uint32_t maxnlink;
477 1.66 lukem int bcfile, entries, flen, glen, ulen, maxflags, maxgroup;
478 1.66 lukem unsigned int maxlen;
479 1.26 lukem int maxuser, needstats;
480 1.27 mycroft const char *user, *group;
481 1.41 enami char buf[21]; /* 64 bits == 20 digits, +1 for NUL */
482 1.24 lukem char nuser[12], ngroup[12];
483 1.26 lukem char *flags = NULL;
484 1.25 mycroft
485 1.7 mycroft /*
486 1.7 mycroft * If list is NULL there are two possibilities: that the parent
487 1.7 mycroft * directory p has no children, or that fts_children() returned an
488 1.12 mycroft * error. We ignore the error case since it will be replicated
489 1.12 mycroft * on the next call to fts_read() on the post-order visit to the
490 1.12 mycroft * directory p, and will be signalled in traverse().
491 1.7 mycroft */
492 1.12 mycroft if (list == NULL)
493 1.7 mycroft return;
494 1.1 cgd
495 1.7 mycroft needstats = f_inode || f_longform || f_size;
496 1.7 mycroft flen = 0;
497 1.26 lukem maxinode = maxnlink = 0;
498 1.7 mycroft bcfile = 0;
499 1.26 lukem maxuser = maxgroup = maxflags = maxlen = 0;
500 1.53 grant btotal = stotal = maxblock = maxsize = 0;
501 1.23 mycroft maxmajor = maxminor = 0;
502 1.7 mycroft for (cur = list, entries = 0; cur; cur = cur->fts_link) {
503 1.7 mycroft if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) {
504 1.12 mycroft warnx("%s: %s",
505 1.12 mycroft cur->fts_name, strerror(cur->fts_errno));
506 1.7 mycroft cur->fts_number = NO_PRINT;
507 1.37 simonb rval = EXIT_FAILURE;
508 1.7 mycroft continue;
509 1.7 mycroft }
510 1.1 cgd
511 1.7 mycroft /*
512 1.7 mycroft * P is NULL if list is the argv list, to which different rules
513 1.7 mycroft * apply.
514 1.7 mycroft */
515 1.7 mycroft if (p == NULL) {
516 1.7 mycroft /* Directories will be displayed later. */
517 1.7 mycroft if (cur->fts_info == FTS_D && !f_listdir) {
518 1.7 mycroft cur->fts_number = NO_PRINT;
519 1.1 cgd continue;
520 1.7 mycroft }
521 1.7 mycroft } else {
522 1.7 mycroft /* Only display dot file if -a/-A set. */
523 1.7 mycroft if (cur->fts_name[0] == '.' && !f_listdot) {
524 1.7 mycroft cur->fts_number = NO_PRINT;
525 1.1 cgd continue;
526 1.7 mycroft }
527 1.7 mycroft }
528 1.7 mycroft if (cur->fts_namelen > maxlen)
529 1.7 mycroft maxlen = cur->fts_namelen;
530 1.7 mycroft if (needstats) {
531 1.7 mycroft sp = cur->fts_statp;
532 1.7 mycroft if (sp->st_blocks > maxblock)
533 1.7 mycroft maxblock = sp->st_blocks;
534 1.7 mycroft if (sp->st_ino > maxinode)
535 1.7 mycroft maxinode = sp->st_ino;
536 1.7 mycroft if (sp->st_nlink > maxnlink)
537 1.7 mycroft maxnlink = sp->st_nlink;
538 1.7 mycroft if (sp->st_size > maxsize)
539 1.7 mycroft maxsize = sp->st_size;
540 1.23 mycroft if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode)) {
541 1.23 mycroft bcfile = 1;
542 1.23 mycroft if (major(sp->st_rdev) > maxmajor)
543 1.23 mycroft maxmajor = major(sp->st_rdev);
544 1.23 mycroft if (minor(sp->st_rdev) > maxminor)
545 1.23 mycroft maxminor = minor(sp->st_rdev);
546 1.23 mycroft }
547 1.7 mycroft
548 1.7 mycroft btotal += sp->st_blocks;
549 1.53 grant stotal += sp->st_size;
550 1.7 mycroft if (f_longform) {
551 1.41 enami if (f_numericonly ||
552 1.41 enami (user = user_from_uid(sp->st_uid, 0)) ==
553 1.41 enami NULL) {
554 1.29 mycroft (void)snprintf(nuser, sizeof(nuser),
555 1.29 mycroft "%u", sp->st_uid);
556 1.41 enami user = nuser;
557 1.41 enami }
558 1.41 enami if (f_numericonly ||
559 1.41 enami (group = group_from_gid(sp->st_gid, 0)) ==
560 1.41 enami NULL) {
561 1.29 mycroft (void)snprintf(ngroup, sizeof(ngroup),
562 1.29 mycroft "%u", sp->st_gid);
563 1.24 lukem group = ngroup;
564 1.24 lukem }
565 1.7 mycroft if ((ulen = strlen(user)) > maxuser)
566 1.7 mycroft maxuser = ulen;
567 1.7 mycroft if ((glen = strlen(group)) > maxgroup)
568 1.7 mycroft maxgroup = glen;
569 1.7 mycroft if (f_flags) {
570 1.12 mycroft flags =
571 1.62 christos flags_to_string((u_long)sp->st_flags, "-");
572 1.7 mycroft if ((flen = strlen(flags)) > maxflags)
573 1.7 mycroft maxflags = flen;
574 1.7 mycroft } else
575 1.7 mycroft flen = 0;
576 1.7 mycroft
577 1.7 mycroft if ((np = malloc(sizeof(NAMES) +
578 1.61 elad ulen + glen + flen + 2)) == NULL)
579 1.40 drochner err(EXIT_FAILURE, NULL);
580 1.7 mycroft
581 1.7 mycroft np->user = &np->data[0];
582 1.7 mycroft (void)strcpy(np->user, user);
583 1.7 mycroft np->group = &np->data[ulen + 1];
584 1.7 mycroft (void)strcpy(np->group, group);
585 1.7 mycroft
586 1.7 mycroft if (f_flags) {
587 1.7 mycroft np->flags = &np->data[ulen + glen + 2];
588 1.7 mycroft (void)strcpy(np->flags, flags);
589 1.63 he free(flags);
590 1.7 mycroft }
591 1.7 mycroft cur->fts_pointer = np;
592 1.7 mycroft }
593 1.1 cgd }
594 1.7 mycroft ++entries;
595 1.1 cgd }
596 1.1 cgd
597 1.7 mycroft if (!entries)
598 1.1 cgd return;
599 1.7 mycroft
600 1.7 mycroft d.list = list;
601 1.7 mycroft d.entries = entries;
602 1.7 mycroft d.maxlen = maxlen;
603 1.7 mycroft if (needstats) {
604 1.7 mycroft d.btotal = btotal;
605 1.53 grant d.stotal = stotal;
606 1.55 simonb if (f_humanize) {
607 1.55 simonb d.s_block = 4; /* min buf length for humanize_number */
608 1.55 simonb } else {
609 1.53 grant (void)snprintf(buf, sizeof(buf), "%llu",
610 1.53 grant (long long)howmany(maxblock, blocksize));
611 1.53 grant d.s_block = strlen(buf);
612 1.68 erh if (f_commas) /* allow for commas before every third digit */
613 1.68 erh d.s_block += (d.s_block - 1) / 3;
614 1.53 grant }
615 1.7 mycroft d.s_flags = maxflags;
616 1.7 mycroft d.s_group = maxgroup;
617 1.62 christos (void)snprintf(buf, sizeof(buf), "%llu",
618 1.62 christos (unsigned long long)maxinode);
619 1.7 mycroft d.s_inode = strlen(buf);
620 1.26 lukem (void)snprintf(buf, sizeof(buf), "%u", maxnlink);
621 1.7 mycroft d.s_nlink = strlen(buf);
622 1.53 grant if (f_humanize) {
623 1.54 simonb d.s_size = 4; /* min buf length for humanize_number */
624 1.53 grant } else {
625 1.54 simonb (void)snprintf(buf, sizeof(buf), "%llu",
626 1.54 simonb (long long)maxsize);
627 1.53 grant d.s_size = strlen(buf);
628 1.68 erh if (f_commas) /* allow for commas before every third digit */
629 1.68 erh d.s_size += (d.s_size - 1) / 3;
630 1.53 grant }
631 1.7 mycroft d.s_user = maxuser;
632 1.23 mycroft if (bcfile) {
633 1.26 lukem (void)snprintf(buf, sizeof(buf), "%u", maxmajor);
634 1.23 mycroft d.s_major = strlen(buf);
635 1.26 lukem (void)snprintf(buf, sizeof(buf), "%u", maxminor);
636 1.23 mycroft d.s_minor = strlen(buf);
637 1.23 mycroft if (d.s_major + d.s_minor + 2 > d.s_size)
638 1.23 mycroft d.s_size = d.s_major + d.s_minor + 2;
639 1.23 mycroft else if (d.s_size - d.s_minor - 2 > d.s_major)
640 1.23 mycroft d.s_major = d.s_size - d.s_minor - 2;
641 1.23 mycroft } else {
642 1.23 mycroft d.s_major = 0;
643 1.23 mycroft d.s_minor = 0;
644 1.23 mycroft }
645 1.7 mycroft }
646 1.7 mycroft
647 1.7 mycroft printfcn(&d);
648 1.7 mycroft output = 1;
649 1.7 mycroft
650 1.7 mycroft if (f_longform)
651 1.7 mycroft for (cur = list; cur; cur = cur->fts_link)
652 1.7 mycroft free(cur->fts_pointer);
653 1.1 cgd }
654 1.1 cgd
655 1.7 mycroft /*
656 1.7 mycroft * Ordering for mastercmp:
657 1.7 mycroft * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories
658 1.7 mycroft * as larger than directories. Within either group, use the sort function.
659 1.7 mycroft * All other levels use the sort function. Error entries remain unsorted.
660 1.7 mycroft */
661 1.7 mycroft static int
662 1.43 lukem mastercmp(const FTSENT **a, const FTSENT **b)
663 1.1 cgd {
664 1.12 mycroft int a_info, b_info;
665 1.1 cgd
666 1.7 mycroft a_info = (*a)->fts_info;
667 1.7 mycroft if (a_info == FTS_ERR)
668 1.7 mycroft return (0);
669 1.7 mycroft b_info = (*b)->fts_info;
670 1.7 mycroft if (b_info == FTS_ERR)
671 1.7 mycroft return (0);
672 1.7 mycroft
673 1.31 thorpej if (a_info == FTS_NS || b_info == FTS_NS) {
674 1.17 mycroft if (b_info != FTS_NS)
675 1.17 mycroft return (1);
676 1.17 mycroft else if (a_info != FTS_NS)
677 1.17 mycroft return (-1);
678 1.17 mycroft else
679 1.18 mycroft return (namecmp(*a, *b));
680 1.31 thorpej }
681 1.7 mycroft
682 1.24 lukem if (a_info != b_info && !f_listdir &&
683 1.24 lukem (*a)->fts_level == FTS_ROOTLEVEL) {
684 1.7 mycroft if (a_info == FTS_D)
685 1.7 mycroft return (1);
686 1.7 mycroft else if (b_info == FTS_D)
687 1.7 mycroft return (-1);
688 1.24 lukem }
689 1.24 lukem return (sortfcn(*a, *b));
690 1.1 cgd }
691