]> git.saurik.com Git - apple/network_cmds.git/blame - ftpd.tproj/ls.c
network_cmds-85.tar.gz
[apple/network_cmds.git] / ftpd.tproj / ls.c
CommitLineData
3e383549
A
1/*
2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Michael Fischbein.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38static const char copyright[] =
39"@(#) Copyright (c) 1989, 1993, 1994\n\
40 The Regents of the University of California. All rights reserved.\n";
41#endif /* not lint */
42
43#ifndef lint
44#if 0
45static char sccsid[] = "@(#)ls.c 8.5 (Berkeley) 4/2/94";
46#else
47static const char rcsid[] =
48 "$FreeBSD: src/bin/ls/ls.c,v 1.45 2000/08/13 12:17:03 joe Exp $";
49#endif
50#endif /* not lint */
51
52#include <sys/types.h>
53#include <sys/stat.h>
54#include <sys/ioctl.h>
55
56#include <dirent.h>
57#include <err.h>
58#include <errno.h>
59#include <fts.h>
60#include <limits.h>
61#include <locale.h>
62#include <stdio.h>
63#include <stdlib.h>
64#include <string.h>
65#include <unistd.h>
66#ifdef COLORLS
67#include <termcap.h>
68#include <signal.h>
69#endif
70
71#include "ls.h"
72#include "ls_extern.h"
73
74#if !defined(HAVE_FFLAGSTOSTR)
75static struct {
76 char *name;
77 u_long flag;
78 int invert;
79} mapping[] = {
80 /* shorter names per flag first, all prefixed by "no" */
81 { "nosappnd", SF_APPEND, 0 },
82 { "nosappend", SF_APPEND, 0 },
83 { "noarch", SF_ARCHIVED, 0 },
84 { "noarchived", SF_ARCHIVED, 0 },
85 { "noschg", SF_IMMUTABLE, 0 },
86 { "noschange", SF_IMMUTABLE, 0 },
87 { "nosimmutable", SF_IMMUTABLE, 0 },
88#if defined(SF_NOUNLINK)
89 { "nosunlnk", SF_NOUNLINK, 0 },
90 { "nosunlink", SF_NOUNLINK, 0 },
91#endif
92 { "nouappnd", UF_APPEND, 0 },
93 { "nouappend", UF_APPEND, 0 },
94 { "nouchg", UF_IMMUTABLE, 0 },
95 { "nouchange", UF_IMMUTABLE, 0 },
96 { "nouimmutable", UF_IMMUTABLE, 0 },
97 { "nodump", UF_NODUMP, 1 },
98 { "noopaque", UF_OPAQUE, 0 }
99#if defined(UF_NOUNLINK)
100,
101 { "nouunlnk", UF_NOUNLINK, 0 },
102 { "nouunlink", UF_NOUNLINK, 0 }
103#endif
104};
105#define longestflaglen 12
106#define nmappings (sizeof(mapping) / sizeof(mapping[0]))
107
108/*
109 * fflagstostr --
110 * Convert file flags to a comma-separated string. If no flags
111 * are set, return the empty string.
112 */
113char *
114fflagstostr(flags)
115 u_long flags;
116{
117 char *string;
118 char *sp, *dp;
119 u_long setflags;
120 int i;
121
122 if ((string = (char *)malloc(nmappings * (longestflaglen + 1))) == NULL)
123 return (NULL);
124
125 setflags = flags;
126 dp = string;
127 for (i = 0; i < nmappings; i++) {
128 if (setflags & mapping[i].flag) {
129 if (dp > string)
130 *dp++ = ',';
131 for (sp = mapping[i].invert ? mapping[i].name :
132 mapping[i].name + 2; *sp; *dp++ = *sp++) ;
133 setflags &= ~mapping[i].flag;
134 }
135 }
136 *dp = '\0';
137 return (string);
138}
139#endif
140
141/*
142 * Upward approximation of the maximum number of characters needed to
143 * represent a value of integral type t as a string, excluding the
144 * NUL terminator, with provision for a sign.
145 */
146#define STRBUF_SIZEOF(t) (1 + CHAR_BIT * sizeof(t) / 3 + 1)
147
148static void display __P((FTSENT *, FTSENT *));
149static u_quad_t makenines __P((u_long));
150static int mastercmp __P((const FTSENT **, const FTSENT **));
151static void traverse __P((int, char **, int));
152
153static void (*printfcn) __P((DISPLAY *));
154static int (*sortfcn) __P((const FTSENT *, const FTSENT *));
155
156long blocksize; /* block size units */
157int termwidth = 80; /* default terminal width */
158
159/* flags */
160int f_accesstime; /* use time of last access */
161int f_column; /* columnated format */
162int f_flags; /* show flags associated with a file */
163int f_inode; /* print inode */
164int f_kblocks; /* print size in kilobytes */
165int f_listdir; /* list actual directory, not contents */
166int f_listdot; /* list files beginning with . */
167int f_longform; /* long listing format */
168int f_nonprint; /* show unprintables as ? */
169int f_nosort; /* don't sort output */
170int f_notabs; /* don't use tab-separated multi-col output */
171int f_numericonly; /* don't convert uid/gid to name */
172int f_octal; /* show unprintables as \xxx */
173int f_octal_escape; /* like f_octal but use C escapes if possible */
174int f_recursive; /* ls subdirectories also */
175int f_reversesort; /* reverse whatever sort is used */
176int f_sectime; /* print the real time for all files */
177int f_singlecol; /* use single column output */
178int f_size; /* list size in short listing */
179int f_statustime; /* use time of last mode change */
180int f_timesort; /* sort by time vice name */
181int f_type; /* add type character for non-regular files */
182int f_whiteout; /* show whiteout entries */
183#ifdef COLORLS
184int f_color; /* add type in color for non-regular files */
185
186char *ansi_bgcol; /* ANSI sequence to set background colour */
187char *ansi_fgcol; /* ANSI sequence to set foreground colour */
188char *ansi_coloff; /* ANSI sequence to reset colours */
189#endif
190
191int rval;
192
193int
194main(argc, argv)
195 int argc;
196 char *argv[];
197{
198 static char dot[] = ".", *dotav[] = { dot, NULL };
199 struct winsize win;
200 int ch, fts_options, notused;
201 char *p;
202
203#ifdef COLORLS
204 char termcapbuf[1024]; /* termcap definition buffer */
205 char tcapbuf[512]; /* capability buffer */
206 char *bp = tcapbuf;
207#endif
208
209 (void) setlocale(LC_ALL, "");
210
211 /* Terminal defaults to -Cq, non-terminal defaults to -1. */
212 if (isatty(STDOUT_FILENO)) {
213 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == -1 ||
214 !win.ws_col) {
215 if ((p = getenv("COLUMNS")) != NULL)
216 termwidth = atoi(p);
217 }
218 else
219 termwidth = win.ws_col;
220 f_column = f_nonprint = 1;
221 } else {
222 f_singlecol = 1;
223 /* retrieve environment variable, in case of explicit -C */
224 if ((p = getenv("COLUMNS")))
225 termwidth = atoi(p);
226 }
227
228 /* Root is -A automatically. */
229 if (!getuid())
230 f_listdot = 1;
231
232 fts_options = FTS_PHYSICAL;
233 while ((ch = getopt(argc, argv, "1ABCFGHLPRTWabcdfgiklnoqrstu")) != -1) {
234 switch (ch) {
235 /*
236 * The -1, -C and -l options all override each other so shell
237 * aliasing works right.
238 */
239 case '1':
240 f_singlecol = 1;
241 f_column = f_longform = 0;
242 break;
243 case 'B':
244 f_nonprint = 0;
245 f_octal = 1;
246 f_octal_escape = 0;
247 break;
248 case 'C':
249 f_column = 1;
250 f_longform = f_singlecol = 0;
251 break;
252 case 'l':
253 f_longform = 1;
254 f_column = f_singlecol = 0;
255 break;
256 /* The -c and -u options override each other. */
257 case 'c':
258 f_statustime = 1;
259 f_accesstime = 0;
260 break;
261 case 'u':
262 f_accesstime = 1;
263 f_statustime = 0;
264 break;
265 case 'F':
266 f_type = 1;
267 break;
268 case 'H':
269 fts_options |= FTS_COMFOLLOW;
270 break;
271 case 'G':
272 setenv("CLICOLOR", "", 1);
273 break;
274 case 'L':
275 fts_options &= ~FTS_PHYSICAL;
276 fts_options |= FTS_LOGICAL;
277 break;
278 case 'P':
279 fts_options &= ~FTS_COMFOLLOW;
280 fts_options &= ~FTS_LOGICAL;
281 fts_options |= FTS_PHYSICAL;
282 break;
283 case 'R':
284 f_recursive = 1;
285 break;
286 case 'a':
287 fts_options |= FTS_SEEDOT;
288 /* FALLTHROUGH */
289 case 'A':
290 f_listdot = 1;
291 break;
292 /* The -d option turns off the -R option. */
293 case 'd':
294 f_listdir = 1;
295 f_recursive = 0;
296 break;
297 case 'f':
298 f_nosort = 1;
299 break;
300 case 'g': /* Compatibility with 4.3BSD. */
301 break;
302 case 'i':
303 f_inode = 1;
304 break;
305 case 'k':
306 f_kblocks = 1;
307 break;
308 case 'n':
309 f_numericonly = 1;
310 break;
311 case 'o':
312 f_flags = 1;
313 break;
314 case 'q':
315 f_nonprint = 1;
316 f_octal = 0;
317 f_octal_escape = 0;
318 break;
319 case 'r':
320 f_reversesort = 1;
321 break;
322 case 's':
323 f_size = 1;
324 break;
325 case 'T':
326 f_sectime = 1;
327 break;
328 case 't':
329 f_timesort = 1;
330 break;
331 case 'W':
332 f_whiteout = 1;
333 break;
334 case 'b':
335 f_nonprint = 0;
336 f_octal = 0;
337 f_octal_escape = 1;
338 break;
339 default:
340 case '?':
341 usage();
342 }
343 }
344 argc -= optind;
345 argv += optind;
346
347 /* Enabling of colours is conditional on the environment. */
348 if (getenv("CLICOLOR") &&
349 (isatty(STDOUT_FILENO) || getenv("CLICOLOR_FORCE")))
350#ifdef COLORLS
351 if (tgetent(termcapbuf, getenv("TERM")) == 1) {
352 ansi_fgcol = tgetstr("AF", &bp);
353 ansi_bgcol = tgetstr("AB", &bp);
354
355 /* To switch colours off use 'op' if
356 * available, otherwise use 'oc', or
357 * don't do colours at all. */
358 ansi_coloff = tgetstr("op", &bp);
359 if (!ansi_coloff)
360 ansi_coloff = tgetstr("oc", &bp);
361 if (ansi_fgcol && ansi_bgcol && ansi_coloff)
362 f_color = 1;
363 }
364#else
365 (void)fprintf(stderr, "Color support not compiled in.\n");
366#endif /*COLORLS*/
367
368#ifdef COLORLS
369 if (f_color) {
370 /*
371 * We can't put tabs and color sequences together:
372 * column number will be incremented incorrectly
373 * for "stty oxtabs" mode.
374 */
375 f_notabs = 1;
376 (void) signal(SIGINT, colorquit);
377 (void) signal(SIGQUIT, colorquit);
378 parsecolors(getenv("LSCOLORS"));
379 }
380#endif
381
382 /*
383 * If not -F, -i, -l, -s or -t options, don't require stat
384 * information, unless in color mode in which case we do
385 * need this to determine which colors to display.
386 */
387 if (!f_inode && !f_longform && !f_size && !f_timesort && !f_type
388#ifdef COLORLS
389 && !f_color
390#endif
391 )
392 fts_options |= FTS_NOSTAT;
393
394 /*
395 * If not -F, -d or -l options, follow any symbolic links listed on
396 * the command line.
397 */
398 if (!f_longform && !f_listdir && !f_type)
399 fts_options |= FTS_COMFOLLOW;
400
401 /*
402 * If -W, show whiteout entries
403 */
404#ifdef FTS_WHITEOUT
405 if (f_whiteout)
406 fts_options |= FTS_WHITEOUT;
407#endif
408
409 /* If -l or -s, figure out block size. */
410 if (f_longform || f_size) {
411 if (f_kblocks)
412 blocksize = 2;
413 else {
414 (void)getbsize(&notused, &blocksize);
415 blocksize /= 512;
416 }
417 }
418
419 /* Select a sort function. */
420 if (f_reversesort) {
421 if (!f_timesort)
422 sortfcn = revnamecmp;
423 else if (f_accesstime)
424 sortfcn = revacccmp;
425 else if (f_statustime)
426 sortfcn = revstatcmp;
427 else /* Use modification time. */
428 sortfcn = revmodcmp;
429 } else {
430 if (!f_timesort)
431 sortfcn = namecmp;
432 else if (f_accesstime)
433 sortfcn = acccmp;
434 else if (f_statustime)
435 sortfcn = statcmp;
436 else /* Use modification time. */
437 sortfcn = modcmp;
438 }
439
440 /* Select a print function. */
441 if (f_singlecol)
442 printfcn = printscol;
443 else if (f_longform)
444 printfcn = printlong;
445 else
446 printfcn = printcol;
447
448 if (argc)
449 traverse(argc, argv, fts_options);
450 else
451 traverse(1, dotav, fts_options);
452 exit(rval);
453}
454
455static int output; /* If anything output. */
456
457/*
458 * Traverse() walks the logical directory structure specified by the argv list
459 * in the order specified by the mastercmp() comparison function. During the
460 * traversal it passes linked lists of structures to display() which represent
461 * a superset (may be exact set) of the files to be displayed.
462 */
463static void
464traverse(argc, argv, options)
465 int argc, options;
466 char *argv[];
467{
468 FTS *ftsp;
469 FTSENT *p, *chp;
470 int ch_options;
471
472 if ((ftsp =
473 fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL)
474 err(1, NULL);
475
476 display(NULL, fts_children(ftsp, 0));
477 if (f_listdir)
478 return;
479
480 /*
481 * If not recursing down this tree and don't need stat info, just get
482 * the names.
483 */
484 ch_options = !f_recursive && options & FTS_NOSTAT ? FTS_NAMEONLY : 0;
485
486 while ((p = fts_read(ftsp)) != NULL)
487 switch (p->fts_info) {
488 case FTS_DC:
489 warnx("%s: directory causes a cycle", p->fts_name);
490 break;
491 case FTS_DNR:
492 case FTS_ERR:
493 warnx("%s: %s", p->fts_name, strerror(p->fts_errno));
494 rval = 1;
495 break;
496 case FTS_D:
497 if (p->fts_level != FTS_ROOTLEVEL &&
498 p->fts_name[0] == '.' && !f_listdot)
499 break;
500
501 /*
502 * If already output something, put out a newline as
503 * a separator. If multiple arguments, precede each
504 * directory with its name.
505 */
506 if (output)
507 (void)printf("\n%s:\n", p->fts_path);
508 else if (argc > 1) {
509 (void)printf("%s:\n", p->fts_path);
510 output = 1;
511 }
512
513 chp = fts_children(ftsp, ch_options);
514 display(p, chp);
515
516 if (!f_recursive && chp != NULL)
517 (void)fts_set(ftsp, p, FTS_SKIP);
518 break;
519 }
520 if (errno)
521 err(1, "fts_read");
522}
523
524/*
525 * Display() takes a linked list of FTSENT structures and passes the list
526 * along with any other necessary information to the print function. P
527 * points to the parent directory of the display list.
528 */
529static void
530display(p, list)
531 FTSENT *p, *list;
532{
533 struct stat *sp;
534 DISPLAY d;
535 FTSENT *cur;
536 NAMES *np;
537 u_quad_t maxsize;
538 u_long btotal, maxblock, maxinode, maxlen, maxnlink;
539 int bcfile, flen, glen, ulen, maxflags, maxgroup, maxuser;
540 char *initmax;
541 int entries, needstats;
542 char *user, *group, *flags;
543 char buf[STRBUF_SIZEOF(u_quad_t) + 1];
544 char ngroup[STRBUF_SIZEOF(uid_t) + 1];
545 char nuser[STRBUF_SIZEOF(gid_t) + 1];
546
547 /*
548 * If list is NULL there are two possibilities: that the parent
549 * directory p has no children, or that fts_children() returned an
550 * error. We ignore the error case since it will be replicated
551 * on the next call to fts_read() on the post-order visit to the
552 * directory p, and will be signaled in traverse().
553 */
554 if (list == NULL)
555 return;
556
557 needstats = f_inode || f_longform || f_size;
558 flen = 0;
559 btotal = 0;
560 initmax = getenv("LS_COLWIDTHS");
561 /* Fields match -lios order. New ones should be added at the end. */
562 if (initmax != NULL && *initmax != '\0') {
563 char *initmax2, *jinitmax;
564 int ninitmax;
565
566 /* Fill-in "::" as "0:0:0" for the sake of scanf. */
567 jinitmax = initmax2 = malloc(strlen(initmax) * 2 + 2);
568 if (jinitmax == NULL)
569 err(1, NULL);
570 if (*initmax == ':')
571 strcpy(initmax2, "0:"), initmax2 += 2;
572 else
573 *initmax2++ = *initmax, *initmax2 = '\0';
574 for (initmax++; *initmax != '\0'; initmax++) {
575 if (initmax[-1] == ':' && initmax[0] == ':') {
576 *initmax2++ = '0';
577 *initmax2++ = initmax[0];
578 initmax2[1] = '\0';
579 } else {
580 *initmax2++ = initmax[0];
581 initmax2[1] = '\0';
582 }
583 }
584 if (initmax2[-1] == ':') strcpy(initmax2, "0");
585
586 ninitmax = sscanf(jinitmax,
587 " %lu : %lu : %lu : %i : %i : %i : %qu : %lu ",
588 &maxinode, &maxblock, &maxnlink, &maxuser,
589 &maxgroup, &maxflags, &maxsize, &maxlen);
590 f_notabs = 1;
591 switch (ninitmax) {
592 case 0: maxinode = 0;
593 case 1: maxblock = 0;
594 case 2: maxnlink = 0;
595 case 3: maxuser = 0;
596 case 4: maxgroup = 0;
597 case 5: maxflags = 0;
598 case 6: maxsize = 0;
599 case 7: maxlen = 0;
600#ifdef COLORLS
601 if (!f_color)
602#endif
603 f_notabs = 0;
604 }
605 maxinode = makenines(maxinode);
606 maxblock = makenines(maxblock);
607 maxnlink = makenines(maxnlink);
608 maxsize = makenines(maxsize);
609 } else if (initmax == NULL || *initmax == '\0')
610 maxblock = maxinode = maxlen = maxnlink =
611 maxuser = maxgroup = maxflags = maxsize = 0;
612 bcfile = 0;
613 flags = NULL;
614 for (cur = list, entries = 0; cur; cur = cur->fts_link) {
615 if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) {
616 warnx("%s: %s",
617 cur->fts_name, strerror(cur->fts_errno));
618 cur->fts_number = NO_PRINT;
619 rval = 1;
620 continue;
621 }
622
623 /*
624 * P is NULL if list is the argv list, to which different rules
625 * apply.
626 */
627 if (p == NULL) {
628 /* Directories will be displayed later. */
629 if (cur->fts_info == FTS_D && !f_listdir) {
630 cur->fts_number = NO_PRINT;
631 continue;
632 }
633 } else {
634 /* Only display dot file if -a/-A set. */
635 if (cur->fts_name[0] == '.' && !f_listdot) {
636 cur->fts_number = NO_PRINT;
637 continue;
638 }
639 }
640 if (cur->fts_namelen > maxlen)
641 maxlen = cur->fts_namelen;
642 if (f_octal || f_octal_escape) {
643 int t = len_octal(cur->fts_name, cur->fts_namelen);
644 if (t > maxlen) maxlen = t;
645 }
646 if (needstats) {
647 sp = cur->fts_statp;
648 if (sp->st_blocks > maxblock)
649 maxblock = sp->st_blocks;
650 if (sp->st_ino > maxinode)
651 maxinode = sp->st_ino;
652 if (sp->st_nlink > maxnlink)
653 maxnlink = sp->st_nlink;
654 if (sp->st_size > maxsize)
655 maxsize = sp->st_size;
656
657 btotal += sp->st_blocks;
658 if (f_longform) {
659 if (f_numericonly) {
660 (void)snprintf(nuser, sizeof(nuser),
661 "%u", sp->st_uid);
662 (void)snprintf(ngroup, sizeof(ngroup),
663 "%u", sp->st_gid);
664 user = nuser;
665 group = ngroup;
666 } else {
667 user = user_from_uid(sp->st_uid, 0);
668 group = group_from_gid(sp->st_gid, 0);
669 }
670 if ((ulen = strlen(user)) > maxuser)
671 maxuser = ulen;
672 if ((glen = strlen(group)) > maxgroup)
673 maxgroup = glen;
674 if (f_flags) {
675 flags = fflagstostr(sp->st_flags);
676 if (flags != NULL && *flags == '\0') {
677 free(flags);
678 flags = strdup("-");
679 }
680 if (flags == NULL)
681 err(1, NULL);
682 if ((flen = strlen(flags)) > maxflags)
683 maxflags = flen;
684 } else
685 flen = 0;
686
687 if ((np = malloc(sizeof(NAMES) +
688 ulen + glen + flen + 3)) == NULL)
689 err(1, NULL);
690
691 np->user = &np->data[0];
692 (void)strcpy(np->user, user);
693 np->group = &np->data[ulen + 1];
694 (void)strcpy(np->group, group);
695
696 if (S_ISCHR(sp->st_mode) ||
697 S_ISBLK(sp->st_mode))
698 bcfile = 1;
699
700 if (f_flags) {
701 np->flags = &np->data[ulen + glen + 2];
702 (void)strcpy(np->flags, flags);
703 free(flags);
704 }
705 cur->fts_pointer = np;
706 }
707 }
708 ++entries;
709 }
710
711 if (!entries)
712 return;
713
714 d.list = list;
715 d.entries = entries;
716 d.maxlen = maxlen;
717 if (needstats) {
718 d.bcfile = bcfile;
719 d.btotal = btotal;
720 (void)snprintf(buf, sizeof(buf), "%lu", maxblock);
721 d.s_block = strlen(buf);
722 d.s_flags = maxflags;
723 d.s_group = maxgroup;
724 (void)snprintf(buf, sizeof(buf), "%lu", maxinode);
725 d.s_inode = strlen(buf);
726 (void)snprintf(buf, sizeof(buf), "%lu", maxnlink);
727 d.s_nlink = strlen(buf);
728 (void)snprintf(buf, sizeof(buf), "%qu", maxsize);
729 d.s_size = strlen(buf);
730 d.s_user = maxuser;
731 }
732
733 printfcn(&d);
734 output = 1;
735
736 if (f_longform)
737 for (cur = list; cur; cur = cur->fts_link)
738 free(cur->fts_pointer);
739}
740
741/*
742 * Ordering for mastercmp:
743 * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories
744 * as larger than directories. Within either group, use the sort function.
745 * All other levels use the sort function. Error entries remain unsorted.
746 */
747static int
748mastercmp(a, b)
749 const FTSENT **a, **b;
750{
751 int a_info, b_info;
752
753 a_info = (*a)->fts_info;
754 if (a_info == FTS_ERR)
755 return (0);
756 b_info = (*b)->fts_info;
757 if (b_info == FTS_ERR)
758 return (0);
759
760 if (a_info == FTS_NS || b_info == FTS_NS)
761 return (namecmp(*a, *b));
762
763 if (a_info != b_info &&
764 (*a)->fts_level == FTS_ROOTLEVEL && !f_listdir) {
765 if (a_info == FTS_D)
766 return (1);
767 if (b_info == FTS_D)
768 return (-1);
769 }
770 return (sortfcn(*a, *b));
771}
772
773/*
774 * Makenines() returns (10**n)-1. This is useful for converting a width
775 * into a number that wide in decimal.
776 */
777static u_quad_t
778makenines(n)
779 u_long n;
780{
781 u_long i;
782 u_quad_t reg;
783
784 reg = 1;
785 /* Use a loop instead of pow(), since all values of n are small. */
786 for (i = 0; i < n; i++)
787 reg *= 10;
788 reg--;
789
790 return reg;
791}