| 1 |
/* |
| 2 |
Copyright (C) 2000-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
/* |
| 20 |
* Copyright (c) 1989, 1993, 1994 |
| 21 |
* The Regents of the University of California. All rights reserved. |
| 22 |
* |
| 23 |
* Redistribution and use in source and binary forms, with or without |
| 24 |
* modification, are permitted provided that the following conditions |
| 25 |
* are met: |
| 26 |
* 1. Redistributions of source code must retain the above copyright |
| 27 |
* notice, this list of conditions and the following disclaimer. |
| 28 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 29 |
* notice, this list of conditions and the following disclaimer in the |
| 30 |
* documentation and/or other materials provided with the distribution. |
| 31 |
* 3. Neither the name of the University nor the names of its contributors |
| 32 |
* may be used to endorse or promote products derived from this software |
| 33 |
* without specific prior written permission. |
| 34 |
* |
| 35 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 36 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 37 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 38 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 39 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 40 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 41 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 42 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 43 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 44 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 45 |
* SUCH DAMAGE. |
| 46 |
*/ |
| 47 |
|
| 48 |
/* This code is derived from software contributed to Berkeley by |
| 49 |
Michael Fischbein. */ |
| 50 |
|
| 51 |
#include <config.h> |
| 52 |
|
| 53 |
#include <sys/types.h> |
| 54 |
#include <sys/stat.h> |
| 55 |
#include <sys/ioctl.h> |
| 56 |
|
| 57 |
#include <errno.h> |
| 58 |
#include <fts_.h> |
| 59 |
#include <stdio.h> |
| 60 |
#include <stdlib.h> |
| 61 |
#include <string.h> |
| 62 |
#include <unistd.h> |
| 63 |
#include <pwd.h> |
| 64 |
#include <grp.h> |
| 65 |
#include <termios.h> |
| 66 |
|
| 67 |
#include <inttostr.h> |
| 68 |
#include "ls.h" |
| 69 |
#include "extern.h" |
| 70 |
|
| 71 |
static void display (FTSENT *, FTSENT *); |
| 72 |
static int mastercmp (const FTSENT **, const FTSENT **); |
| 73 |
static void traverse (int, char **, int); |
| 74 |
|
| 75 |
static void (*printfcn) (DISPLAY *); |
| 76 |
static int (*sortfcn) (const FTSENT *, const FTSENT *); |
| 77 |
|
| 78 |
#define BY_NAME 0 |
| 79 |
#define BY_SIZE 1 |
| 80 |
#define BY_TIME 2 |
| 81 |
|
| 82 |
long blocksize; /* block size units */ |
| 83 |
int termwidth = 80; /* default terminal width */ |
| 84 |
int sortkey = BY_NAME; |
| 85 |
|
| 86 |
static int output; /* If anything was output. */ |
| 87 |
|
| 88 |
/* flags */ |
| 89 |
int f_accesstime; /* use time of last access */ |
| 90 |
int f_column; /* columnated format */ |
| 91 |
int f_columnacross; /* columnated format, sorted across */ |
| 92 |
int f_flags; /* show flags associated with a file */ |
| 93 |
int f_inode; /* print inode */ |
| 94 |
int f_listdir; /* list actual directory, not contents */ |
| 95 |
int f_listdot; /* list files beginning with . */ |
| 96 |
int f_longform; /* long listing format */ |
| 97 |
int f_newline; /* if precede with newline */ |
| 98 |
int f_nonprint; /* show unprintables as ? */ |
| 99 |
int f_nosort; /* don't sort output */ |
| 100 |
int f_numericonly; /* don't expand uid to symbolic name */ |
| 101 |
int f_recursive; /* ls subdirectories also */ |
| 102 |
int f_reversesort; /* reverse whatever sort is used */ |
| 103 |
int f_sectime; /* print the real time for all files */ |
| 104 |
int f_singlecol; /* use single column output */ |
| 105 |
int f_size; /* list size in short listing */ |
| 106 |
int f_statustime; /* use time of last mode change */ |
| 107 |
int f_stream; /* stream format */ |
| 108 |
int f_dirname; /* if precede with directory name */ |
| 109 |
int f_type; /* add type character for non-regular files */ |
| 110 |
int f_typedir; /* add type character for directories */ |
| 111 |
int f_whiteout; /* show whiteout entries */ |
| 112 |
|
| 113 |
int rval; |
| 114 |
|
| 115 |
int |
| 116 |
ls_main (int argc, char **argv) |
| 117 |
{ |
| 118 |
static char dot[] = ".", *dotav[] = { dot, NULL }; |
| 119 |
struct winsize win; |
| 120 |
int ch, fts_options; |
| 121 |
char *p; |
| 122 |
|
| 123 |
/* |
| 124 |
* Clear all settings made in any previous call. |
| 125 |
*/ |
| 126 |
output = 0; |
| 127 |
|
| 128 |
f_accesstime = f_column = f_columnacross = f_flags = f_inode = 0; |
| 129 |
f_listdir = f_listdot = f_longform = f_newline = 0; |
| 130 |
f_nonprint = f_nosort = f_numericonly = 0; |
| 131 |
f_recursive = f_reversesort = f_sectime = f_singlecol = 0; |
| 132 |
f_size = f_statustime = f_stream = f_dirname = 0; |
| 133 |
f_type = f_typedir = f_whiteout = 0; |
| 134 |
|
| 135 |
/* Terminal defaults to -Cq, non-terminal defaults to -1. */ |
| 136 |
if (isatty (STDOUT_FILENO)) |
| 137 |
{ |
| 138 |
p = getenv ("COLUMNS"); |
| 139 |
if (p != NULL) |
| 140 |
termwidth = atoi (p); |
| 141 |
else if (ioctl (STDOUT_FILENO, TIOCGWINSZ, &win) == 0 && win.ws_col > 0) |
| 142 |
termwidth = win.ws_col; |
| 143 |
f_column = f_nonprint = 1; |
| 144 |
} |
| 145 |
else |
| 146 |
f_singlecol = 1; |
| 147 |
|
| 148 |
/* Root is -A automatically. */ |
| 149 |
if (!getuid ()) |
| 150 |
f_listdot = 1; |
| 151 |
|
| 152 |
optind = 1; /* Reset for reentrant scanning. */ |
| 153 |
|
| 154 |
fts_options = FTS_PHYSICAL | FTS_NOCHDIR; |
| 155 |
while ((ch = getopt (argc, argv, "1ACFLRSTWacdfgiklmnopqrstux")) != -1) |
| 156 |
{ |
| 157 |
switch (ch) |
| 158 |
{ |
| 159 |
/* |
| 160 |
* The options -1, -C, -l, -m, -n and -x all overrule each |
| 161 |
* other so shell aliasing works right. |
| 162 |
*/ |
| 163 |
case '1': |
| 164 |
f_singlecol = 1; |
| 165 |
f_column = f_columnacross = f_longform = f_stream = 0; |
| 166 |
break; |
| 167 |
case 'C': |
| 168 |
f_column = 1; |
| 169 |
f_longform = f_columnacross = f_singlecol = f_stream = 0; |
| 170 |
break; |
| 171 |
case 'l': |
| 172 |
f_longform = 1; |
| 173 |
f_numericonly = 0; |
| 174 |
f_column = f_columnacross = f_singlecol = f_stream = 0; |
| 175 |
break; |
| 176 |
case 'm': |
| 177 |
f_stream = 1; |
| 178 |
f_column = f_columnacross = f_longform = f_singlecol = 0; |
| 179 |
break; |
| 180 |
case 'x': |
| 181 |
f_columnacross = 1; |
| 182 |
f_column = f_longform = f_singlecol = f_stream = 0; |
| 183 |
break; |
| 184 |
case 'n': |
| 185 |
f_longform = 1; |
| 186 |
f_numericonly = 1; |
| 187 |
f_column = f_columnacross = f_singlecol = f_stream = 0; |
| 188 |
break; |
| 189 |
/* The -c and -u options override each other. */ |
| 190 |
case 'c': |
| 191 |
f_statustime = 1; |
| 192 |
f_accesstime = 0; |
| 193 |
break; |
| 194 |
case 'u': |
| 195 |
f_accesstime = 1; |
| 196 |
f_statustime = 0; |
| 197 |
break; |
| 198 |
case 'F': |
| 199 |
f_type = 1; |
| 200 |
break; |
| 201 |
case 'L': |
| 202 |
fts_options &= ~FTS_PHYSICAL; |
| 203 |
fts_options |= FTS_LOGICAL; |
| 204 |
break; |
| 205 |
case 'R': |
| 206 |
f_recursive = 1; |
| 207 |
break; |
| 208 |
case 'a': |
| 209 |
fts_options |= FTS_SEEDOT; |
| 210 |
case 'A': |
| 211 |
f_listdot = 1; |
| 212 |
break; |
| 213 |
/* The -d option turns off the -R option. */ |
| 214 |
case 'd': |
| 215 |
f_listdir = 1; |
| 216 |
f_recursive = 0; |
| 217 |
break; |
| 218 |
case 'f': |
| 219 |
f_nosort = 1; |
| 220 |
break; |
| 221 |
case 'g': /* Compatibility with 4.3BSD. */ |
| 222 |
break; |
| 223 |
case 'i': |
| 224 |
f_inode = 1; |
| 225 |
break; |
| 226 |
case 'k': |
| 227 |
blocksize = 1024; |
| 228 |
break; |
| 229 |
case 'o': |
| 230 |
f_flags = 1; |
| 231 |
break; |
| 232 |
case 'p': |
| 233 |
f_typedir = 1; |
| 234 |
break; |
| 235 |
case 'q': |
| 236 |
f_nonprint = 1; |
| 237 |
break; |
| 238 |
case 'r': |
| 239 |
f_reversesort = 1; |
| 240 |
break; |
| 241 |
case 'S': |
| 242 |
sortkey = BY_SIZE; |
| 243 |
break; |
| 244 |
case 's': |
| 245 |
f_size = 1; |
| 246 |
break; |
| 247 |
case 'T': |
| 248 |
f_sectime = 1; |
| 249 |
break; |
| 250 |
case 't': |
| 251 |
sortkey = BY_TIME; |
| 252 |
break; |
| 253 |
case 'W': |
| 254 |
f_whiteout = 1; |
| 255 |
break; |
| 256 |
default: |
| 257 |
return usage (); |
| 258 |
} |
| 259 |
} |
| 260 |
argc -= optind; |
| 261 |
argv += optind; |
| 262 |
|
| 263 |
/* |
| 264 |
* If not -F, -i, -l, -p, -S, -s or -t options, don't require stat |
| 265 |
* information. |
| 266 |
*/ |
| 267 |
if (!f_longform && !f_inode && !f_size && !f_type && !f_typedir && |
| 268 |
sortkey == BY_NAME) |
| 269 |
fts_options |= FTS_NOSTAT; |
| 270 |
|
| 271 |
/* |
| 272 |
* If not -F, -d or -l options, follow any symbolic links listed on |
| 273 |
* the command line. |
| 274 |
*/ |
| 275 |
if (!f_longform && !f_listdir && !f_type) |
| 276 |
fts_options |= FTS_COMFOLLOW; |
| 277 |
|
| 278 |
/* |
| 279 |
* If -W, show whiteout entries |
| 280 |
*/ |
| 281 |
#ifdef FTS_WHITEOUT |
| 282 |
if (f_whiteout) |
| 283 |
fts_options |= FTS_WHITEOUT; |
| 284 |
#endif |
| 285 |
|
| 286 |
/* If -l or -s, figure out block size. */ |
| 287 |
if (f_longform || f_size) |
| 288 |
{ |
| 289 |
blocksize = 1024; /* Fuck this hair-splitting */ |
| 290 |
blocksize /= 512; |
| 291 |
} |
| 292 |
|
| 293 |
/* Select a sort function. */ |
| 294 |
if (f_reversesort) |
| 295 |
{ |
| 296 |
switch (sortkey) |
| 297 |
{ |
| 298 |
case BY_NAME: |
| 299 |
sortfcn = revnamecmp; |
| 300 |
break; |
| 301 |
case BY_SIZE: |
| 302 |
sortfcn = revsizecmp; |
| 303 |
break; |
| 304 |
case BY_TIME: |
| 305 |
if (f_accesstime) |
| 306 |
sortfcn = revacccmp; |
| 307 |
else if (f_statustime) |
| 308 |
sortfcn = revstatcmp; |
| 309 |
else /* Use modification time. */ |
| 310 |
sortfcn = revmodcmp; |
| 311 |
break; |
| 312 |
} |
| 313 |
} |
| 314 |
else |
| 315 |
{ |
| 316 |
switch (sortkey) |
| 317 |
{ |
| 318 |
case BY_NAME: |
| 319 |
sortfcn = namecmp; |
| 320 |
break; |
| 321 |
case BY_SIZE: |
| 322 |
sortfcn = sizecmp; |
| 323 |
break; |
| 324 |
case BY_TIME: |
| 325 |
if (f_accesstime) |
| 326 |
sortfcn = acccmp; |
| 327 |
else if (f_statustime) |
| 328 |
sortfcn = statcmp; |
| 329 |
else /* Use modification time. */ |
| 330 |
sortfcn = modcmp; |
| 331 |
break; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
/* Select a print function. */ |
| 336 |
if (f_singlecol) |
| 337 |
printfcn = printscol; |
| 338 |
else if (f_columnacross) |
| 339 |
printfcn = printacol; |
| 340 |
else if (f_longform) |
| 341 |
printfcn = printlong; |
| 342 |
else if (f_stream) |
| 343 |
printfcn = printstream; |
| 344 |
else |
| 345 |
printfcn = printcol; |
| 346 |
|
| 347 |
if (argc) |
| 348 |
traverse (argc, argv, fts_options); |
| 349 |
else |
| 350 |
traverse (1, dotav, fts_options); |
| 351 |
return (rval); |
| 352 |
} |
| 353 |
|
| 354 |
/* |
| 355 |
* Traverse() walks the logical directory structure specified by the argv list |
| 356 |
* in the order specified by the mastercmp() comparison function. During the |
| 357 |
* traversal it passes linked lists of structures to display() which represent |
| 358 |
* a superset (may be exact set) of the files to be displayed. |
| 359 |
*/ |
| 360 |
static void |
| 361 |
traverse (int argc, char **argv, int options) |
| 362 |
{ |
| 363 |
FTS *ftsp; |
| 364 |
FTSENT *p, *chp; |
| 365 |
int ch_options; |
| 366 |
|
| 367 |
ftsp = fts_open (argv, options, f_nosort ? NULL : mastercmp); |
| 368 |
if (ftsp == NULL) |
| 369 |
{ |
| 370 |
fprintf (stderr, "%s: fts_open: %s", argv[0], strerror (errno)); |
| 371 |
rval = EXIT_FAILURE; |
| 372 |
return; |
| 373 |
} |
| 374 |
|
| 375 |
display (NULL, fts_children (ftsp, 0)); |
| 376 |
if (f_listdir) |
| 377 |
return; |
| 378 |
|
| 379 |
/* |
| 380 |
* If not recursing down this tree and don't need stat info, just get |
| 381 |
* the names. |
| 382 |
*/ |
| 383 |
ch_options = !f_recursive && options & FTS_NOSTAT ? FTS_NAMEONLY : 0; |
| 384 |
|
| 385 |
while ((p = fts_read (ftsp)) != NULL) |
| 386 |
switch (p->fts_info) |
| 387 |
{ |
| 388 |
case FTS_D: |
| 389 |
if (p->fts_name[0] == '.' && |
| 390 |
p->fts_level != FTS_ROOTLEVEL && !f_listdot) |
| 391 |
break; |
| 392 |
|
| 393 |
/* |
| 394 |
* If already output something, put out a newline as |
| 395 |
* a separator. If multiple arguments, precede each |
| 396 |
* directory with its name. |
| 397 |
*/ |
| 398 |
if (output) |
| 399 |
printf ("\n%s:\n", p->fts_path); |
| 400 |
else if (argc > 1) |
| 401 |
{ |
| 402 |
printf ("%s:\n", p->fts_path); |
| 403 |
output = 1; |
| 404 |
} |
| 405 |
|
| 406 |
chp = fts_children (ftsp, ch_options); |
| 407 |
display (p, chp); |
| 408 |
|
| 409 |
if (!f_recursive && chp != NULL) |
| 410 |
fts_set (ftsp, p, FTS_SKIP); |
| 411 |
break; |
| 412 |
case FTS_DC: |
| 413 |
fprintf (stderr, "%s: directory causes a cycle", p->fts_name); |
| 414 |
break; |
| 415 |
case FTS_DNR: |
| 416 |
case FTS_ERR: |
| 417 |
fprintf (stderr, "%s: %s\n", p->fts_name, strerror (p->fts_errno)); |
| 418 |
rval = 1; |
| 419 |
break; |
| 420 |
} |
| 421 |
if (errno) |
| 422 |
{ |
| 423 |
fprintf (stderr, "fts_read: %s", strerror (errno)); |
| 424 |
rval = EXIT_FAILURE; |
| 425 |
return; |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
/* |
| 430 |
* Display() takes a linked list of FTSENT structures and passes the list |
| 431 |
* along with any other necessary information to the print function. P |
| 432 |
* points to the parent directory of the display list. |
| 433 |
*/ |
| 434 |
static void |
| 435 |
display (FTSENT *p, FTSENT *list) |
| 436 |
{ |
| 437 |
struct stat *sp; |
| 438 |
DISPLAY d; |
| 439 |
FTSENT *cur; |
| 440 |
NAMES *np; |
| 441 |
unsigned long long maxsize; |
| 442 |
unsigned long btotal, maxinode, maxlen, maxnlink; |
| 443 |
long maxblock; |
| 444 |
int bcfile, flen, glen, ulen, maxflags, maxgroup, maxuser; |
| 445 |
int entries, needstats; |
| 446 |
char *user = NULL, *group = NULL, buf[INT_BUFSIZE_BOUND (uintmax_t)]; |
| 447 |
char nuser[INT_BUFSIZE_BOUND (uintmax_t)], |
| 448 |
ngroup[INT_BUFSIZE_BOUND (uintmax_t)]; |
| 449 |
char *flags = NULL; |
| 450 |
|
| 451 |
/* |
| 452 |
* If list is NULL there are two possibilities: that the parent |
| 453 |
* directory p has no children, or that fts_children() returned an |
| 454 |
* error. We ignore the error case since it will be replicated |
| 455 |
* on the next call to fts_read() on the post-order visit to the |
| 456 |
* directory p, and will be signalled in traverse(). |
| 457 |
*/ |
| 458 |
if (list == NULL) |
| 459 |
return; |
| 460 |
|
| 461 |
needstats = f_inode || f_longform || f_size; |
| 462 |
flen = 0; |
| 463 |
btotal = maxblock = maxinode = maxlen = maxnlink = 0; |
| 464 |
bcfile = 0; |
| 465 |
maxuser = maxgroup = maxflags = 0; |
| 466 |
maxsize = 0; |
| 467 |
for (cur = list, entries = 0; cur; cur = cur->fts_link) |
| 468 |
{ |
| 469 |
if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) |
| 470 |
{ |
| 471 |
fprintf (stderr, "%s: %s\n", |
| 472 |
cur->fts_name, strerror (cur->fts_errno)); |
| 473 |
cur->fts_number = NO_PRINT; |
| 474 |
rval = 1; |
| 475 |
continue; |
| 476 |
} |
| 477 |
|
| 478 |
/* |
| 479 |
* P is NULL if list is the argv list, to which different rules |
| 480 |
* apply. |
| 481 |
*/ |
| 482 |
if (p == NULL) |
| 483 |
{ |
| 484 |
/* Directories will be displayed later. */ |
| 485 |
if (cur->fts_info == FTS_D && !f_listdir) |
| 486 |
{ |
| 487 |
cur->fts_number = NO_PRINT; |
| 488 |
continue; |
| 489 |
} |
| 490 |
} |
| 491 |
else |
| 492 |
{ |
| 493 |
/* Only display dot file if -a/-A set. */ |
| 494 |
if (cur->fts_name[0] == '.' && !f_listdot) |
| 495 |
{ |
| 496 |
cur->fts_number = NO_PRINT; |
| 497 |
continue; |
| 498 |
} |
| 499 |
} |
| 500 |
if (cur->fts_namelen > maxlen) |
| 501 |
maxlen = cur->fts_namelen; |
| 502 |
if (needstats) |
| 503 |
{ |
| 504 |
sp = cur->fts_statp; |
| 505 |
if (sp->st_blocks > maxblock) |
| 506 |
maxblock = sp->st_blocks; |
| 507 |
if (sp->st_ino > maxinode) |
| 508 |
maxinode = sp->st_ino; |
| 509 |
if (sp->st_nlink > maxnlink) |
| 510 |
maxnlink = sp->st_nlink; |
| 511 |
if (sp->st_size > (off_t) maxsize) |
| 512 |
maxsize = sp->st_size; |
| 513 |
|
| 514 |
btotal += sp->st_blocks; |
| 515 |
if (f_longform) |
| 516 |
{ |
| 517 |
struct passwd *pwd; |
| 518 |
struct group *grp; |
| 519 |
|
| 520 |
user = group = NULL; |
| 521 |
|
| 522 |
if (!f_numericonly) |
| 523 |
{ |
| 524 |
pwd = getpwuid (sp->st_uid); |
| 525 |
if (pwd) |
| 526 |
user = pwd->pw_name; |
| 527 |
grp = getgrgid (sp->st_gid); |
| 528 |
if (grp) |
| 529 |
group = grp->gr_name; |
| 530 |
} |
| 531 |
if (!user) |
| 532 |
user = umaxtostr (sp->st_uid, nuser); |
| 533 |
if (!group) |
| 534 |
group = umaxtostr (sp->st_gid, ngroup); |
| 535 |
|
| 536 |
ulen = strlen (user); |
| 537 |
if (ulen > maxuser) |
| 538 |
maxuser = ulen; |
| 539 |
glen = strlen (group); |
| 540 |
if (glen > maxgroup) |
| 541 |
maxgroup = glen; |
| 542 |
|
| 543 |
if (f_flags) |
| 544 |
{ |
| 545 |
flags = "-"; |
| 546 |
flen = strlen (flags); |
| 547 |
if (flen > maxflags) |
| 548 |
maxflags = flen; |
| 549 |
} |
| 550 |
else |
| 551 |
flen = 0; |
| 552 |
|
| 553 |
np = malloc (sizeof (NAMES) + ulen + glen + flen + 3); |
| 554 |
if (np == NULL) |
| 555 |
{ |
| 556 |
fprintf (stderr, "malloc: %s", strerror (errno)); |
| 557 |
rval = EXIT_FAILURE; |
| 558 |
return; |
| 559 |
} |
| 560 |
|
| 561 |
np->user = &np->data[0]; |
| 562 |
strcpy (np->user, user); |
| 563 |
np->group = &np->data[ulen + 1]; |
| 564 |
strcpy (np->group, group); |
| 565 |
|
| 566 |
if (S_ISCHR (sp->st_mode) || S_ISBLK (sp->st_mode)) |
| 567 |
bcfile = 1; |
| 568 |
|
| 569 |
if (f_flags) |
| 570 |
{ |
| 571 |
np->flags = &np->data[ulen + glen + 2]; |
| 572 |
strcpy (np->flags, flags); |
| 573 |
} |
| 574 |
cur->fts_pointer = np; |
| 575 |
} |
| 576 |
} |
| 577 |
++entries; |
| 578 |
} |
| 579 |
|
| 580 |
if (!entries) |
| 581 |
return; |
| 582 |
|
| 583 |
d.list = list; |
| 584 |
d.entries = entries; |
| 585 |
d.maxlen = maxlen; |
| 586 |
if (needstats) |
| 587 |
{ |
| 588 |
d.bcfile = bcfile; |
| 589 |
d.btotal = btotal; |
| 590 |
d.s_block = strlen (umaxtostr (maxblock, buf)); |
| 591 |
d.s_flags = maxflags; |
| 592 |
d.s_group = maxgroup; |
| 593 |
d.s_inode = strlen (umaxtostr (maxinode, buf)); |
| 594 |
d.s_nlink = strlen (umaxtostr (maxnlink, buf)); |
| 595 |
d.s_size = strlen (umaxtostr (maxsize, buf)); |
| 596 |
d.s_user = maxuser; |
| 597 |
} |
| 598 |
|
| 599 |
printfcn (&d); |
| 600 |
output = 1; |
| 601 |
|
| 602 |
if (f_longform) |
| 603 |
for (cur = list; cur; cur = cur->fts_link) |
| 604 |
free (cur->fts_pointer); |
| 605 |
} |
| 606 |
|
| 607 |
/* |
| 608 |
* Ordering for mastercmp: |
| 609 |
* If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories |
| 610 |
* as larger than directories. Within either group, use the sort function. |
| 611 |
* All other levels use the sort function. Error entries remain unsorted. |
| 612 |
*/ |
| 613 |
static int |
| 614 |
mastercmp (const FTSENT **a, const FTSENT **b) |
| 615 |
{ |
| 616 |
int a_info, b_info; |
| 617 |
|
| 618 |
a_info = (*a)->fts_info; |
| 619 |
if (a_info == FTS_ERR) |
| 620 |
return (0); |
| 621 |
b_info = (*b)->fts_info; |
| 622 |
if (b_info == FTS_ERR) |
| 623 |
return (0); |
| 624 |
|
| 625 |
if (a_info == FTS_NS || b_info == FTS_NS) |
| 626 |
{ |
| 627 |
if (b_info != FTS_NS) |
| 628 |
return (1); |
| 629 |
else if (a_info != FTS_NS) |
| 630 |
return (-1); |
| 631 |
else |
| 632 |
return (namecmp (*a, *b)); |
| 633 |
} |
| 634 |
|
| 635 |
if (a_info != b_info && (*a)->fts_level == FTS_ROOTLEVEL && !f_listdir) |
| 636 |
{ |
| 637 |
if (a_info == FTS_D) |
| 638 |
return (1); |
| 639 |
if (b_info == FTS_D) |
| 640 |
return (-1); |
| 641 |
} |
| 642 |
return (sortfcn (*a, *b)); |
| 643 |
} |