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