creds: minor cleanups for --newline= handling (#41329)

Prompted by #41325

Fixes #41348
This commit is contained in:
Mike Yuan
2026-03-26 18:36:51 +01:00
committed by GitHub
3 changed files with 13 additions and 20 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include "assert-util.h"
#include "assert-fundamental.h"
#include "cleanup-fundamental.h" /* IWYU pragma: export */
typedef void (*free_func_t)(void *p);
+10 -16
View File
@@ -398,8 +398,6 @@ static int transcode(
}
static int print_newline(FILE *f, const char *data, size_t l) {
int fd;
assert(f);
assert(data || l == 0);
@@ -411,12 +409,14 @@ static int print_newline(FILE *f, const char *data, size_t l) {
if (l > 0 && data[l-1] == '\n')
return 0;
/* Don't bother unless this is a tty */
fd = fileno(f);
if (fd >= 0 && !isatty_safe(fd))
return 0;
/* If not explicitly requested, don't bother if the output is not a tty */
if (arg_newline < 0) {
int fd = fileno(f);
if (fd >= 0 && !isatty_safe(fd))
return 0;
}
if (fputc('\n', f) != '\n')
if (fputc('\n', f) == EOF)
return log_error_errno(errno, "Failed to write trailing newline: %m");
return 1;
@@ -940,15 +940,9 @@ static int parse_argv(int argc, char *argv[]) {
break;
case ARG_NEWLINE:
if (isempty(optarg) || streq(optarg, "auto"))
arg_newline = -1;
else {
r = parse_boolean_argument("--newline=", optarg, NULL);
if (r < 0)
return r;
arg_newline = r;
}
r = parse_tristate_argument_with_auto("--newline=", optarg, &arg_newline);
if (r < 0)
return r;
break;
case 'p':
+2 -3
View File
@@ -114,10 +114,9 @@ run_with_cred_compare "mycred:" "" cat mycred
run_with_cred_compare "mycred:\n" "\n" cat mycred
run_with_cred_compare "mycred:foo" "foo" cat mycred
run_with_cred_compare "mycred:foo" "foofoofoo" cat mycred mycred mycred
# Note: --newline= does nothing when stdout is not a tty, which is the case here
run_with_cred_compare "mycred:foo" "foo" --newline=yes cat mycred
run_with_cred_compare "mycred:foo" "foo" --newline=no cat mycred
run_with_cred_compare "mycred:foo" "foo" --newline=auto cat mycred
run_with_cred_compare "mycred:foo" "foo" --newline=no cat mycred
run_with_cred_compare "mycred:foo" "foo\n" --newline=yes cat mycred
run_with_cred_compare "mycred:foo" "foo" --transcode=no cat mycred
run_with_cred_compare "mycred:foo" "foo" --transcode=0 cat mycred
run_with_cred_compare "mycred:foo" "foo" --transcode=false cat mycred