Commit Graph

143 Commits

Author SHA1 Message Date
Karel Zak cd969e8908 more: fix null-pointer dereference
The command allows executing arbitrary shell commands while viewing a file by
entering '!' followed by the command. Entering a command that contains a '%',
'!', or '\' causes a segmentation violation.

The same more(1) function has a problem when not file is specified (cat
/etc/passwd | more) on command line.

Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1975153
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-07-20 11:50:52 +02:00
Karel Zak 86328e78ea include/c: add drop_permissions(), consolidate UID/GID reset
Fixes: https://github.com/karelzak/util-linux/issues/1354
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-07-20 11:31:43 +02:00
Karel Zak 42a9c18706 more: add __format__ attribute
Reported-by: Jan Pazdziora <jpazdziora@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-07-20 11:30:00 +02:00
Karel Zak 37bb1b07e0 more: fix setuid/setgid order
The rule is pretty simple, always use setgid() before setuid().

Reported-by: Jan Pazdziora <jpazdziora@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-07-20 11:23:12 +02:00
Sami Kerola ec4153cc28
more: fix floating point exception core dump
Make the code avoid divided by zero.  This can happen when file has content
but is zero in size.  Such files can be found from procfs, possibly some
other pseudo-filesystems.  To reproduce the issue run the following.

    $ more /proc/crypto
    ...
    Floating point exception (core dumped)

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2021-05-19 20:23:48 +01:00
Hannes Müller e899168a1d more: fix ARROW_DOWN and PAGE_DOWN behaviour to not skip lines
Currently ARROW_DOWN and PAGE_DOWN is bound to command 's' (skip lines).
But this behaviour is not what the user expects, since at least one line
is always missing! Furthermore at the end of the file there is typically
this skipping lines message, if the remainder does not fit in a
complete screen. This conflicts also with e.g. less and man PAGE_DOWN
behaviour. The 'natural' behaviour is binding to
more_kc_jump_lines_per_screen.
If more is extended to also jump only a specific number of lines forward,
this would be even a better binding for ARROW_DOWN.
2021-03-17 20:42:11 +01:00
Hannes Müller 9f303a2bf4 more: fix command 'f' (screen forward) behaviour
Currently 'f' behaves like 's', which is not correct according to
man page.
Bug was introduced with 1f861935a7
2021-03-17 20:39:31 +01:00
Sami Kerola 32a4efc567
more: improve error messaging when input file is directory
Before this change the error message about directory was not informative,
and a little bit out of place.

    $ touch empty ; ./more ./empty /
    ::::::::::::::
    ./empty
    ::::::::::::::
    magic failed: cannot read fd 4 (Is a directory)
    ::::::::::::::
    /
    ::::::::::::::

After the change messaging is reverted back to what it used to be.

    $ ./more ./empty /
    ::::::::::::::
    ./empty
    ::::::::::::::

    *** /: directory ***

And while on it lets remove repeated fstat() call.

Fixes: 09070e1a65
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-11-14 16:07:05 +00:00
Sami Kerola 2eb527722a more: avoid libmagic telling an empty file is binary
My earlier change that took libmagic in use to identify mime-type of an input
file caused empty files to be marked binary.  Before the change empty files
were simply displayed as empty.  This change will restore that behavior.

Addresses: 09070e1a65
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-05-25 14:16:45 +02:00
Sami Kerola 1f861935a7 more: make page and arrow up/down to update view
Aim was to introduce page and arrow up/down keys to more(1), but that
also required merging colon_command() and more_key_command() functions.

The more_key_commands enum is pointless from computers point of view.
The command identification performed in read_command() inline with
more_key_command() execution -- but that would be hard for humans, and
source code ought to serve both parties.

Reference: https://github.com/karelzak/util-linux/pull/1003
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-05-12 10:22:09 +02:00
Sami Kerola 09070e1a65 more: use libmagic to identify binary files
As the old commend said: 'This code would best be shared with the file(1)
program or, perhaps, more should not try to be so smart'.  Now at configure
time one can choose whether more(1) is sharing code with file(1), or not.

Addresses: http://bugs.debian.org/139205
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-05-12 10:22:09 +02:00
Rosen Penev 042f62dfc5
[clang-tidy] do not use else after return
Found with readability-else-after-return

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2020-04-20 13:20:59 -07:00
Sami Kerola ed292a08dd
include: add remove_entry() to env.h
A function to remove an command-line option argument, or environment
variable.

Requested-by: Karel Zak <kzak@redhat.com>
Reference: https://github.com/karelzak/util-linux/pull/1003#discussion_r403988092
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-04-13 12:14:08 +01:00
Sami Kerola 837f49c6bf
more: fix moving backwards so that it can reach begining of the file
Moving backwards has worked fine until reaching start of file.  At that
point more printout had one line too much in output causing more seemingly
never to be able to reach the start.  That is now fixed, with clean ups to
skip_backwards() making it less confusing.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-04-13 12:14:08 +01:00
Sami Kerola e3cb27f535
more: use getopt_long() to parse options
This commit also includes fix to how initial skip lines and search are
instructed in the code.  Earlier version was pretty close impossible to make
work with getopt_long() and had minor flaw - if both initial skip lines and
search were defined at the same time the skipping did not happen.  That is
now corrected.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-04-13 12:14:08 +01:00
Sami Kerola 94bece3c22
more: remove underlining related code
Linux terminal handles underlining just fine without more executable needing
to do anything extra.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-04-13 12:14:08 +01:00
Sami Kerola 6dcaaa9ef5
more: fix SIGSTOP and SIGCONT handling
When suspending only the more process.  Sending signal to process group
makes signal destination unnecessarily vague.  After the suspend is over
SIGCONT is expected, and it needs to ensure output terminal settings are
what more needs.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-04-13 12:14:07 +01:00
Sami Kerola c366ebc7ef
more: make execute() more robust and timely
The wait() is now a little more robust by being more tolerate rogue SIGCHLDs
and unblocked signals.  The repeated fork() and sleep() is removed, if first
try does not success give up without delay to provide user timely feedback.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-04-13 12:14:07 +01:00
Sami Kerola f3c044eeed
more: target all standard streams when calling fflush()
If streams need to be flushed do not try to be clever, just flush all of
them.  That lead to finding unnecessary print out in run_shell() that is
removed in this commit.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-04-13 12:14:07 +01:00
Sami Kerola 21d096239c
more: remove kill_line() in favor of erase_prompt()
These functions did similar things, so there is no need for both of them.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-04-13 12:14:07 +01:00
Sami Kerola 508b2d79df
more: avoid defining special characters locally
The sys/ttydefaults.h can be used for CTRL() and rubout that is CERASE.  For
alarm it's best to keep things simple and call printf() alarm modifier.
QUIT was not in use, so it is just deleted.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-04-13 12:14:07 +01:00
Sami Kerola ba105bb5eb
more: use off_t and cc_t to clarify what variables attempt to represent
As a minor robustness improvement use ftello() to find out file position
rather than add and substract from old value.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-04-13 12:14:07 +01:00
Sami Kerola 437cec386f
more: do not reset parent process terminal in execute()
Only the terminal of the child process, that is the command being executed,
needs the reset.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-04-13 12:14:07 +01:00
Sami Kerola 73ff5a9561
more: use single exit path to ensure resource freeing is unified
And be a little more complete all the allocations that can be released are,
but there is a small catch.  As mentioned in ncurses FAQ some leaks are
intentional, and that's the way they are.

Reference: https://invisible-island.net/ncurses/ncurses.faq.html#config_leaks
Reference: http://man7.org/linux/man-pages/man3/_nc_free_tinfo.3x.html
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-04-13 12:13:51 +01:00
Sami Kerola 5cd4550251
more: refactor and clarify code
Use the backspace provided dy terminfo, and if that is not possible use
fallback.

Simplify entering and exiting standard mode.  Now when these std_enter and
std_exit variables are initialized correctly they do not need to be checked
quite as complex way as earlier.

Do not call prepare_line_buffer() unnecessarily.  The line buffer needs only
to be prepared only at times of more(1) initialization and when terminal
size changes.

Add a function to print various separators, that is better than static
strings.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-03-28 07:55:59 +00:00
Sami Kerola 63f96f682f
more: add display_file() to show files and stdin
Earlier main() inline code implemented input files and stdin displaying
separately.  With small restructuring this code can be shared.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-03-28 07:55:59 +00:00
Sami Kerola 22ff9a38cb
more: simplify initterm()
Return rather than use long 'else' statement.  Also unwind some overly
compact variable set and test code, that can be best observed how
underlining settings are now handled.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-03-28 07:55:59 +00:00
Sami Kerola 82129a6fca
more: move currently open file to control structure
Currently open file is needed a bit here and there.  Move it to control
structure to avoid passing it around.  This also makes fixing memory leak at
more_exit() very easy.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-03-28 07:55:59 +00:00
Sami Kerola 1f5d21ec33
more: move code blocks from more_key_command() to functions
The more_key_command() was overly long.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-03-28 07:55:58 +00:00
Sami Kerola b6818219fb
more: move runtime usage output to a function
This change also removes a ghost prompt that was left in place when printing
help.  When looking old commits the output fluke has been around at least
since util-linux v2.2.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-03-28 07:55:58 +00:00
Sami Kerola f53edc0ee6
more: restructure print_buf() if-else with continue
Replacing long 'else' with 'continue' allows dropping one level of
indentation.  Main aim is to improve code readability by reducing
complexity one needs to keep track while looking the code.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-03-28 07:55:58 +00:00
Sami Kerola f8492042f5
more: tell in run time help what the 'v' will execute as editor
In same go use well known paths from system include file.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-03-28 07:55:58 +00:00
Sami Kerola 8fb5fe304a
more: drop setuid permissions before executing anything
Pagers are not expected to have setuid or setgid bits, but assuming such
mistake has taken place try to avoid privilege escalation.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-03-28 07:55:58 +00:00
Sami Kerola 474c61c0a8
more: do not allocate shell command buffer from stack
This change has three possitive effects:  Avoid expansion code when it is
not needed.  Stop wasting memory from stack for something rarely needed.
When command buffer is needed stop limiting it's size artificially.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-03-27 21:06:08 +00:00
Sami Kerola 823ef319b6
more: replace siglongjmp() and signal() calls with signalfd()
From man siglongjmp(3) 'longjmp() and siglongjmp() make programs hard to
understand and maintain.  If possible, an alternative should be used.'  That
manual page remark summarizes quite well why more(1) needs to move away from
jumping around.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-03-27 21:06:01 +00:00
Sami Kerola 218b1dd6f9
misc: fix typos [codespell]
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2019-09-12 19:41:46 +01:00
Karel Zak 2c308875a7 misc: consolidate version printing and close_stdout()
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-04-16 15:14:13 +02:00
Sami Kerola 275d47c9b7
more: rename functions
This clarifies what various function calls are doing.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2018-09-02 18:30:12 +01:00
Sami Kerola d910f31110
more: rename variable names
Try to make variable names to tell what they do.  Earlier names have been in
more(1) since the command was first wrote, and it looks like coding
practices has changed since late 70's.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2018-09-02 18:28:09 +01:00
Sami Kerola 1e2cd595eb
more: remove unnecessary ifdef preprosessor directives
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2018-09-02 18:28:09 +01:00
Sami Kerola d2b540022b
more: do not call fileno() for std{in,out,err} streams
These file descriptor numbers are well known, use them from unistd.h

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2018-09-02 18:28:08 +01:00
Sami Kerola 0b735ea49e
more: remove 'register' keywords
One can only assume someone tried to make more(1) to run quicker.  More up
to date assumption is that compilers are fully capable optimizing binaries
without these sort of hints.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2018-09-02 18:28:08 +01:00
Sami Kerola 70824899b0
more: remove pointless functions
The curs_terminfo(3X) defines putp() as tputs(str, 1, putchar), so all of
these five functions can be replaced with putp().

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2018-09-02 18:28:08 +01:00
Sami Kerola f728d8baae
more: return is statement not a function
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2018-09-02 18:28:08 +01:00
Sami Kerola c8b1fa6046
more: remove dead code and useless comments
Removal of STOP requires explanation.  Looking unix-history-repo getline()
function in first BSD-3 version could return STOP.  By next tag BSD-4 the
return STOP had disappeared.  If I read this correctly that was a partial
removal, and second part has waited to be completed since 1980-11-16 when
BSD-4 was released.

Reference: https://github.com/dspinellis/unix-history-repo/blob/BSD-3-Snapshot-Development/usr/src/cmd/more.c#L501
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2018-09-02 18:28:08 +01:00
Sami Kerola bd6a253f69
more: remove function like preprocessor defines
Inlining code using preprocessor function like macros is bad for
readability, and prone to errors.  Besides this is a pager, who cares if
code is few milliseconds slower.

Requestee-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2018-06-18 20:19:33 +01:00
Sami Kerola 7cc31c18d7
more: remove global variables, add struct more_control
This is rather big change, but that is the only way to do this sort job.  To
keep this change relatively understandable only moves global variables to a
state structure.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2018-06-18 20:19:33 +01:00
Sami Kerola 82cfa6f20f
more: move couple functions
Earlier commit moved lots of functions to work without declarations, but to
be able to get rid of global variables few more moves is needed.

Reference: a8f98304e6
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2018-06-18 20:19:33 +01:00
Sami Kerola a8f98304e6 more: reorder global declarations
Group include, defines and such together, and move items inbetween functions
on top of the source file so that everything can be seen easily.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2018-05-23 10:41:46 +02:00
Sami Kerola f16ca88d9c more: remove function prototypes
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2018-05-23 10:41:46 +02:00