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