/* SPDX-License-Identifier: LGPL-2.1+ */
-/***
- This file is part of systemd.
-
- Copyright 2010 Lennart Poettering
-***/
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
-//#include <linux/kd.h>
-//#include <linux/tiocl.h>
-//#include <linux/vt.h>
+#include <linux/kd.h>
+#include <linux/tiocl.h>
+#include <linux/vt.h>
//#include <poll.h>
//#include <signal.h>
#include <stdarg.h>
#include "alloc-util.h"
//#include "copy.h"
+//#include "def.h"
#include "env-util.h"
#include "fd-util.h"
#include "fileio.h"
}
bool on_tty(void) {
+
+ /* We check both stdout and stderr, so that situations where pipes on the shell are used are reliably
+ * recognized, regardless if only the output or the errors are piped to some place. Since on_tty() is generally
+ * used to default to a safer, non-interactive, non-color mode of operation it's probably good to be defensive
+ * here, and check for both. Note that we don't check for STDIN_FILENO, because it should fine to use fancy
+ * terminal functionality when outputting stuff, even if the input is piped to us. */
+
if (cached_on_tty < 0)
- cached_on_tty = isatty(STDOUT_FILENO) > 0;
+ cached_on_tty =
+ isatty(STDOUT_FILENO) > 0 &&
+ isatty(STDERR_FILENO) > 0;
return cached_on_tty;
}
return 0;
}
+#if 0 /// UNNEEDED by elogind
static bool urlify_enabled(void) {
static int cached_urlify_enabled = -1;
}
static int cat_file(const char *filename, bool newline) {
- _cleanup_close_ int fd;
+ _cleanup_fclose_ FILE *f = NULL;
+ _cleanup_free_ char *urlified = NULL;
+ int r;
- fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOCTTY);
- if (fd < 0)
+ f = fopen(filename, "re");
+ if (!f)
return -errno;
+ r = terminal_urlify_path(filename, NULL, &urlified);
+ if (r < 0)
+ return r;
+
printf("%s%s# %s%s\n",
newline ? "\n" : "",
ansi_highlight_blue(),
- filename,
+ urlified,
ansi_normal());
fflush(stdout);
- return copy_bytes(fd, STDOUT_FILENO, (uint64_t) -1, 0);
+ for (;;) {
+ _cleanup_free_ char *line = NULL;
+
+ r = read_line(f, LONG_LINE_MAX, &line);
+ if (r < 0)
+ return log_error_errno(r, "Failed to read \"%s\": %m", filename);
+ if (r == 0)
+ break;
+
+ puts(line);
+ }
+
+ return 0;
}
-int cat_files(const char *file, char **dropins) {
+int cat_files(const char *file, char **dropins, CatFlags flags) {
char **path;
int r;
if (file) {
r = cat_file(file, false);
- if (r < 0)
+ if (r == -ENOENT && (flags & CAT_FLAGS_MAIN_FILE_OPTIONAL))
+ printf("%s# config file %s not found%s\n",
+ ansi_highlight_magenta(),
+ file,
+ ansi_normal());
+ else if (r < 0)
return log_warning_errno(r, "Failed to cat %s: %m", file);
}
return 0;
}
+
+void print_separator(void) {
+
+ /* Outputs a separator line that resolves to whitespace when copied from the terminal. We do that by outputting
+ * one line filled with spaces with ANSI underline set, followed by a second (empty) line. */
+
+ if (underline_enabled()) {
+ size_t i, c;
+
+ c = columns();
+
+ flockfile(stdout);
+ fputs_unlocked(ANSI_UNDERLINE, stdout);
+
+ for (i = 0; i < c; i++)
+ fputc_unlocked(' ', stdout);
+
+ fputs_unlocked(ANSI_NORMAL "\n\n", stdout);
+ funlockfile(stdout);
+ } else
+ fputs("\n\n", stdout);
+}
+#endif // 0