Commit Graph
9123 Commits
Author SHA1 Message Date
Paul Eggert 976f8abc19 tail: record file offset more carefully
* src/tail.c (struct File_spec): New member read_pos, replacing
size, since the value was really a read position not a size.
All uses changed.
(xlseek): Move defn up.
(record_open_fd): If the read_pos (formerly) size arg is unknown,
compute it here if it is a regular file.
(file_lines): Return the resulting read pos (or -1 on failure)
instead of storing it via a pointer.  Caller changed.
Simplify by using SEEK_CUR instead of SEEK_SET when that is easy.
Avoid reading the same data twice when there are not enough
lines in the file.
(pipe_lines): Return -2 on success, -1 on failure, rather than
updating a read pos via a pointer (which was weird for pipes anyway).
Caller changed.
(pipe_bytes, tail_bytes, tail_lines, tail):
Return -1 on failure, a file offset if known, and < -1 otherwise,
instead of storing a file offset via a pointer.  Caller changed.
(pipe_bytes): Take initial file offset as an arg, or -1 if unknown.
(start_bytes, start_lines): Return -1 (not 1) on error, -2 (not
-1) on EOF, and do not accept pointer to read pos as an arg since
neither we nor our caller know the read pos.  Callers changed.
(recheck): Do not assume a newly-opened file is at offset zero,
as this is not always true on Solaris.
(tail_forever, check_fspec): Use dump_remainder result only
on regular files, to prevent (very unlikely) overflow.
(tail_file): Remove no-longer-needed TAIL_TEST_SLEEP code.
2025-08-03 19:48:06 -07:00
Paul Eggert d3c7072a09 maint: prefer 'read' to 'safe_read'
In the old days, safe_read acted more like what full_read does now.
When that went away, some code that invoked safe_read should
have gone back to plain 'read' but I guess we never got around to it.
Simplify this code by going back to plain 'read'.
Use safe_read only in csplit.c, which has a signal handler
and where 'read' can therefore fail with EINTR.
Although safe_read also checks for oversize buffers,
that is better done via io_blksize.
* src/cat.c (simple_cat, cat):
* src/head.c (copy_fd, elide_tail_lines_pipe)
* src/tac.c (tac_seekable, copy_to_temp):
(elide_tail_lines_seekable, head_lines):
* src/tail.c (dump_remainder, file_lines, pipe_lines)
(pipe_bytes, start_bytes, start_lines, tail_forever_inotify):
* src/tr.c (plain_read):
Use plain 'read', not safe_read, since there is no
need to worry about signals or oversize requests.
Also, there is no longer a need to include safe-read.h.
* src/ioblksize.h: Include sys-limits.h, for SYS_BUFSIZE_MAX.
(io_blksize): Max out at SYS_BUFSIZE_MAX.
2025-08-03 19:48:05 -07:00
Paul Eggert 3820447667 tail: prefer signed types to size_t, blksize_t
* src/tail.c (struct File_spec, xwrite_stdout, file_lines)
(pipe_lines, pipe_bytes, start_bytes, any_live_files)
(tail_forever, any_remote_file, any_non_remote_file)
(any_symlinks, any_non_regular_fifo, tailable_stdin)
(tail_forever_inotify, ignore_fifo_and_pipe, main):
Prefer a signed type to size_t, if possible.
Ordinarily this is idx_t, but use int when the value
must fit in int anyway.
(file_lines): Similarly for blksize_t, which had no business
being here anyway.
(main): Check for overflow in the oddball case where ptrdiff_t is
narrower than int.
2025-08-03 19:48:05 -07:00
Paul Eggert 0625370b7a tail: prefer intmax_t to uintmax_t
Signed types let us debug better, by using -fsanitize=undefined.
* doc/local.mk (doc/constants.texi):
Adjust change from macro to enum.
* src/tail.c (COPY_TO_EOF, COPY_A_BUFFER)
(DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS):
Now enum constants, not macros.
(COPY_TO_EOF, COPY_A_BUFFER): Now negative, not positive.
(count_t): New typedef.  Use it instead of uintmax_t.
(COUNT_MAX): New macro; use it instead of UINTMAX_MAX.
(struct File_spec, max_n_unchanged_stats_between_opens)
(dump_remainder, file_lines, pipe_lines, pipe_bytes)
(start_bytes, start_lines, tail_forever, check_fspec)
(tail_forever_inotify, tail_bytes, tail_lines, tail, tail_file)
(parse_obsolete_option, parse_options, main):
Prefer count_t to uintmax_t.
2025-08-03 19:48:05 -07:00
Paul Eggert fee8859750 tail: don’t output more lines than requested
* src/tail.c (file_lines): Fix an unlikely bug where ‘tail -n N’
could output more than N lines if standard input is a largish
regular file with large initial offset that starts with (say) N-1
lines after the initial offset, but grows to N+1 lines between the
fstat and read calls.  In this case ‘tail -n N’ now outputs N-1
lines, not N+1; that is, it pretends the file grew after ‘tail’
read it.  That is better than outputting more than N lines.
2025-08-03 19:48:05 -07:00
Paul Eggert eb0086669f tail: xlseek switch → table
* src/tail.c (xlseek): Turn a switch statement into a table.
2025-08-03 19:48:05 -07:00
Paul Eggert d584545299 tail: prettyname cleanup
* src/tail.c: Use ‘prettyname’ consistently as the identifier
for a prettified file name, as opposed to ‘pretty_filename’,
‘pretty_name’, and ‘name’.  This makes the code easier to follow.
(struct File_spec): New member prettyname.
(pretty_name): Remove.
All uses of pretty_name (f) replaced by f->prettyname.
(close_fd, fremote): Accept struct File_spec, not name.
All callers changed.
(main): Initialize the new prettyname member.
This is simpler/smaller than calling pretty_name everywhere.
2025-08-03 19:48:05 -07:00
Paul Eggert 10aa908ebd tail: optimize tail -n +2**63
* src/tail.c (tail_lines): Also optimize ‘tail -n +N’
on a seekable file, where OFF_T_MAX <= N < UINTMAX_MAX.
Of course this is very unlikely.
2025-08-03 19:48:05 -07:00
Paul Eggert 3918655d4b tail: refactor SEEK_END and lines
* src/tail.c (tail_lines): Refactor to simplify the confusing
code for using SEEK_END when counting lines.  The old code
had a ‘end_pos != 0’ expression that was always true.
2025-08-03 19:48:05 -07:00
Paul Eggert 09fc945b37 tail: refactor to skip stat call on failure
* src/tail.c (tail_bytes): New function.
(tail_bytes, tail_lines, tail): Accept struct stat pointer from
caller instead of calling fstat ourselves.  All callers changed.
(tail_file): Skip a call to fstat if fstat already failed.
* tests/tail/follow-stdin.sh: Adjust to match new behavior
on failure, which omits a redundant diagnostic.
2025-08-03 19:48:05 -07:00
Paul Eggert d58aa27d06 tail: speed up -c N for huge N
When the user specifies -c N where 2**63 <= N, don’t give
up and use the slow method (which will exhaust memory if
the file is large).  Instead, treat it as N = 2**63 - 1,
since that has equivalent effect.
* src/tail.c (tail_bytes): With -c N and large N, adjust
the code so that lseeks can still be used without
affecting correctness.  Formerly the code gave up and
did a sequential pass through the whole input, which
could easily exhaust memory.
2025-08-03 19:48:05 -07:00
Paul Eggert 6439858056 tail: allow >=2**64 in traditional form
This better matches the treatment of POSIX form,
e.g., ‘tail +Nc’ is now like ‘tail -c +N’ even when N is large.
* src/tail.c: Don’t include xstrtol.h.
(parse_obsolete_option): Treat numbers greater than UINTMAX_MAX as
if they are UINTMAX_MAX.  Parse the number by hand with saturating
arithmetic; nowadays that’s simpler than using xstrtoumax.  There
is no need for a diagnostic now, as the error cannot happen any more.
* tests/tail/tail.pl (obs-plus-c3): New test.
2025-08-03 19:48:04 -07:00
Paul Eggert ae37265727 tail: check OFF_T_MAX vs COPY_A_BUFFER
* src/tail.c: Document an otherwise-unstated assumption.
2025-08-03 19:48:04 -07:00
Paul Eggert 468f821150 tail: fix SEEK_END typo
* src/tail.c (tail_lines): Fix embarrassing SEEK_END typo.
Luckily this matters only in never-used (though valid) invocations.
2025-08-03 19:48:04 -07:00
Paul Eggert c667ca229f tail: simplify -c+ on regular files
* src/tail.c (tail_bytes): Simplify the -c+N case by treating
regular files like other files; if the lseek fails for whatever
reason, fall back on the unoptimized version instead of reporting
a fatal error.
2025-08-03 19:48:04 -07:00
Paul Eggert 938035b9b4 maint: don’t lseek memory objects
This affects behavior only on memory objects, which are not in Linux.
Formerly the code would use lseek on these objects,
but POSIX says the result of lseek is unspecified on them,
and in QNX lseek has undefined behavior on typed memory objects.
* src/head.c (elide_tail_bytes_file, elide_tail_lines_file):
Omit unnecessary uses of presume_input_pipe.
Improve some out-of-date comments.
(head): Do not assume a file is seekable merely because its
st_size is usable.  Instead, seek only on regular files.
* src/od.c (skip): Do not seek on memory objects.
2025-08-03 19:48:04 -07:00
Paul Eggert dfe4df5694 wc: don’t assume opening a file puts you at start
This is not true on Solaris when opening /dev/stdin; it could be
at a nonzero file offset.  Arguably Linux should do likewise.
* src/wc.c (wc): Omit last arg, and act as if it is always -1.
All uses changed.
2025-08-03 19:48:04 -07:00
Paul Eggert c9e2dd531d head: simplify head_bytes
* src/head.c (diagnose_read_failure): New function.
Use it when possible, to simplify callers.
(head_bytes): Use copy_fd and diagnose_copy_fd_failure instead of
reinventing the wheel.
2025-08-03 19:48:04 -07:00
Paul Eggert ca30c06e4c head: omit unnecessary lseek
* src/head.c (elseek_diagnostic): New function.
(elseek): Use it.
(head_lines): Use it to avoid an unnecessary lseek.
2025-08-03 19:48:04 -07:00
Collin Funk 2ff3c65e94 readlink: improve documentation for POSIXLY_CORRECT in --help
* NEWS: Improve wording to not imply POSIXLY_CORRECT=1 readlink -q will
be verbose.
* src/readlink.c (usage): Mention the affect of POSIXLY_CORRECT on
-s (--silent), -q (--quiet), and -v (--verbose) in the help message.
(main): Remove spurious newline added by previous commit.
* doc/coreutils.texi (readlink invocation): Mention that -s (--silent)
and -q (--quiet) are not the default when POSIXLY_CORRECT is set.
2025-08-03 12:33:55 -07:00
Collin Funk ecd803805d readlink: emit errors when POSIXLY_CORRECT is set
* src/readlink.c (main): Set verbose if the POSIXLY_CORRECT environment
variable is set.
* tests/readlink/readlink-posix.sh: New file.
* tests/local.mk (all_tests): Add it.
* NEWS: Mention the change.
* doc/coreutils.texi (readlink invocation): Document the behavior of
POSIXLY_CORRECT.
2025-08-03 10:55:54 -07:00
Bruno Haible 0142856e7b maint: date: refactor to be entirely bottom-up
* src/date.c (show_date_helper): Move function. Remove forward
declaration.
2025-08-01 13:31:28 +01:00
Bruno Haible 4d03487bbb date: fix calendar-related test failures on macOS and OpenBSD
* bootstrap.conf (gnulib_modules): Add xsetenv.
* src/date.c: Include xsetenv.h.
(set_LC_TIME): New function.
(show_date_helper): Call set_LC_TIME instead of setlocale.
* po/POTFILES.in: Add lib/xsetenv.c.
Fixes https://bugs.gnu.org/79118
2025-07-31 21:54:49 -07:00
Collin Funk 6c2117ab80 build: fix previous commit
* src/local.mk (src/speedlist.h): Pass correct arguments $(COMPILE). Use
tabs consistently.
2025-07-31 17:57:59 -07:00
Collin Funk 72d483dab7 build: don't use '$<' in non-suffix rules
* src/local.mk (src/speedlist.h): Use the file name instead of '$<'
which is not supported by OpenBSD make.
2025-07-30 16:33:24 -07:00
Pádraig Brady a7c63296c1 du: improve diagnostics for --time-style
* src/system.h (x_timestyle_args): A new function refactored from ...
* src/ls.c (decode_switches): ... here.
* src/du.c: Use refactored x_timestyle_args() to output a custom error.
Addresses https://bugs.gnu.org/79113
2025-07-29 11:43:09 +01:00
Pádraig Brady 69f21c5d46 doc: use consistent references to standard files
* cfg.mk (sc_standard_outputs): A new syntax check to
enforce standard references.
* doc/coreutils.texi: s/stderr/standard error/ etc.
* src/date.c: Likewise.
* src/dd.c: Likewise.
* src/env.c: Likewise.
* src/sort.c: Likewise.
* src/split.c: Likewise.
* src/stty.c: Likewise.
* src/timeout.c: Likewise.
* src/who.c: Likewise.
2025-07-29 11:43:09 +01:00
Paul Eggert 4a2402dcfa doc: mention 2025 speedup on large composites 2025-07-27 07:30:49 -07:00
Collin Funk ba8e55ea95 maint: avoid syntax-check failure from previous commit
* src/test.c (binary_operator): Place string argument on a separate line
to avoid sc_error_quotes syntax-check failure.
2025-07-26 00:17:16 -07:00
Paul Eggert 3c40470c5d test: simplify for clarity
This should help avoid further audit confusion,
such as was just fixed by removing a FIXME.
* src/test.c (enum binop): New type.
(get_mtime): Return a struct timespec instead of returning
a bool and storing a struct timespec.  All callers changed.
(binop): Return an int recording either success (an enum binop)
or failure (-1).  All callers changed.
(binary_operator): Accept an enum binop, so that we need
not recompute the op type.  All callers changed.  Simplify.
2025-07-25 12:27:39 -07:00
Harry Fellowes ecde78e1df test: removed dead code for unrecognised binary operators
the fallback error in binary_operator() was unreachable, since invalid
binary operators are rejected in three_arguments() via binop() which
returns false and prevents entry into binary_operator().

this dead code was unreachable and safe to remove

ref: line 636 where "binary operator expected" is generated.

Signed-off-by: Harry Fellowes <harryfellowes1@gmail.com>
Copyright-paperwork-exempt: Yes
2025-07-25 12:27:39 -07:00
Bruno Haible c3cea90de3 date: force a Gregorian calendar for options --iso-8601 and --rfc-3339
* src/date.c (show_date_helper): Add a use_c_locale parameter.
(batch_convert): Add a format_in_c_locale parameter.
(main): Set format_in_c_locale to true if any of the options --rfc-3339,
--iso-8601, -R is seen.
2025-07-21 21:16:03 +01:00
Paul Eggert 1dda42fa58 factor: remove SINGLE_WORKS
* src/factor.c (SINGLE_WORKS): Remove, and remove its use,
as the buggy code that it is referring to has been removed.
2025-07-09 22:27:29 -07:00
Paul Eggert 5b2ddab2d3 factor: use 64-bit internal counters
* src/factor.c (factor_using_pollard_rho)
(factor_using_pollard_rho2, mp_factor_using_pollard_rho):
Use int_fast64_t for internal counters rather than int, as int
could overflow on some somewhat-practical examples.  Problem
discovered on a hypothetical platform where W_TYPE_SIZE is neither
32 nor 64, when factoring 0x7ffffffffffffeab7fffffffffff7369 ==
170141183460469225450570946617781744489, causing k to overflow in
mp_factor_using_pollard_rho.  Presumably a similar problem exists
in the previous stable coreutils 9.7, too, on 32-bit platforms
with somewhat-larger test cases, though I haven’t take the
somewhat-extensive CPU time to discover it.
2025-07-09 22:01:42 -07:00
Paul Eggert 4171ca3353 factor: fix mp_factor_using_pollard_rho aliasing
* src/factor.c (mp_factor_using_pollard_rho):
Fix recently-introduced aliasing bug by computing q
before g gets updated in place.  Problem discovered
on a hypothetical platform where W_TYPE_SIZE
is neither 32 nor 64.
2025-07-09 22:01:42 -07:00
Paul Eggert bbca1c106c factor: speed up converting strings to uuint
* src/factor.c: Do not include c-ctype.h.
(strtouuint): Don’t bother generating a number on
error; just return a strtol_error value other than LONGINT_OK.
Speed up overflow checking.
2025-07-09 17:12:40 -07:00
Paul Eggert 035b837146 factor: simplify primes table
* src/factor.c (primes_ptab): New table of primes, replacing
primes_diff and primes_diff8.  All uses changed.  This is simpler
and should improve performance slightly.  Although this limits the
table’s primes to 2**15 instead of to 668221, the limit can easily
grow to 2**32 by changing the type of ‘prime’, without hurting
performance significantly compared to the primes_diff and
primes_diff8 approach.
* src/make-prime-list.c (output_primes):
For each prime p, output p instead of two differences.
2025-07-09 17:12:40 -07:00
Paul Eggert b754d14117 factor: prefer false for boolean 0
* src/factor.c: Prefer false to 0 for booleans.
2025-07-09 17:12:40 -07:00
Paul Eggert d658d121c3 maint: port speedgen to FreeBSD 15
* src/speedgen: Avoid ‘trap 'command' ERR’, as ERR
does not conform to POSIX and does not work in
FreeBSD 15 sh.
2025-07-09 17:12:40 -07:00
Paul Eggert ef456bef37 factor: speed up Pollard-rho loop counters
* src/factor.c (factor_using_pollard_rho)
(factor_using_pollard_rho2, mp_factor_using_pollard_rho):
Use int, not unsigned long int, for counters that won’t go above
2**31 on practical platforms.  This yields a significant speedup
on GCC 15 x86-64, and using signed values allows for automatic
checks for overflow when using gcc -fsanitize=undefined.
2025-07-09 17:12:40 -07:00
Paul Eggert 033a50af8b factor: avoid an mpz init+clear
* src/factor.c (mp_factor_insert_ui): Rename fom
mp_factor_insert_ui, and change arg type from unsigned long int to
mp_limb_t.  All uses changed.  This avoids creating and freeing a
small mpz_t.
(mp_factor_using_division): Add a static assert requiring that
that mp_limb_t be wide enough, which it should be (and is in
standard GMP).
2025-07-09 17:12:40 -07:00
Paul Eggert 2ba4216441 factor: Pollard-rho a is now mp_limb_t
* src/factor.c (factor_using_pollard_rho)
(factor_using_pollard_rho2): Use mp_limb_t, not unsigned long int,
for a parameter.  This avoids some casts, and avoids a theoretical
bug where converting to mp_limb_t loses info.
2025-07-09 17:12:40 -07:00
Paul Eggert d4cff38776 factor: redo ge2 in terms of lt2
lt2 a bit more natural, given the current implementation.
* src/factor.c (lt2): New function.
(ge2): Rewrite in terms of lt2.
(gt2): Remove.  All callers changed to use lt2.
2025-07-09 17:12:40 -07:00
Paul Eggert d70e07668c factor: speed up umul_ppmm when !USE_LONGLONG_H
* src/factor.c (umul_ppmm): When !USE_LONGLONG_H so we need to
define this, speed things up if there is an unsigned type uuroom_t
wide enough to hold two words.  Do not make a similar change for
udiv_qrnnd, as it is not performance critical and anyway on GCC 15
x86-64 that would mean a subroutine call.
2025-07-09 17:12:40 -07:00
Paul Eggert e16d3f38f3 factor: streamline gcd2_odd
* src/factor.c (gcd2_odd): Omit a gt2 and adjusts other
tests accordingly.  Omit duplicate code that makes A odd.
2025-07-09 17:12:40 -07:00
Paul Eggert 37fbbc5373 factor: faster gcd_odd since 2nd is odd
* src/factor.c (gcd_odd, gcd2_odd): Speed up, given that the
second argument is always odd.
(gcd_odd): Avoid recomputing a temporary.
(gcd2_odd): Test for zero only if a multiple of B.
This saves an ‘assume’.
2025-07-09 17:12:40 -07:00
Paul Eggert 2ce3fc0c1b doc: update ‘factor’ benchmarks 2025-07-09 17:12:40 -07:00
Paul Eggert 3ee25abfd3 factor: prefer exact division
* src/factor.c (mp_factor_using_division, mp_factor_using_pollard_rho):
Use exact division when it suffices, as it’s typically faster.
2025-07-09 17:12:40 -07:00
Paul Eggert 73bb62f7df factor: tune submod2
* src/factor.c (submod2): Use ckd_sub to subtract by hand rather
than using sub_ddmmss plus a compare.  This speeds things up a
bit, on x86-64 with GCC 15 anyway.
2025-07-09 17:12:40 -07:00
Paul Eggert b0c58a8b29 factor: DELIM and DIVBLOCK no longer macros
* src/factor.c (DELIM): Now a constant, not a macro.
(divblock): Now a static inline function, not a macro DIVBLOCK.
All uses changed.
2025-07-09 17:12:40 -07:00