1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
|
/* listfile.c -- display a long listing of a file
Copyright (C) 1991-2021 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/* config.h must be included first. */
#include <config.h>
/* system headers. */
#include <alloca.h>
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h> /* for readlink() */
/* gnulib headers. */
#include "areadlink.h"
#include "error.h"
#include "filemode.h"
#include "human.h"
#include "mbswidth.h"
#include "idcache.h"
#include "pathmax.h"
#include "stat-size.h"
/* find headers. */
#include "system.h"
#include "die.h"
#include "listfile.h"
/* Since major is a function on SVR4, we can't use `ifndef major'. */
#ifdef MAJOR_IN_MKDEV
# include <sys/mkdev.h>
#else
# ifdef MAJOR_IN_SYSMACROS
# include <sys/sysmacros.h>
# else
# ifndef major /* Might be defined in sys/types.h. */
# define major(dev) (((dev) >> 8) & 0xff)
# define minor(dev) ((dev) & 0xff)
# endif
# endif
#endif
static bool print_name (register const char *p, FILE *stream, int literal_control_chars);
/* We have some minimum field sizes, though we try to widen these fields on systems
* where we discover examples where the field width we started with is not enough. */
static int inode_number_width = 9;
static int block_size_width = 6;
static int nlink_width = 3;
static int owner_width = 8;
static int group_width = 8;
/* We don't print st_author even if the system has it. */
static int major_device_number_width = 3;
static int minor_device_number_width = 3;
static int file_size_width = 8;
static bool print_num(FILE *stream, unsigned long num, int *width)
{
const int chars_out = fprintf (stream, "%*lu", *width, num);
if (chars_out >= 0)
{
if (*width < chars_out)
*width = chars_out;
return true;
}
return false;
}
/* NAME is the name to print.
RELNAME is the path to access it from the current directory.
STATP is the results of stat or lstat on it.
Use CURRENT_TIME to decide whether to print yyyy or hh:mm.
Use OUTPUT_BLOCK_SIZE to determine how to print file block counts
and sizes.
STREAM is the stdio stream to print on. */
void
list_file (const char *name,
int dir_fd,
const char *relname,
const struct stat *statp,
time_t current_time,
int output_block_size,
int literal_control_chars,
FILE *stream)
{
char modebuf[12];
struct tm const *when_local;
char const *user_name;
char const *group_name;
char hbuf[LONGEST_HUMAN_READABLE + 1];
bool output_good = true;
int chars_out;
int failed_at = 000;
#if HAVE_ST_DM_MODE
/* Cray DMF: look at the file's migrated, not real, status */
strmode (statp->st_dm_mode, modebuf);
#else
strmode (statp->st_mode, modebuf);
#endif
chars_out = fprintf (stream, "%*s", inode_number_width,
human_readable ((uintmax_t) statp->st_ino, hbuf,
human_ceiling,
1u, 1u));
if (chars_out < 0)
{
output_good = false;
failed_at = 100;
}
else if (chars_out > inode_number_width)
{
inode_number_width = chars_out;
}
if (output_good)
{
if (EOF == putc(' ', stream))
{
output_good = false;
failed_at = 150;
}
chars_out = fprintf (stream, "%*s",
block_size_width,
human_readable ((uintmax_t) ST_NBLOCKS (*statp), hbuf,
human_ceiling,
ST_NBLOCKSIZE, output_block_size));
if (chars_out < 0)
{
output_good = false;
failed_at = 200;
}
else
{
if (chars_out > block_size_width)
block_size_width = chars_out;
}
}
if (output_good)
{
if (EOF == putc(' ', stream))
{
output_good = false;
failed_at = 250;
}
}
if (output_good)
{
/* modebuf includes the space between the mode and the number of links,
as the POSIX "optional alternate access method flag". */
if (fputs (modebuf, stream) < 0)
{
output_good = false;
failed_at = 275;
}
}
if (output_good)
{
/* This format used to end in a space, but the output of "ls"
has only one space between the link count and the owner name,
so we removed the trailing space. Happily this also makes it
easier to update nlink_width. */
chars_out = fprintf (stream, "%*lu",
nlink_width, (unsigned long) statp->st_nlink);
if (chars_out < 0)
{
output_good = false;
failed_at = 300;
}
else
{
if (chars_out > nlink_width)
nlink_width = chars_out;
}
}
if (output_good)
{
if (EOF == putc(' ', stream))
{
output_good = false;
failed_at = 250;
}
user_name = getuser (statp->st_uid);
if (user_name)
{
int len = mbswidth (user_name, 0);
if (len > owner_width)
owner_width = len;
output_good = (fprintf (stream, "%-*s ", owner_width, user_name) >= 0);
if (!output_good)
failed_at = 400;
}
else
{
chars_out = fprintf (stream, "%-8lu ", (unsigned long) statp->st_uid);
if (chars_out > owner_width)
owner_width = chars_out;
output_good = (chars_out > 0);
if (!output_good)
failed_at = 450;
}
}
if (output_good)
{
group_name = getgroup (statp->st_gid);
if (group_name)
{
int len = mbswidth (group_name, 0);
if (len > group_width)
group_width = len;
output_good = (fprintf (stream, "%-*s ", group_width, group_name) >= 0);
if (!output_good)
failed_at = 500;
}
else
{
chars_out = fprintf (stream, "%-*lu",
group_width, (unsigned long) statp->st_gid);
if (chars_out > group_width)
group_width = chars_out;
output_good = (chars_out >= 0);
if (output_good)
{
if (EOF == putc(' ', stream))
{
output_good = false;
failed_at = 525;
}
}
else
{
if (!output_good)
failed_at = 550;
}
}
}
if (output_good)
{
if (S_ISCHR (statp->st_mode) || S_ISBLK (statp->st_mode))
{
#ifdef HAVE_STRUCT_STAT_ST_RDEV
if (!print_num (stream,
(unsigned long) major (statp->st_rdev),
&major_device_number_width))
{
output_good = false;
failed_at = 600;
}
if (output_good)
{
if (fprintf (stream, ", ") < 0)
{
output_good = false;
failed_at = 625;
}
}
if (output_good)
{
if (!print_num (stream,
(unsigned long) minor (statp->st_rdev),
&minor_device_number_width))
{
output_good = false;
failed_at = 650;
}
}
#else
if (fprintf (stream, "%*s %*s",
major_device_number_width,
minor_device_number_width) < 0)
{
output_good = false;
failed_at = 700;
}
#endif
}
else
{
const int blocksize = output_block_size < 0 ? output_block_size : 1;
chars_out = fprintf (stream, "%*s",
file_size_width,
human_readable ((uintmax_t) statp->st_size, hbuf,
human_ceiling,
1, blocksize));
if (chars_out < 0)
{
output_good = false;
failed_at = 800;
}
else
{
if (chars_out > file_size_width)
{
file_size_width = chars_out;
}
}
}
}
if (output_good)
{
if (EOF == putc(' ', stream))
{
output_good = false;
failed_at = 850;
}
}
if (output_good)
{
if ((when_local = localtime (&statp->st_mtime)))
{
char init_bigbuf[256];
char *buf = init_bigbuf;
size_t bufsize = sizeof init_bigbuf;
/* Use strftime rather than ctime, because the former can produce
locale-dependent names for the month (%b).
Output the year if the file is fairly old or in the future.
POSIX says the cutoff is 6 months old;
approximate this by 6*30 days.
Allow a 1 hour slop factor for what is considered "the future",
to allow for NFS server/client clock disagreement. */
char const *fmt =
((current_time - 6 * 30 * 24 * 60 * 60 <= statp->st_mtime
&& statp->st_mtime <= current_time + 60 * 60)
? "%b %e %H:%M"
: "%b %e %Y");
while (!strftime (buf, bufsize, fmt, when_local))
buf = alloca (bufsize *= 2);
if (fprintf (stream, "%s ", buf) < 0)
{
output_good = false;
failed_at = 900;
}
}
else
{
/* The time cannot be represented as a local time;
print it as a huge integer number of seconds. */
int width = 12;
if (statp->st_mtime < 0)
{
char const *num = human_readable (- (uintmax_t) statp->st_mtime,
hbuf, human_ceiling, 1, 1);
int sign_width = width - strlen (num);
if (fprintf (stream, "%*s%s ",
sign_width < 0 ? 0 : sign_width, "-", num) < 0)
{
output_good = false;
failed_at = 1000;
}
}
else
{
if (fprintf (stream, "%*s ", width,
human_readable ((uintmax_t) statp->st_mtime, hbuf,
human_ceiling,
1, 1)) < 0)
{
output_good = false;
failed_at = 1100;
}
}
}
}
if (output_good)
{
output_good = print_name (name, stream, literal_control_chars);
if (!output_good)
{
failed_at = 1200;
}
}
if (output_good)
{
if (S_ISLNK (statp->st_mode))
{
char *linkname = areadlinkat (dir_fd, relname);
if (linkname)
{
if (fputs (" -> ", stream) < 0)
{
output_good = false;
failed_at = 1300;
}
if (output_good)
{
output_good = print_name (linkname, stream, literal_control_chars);
if (!output_good)
{
failed_at = 1350;
}
}
}
else
{
/* POSIX requires in the case of find that if we issue a
* diagnostic we should have a nonzero status. However,
* this function doesn't have a way of telling the caller to
* do that. However, since this function is only used when
* processing "-ls", we're already using an extension.
*/
error (0, errno, "%s", name);
}
free (linkname);
}
if (output_good)
{
if (EOF == putc ('\n', stream))
{
output_good = false;
if (!output_good)
{
failed_at = 1400;
}
}
}
}
if (!output_good)
{
die (EXIT_FAILURE, errno, _("Failed to write output (at stage %d)"), failed_at);
}
}
static bool
print_name_without_quoting (const char *p, FILE *stream)
{
return (fprintf (stream, "%s", p) >= 0);
}
static bool
print_name_with_quoting (register const char *p, FILE *stream)
{
register unsigned char c;
while ((c = *p++) != '\0')
{
int fprintf_result = -1;
switch (c)
{
case '\\':
fprintf_result = fprintf (stream, "\\\\");
break;
case '\n':
fprintf_result = fprintf (stream, "\\n");
break;
case '\b':
fprintf_result = fprintf (stream, "\\b");
break;
case '\r':
fprintf_result = fprintf (stream, "\\r");
break;
case '\t':
fprintf_result = fprintf (stream, "\\t");
break;
case '\f':
fprintf_result = fprintf (stream, "\\f");
break;
case ' ':
fprintf_result = fprintf (stream, "\\ ");
break;
case '"':
fprintf_result = fprintf (stream, "\\\"");
break;
default:
if (c > 040 && c < 0177)
{
if (EOF == putc (c, stream))
return false;
fprintf_result = 1; /* otherwise it's used uninitialized. */
}
else
{
fprintf_result = fprintf (stream, "\\%03o", (unsigned int) c);
}
}
if (fprintf_result < 0)
return false;
}
return true;
}
static bool print_name (register const char *p, FILE *stream, int literal_control_chars)
{
if (literal_control_chars)
return print_name_without_quoting (p, stream);
else
return print_name_with_quoting (p, stream);
}
|