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