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