1*e8ccbb3bSSevan Janiyan /* $NetBSD: ls.c,v 1.76 2017/02/06 21:06:04 rin Exp $ */
2b7ef8cfbSLionel Sambuc
3b7ef8cfbSLionel Sambuc /*
4b7ef8cfbSLionel Sambuc * Copyright (c) 1989, 1993, 1994
5b7ef8cfbSLionel Sambuc * The Regents of the University of California. All rights reserved.
6b7ef8cfbSLionel Sambuc *
7b7ef8cfbSLionel Sambuc * This code is derived from software contributed to Berkeley by
8b7ef8cfbSLionel Sambuc * Michael Fischbein.
9b7ef8cfbSLionel Sambuc *
10b7ef8cfbSLionel Sambuc * Redistribution and use in source and binary forms, with or without
11b7ef8cfbSLionel Sambuc * modification, are permitted provided that the following conditions
12b7ef8cfbSLionel Sambuc * are met:
13b7ef8cfbSLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14b7ef8cfbSLionel Sambuc * notice, this list of conditions and the following disclaimer.
15b7ef8cfbSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16b7ef8cfbSLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17b7ef8cfbSLionel Sambuc * documentation and/or other materials provided with the distribution.
18b7ef8cfbSLionel Sambuc * 3. Neither the name of the University nor the names of its contributors
19b7ef8cfbSLionel Sambuc * may be used to endorse or promote products derived from this software
20b7ef8cfbSLionel Sambuc * without specific prior written permission.
21b7ef8cfbSLionel Sambuc *
22b7ef8cfbSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23b7ef8cfbSLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24b7ef8cfbSLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25b7ef8cfbSLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26b7ef8cfbSLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27b7ef8cfbSLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28b7ef8cfbSLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29b7ef8cfbSLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30b7ef8cfbSLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31b7ef8cfbSLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32b7ef8cfbSLionel Sambuc * SUCH DAMAGE.
33b7ef8cfbSLionel Sambuc */
34b7ef8cfbSLionel Sambuc
35b7ef8cfbSLionel Sambuc #include <sys/cdefs.h>
36b7ef8cfbSLionel Sambuc #ifndef lint
37b7ef8cfbSLionel Sambuc __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\
38b7ef8cfbSLionel Sambuc The Regents of the University of California. All rights reserved.");
39b7ef8cfbSLionel Sambuc #endif /* not lint */
40b7ef8cfbSLionel Sambuc
41b7ef8cfbSLionel Sambuc #ifndef lint
42b7ef8cfbSLionel Sambuc #if 0
43b7ef8cfbSLionel Sambuc static char sccsid[] = "@(#)ls.c 8.7 (Berkeley) 8/5/94";
44b7ef8cfbSLionel Sambuc #else
45*e8ccbb3bSSevan Janiyan __RCSID("$NetBSD: ls.c,v 1.76 2017/02/06 21:06:04 rin Exp $");
46b7ef8cfbSLionel Sambuc #endif
47b7ef8cfbSLionel Sambuc #endif /* not lint */
48b7ef8cfbSLionel Sambuc
49b7ef8cfbSLionel Sambuc #include <sys/param.h>
50b7ef8cfbSLionel Sambuc #include <sys/types.h>
51b7ef8cfbSLionel Sambuc #include <sys/stat.h>
52b7ef8cfbSLionel Sambuc #include <sys/ioctl.h>
53b7ef8cfbSLionel Sambuc
54b7ef8cfbSLionel Sambuc #include <dirent.h>
55b7ef8cfbSLionel Sambuc #include <err.h>
56b7ef8cfbSLionel Sambuc #include <errno.h>
57b7ef8cfbSLionel Sambuc #include <fts.h>
58b7ef8cfbSLionel Sambuc #include <locale.h>
59b7ef8cfbSLionel Sambuc #include <stdio.h>
60b7ef8cfbSLionel Sambuc #include <stdlib.h>
61b7ef8cfbSLionel Sambuc #include <string.h>
62b7ef8cfbSLionel Sambuc #include <unistd.h>
63b7ef8cfbSLionel Sambuc #include <termios.h>
64b7ef8cfbSLionel Sambuc #include <pwd.h>
65b7ef8cfbSLionel Sambuc #include <grp.h>
66b7ef8cfbSLionel Sambuc #include <util.h>
67b7ef8cfbSLionel Sambuc
68b7ef8cfbSLionel Sambuc #include "ls.h"
69b7ef8cfbSLionel Sambuc #include "extern.h"
70b7ef8cfbSLionel Sambuc
71b7ef8cfbSLionel Sambuc static void display(FTSENT *, FTSENT *);
72b7ef8cfbSLionel Sambuc static int mastercmp(const FTSENT **, const FTSENT **);
73b7ef8cfbSLionel Sambuc static void traverse(int, char **, int);
74b7ef8cfbSLionel Sambuc
75b7ef8cfbSLionel Sambuc static void (*printfcn)(DISPLAY *);
76b7ef8cfbSLionel Sambuc static int (*sortfcn)(const FTSENT *, const FTSENT *);
77b7ef8cfbSLionel Sambuc
78b7ef8cfbSLionel Sambuc #define BY_NAME 0
79b7ef8cfbSLionel Sambuc #define BY_SIZE 1
80b7ef8cfbSLionel Sambuc #define BY_TIME 2
81b7ef8cfbSLionel Sambuc
82b7ef8cfbSLionel Sambuc long blocksize; /* block size units */
83b7ef8cfbSLionel Sambuc int termwidth = 80; /* default terminal width */
84b7ef8cfbSLionel Sambuc int sortkey = BY_NAME;
85b7ef8cfbSLionel Sambuc int rval = EXIT_SUCCESS; /* exit value - set if error encountered */
86b7ef8cfbSLionel Sambuc
87b7ef8cfbSLionel Sambuc /* flags */
88b7ef8cfbSLionel Sambuc int f_accesstime; /* use time of last access */
89b7ef8cfbSLionel Sambuc int f_column; /* columnated format */
90b7ef8cfbSLionel Sambuc int f_columnacross; /* columnated format, sorted across */
91b7ef8cfbSLionel Sambuc int f_flags; /* show flags associated with a file */
92b7ef8cfbSLionel Sambuc int f_grouponly; /* long listing without owner */
93b7ef8cfbSLionel Sambuc int f_humanize; /* humanize the size field */
94b7ef8cfbSLionel Sambuc int f_commas; /* separate size field with comma */
95b7ef8cfbSLionel Sambuc int f_inode; /* print inode */
96b7ef8cfbSLionel Sambuc int f_listdir; /* list actual directory, not contents */
97b7ef8cfbSLionel Sambuc int f_listdot; /* list files beginning with . */
98b7ef8cfbSLionel Sambuc int f_longform; /* long listing format */
99b7ef8cfbSLionel Sambuc int f_nonprint; /* show unprintables as ? */
100b7ef8cfbSLionel Sambuc int f_nosort; /* don't sort output */
101b7ef8cfbSLionel Sambuc int f_numericonly; /* don't convert uid/gid to name */
102b7ef8cfbSLionel Sambuc int f_octal; /* print octal escapes for nongraphic characters */
103b7ef8cfbSLionel Sambuc int f_octal_escape; /* like f_octal but use C escapes if possible */
104b7ef8cfbSLionel Sambuc int f_recursive; /* ls subdirectories also */
105b7ef8cfbSLionel Sambuc int f_reversesort; /* reverse whatever sort is used */
106b7ef8cfbSLionel Sambuc int f_sectime; /* print the real time for all files */
107b7ef8cfbSLionel Sambuc int f_singlecol; /* use single column output */
108b7ef8cfbSLionel Sambuc int f_size; /* list size in short listing */
109b7ef8cfbSLionel Sambuc int f_statustime; /* use time of last mode change */
110b7ef8cfbSLionel Sambuc int f_stream; /* stream format */
111b7ef8cfbSLionel Sambuc int f_type; /* add type character for non-regular files */
112b7ef8cfbSLionel Sambuc int f_typedir; /* add type character for directories */
113b7ef8cfbSLionel Sambuc int f_whiteout; /* show whiteout entries */
1140a6a1f1dSLionel Sambuc int f_fullpath; /* print full pathname, not filename */
1150a6a1f1dSLionel Sambuc int f_leafonly; /* when recursing, print leaf names only */
116b7ef8cfbSLionel Sambuc
117b7ef8cfbSLionel Sambuc __dead static void
usage(void)118b7ef8cfbSLionel Sambuc usage(void)
119b7ef8cfbSLionel Sambuc {
120b7ef8cfbSLionel Sambuc
121b7ef8cfbSLionel Sambuc (void)fprintf(stderr,
1220a6a1f1dSLionel Sambuc "usage: %s [-1AaBbCcdFfghikLlMmnOoPpqRrSsTtuWwXx] [file ...]\n",
123b7ef8cfbSLionel Sambuc getprogname());
124b7ef8cfbSLionel Sambuc exit(EXIT_FAILURE);
125b7ef8cfbSLionel Sambuc /* NOTREACHED */
126b7ef8cfbSLionel Sambuc }
127b7ef8cfbSLionel Sambuc
128b7ef8cfbSLionel Sambuc int
ls_main(int argc,char * argv[])129b7ef8cfbSLionel Sambuc ls_main(int argc, char *argv[])
130b7ef8cfbSLionel Sambuc {
131b7ef8cfbSLionel Sambuc static char dot[] = ".", *dotav[] = { dot, NULL };
132b7ef8cfbSLionel Sambuc struct winsize win;
133b7ef8cfbSLionel Sambuc int ch, fts_options;
134b7ef8cfbSLionel Sambuc int kflag = 0;
135b7ef8cfbSLionel Sambuc const char *p;
136b7ef8cfbSLionel Sambuc
137b7ef8cfbSLionel Sambuc setprogname(argv[0]);
138b7ef8cfbSLionel Sambuc (void)setlocale(LC_ALL, "");
139b7ef8cfbSLionel Sambuc
140b7ef8cfbSLionel Sambuc /* Terminal defaults to -Cq, non-terminal defaults to -1. */
141b7ef8cfbSLionel Sambuc if (isatty(STDOUT_FILENO)) {
142b7ef8cfbSLionel Sambuc if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 &&
143b7ef8cfbSLionel Sambuc win.ws_col > 0)
144b7ef8cfbSLionel Sambuc termwidth = win.ws_col;
145b7ef8cfbSLionel Sambuc f_column = f_nonprint = 1;
146b7ef8cfbSLionel Sambuc } else
147b7ef8cfbSLionel Sambuc f_singlecol = 1;
148b7ef8cfbSLionel Sambuc
149b7ef8cfbSLionel Sambuc /* Root is -A automatically. */
150b7ef8cfbSLionel Sambuc if (!getuid())
151b7ef8cfbSLionel Sambuc f_listdot = 1;
152b7ef8cfbSLionel Sambuc
153b7ef8cfbSLionel Sambuc fts_options = FTS_PHYSICAL;
1540a6a1f1dSLionel Sambuc while ((ch = getopt(argc, argv, "1AaBbCcdFfghikLlMmnOoPpqRrSsTtuWwXx"))
1550a6a1f1dSLionel Sambuc != -1) {
156b7ef8cfbSLionel Sambuc switch (ch) {
157b7ef8cfbSLionel Sambuc /*
158b7ef8cfbSLionel Sambuc * The -1, -C, -l, -m and -x options all override each other so
159b7ef8cfbSLionel Sambuc * shell aliasing works correctly.
160b7ef8cfbSLionel Sambuc */
161b7ef8cfbSLionel Sambuc case '1':
162b7ef8cfbSLionel Sambuc f_singlecol = 1;
163b7ef8cfbSLionel Sambuc f_column = f_columnacross = f_longform = f_stream = 0;
164b7ef8cfbSLionel Sambuc break;
165b7ef8cfbSLionel Sambuc case 'C':
166b7ef8cfbSLionel Sambuc f_column = 1;
167b7ef8cfbSLionel Sambuc f_columnacross = f_longform = f_singlecol = f_stream =
168b7ef8cfbSLionel Sambuc 0;
169b7ef8cfbSLionel Sambuc break;
170b7ef8cfbSLionel Sambuc case 'g':
171b7ef8cfbSLionel Sambuc if (f_grouponly != -1)
172b7ef8cfbSLionel Sambuc f_grouponly = 1;
173b7ef8cfbSLionel Sambuc f_longform = 1;
174b7ef8cfbSLionel Sambuc f_column = f_columnacross = f_singlecol = f_stream = 0;
175b7ef8cfbSLionel Sambuc break;
176b7ef8cfbSLionel Sambuc case 'l':
177b7ef8cfbSLionel Sambuc f_longform = 1;
178b7ef8cfbSLionel Sambuc f_column = f_columnacross = f_singlecol = f_stream = 0;
179b7ef8cfbSLionel Sambuc /* Never let -g take precedence over -l. */
180b7ef8cfbSLionel Sambuc f_grouponly = -1;
181b7ef8cfbSLionel Sambuc break;
182b7ef8cfbSLionel Sambuc case 'm':
183b7ef8cfbSLionel Sambuc f_stream = 1;
184b7ef8cfbSLionel Sambuc f_column = f_columnacross = f_longform = f_singlecol =
185b7ef8cfbSLionel Sambuc 0;
186b7ef8cfbSLionel Sambuc break;
187b7ef8cfbSLionel Sambuc case 'x':
188b7ef8cfbSLionel Sambuc f_columnacross = 1;
189b7ef8cfbSLionel Sambuc f_column = f_longform = f_singlecol = f_stream = 0;
190b7ef8cfbSLionel Sambuc break;
191b7ef8cfbSLionel Sambuc /* The -c and -u options override each other. */
192b7ef8cfbSLionel Sambuc case 'c':
193b7ef8cfbSLionel Sambuc f_statustime = 1;
194b7ef8cfbSLionel Sambuc f_accesstime = 0;
195b7ef8cfbSLionel Sambuc break;
196b7ef8cfbSLionel Sambuc case 'u':
197b7ef8cfbSLionel Sambuc f_accesstime = 1;
198b7ef8cfbSLionel Sambuc f_statustime = 0;
199b7ef8cfbSLionel Sambuc break;
200b7ef8cfbSLionel Sambuc case 'F':
201b7ef8cfbSLionel Sambuc f_type = 1;
202b7ef8cfbSLionel Sambuc break;
203b7ef8cfbSLionel Sambuc case 'L':
204b7ef8cfbSLionel Sambuc fts_options &= ~FTS_PHYSICAL;
205b7ef8cfbSLionel Sambuc fts_options |= FTS_LOGICAL;
206b7ef8cfbSLionel Sambuc break;
207b7ef8cfbSLionel Sambuc case 'R':
208b7ef8cfbSLionel Sambuc f_recursive = 1;
209b7ef8cfbSLionel Sambuc break;
2100a6a1f1dSLionel Sambuc case 'f':
2110a6a1f1dSLionel Sambuc f_nosort = 1;
2120a6a1f1dSLionel Sambuc /* FALLTHROUGH */
213b7ef8cfbSLionel Sambuc case 'a':
214b7ef8cfbSLionel Sambuc fts_options |= FTS_SEEDOT;
215b7ef8cfbSLionel Sambuc /* FALLTHROUGH */
216b7ef8cfbSLionel Sambuc case 'A':
217b7ef8cfbSLionel Sambuc f_listdot = 1;
218b7ef8cfbSLionel Sambuc break;
219b7ef8cfbSLionel Sambuc /* The -B option turns off the -b, -q and -w options. */
220b7ef8cfbSLionel Sambuc case 'B':
221b7ef8cfbSLionel Sambuc f_nonprint = 0;
222b7ef8cfbSLionel Sambuc f_octal = 1;
223b7ef8cfbSLionel Sambuc f_octal_escape = 0;
224b7ef8cfbSLionel Sambuc break;
225b7ef8cfbSLionel Sambuc /* The -b option turns off the -B, -q and -w options. */
226b7ef8cfbSLionel Sambuc case 'b':
227b7ef8cfbSLionel Sambuc f_nonprint = 0;
228b7ef8cfbSLionel Sambuc f_octal = 0;
229b7ef8cfbSLionel Sambuc f_octal_escape = 1;
230b7ef8cfbSLionel Sambuc break;
231b7ef8cfbSLionel Sambuc /* The -d option turns off the -R option. */
232b7ef8cfbSLionel Sambuc case 'd':
233b7ef8cfbSLionel Sambuc f_listdir = 1;
234b7ef8cfbSLionel Sambuc f_recursive = 0;
235b7ef8cfbSLionel Sambuc break;
236b7ef8cfbSLionel Sambuc case 'i':
237b7ef8cfbSLionel Sambuc f_inode = 1;
238b7ef8cfbSLionel Sambuc break;
239b7ef8cfbSLionel Sambuc case 'k':
240b7ef8cfbSLionel Sambuc blocksize = 1024;
241b7ef8cfbSLionel Sambuc kflag = 1;
242b7ef8cfbSLionel Sambuc f_humanize = 0;
243b7ef8cfbSLionel Sambuc break;
244b7ef8cfbSLionel Sambuc /* The -h option forces all sizes to be measured in bytes. */
245b7ef8cfbSLionel Sambuc case 'h':
246b7ef8cfbSLionel Sambuc f_humanize = 1;
247b7ef8cfbSLionel Sambuc kflag = 0;
248b7ef8cfbSLionel Sambuc f_commas = 0;
249b7ef8cfbSLionel Sambuc break;
250b7ef8cfbSLionel Sambuc case 'M':
251b7ef8cfbSLionel Sambuc f_humanize = 0;
252b7ef8cfbSLionel Sambuc f_commas = 1;
253b7ef8cfbSLionel Sambuc break;
254b7ef8cfbSLionel Sambuc case 'n':
255b7ef8cfbSLionel Sambuc f_numericonly = 1;
256b7ef8cfbSLionel Sambuc f_longform = 1;
257b7ef8cfbSLionel Sambuc f_column = f_columnacross = f_singlecol = f_stream = 0;
258b7ef8cfbSLionel Sambuc break;
2590a6a1f1dSLionel Sambuc case 'O':
2600a6a1f1dSLionel Sambuc f_leafonly = 1;
2610a6a1f1dSLionel Sambuc break;
262b7ef8cfbSLionel Sambuc case 'o':
263b7ef8cfbSLionel Sambuc f_flags = 1;
264b7ef8cfbSLionel Sambuc break;
2650a6a1f1dSLionel Sambuc case 'P':
2660a6a1f1dSLionel Sambuc f_fullpath = 1;
2670a6a1f1dSLionel Sambuc break;
268b7ef8cfbSLionel Sambuc case 'p':
269b7ef8cfbSLionel Sambuc f_typedir = 1;
270b7ef8cfbSLionel Sambuc break;
271b7ef8cfbSLionel Sambuc /* The -q option turns off the -B, -b and -w options. */
272b7ef8cfbSLionel Sambuc case 'q':
273b7ef8cfbSLionel Sambuc f_nonprint = 1;
274b7ef8cfbSLionel Sambuc f_octal = 0;
275b7ef8cfbSLionel Sambuc f_octal_escape = 0;
276b7ef8cfbSLionel Sambuc break;
277b7ef8cfbSLionel Sambuc case 'r':
278b7ef8cfbSLionel Sambuc f_reversesort = 1;
279b7ef8cfbSLionel Sambuc break;
280b7ef8cfbSLionel Sambuc case 'S':
281b7ef8cfbSLionel Sambuc sortkey = BY_SIZE;
282b7ef8cfbSLionel Sambuc break;
283b7ef8cfbSLionel Sambuc case 's':
284b7ef8cfbSLionel Sambuc f_size = 1;
285b7ef8cfbSLionel Sambuc break;
286b7ef8cfbSLionel Sambuc case 'T':
287b7ef8cfbSLionel Sambuc f_sectime = 1;
288b7ef8cfbSLionel Sambuc break;
289b7ef8cfbSLionel Sambuc case 't':
290b7ef8cfbSLionel Sambuc sortkey = BY_TIME;
291b7ef8cfbSLionel Sambuc break;
292b7ef8cfbSLionel Sambuc case 'W':
293b7ef8cfbSLionel Sambuc f_whiteout = 1;
294b7ef8cfbSLionel Sambuc break;
295b7ef8cfbSLionel Sambuc /* The -w option turns off the -B, -b and -q options. */
296b7ef8cfbSLionel Sambuc case 'w':
297b7ef8cfbSLionel Sambuc f_nonprint = 0;
298b7ef8cfbSLionel Sambuc f_octal = 0;
299b7ef8cfbSLionel Sambuc f_octal_escape = 0;
300b7ef8cfbSLionel Sambuc break;
3010a6a1f1dSLionel Sambuc case 'X':
3020a6a1f1dSLionel Sambuc fts_options |= FTS_XDEV;
3030a6a1f1dSLionel Sambuc break;
304b7ef8cfbSLionel Sambuc default:
305b7ef8cfbSLionel Sambuc case '?':
306b7ef8cfbSLionel Sambuc usage();
307b7ef8cfbSLionel Sambuc }
308b7ef8cfbSLionel Sambuc }
309b7ef8cfbSLionel Sambuc argc -= optind;
310b7ef8cfbSLionel Sambuc argv += optind;
311b7ef8cfbSLionel Sambuc
312b7ef8cfbSLionel Sambuc if (f_column || f_columnacross || f_stream) {
313b7ef8cfbSLionel Sambuc if ((p = getenv("COLUMNS")) != NULL)
314b7ef8cfbSLionel Sambuc termwidth = atoi(p);
315b7ef8cfbSLionel Sambuc }
316b7ef8cfbSLionel Sambuc
317b7ef8cfbSLionel Sambuc /*
318b7ef8cfbSLionel Sambuc * If both -g and -l options, let -l take precedence.
319b7ef8cfbSLionel Sambuc */
320b7ef8cfbSLionel Sambuc if (f_grouponly == -1)
321b7ef8cfbSLionel Sambuc f_grouponly = 0;
322b7ef8cfbSLionel Sambuc
323b7ef8cfbSLionel Sambuc /*
324b7ef8cfbSLionel Sambuc * If not -F, -i, -l, -p, -S, -s or -t options, don't require stat
325b7ef8cfbSLionel Sambuc * information.
326b7ef8cfbSLionel Sambuc */
327b7ef8cfbSLionel Sambuc if (!f_inode && !f_longform && !f_size && !f_type && !f_typedir &&
328b7ef8cfbSLionel Sambuc sortkey == BY_NAME)
329b7ef8cfbSLionel Sambuc fts_options |= FTS_NOSTAT;
330b7ef8cfbSLionel Sambuc
331b7ef8cfbSLionel Sambuc /*
332b7ef8cfbSLionel Sambuc * If not -F, -d or -l options, follow any symbolic links listed on
333b7ef8cfbSLionel Sambuc * the command line.
334b7ef8cfbSLionel Sambuc */
335b7ef8cfbSLionel Sambuc if (!f_longform && !f_listdir && !f_type)
336b7ef8cfbSLionel Sambuc fts_options |= FTS_COMFOLLOW;
337b7ef8cfbSLionel Sambuc
338b7ef8cfbSLionel Sambuc /*
339b7ef8cfbSLionel Sambuc * If -W, show whiteout entries
340b7ef8cfbSLionel Sambuc */
341b7ef8cfbSLionel Sambuc #ifdef FTS_WHITEOUT
342b7ef8cfbSLionel Sambuc if (f_whiteout)
343b7ef8cfbSLionel Sambuc fts_options |= FTS_WHITEOUT;
344b7ef8cfbSLionel Sambuc #endif
345b7ef8cfbSLionel Sambuc
34684d9c625SLionel Sambuc /* If -i, -l, or -s, figure out block size. */
347b7ef8cfbSLionel Sambuc if (f_inode || f_longform || f_size) {
348b7ef8cfbSLionel Sambuc if (!kflag)
349b7ef8cfbSLionel Sambuc (void)getbsize(NULL, &blocksize);
350b7ef8cfbSLionel Sambuc blocksize /= 512;
351b7ef8cfbSLionel Sambuc }
352b7ef8cfbSLionel Sambuc
353b7ef8cfbSLionel Sambuc /* Select a sort function. */
354b7ef8cfbSLionel Sambuc if (f_reversesort) {
355b7ef8cfbSLionel Sambuc switch (sortkey) {
356b7ef8cfbSLionel Sambuc case BY_NAME:
357b7ef8cfbSLionel Sambuc sortfcn = revnamecmp;
358b7ef8cfbSLionel Sambuc break;
359b7ef8cfbSLionel Sambuc case BY_SIZE:
360b7ef8cfbSLionel Sambuc sortfcn = revsizecmp;
361b7ef8cfbSLionel Sambuc break;
362b7ef8cfbSLionel Sambuc case BY_TIME:
363b7ef8cfbSLionel Sambuc if (f_accesstime)
364b7ef8cfbSLionel Sambuc sortfcn = revacccmp;
365b7ef8cfbSLionel Sambuc else if (f_statustime)
366b7ef8cfbSLionel Sambuc sortfcn = revstatcmp;
367b7ef8cfbSLionel Sambuc else /* Use modification time. */
368b7ef8cfbSLionel Sambuc sortfcn = revmodcmp;
369b7ef8cfbSLionel Sambuc break;
370b7ef8cfbSLionel Sambuc }
371b7ef8cfbSLionel Sambuc } else {
372b7ef8cfbSLionel Sambuc switch (sortkey) {
373b7ef8cfbSLionel Sambuc case BY_NAME:
374b7ef8cfbSLionel Sambuc sortfcn = namecmp;
375b7ef8cfbSLionel Sambuc break;
376b7ef8cfbSLionel Sambuc case BY_SIZE:
377b7ef8cfbSLionel Sambuc sortfcn = sizecmp;
378b7ef8cfbSLionel Sambuc break;
379b7ef8cfbSLionel Sambuc case BY_TIME:
380b7ef8cfbSLionel Sambuc if (f_accesstime)
381b7ef8cfbSLionel Sambuc sortfcn = acccmp;
382b7ef8cfbSLionel Sambuc else if (f_statustime)
383b7ef8cfbSLionel Sambuc sortfcn = statcmp;
384b7ef8cfbSLionel Sambuc else /* Use modification time. */
385b7ef8cfbSLionel Sambuc sortfcn = modcmp;
386b7ef8cfbSLionel Sambuc break;
387b7ef8cfbSLionel Sambuc }
388b7ef8cfbSLionel Sambuc }
389b7ef8cfbSLionel Sambuc
390b7ef8cfbSLionel Sambuc /* Select a print function. */
391b7ef8cfbSLionel Sambuc if (f_singlecol)
392b7ef8cfbSLionel Sambuc printfcn = printscol;
393b7ef8cfbSLionel Sambuc else if (f_columnacross)
394b7ef8cfbSLionel Sambuc printfcn = printacol;
395b7ef8cfbSLionel Sambuc else if (f_longform)
396b7ef8cfbSLionel Sambuc printfcn = printlong;
397b7ef8cfbSLionel Sambuc else if (f_stream)
398b7ef8cfbSLionel Sambuc printfcn = printstream;
399b7ef8cfbSLionel Sambuc else
400b7ef8cfbSLionel Sambuc printfcn = printcol;
401b7ef8cfbSLionel Sambuc
402b7ef8cfbSLionel Sambuc if (argc)
403b7ef8cfbSLionel Sambuc traverse(argc, argv, fts_options);
404b7ef8cfbSLionel Sambuc else
405b7ef8cfbSLionel Sambuc traverse(1, dotav, fts_options);
406b7ef8cfbSLionel Sambuc return rval;
407b7ef8cfbSLionel Sambuc /* NOTREACHED */
408b7ef8cfbSLionel Sambuc }
409b7ef8cfbSLionel Sambuc
410b7ef8cfbSLionel Sambuc static int output; /* If anything output. */
411b7ef8cfbSLionel Sambuc
412b7ef8cfbSLionel Sambuc /*
413b7ef8cfbSLionel Sambuc * Traverse() walks the logical directory structure specified by the argv list
414b7ef8cfbSLionel Sambuc * in the order specified by the mastercmp() comparison function. During the
415b7ef8cfbSLionel Sambuc * traversal it passes linked lists of structures to display() which represent
416b7ef8cfbSLionel Sambuc * a superset (may be exact set) of the files to be displayed.
417b7ef8cfbSLionel Sambuc */
418b7ef8cfbSLionel Sambuc static void
traverse(int argc,char * argv[],int options)419b7ef8cfbSLionel Sambuc traverse(int argc, char *argv[], int options)
420b7ef8cfbSLionel Sambuc {
421b7ef8cfbSLionel Sambuc FTS *ftsp;
422b7ef8cfbSLionel Sambuc FTSENT *p, *chp;
423b7ef8cfbSLionel Sambuc int ch_options, error;
424b7ef8cfbSLionel Sambuc
425b7ef8cfbSLionel Sambuc if ((ftsp =
426b7ef8cfbSLionel Sambuc fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL)
427b7ef8cfbSLionel Sambuc err(EXIT_FAILURE, NULL);
428b7ef8cfbSLionel Sambuc
429b7ef8cfbSLionel Sambuc display(NULL, fts_children(ftsp, 0));
430b7ef8cfbSLionel Sambuc if (f_listdir) {
431b7ef8cfbSLionel Sambuc (void)fts_close(ftsp);
432b7ef8cfbSLionel Sambuc return;
433b7ef8cfbSLionel Sambuc }
434b7ef8cfbSLionel Sambuc
435b7ef8cfbSLionel Sambuc /*
436b7ef8cfbSLionel Sambuc * If not recursing down this tree and don't need stat info, just get
437b7ef8cfbSLionel Sambuc * the names.
438b7ef8cfbSLionel Sambuc */
439b7ef8cfbSLionel Sambuc ch_options = !f_recursive && options & FTS_NOSTAT ? FTS_NAMEONLY : 0;
440b7ef8cfbSLionel Sambuc
441b7ef8cfbSLionel Sambuc while ((p = fts_read(ftsp)) != NULL)
442b7ef8cfbSLionel Sambuc switch (p->fts_info) {
443b7ef8cfbSLionel Sambuc case FTS_DC:
444b7ef8cfbSLionel Sambuc warnx("%s: directory causes a cycle", p->fts_name);
445b7ef8cfbSLionel Sambuc break;
446b7ef8cfbSLionel Sambuc case FTS_DNR:
447b7ef8cfbSLionel Sambuc case FTS_ERR:
448b7ef8cfbSLionel Sambuc warnx("%s: %s", p->fts_name, strerror(p->fts_errno));
449b7ef8cfbSLionel Sambuc rval = EXIT_FAILURE;
450b7ef8cfbSLionel Sambuc break;
451b7ef8cfbSLionel Sambuc case FTS_D:
452b7ef8cfbSLionel Sambuc if (p->fts_level != FTS_ROOTLEVEL &&
453b7ef8cfbSLionel Sambuc p->fts_name[0] == '.' && !f_listdot)
454b7ef8cfbSLionel Sambuc break;
455b7ef8cfbSLionel Sambuc
456b7ef8cfbSLionel Sambuc /*
457b7ef8cfbSLionel Sambuc * If already output something, put out a newline as
458b7ef8cfbSLionel Sambuc * a separator. If multiple arguments, precede each
459b7ef8cfbSLionel Sambuc * directory with its name.
460b7ef8cfbSLionel Sambuc */
4610a6a1f1dSLionel Sambuc if (!f_leafonly) {
462b7ef8cfbSLionel Sambuc if (output)
463b7ef8cfbSLionel Sambuc (void)printf("\n%s:\n", p->fts_path);
464b7ef8cfbSLionel Sambuc else if (argc > 1) {
465b7ef8cfbSLionel Sambuc (void)printf("%s:\n", p->fts_path);
466b7ef8cfbSLionel Sambuc output = 1;
467b7ef8cfbSLionel Sambuc }
4680a6a1f1dSLionel Sambuc }
469b7ef8cfbSLionel Sambuc
470b7ef8cfbSLionel Sambuc chp = fts_children(ftsp, ch_options);
471b7ef8cfbSLionel Sambuc display(p, chp);
472b7ef8cfbSLionel Sambuc
473b7ef8cfbSLionel Sambuc if (!f_recursive && chp != NULL)
474b7ef8cfbSLionel Sambuc (void)fts_set(ftsp, p, FTS_SKIP);
475b7ef8cfbSLionel Sambuc break;
476b7ef8cfbSLionel Sambuc }
477b7ef8cfbSLionel Sambuc error = errno;
478b7ef8cfbSLionel Sambuc (void)fts_close(ftsp);
479b7ef8cfbSLionel Sambuc errno = error;
480b7ef8cfbSLionel Sambuc if (errno)
481b7ef8cfbSLionel Sambuc err(EXIT_FAILURE, "fts_read");
482b7ef8cfbSLionel Sambuc }
483b7ef8cfbSLionel Sambuc
484b7ef8cfbSLionel Sambuc /*
485b7ef8cfbSLionel Sambuc * Display() takes a linked list of FTSENT structures and passes the list
486b7ef8cfbSLionel Sambuc * along with any other necessary information to the print function. P
487b7ef8cfbSLionel Sambuc * points to the parent directory of the display list.
488b7ef8cfbSLionel Sambuc */
489b7ef8cfbSLionel Sambuc static void
display(FTSENT * p,FTSENT * list)490b7ef8cfbSLionel Sambuc display(FTSENT *p, FTSENT *list)
491b7ef8cfbSLionel Sambuc {
492b7ef8cfbSLionel Sambuc struct stat *sp;
493b7ef8cfbSLionel Sambuc DISPLAY d;
494b7ef8cfbSLionel Sambuc FTSENT *cur;
495b7ef8cfbSLionel Sambuc NAMES *np;
496b7ef8cfbSLionel Sambuc u_int64_t btotal, stotal;
497b7ef8cfbSLionel Sambuc off_t maxsize;
498b7ef8cfbSLionel Sambuc blkcnt_t maxblock;
499b7ef8cfbSLionel Sambuc ino_t maxinode;
500b7ef8cfbSLionel Sambuc int maxmajor, maxminor;
501b7ef8cfbSLionel Sambuc uint32_t maxnlink;
502b7ef8cfbSLionel Sambuc int bcfile, entries, flen, glen, ulen, maxflags, maxgroup;
503b7ef8cfbSLionel Sambuc unsigned int maxlen;
504b7ef8cfbSLionel Sambuc int maxuser, needstats;
505b7ef8cfbSLionel Sambuc const char *user, *group;
506b7ef8cfbSLionel Sambuc char buf[21]; /* 64 bits == 20 digits, +1 for NUL */
507b7ef8cfbSLionel Sambuc char nuser[12], ngroup[12];
508b7ef8cfbSLionel Sambuc char *flags = NULL;
509b7ef8cfbSLionel Sambuc
510b7ef8cfbSLionel Sambuc /*
511b7ef8cfbSLionel Sambuc * If list is NULL there are two possibilities: that the parent
512b7ef8cfbSLionel Sambuc * directory p has no children, or that fts_children() returned an
513b7ef8cfbSLionel Sambuc * error. We ignore the error case since it will be replicated
514b7ef8cfbSLionel Sambuc * on the next call to fts_read() on the post-order visit to the
515b7ef8cfbSLionel Sambuc * directory p, and will be signalled in traverse().
516b7ef8cfbSLionel Sambuc */
517b7ef8cfbSLionel Sambuc if (list == NULL)
518b7ef8cfbSLionel Sambuc return;
519b7ef8cfbSLionel Sambuc
520b7ef8cfbSLionel Sambuc needstats = f_inode || f_longform || f_size;
521b7ef8cfbSLionel Sambuc flen = 0;
522b7ef8cfbSLionel Sambuc maxinode = maxnlink = 0;
523b7ef8cfbSLionel Sambuc bcfile = 0;
524b7ef8cfbSLionel Sambuc maxuser = maxgroup = maxflags = maxlen = 0;
525b7ef8cfbSLionel Sambuc btotal = stotal = maxblock = maxsize = 0;
526b7ef8cfbSLionel Sambuc maxmajor = maxminor = 0;
527b7ef8cfbSLionel Sambuc for (cur = list, entries = 0; cur; cur = cur->fts_link) {
528b7ef8cfbSLionel Sambuc if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) {
529b7ef8cfbSLionel Sambuc warnx("%s: %s",
530b7ef8cfbSLionel Sambuc cur->fts_name, strerror(cur->fts_errno));
531b7ef8cfbSLionel Sambuc cur->fts_number = NO_PRINT;
532b7ef8cfbSLionel Sambuc rval = EXIT_FAILURE;
533b7ef8cfbSLionel Sambuc continue;
534b7ef8cfbSLionel Sambuc }
535b7ef8cfbSLionel Sambuc
536b7ef8cfbSLionel Sambuc /*
537b7ef8cfbSLionel Sambuc * P is NULL if list is the argv list, to which different rules
538b7ef8cfbSLionel Sambuc * apply.
539b7ef8cfbSLionel Sambuc */
540b7ef8cfbSLionel Sambuc if (p == NULL) {
541b7ef8cfbSLionel Sambuc /* Directories will be displayed later. */
542b7ef8cfbSLionel Sambuc if (cur->fts_info == FTS_D && !f_listdir) {
543b7ef8cfbSLionel Sambuc cur->fts_number = NO_PRINT;
544b7ef8cfbSLionel Sambuc continue;
545b7ef8cfbSLionel Sambuc }
546b7ef8cfbSLionel Sambuc } else {
547b7ef8cfbSLionel Sambuc /* Only display dot file if -a/-A set. */
548b7ef8cfbSLionel Sambuc if (cur->fts_name[0] == '.' && !f_listdot) {
549b7ef8cfbSLionel Sambuc cur->fts_number = NO_PRINT;
550b7ef8cfbSLionel Sambuc continue;
551b7ef8cfbSLionel Sambuc }
552b7ef8cfbSLionel Sambuc }
553b7ef8cfbSLionel Sambuc if (cur->fts_namelen > maxlen)
554b7ef8cfbSLionel Sambuc maxlen = cur->fts_namelen;
555b7ef8cfbSLionel Sambuc if (needstats) {
556b7ef8cfbSLionel Sambuc sp = cur->fts_statp;
557b7ef8cfbSLionel Sambuc if (sp->st_blocks > maxblock)
558b7ef8cfbSLionel Sambuc maxblock = sp->st_blocks;
559b7ef8cfbSLionel Sambuc if (sp->st_ino > maxinode)
560b7ef8cfbSLionel Sambuc maxinode = sp->st_ino;
561b7ef8cfbSLionel Sambuc if (sp->st_nlink > maxnlink)
562b7ef8cfbSLionel Sambuc maxnlink = sp->st_nlink;
563b7ef8cfbSLionel Sambuc if (sp->st_size > maxsize)
564b7ef8cfbSLionel Sambuc maxsize = sp->st_size;
565b7ef8cfbSLionel Sambuc if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode)) {
566b7ef8cfbSLionel Sambuc bcfile = 1;
567b7ef8cfbSLionel Sambuc if (major(sp->st_rdev) > maxmajor)
568b7ef8cfbSLionel Sambuc maxmajor = major(sp->st_rdev);
569b7ef8cfbSLionel Sambuc if (minor(sp->st_rdev) > maxminor)
570b7ef8cfbSLionel Sambuc maxminor = minor(sp->st_rdev);
571b7ef8cfbSLionel Sambuc }
572b7ef8cfbSLionel Sambuc
573b7ef8cfbSLionel Sambuc btotal += sp->st_blocks;
574b7ef8cfbSLionel Sambuc stotal += sp->st_size;
575b7ef8cfbSLionel Sambuc if (f_longform) {
576b7ef8cfbSLionel Sambuc if (f_numericonly ||
577b7ef8cfbSLionel Sambuc (user = user_from_uid(sp->st_uid, 0)) ==
578b7ef8cfbSLionel Sambuc NULL) {
579b7ef8cfbSLionel Sambuc (void)snprintf(nuser, sizeof(nuser),
580b7ef8cfbSLionel Sambuc "%u", sp->st_uid);
581b7ef8cfbSLionel Sambuc user = nuser;
582b7ef8cfbSLionel Sambuc }
583b7ef8cfbSLionel Sambuc if (f_numericonly ||
584b7ef8cfbSLionel Sambuc (group = group_from_gid(sp->st_gid, 0)) ==
585b7ef8cfbSLionel Sambuc NULL) {
586b7ef8cfbSLionel Sambuc (void)snprintf(ngroup, sizeof(ngroup),
587b7ef8cfbSLionel Sambuc "%u", sp->st_gid);
588b7ef8cfbSLionel Sambuc group = ngroup;
589b7ef8cfbSLionel Sambuc }
590b7ef8cfbSLionel Sambuc if ((ulen = strlen(user)) > maxuser)
591b7ef8cfbSLionel Sambuc maxuser = ulen;
592b7ef8cfbSLionel Sambuc if ((glen = strlen(group)) > maxgroup)
593b7ef8cfbSLionel Sambuc maxgroup = glen;
594b7ef8cfbSLionel Sambuc if (f_flags) {
595b7ef8cfbSLionel Sambuc flags =
596b7ef8cfbSLionel Sambuc flags_to_string((u_long)sp->st_flags, "-");
597b7ef8cfbSLionel Sambuc if ((flen = strlen(flags)) > maxflags)
598b7ef8cfbSLionel Sambuc maxflags = flen;
599b7ef8cfbSLionel Sambuc } else
600b7ef8cfbSLionel Sambuc flen = 0;
601b7ef8cfbSLionel Sambuc
602b7ef8cfbSLionel Sambuc if ((np = malloc(sizeof(NAMES) +
603b7ef8cfbSLionel Sambuc ulen + glen + flen + 2)) == NULL)
604b7ef8cfbSLionel Sambuc err(EXIT_FAILURE, NULL);
605b7ef8cfbSLionel Sambuc
606b7ef8cfbSLionel Sambuc np->user = &np->data[0];
607b7ef8cfbSLionel Sambuc (void)strcpy(np->user, user);
608b7ef8cfbSLionel Sambuc np->group = &np->data[ulen + 1];
609b7ef8cfbSLionel Sambuc (void)strcpy(np->group, group);
610b7ef8cfbSLionel Sambuc
611b7ef8cfbSLionel Sambuc if (f_flags) {
612b7ef8cfbSLionel Sambuc np->flags = &np->data[ulen + glen + 2];
613b7ef8cfbSLionel Sambuc (void)strcpy(np->flags, flags);
614b7ef8cfbSLionel Sambuc free(flags);
615b7ef8cfbSLionel Sambuc }
616b7ef8cfbSLionel Sambuc cur->fts_pointer = np;
617b7ef8cfbSLionel Sambuc }
618b7ef8cfbSLionel Sambuc }
619b7ef8cfbSLionel Sambuc ++entries;
620b7ef8cfbSLionel Sambuc }
621b7ef8cfbSLionel Sambuc
622b7ef8cfbSLionel Sambuc if (!entries)
623b7ef8cfbSLionel Sambuc return;
624b7ef8cfbSLionel Sambuc
625b7ef8cfbSLionel Sambuc d.list = list;
626b7ef8cfbSLionel Sambuc d.entries = entries;
627b7ef8cfbSLionel Sambuc d.maxlen = maxlen;
628b7ef8cfbSLionel Sambuc if (needstats) {
629b7ef8cfbSLionel Sambuc d.btotal = btotal;
630b7ef8cfbSLionel Sambuc d.stotal = stotal;
631b7ef8cfbSLionel Sambuc if (f_humanize) {
632b7ef8cfbSLionel Sambuc d.s_block = 4; /* min buf length for humanize_number */
633b7ef8cfbSLionel Sambuc } else {
634*e8ccbb3bSSevan Janiyan (void)snprintf(buf, sizeof(buf), "%lld",
635b7ef8cfbSLionel Sambuc (long long)howmany(maxblock, blocksize));
636b7ef8cfbSLionel Sambuc d.s_block = strlen(buf);
637b7ef8cfbSLionel Sambuc if (f_commas) /* allow for commas before every third digit */
638b7ef8cfbSLionel Sambuc d.s_block += (d.s_block - 1) / 3;
639b7ef8cfbSLionel Sambuc }
640b7ef8cfbSLionel Sambuc d.s_flags = maxflags;
641b7ef8cfbSLionel Sambuc d.s_group = maxgroup;
642b7ef8cfbSLionel Sambuc (void)snprintf(buf, sizeof(buf), "%llu",
643b7ef8cfbSLionel Sambuc (unsigned long long)maxinode);
644b7ef8cfbSLionel Sambuc d.s_inode = strlen(buf);
645b7ef8cfbSLionel Sambuc (void)snprintf(buf, sizeof(buf), "%u", maxnlink);
646b7ef8cfbSLionel Sambuc d.s_nlink = strlen(buf);
647b7ef8cfbSLionel Sambuc if (f_humanize) {
648b7ef8cfbSLionel Sambuc d.s_size = 4; /* min buf length for humanize_number */
649b7ef8cfbSLionel Sambuc } else {
650*e8ccbb3bSSevan Janiyan (void)snprintf(buf, sizeof(buf), "%lld",
651b7ef8cfbSLionel Sambuc (long long)maxsize);
652b7ef8cfbSLionel Sambuc d.s_size = strlen(buf);
653b7ef8cfbSLionel Sambuc if (f_commas) /* allow for commas before every third digit */
654b7ef8cfbSLionel Sambuc d.s_size += (d.s_size - 1) / 3;
655b7ef8cfbSLionel Sambuc }
656b7ef8cfbSLionel Sambuc d.s_user = maxuser;
657b7ef8cfbSLionel Sambuc if (bcfile) {
658*e8ccbb3bSSevan Janiyan (void)snprintf(buf, sizeof(buf), "%d", maxmajor);
659b7ef8cfbSLionel Sambuc d.s_major = strlen(buf);
660*e8ccbb3bSSevan Janiyan (void)snprintf(buf, sizeof(buf), "%d", maxminor);
661b7ef8cfbSLionel Sambuc d.s_minor = strlen(buf);
662b7ef8cfbSLionel Sambuc if (d.s_major + d.s_minor + 2 > d.s_size)
663b7ef8cfbSLionel Sambuc d.s_size = d.s_major + d.s_minor + 2;
664b7ef8cfbSLionel Sambuc else if (d.s_size - d.s_minor - 2 > d.s_major)
665b7ef8cfbSLionel Sambuc d.s_major = d.s_size - d.s_minor - 2;
666b7ef8cfbSLionel Sambuc } else {
667b7ef8cfbSLionel Sambuc d.s_major = 0;
668b7ef8cfbSLionel Sambuc d.s_minor = 0;
669b7ef8cfbSLionel Sambuc }
670b7ef8cfbSLionel Sambuc }
671b7ef8cfbSLionel Sambuc
672b7ef8cfbSLionel Sambuc printfcn(&d);
673b7ef8cfbSLionel Sambuc output = 1;
674b7ef8cfbSLionel Sambuc
675b7ef8cfbSLionel Sambuc if (f_longform)
676b7ef8cfbSLionel Sambuc for (cur = list; cur; cur = cur->fts_link)
677b7ef8cfbSLionel Sambuc free(cur->fts_pointer);
678b7ef8cfbSLionel Sambuc }
679b7ef8cfbSLionel Sambuc
680b7ef8cfbSLionel Sambuc /*
681b7ef8cfbSLionel Sambuc * Ordering for mastercmp:
682b7ef8cfbSLionel Sambuc * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories
683b7ef8cfbSLionel Sambuc * as larger than directories. Within either group, use the sort function.
684b7ef8cfbSLionel Sambuc * All other levels use the sort function. Error entries remain unsorted.
685b7ef8cfbSLionel Sambuc */
686b7ef8cfbSLionel Sambuc static int
mastercmp(const FTSENT ** a,const FTSENT ** b)687b7ef8cfbSLionel Sambuc mastercmp(const FTSENT **a, const FTSENT **b)
688b7ef8cfbSLionel Sambuc {
689b7ef8cfbSLionel Sambuc int a_info, b_info;
690b7ef8cfbSLionel Sambuc
691b7ef8cfbSLionel Sambuc a_info = (*a)->fts_info;
692b7ef8cfbSLionel Sambuc if (a_info == FTS_ERR)
693b7ef8cfbSLionel Sambuc return (0);
694b7ef8cfbSLionel Sambuc b_info = (*b)->fts_info;
695b7ef8cfbSLionel Sambuc if (b_info == FTS_ERR)
696b7ef8cfbSLionel Sambuc return (0);
697b7ef8cfbSLionel Sambuc
698b7ef8cfbSLionel Sambuc if (a_info == FTS_NS || b_info == FTS_NS) {
699b7ef8cfbSLionel Sambuc if (b_info != FTS_NS)
700b7ef8cfbSLionel Sambuc return (1);
701b7ef8cfbSLionel Sambuc else if (a_info != FTS_NS)
702b7ef8cfbSLionel Sambuc return (-1);
703b7ef8cfbSLionel Sambuc else
704b7ef8cfbSLionel Sambuc return (namecmp(*a, *b));
705b7ef8cfbSLionel Sambuc }
706b7ef8cfbSLionel Sambuc
707b7ef8cfbSLionel Sambuc if (a_info != b_info && !f_listdir &&
708b7ef8cfbSLionel Sambuc (*a)->fts_level == FTS_ROOTLEVEL) {
709b7ef8cfbSLionel Sambuc if (a_info == FTS_D)
710b7ef8cfbSLionel Sambuc return (1);
711b7ef8cfbSLionel Sambuc else if (b_info == FTS_D)
712b7ef8cfbSLionel Sambuc return (-1);
713b7ef8cfbSLionel Sambuc }
714b7ef8cfbSLionel Sambuc return (sortfcn(*a, *b));
715b7ef8cfbSLionel Sambuc }
716