ls.c revision 1.14 1 1.14 cgd /* $NetBSD: ls.c,v 1.14 1995/03/21 09:06:30 cgd 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.1 cgd #ifndef lint
40 1.7 mycroft static char copyright[] =
41 1.12 mycroft "@(#) Copyright (c) 1989, 1993, 1994\n\
42 1.12 mycroft 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.14 cgd static char rcsid[] = "$NetBSD: ls.c,v 1.14 1995/03/21 09:06:30 cgd 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.12 mycroft #include <stdio.h>
62 1.7 mycroft #include <stdlib.h>
63 1.1 cgd #include <string.h>
64 1.12 mycroft #include <unistd.h>
65 1.12 mycroft
66 1.1 cgd #include "ls.h"
67 1.7 mycroft #include "extern.h"
68 1.1 cgd
69 1.7 mycroft char *group_from_gid __P((u_int, int));
70 1.7 mycroft char *user_from_uid __P((u_int, int));
71 1.1 cgd
72 1.7 mycroft static void display __P((FTSENT *, FTSENT *));
73 1.7 mycroft static int mastercmp __P((const FTSENT **, const FTSENT **));
74 1.7 mycroft static void traverse __P((int, char **, int));
75 1.7 mycroft
76 1.7 mycroft static void (*printfcn) __P((DISPLAY *));
77 1.7 mycroft static int (*sortfcn) __P((const FTSENT *, const FTSENT *));
78 1.7 mycroft
79 1.12 mycroft #define BY_NAME 0
80 1.12 mycroft #define BY_SIZE 1
81 1.8 mycroft #define BY_TIME 2
82 1.8 mycroft
83 1.7 mycroft long blocksize; /* block size units */
84 1.1 cgd int termwidth = 80; /* default terminal width */
85 1.8 mycroft int sortkey = BY_NAME;
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.7 mycroft int f_flags; /* show flags associated with a file */
91 1.1 cgd int f_inode; /* print inode */
92 1.1 cgd int f_listdir; /* list actual directory, not contents */
93 1.1 cgd int f_listdot; /* list files beginning with . */
94 1.1 cgd int f_longform; /* long listing format */
95 1.1 cgd int f_newline; /* if precede with newline */
96 1.1 cgd int f_nonprint; /* show unprintables as ? */
97 1.1 cgd int f_nosort; /* don't sort output */
98 1.1 cgd int f_recursive; /* ls subdirectories also */
99 1.1 cgd int f_reversesort; /* reverse whatever sort is used */
100 1.1 cgd int f_sectime; /* print the real time for all files */
101 1.1 cgd int f_singlecol; /* use single column output */
102 1.1 cgd int f_size; /* list size in short listing */
103 1.1 cgd int f_statustime; /* use time of last mode change */
104 1.1 cgd int f_dirname; /* if precede with directory name */
105 1.1 cgd int f_type; /* add type character for non-regular files */
106 1.13 mycroft int f_whiteout; /* show whiteout entries */
107 1.1 cgd
108 1.7 mycroft int
109 1.1 cgd main(argc, argv)
110 1.1 cgd int argc;
111 1.7 mycroft char *argv[];
112 1.1 cgd {
113 1.7 mycroft static char dot[] = ".", *dotav[] = { dot, NULL };
114 1.1 cgd struct winsize win;
115 1.7 mycroft int ch, fts_options, notused;
116 1.7 mycroft int kflag = 0;
117 1.7 mycroft char *p;
118 1.7 mycroft
119 1.7 mycroft /* Terminal defaults to -Cq, non-terminal defaults to -1. */
120 1.7 mycroft if (isatty(STDOUT_FILENO)) {
121 1.7 mycroft if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == -1 ||
122 1.7 mycroft !win.ws_col) {
123 1.12 mycroft if ((p = getenv("COLUMNS")) != NULL)
124 1.1 cgd termwidth = atoi(p);
125 1.1 cgd }
126 1.1 cgd else
127 1.1 cgd termwidth = win.ws_col;
128 1.7 mycroft f_column = f_nonprint = 1;
129 1.1 cgd } else
130 1.1 cgd f_singlecol = 1;
131 1.1 cgd
132 1.7 mycroft /* Root is -A automatically. */
133 1.1 cgd if (!getuid())
134 1.1 cgd f_listdot = 1;
135 1.1 cgd
136 1.7 mycroft fts_options = FTS_PHYSICAL;
137 1.13 mycroft while ((ch = getopt(argc, argv, "1ACFLRSTWacdfgikloqrstu")) != -1) {
138 1.1 cgd switch (ch) {
139 1.1 cgd /*
140 1.7 mycroft * The -1, -C and -l options all override each other so shell
141 1.7 mycroft * aliasing works right.
142 1.1 cgd */
143 1.1 cgd case '1':
144 1.1 cgd f_singlecol = 1;
145 1.1 cgd f_column = f_longform = 0;
146 1.1 cgd break;
147 1.1 cgd case 'C':
148 1.1 cgd f_column = 1;
149 1.1 cgd f_longform = f_singlecol = 0;
150 1.1 cgd break;
151 1.1 cgd case 'l':
152 1.1 cgd f_longform = 1;
153 1.1 cgd f_column = f_singlecol = 0;
154 1.1 cgd break;
155 1.7 mycroft /* The -c and -u options override each other. */
156 1.1 cgd case 'c':
157 1.1 cgd f_statustime = 1;
158 1.1 cgd f_accesstime = 0;
159 1.1 cgd break;
160 1.1 cgd case 'u':
161 1.1 cgd f_accesstime = 1;
162 1.1 cgd f_statustime = 0;
163 1.1 cgd break;
164 1.1 cgd case 'F':
165 1.1 cgd f_type = 1;
166 1.1 cgd break;
167 1.1 cgd case 'L':
168 1.7 mycroft fts_options &= ~FTS_PHYSICAL;
169 1.7 mycroft fts_options |= FTS_LOGICAL;
170 1.1 cgd break;
171 1.1 cgd case 'R':
172 1.1 cgd f_recursive = 1;
173 1.1 cgd break;
174 1.1 cgd case 'a':
175 1.7 mycroft fts_options |= FTS_SEEDOT;
176 1.1 cgd /* FALLTHROUGH */
177 1.1 cgd case 'A':
178 1.1 cgd f_listdot = 1;
179 1.1 cgd break;
180 1.7 mycroft /* The -d option turns off the -R option. */
181 1.1 cgd case 'd':
182 1.1 cgd f_listdir = 1;
183 1.7 mycroft f_recursive = 0;
184 1.1 cgd break;
185 1.1 cgd case 'f':
186 1.1 cgd f_nosort = 1;
187 1.1 cgd break;
188 1.7 mycroft case 'g': /* Compatibility with 4.3BSD. */
189 1.1 cgd break;
190 1.1 cgd case 'i':
191 1.1 cgd f_inode = 1;
192 1.1 cgd break;
193 1.1 cgd case 'k':
194 1.7 mycroft blocksize = 1024;
195 1.7 mycroft kflag = 1;
196 1.1 cgd break;
197 1.7 mycroft case 'o':
198 1.7 mycroft f_flags = 1;
199 1.7 mycroft break;
200 1.1 cgd case 'q':
201 1.1 cgd f_nonprint = 1;
202 1.1 cgd break;
203 1.1 cgd case 'r':
204 1.1 cgd f_reversesort = 1;
205 1.1 cgd break;
206 1.8 mycroft case 'S':
207 1.8 mycroft sortkey = BY_SIZE;
208 1.8 mycroft break;
209 1.1 cgd case 's':
210 1.1 cgd f_size = 1;
211 1.1 cgd break;
212 1.1 cgd case 'T':
213 1.1 cgd f_sectime = 1;
214 1.1 cgd break;
215 1.1 cgd case 't':
216 1.8 mycroft sortkey = BY_TIME;
217 1.1 cgd break;
218 1.13 mycroft case 'W':
219 1.13 mycroft f_whiteout = 1;
220 1.13 mycroft break;
221 1.1 cgd default:
222 1.1 cgd case '?':
223 1.1 cgd usage();
224 1.1 cgd }
225 1.1 cgd }
226 1.1 cgd argc -= optind;
227 1.1 cgd argv += optind;
228 1.1 cgd
229 1.7 mycroft /*
230 1.8 mycroft * If not -F, -i, -l, -S, -s or -t options, don't require stat
231 1.7 mycroft * information.
232 1.7 mycroft */
233 1.8 mycroft if (!f_inode && !f_longform && !f_size && !f_type &&
234 1.8 mycroft sortkey == BY_NAME)
235 1.7 mycroft fts_options |= FTS_NOSTAT;
236 1.7 mycroft
237 1.7 mycroft /*
238 1.7 mycroft * If not -F, -d or -l options, follow any symbolic links listed on
239 1.7 mycroft * the command line.
240 1.7 mycroft */
241 1.7 mycroft if (!f_longform && !f_listdir && !f_type)
242 1.7 mycroft fts_options |= FTS_COMFOLLOW;
243 1.13 mycroft
244 1.13 mycroft /*
245 1.13 mycroft * If -W, show whiteout entries.
246 1.13 mycroft */
247 1.13 mycroft #ifdef FTS_WHITEOUT
248 1.13 mycroft if (f_whiteout)
249 1.13 mycroft fts_options |= FTS_WHITEOUT;
250 1.13 mycroft #endif
251 1.1 cgd
252 1.7 mycroft /* If -l or -s, figure out block size. */
253 1.7 mycroft if (f_longform || f_size) {
254 1.9 cgd if (!kflag)
255 1.9 cgd (void)getbsize(¬used, &blocksize);
256 1.7 mycroft blocksize /= 512;
257 1.7 mycroft }
258 1.1 cgd
259 1.7 mycroft /* Select a sort function. */
260 1.1 cgd if (f_reversesort) {
261 1.8 mycroft switch (sortkey) {
262 1.8 mycroft case BY_NAME:
263 1.1 cgd sortfcn = revnamecmp;
264 1.8 mycroft break;
265 1.8 mycroft case BY_SIZE:
266 1.8 mycroft sortfcn = revsizecmp;
267 1.8 mycroft break;
268 1.8 mycroft case BY_TIME:
269 1.8 mycroft if (f_accesstime)
270 1.8 mycroft sortfcn = revacccmp;
271 1.8 mycroft else if (f_statustime)
272 1.8 mycroft sortfcn = revstatcmp;
273 1.12 mycroft else /* Use modification time. */
274 1.8 mycroft sortfcn = revmodcmp;
275 1.8 mycroft break;
276 1.8 mycroft }
277 1.1 cgd } else {
278 1.8 mycroft switch (sortkey) {
279 1.8 mycroft case BY_NAME:
280 1.1 cgd sortfcn = namecmp;
281 1.8 mycroft break;
282 1.8 mycroft case BY_SIZE:
283 1.8 mycroft sortfcn = sizecmp;
284 1.8 mycroft break;
285 1.8 mycroft case BY_TIME:
286 1.8 mycroft if (f_accesstime)
287 1.8 mycroft sortfcn = acccmp;
288 1.8 mycroft else if (f_statustime)
289 1.8 mycroft sortfcn = statcmp;
290 1.12 mycroft else /* Use modification time. */
291 1.8 mycroft sortfcn = modcmp;
292 1.8 mycroft break;
293 1.8 mycroft }
294 1.1 cgd }
295 1.1 cgd
296 1.7 mycroft /* Select a print function. */
297 1.1 cgd if (f_singlecol)
298 1.1 cgd printfcn = printscol;
299 1.1 cgd else if (f_longform)
300 1.1 cgd printfcn = printlong;
301 1.1 cgd else
302 1.1 cgd printfcn = printcol;
303 1.1 cgd
304 1.7 mycroft if (argc)
305 1.7 mycroft traverse(argc, argv, fts_options);
306 1.7 mycroft else
307 1.7 mycroft traverse(1, dotav, fts_options);
308 1.1 cgd exit(0);
309 1.1 cgd }
310 1.1 cgd
311 1.7 mycroft static int output; /* If anything output. */
312 1.1 cgd
313 1.7 mycroft /*
314 1.7 mycroft * Traverse() walks the logical directory structure specified by the argv list
315 1.7 mycroft * in the order specified by the mastercmp() comparison function. During the
316 1.7 mycroft * traversal it passes linked lists of structures to display() which represent
317 1.7 mycroft * a superset (may be exact set) of the files to be displayed.
318 1.7 mycroft */
319 1.7 mycroft static void
320 1.7 mycroft traverse(argc, argv, options)
321 1.7 mycroft int argc, options;
322 1.7 mycroft char *argv[];
323 1.1 cgd {
324 1.12 mycroft FTS *ftsp;
325 1.12 mycroft FTSENT *p, *chp;
326 1.7 mycroft int ch_options;
327 1.7 mycroft
328 1.7 mycroft if ((ftsp =
329 1.7 mycroft fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL)
330 1.9 cgd err(1, NULL);
331 1.7 mycroft
332 1.7 mycroft display(NULL, fts_children(ftsp, 0));
333 1.7 mycroft if (f_listdir)
334 1.7 mycroft return;
335 1.1 cgd
336 1.1 cgd /*
337 1.7 mycroft * If not recursing down this tree and don't need stat info, just get
338 1.7 mycroft * the names.
339 1.1 cgd */
340 1.7 mycroft ch_options = !f_recursive && options & FTS_NOSTAT ? FTS_NAMEONLY : 0;
341 1.1 cgd
342 1.12 mycroft while ((p = fts_read(ftsp)) != NULL)
343 1.12 mycroft switch (p->fts_info) {
344 1.7 mycroft case FTS_DC:
345 1.9 cgd warnx("%s: directory causes a cycle", p->fts_name);
346 1.7 mycroft break;
347 1.12 mycroft case FTS_DNR:
348 1.12 mycroft case FTS_ERR:
349 1.12 mycroft warnx("%s: %s", p->fts_name, strerror(p->fts_errno));
350 1.12 mycroft break;
351 1.7 mycroft case FTS_D:
352 1.7 mycroft if (p->fts_level != FTS_ROOTLEVEL &&
353 1.7 mycroft p->fts_name[0] == '.' && !f_listdot)
354 1.7 mycroft break;
355 1.1 cgd
356 1.7 mycroft /*
357 1.7 mycroft * If already output something, put out a newline as
358 1.7 mycroft * a separator. If multiple arguments, precede each
359 1.7 mycroft * directory with its name.
360 1.7 mycroft */
361 1.7 mycroft if (output)
362 1.7 mycroft (void)printf("\n%s:\n", p->fts_path);
363 1.7 mycroft else if (argc > 1) {
364 1.7 mycroft (void)printf("%s:\n", p->fts_path);
365 1.7 mycroft output = 1;
366 1.7 mycroft }
367 1.1 cgd
368 1.12 mycroft chp = fts_children(ftsp, ch_options);
369 1.12 mycroft display(p, chp);
370 1.1 cgd
371 1.12 mycroft if (!f_recursive && chp != NULL)
372 1.7 mycroft (void)fts_set(ftsp, p, FTS_SKIP);
373 1.7 mycroft break;
374 1.1 cgd }
375 1.12 mycroft if (errno)
376 1.12 mycroft err(1, "fts_read");
377 1.1 cgd }
378 1.1 cgd
379 1.7 mycroft /*
380 1.7 mycroft * Display() takes a linked list of FTSENT structures and passes the list
381 1.7 mycroft * along with any other necessary information to the print function. P
382 1.7 mycroft * points to the parent directory of the display list.
383 1.7 mycroft */
384 1.7 mycroft static void
385 1.7 mycroft display(p, list)
386 1.12 mycroft FTSENT *p, *list;
387 1.1 cgd {
388 1.7 mycroft struct stat *sp;
389 1.7 mycroft DISPLAY d;
390 1.12 mycroft FTSENT *cur;
391 1.7 mycroft NAMES *np;
392 1.12 mycroft u_quad_t maxsize;
393 1.7 mycroft u_long btotal, maxblock, maxinode, maxlen, maxnlink;
394 1.7 mycroft int bcfile, flen, glen, ulen, maxflags, maxgroup, maxuser;
395 1.7 mycroft int entries, needstats;
396 1.7 mycroft char *user, *group, *flags, buf[20]; /* 32 bits == 10 digits */
397 1.1 cgd
398 1.7 mycroft /*
399 1.7 mycroft * If list is NULL there are two possibilities: that the parent
400 1.7 mycroft * directory p has no children, or that fts_children() returned an
401 1.12 mycroft * error. We ignore the error case since it will be replicated
402 1.12 mycroft * on the next call to fts_read() on the post-order visit to the
403 1.12 mycroft * directory p, and will be signalled in traverse().
404 1.7 mycroft */
405 1.12 mycroft if (list == NULL)
406 1.7 mycroft return;
407 1.1 cgd
408 1.7 mycroft needstats = f_inode || f_longform || f_size;
409 1.7 mycroft flen = 0;
410 1.7 mycroft btotal = maxblock = maxinode = maxlen = maxnlink = 0;
411 1.7 mycroft bcfile = 0;
412 1.7 mycroft maxuser = maxgroup = maxflags = 0;
413 1.7 mycroft maxsize = 0;
414 1.7 mycroft for (cur = list, entries = 0; cur; cur = cur->fts_link) {
415 1.7 mycroft if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) {
416 1.12 mycroft warnx("%s: %s",
417 1.12 mycroft cur->fts_name, strerror(cur->fts_errno));
418 1.7 mycroft cur->fts_number = NO_PRINT;
419 1.7 mycroft continue;
420 1.7 mycroft }
421 1.1 cgd
422 1.7 mycroft /*
423 1.7 mycroft * P is NULL if list is the argv list, to which different rules
424 1.7 mycroft * apply.
425 1.7 mycroft */
426 1.7 mycroft if (p == NULL) {
427 1.7 mycroft /* Directories will be displayed later. */
428 1.7 mycroft if (cur->fts_info == FTS_D && !f_listdir) {
429 1.7 mycroft cur->fts_number = NO_PRINT;
430 1.1 cgd continue;
431 1.7 mycroft }
432 1.7 mycroft } else {
433 1.7 mycroft /* Only display dot file if -a/-A set. */
434 1.7 mycroft if (cur->fts_name[0] == '.' && !f_listdot) {
435 1.7 mycroft cur->fts_number = NO_PRINT;
436 1.1 cgd continue;
437 1.7 mycroft }
438 1.7 mycroft }
439 1.7 mycroft if (f_nonprint)
440 1.7 mycroft prcopy(cur->fts_name, cur->fts_name, cur->fts_namelen);
441 1.7 mycroft if (cur->fts_namelen > maxlen)
442 1.7 mycroft maxlen = cur->fts_namelen;
443 1.7 mycroft if (needstats) {
444 1.7 mycroft sp = cur->fts_statp;
445 1.7 mycroft if (sp->st_blocks > maxblock)
446 1.7 mycroft maxblock = sp->st_blocks;
447 1.7 mycroft if (sp->st_ino > maxinode)
448 1.7 mycroft maxinode = sp->st_ino;
449 1.7 mycroft if (sp->st_nlink > maxnlink)
450 1.7 mycroft maxnlink = sp->st_nlink;
451 1.7 mycroft if (sp->st_size > maxsize)
452 1.7 mycroft maxsize = sp->st_size;
453 1.7 mycroft
454 1.7 mycroft btotal += sp->st_blocks;
455 1.7 mycroft if (f_longform) {
456 1.7 mycroft user = user_from_uid(sp->st_uid, 0);
457 1.7 mycroft if ((ulen = strlen(user)) > maxuser)
458 1.7 mycroft maxuser = ulen;
459 1.7 mycroft group = group_from_gid(sp->st_gid, 0);
460 1.7 mycroft if ((glen = strlen(group)) > maxgroup)
461 1.7 mycroft maxgroup = glen;
462 1.7 mycroft if (f_flags) {
463 1.12 mycroft flags =
464 1.12 mycroft flags_to_string(sp->st_flags, "-");
465 1.7 mycroft if ((flen = strlen(flags)) > maxflags)
466 1.7 mycroft maxflags = flen;
467 1.7 mycroft } else
468 1.7 mycroft flen = 0;
469 1.7 mycroft
470 1.7 mycroft if ((np = malloc(sizeof(NAMES) +
471 1.7 mycroft ulen + glen + flen + 3)) == NULL)
472 1.9 cgd err(1, NULL);
473 1.7 mycroft
474 1.7 mycroft np->user = &np->data[0];
475 1.7 mycroft (void)strcpy(np->user, user);
476 1.7 mycroft np->group = &np->data[ulen + 1];
477 1.7 mycroft (void)strcpy(np->group, group);
478 1.7 mycroft
479 1.7 mycroft if (S_ISCHR(sp->st_mode) ||
480 1.7 mycroft S_ISBLK(sp->st_mode))
481 1.7 mycroft bcfile = 1;
482 1.7 mycroft
483 1.7 mycroft if (f_flags) {
484 1.7 mycroft np->flags = &np->data[ulen + glen + 2];
485 1.7 mycroft (void)strcpy(np->flags, flags);
486 1.7 mycroft }
487 1.7 mycroft cur->fts_pointer = np;
488 1.7 mycroft }
489 1.1 cgd }
490 1.7 mycroft ++entries;
491 1.1 cgd }
492 1.1 cgd
493 1.7 mycroft if (!entries)
494 1.1 cgd return;
495 1.7 mycroft
496 1.7 mycroft d.list = list;
497 1.7 mycroft d.entries = entries;
498 1.7 mycroft d.maxlen = maxlen;
499 1.7 mycroft if (needstats) {
500 1.7 mycroft d.bcfile = bcfile;
501 1.7 mycroft d.btotal = btotal;
502 1.7 mycroft (void)snprintf(buf, sizeof(buf), "%lu", maxblock);
503 1.7 mycroft d.s_block = strlen(buf);
504 1.7 mycroft d.s_flags = maxflags;
505 1.7 mycroft d.s_group = maxgroup;
506 1.7 mycroft (void)snprintf(buf, sizeof(buf), "%lu", maxinode);
507 1.7 mycroft d.s_inode = strlen(buf);
508 1.7 mycroft (void)snprintf(buf, sizeof(buf), "%lu", maxnlink);
509 1.7 mycroft d.s_nlink = strlen(buf);
510 1.7 mycroft (void)snprintf(buf, sizeof(buf), "%qu", maxsize);
511 1.7 mycroft d.s_size = strlen(buf);
512 1.7 mycroft d.s_user = maxuser;
513 1.7 mycroft }
514 1.7 mycroft
515 1.7 mycroft printfcn(&d);
516 1.7 mycroft output = 1;
517 1.7 mycroft
518 1.7 mycroft if (f_longform)
519 1.7 mycroft for (cur = list; cur; cur = cur->fts_link)
520 1.7 mycroft free(cur->fts_pointer);
521 1.1 cgd }
522 1.1 cgd
523 1.7 mycroft /*
524 1.7 mycroft * Ordering for mastercmp:
525 1.7 mycroft * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories
526 1.7 mycroft * as larger than directories. Within either group, use the sort function.
527 1.7 mycroft * All other levels use the sort function. Error entries remain unsorted.
528 1.7 mycroft */
529 1.7 mycroft static int
530 1.7 mycroft mastercmp(a, b)
531 1.7 mycroft const FTSENT **a, **b;
532 1.1 cgd {
533 1.12 mycroft int a_info, b_info;
534 1.1 cgd
535 1.7 mycroft a_info = (*a)->fts_info;
536 1.7 mycroft if (a_info == FTS_ERR)
537 1.7 mycroft return (0);
538 1.7 mycroft b_info = (*b)->fts_info;
539 1.7 mycroft if (b_info == FTS_ERR)
540 1.7 mycroft return (0);
541 1.7 mycroft
542 1.7 mycroft if (a_info == FTS_NS || b_info == FTS_NS)
543 1.7 mycroft return (namecmp(*a, *b));
544 1.7 mycroft
545 1.7 mycroft if (a_info == b_info)
546 1.7 mycroft return (sortfcn(*a, *b));
547 1.7 mycroft
548 1.7 mycroft if ((*a)->fts_level == FTS_ROOTLEVEL)
549 1.7 mycroft if (a_info == FTS_D)
550 1.7 mycroft return (1);
551 1.7 mycroft else if (b_info == FTS_D)
552 1.7 mycroft return (-1);
553 1.1 cgd else
554 1.7 mycroft return (sortfcn(*a, *b));
555 1.7 mycroft else
556 1.7 mycroft return (sortfcn(*a, *b));
557 1.1 cgd }
558