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