[systemd-devel] [PATCH] pager: support SYSTEMD_LESS environment variable

Lennart Poettering lennart at poettering.net
Tue Feb 11 18:10:54 PST 2014


On Wed, 12.02.14 02:58, Jason A. Donenfeld (Jason at zx2c4.com) wrote:

> This allows customization of the arguments used by less. The main
> motivation is that some folks might not like having --no-init on every
> invocation of less.
> ---
> If you'd like me to update some documentation, let me know what
> files I should edit, and I'll send a v2.
> 
>  src/shared/pager.c | 17 +++++++++++++----
>  1 file changed, 13 insertions(+), 4 deletions(-)
> 
> diff --git a/src/shared/pager.c b/src/shared/pager.c
> index 72a29f2..4a7be4e 100644
> --- a/src/shared/pager.c
> +++ b/src/shared/pager.c
> @@ -50,6 +50,8 @@ noreturn static void pager_fallback(void) {
>  int pager_open(bool jump_to_end) {
>          int fd[2];
>          const char *pager;
> +        char *less_opts;

Needs to be "const", otherwise assigning a literal string will generate
casting errors....

> +        _cleanup_free_ *joined_less_opts = NULL;
>          pid_t parent_pid;
>          int r;
>  
> @@ -88,10 +90,17 @@ int pager_open(bool jump_to_end) {
>                  dup2(fd[0], STDIN_FILENO);
>                  close_pipe(fd);
>  
> -                if (jump_to_end)
> -                        setenv("LESS", "FRSXMK+G", 1);
> -                else
> -                        setenv("LESS", "FRSXMK", 1);
> +                less_opts = getenv("SYSTEMD_LESS");
> +                if (!less_opts) {
> +                        if (jump_to_end)
> +                                less_opts = "FRSXMK+G";
> +                        else
> +                                less_opts = "FRSXMK";
> +                } else if (jump_to_end) {
> +                        joined_less_opts = strjoin(less_opts, " +G",
> -                        NULL);

Needs OOM check. Also, for the special case of joining exactly 2
strings, use strappend(), it's a bit more efficient...

> +                        less_opts = joined_less_opts;
> +                }
> +                setenv("LESS", less_opts, 1);
>  
>                  /* Make sure the pager goes away when the parent dies */
>                  if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)

Otherwise looks good.

Lennart

-- 
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list