X Tutup
Skip to content

Commit b80ef40

Browse files
committed
ask-password: add "-n" switch for disabling trailing newline
This is similar to the "-n" switch of the "echo" command.
1 parent 6222acc commit b80ef40

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

man/systemd-ask-password.xml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,19 @@
210210
<varlistentry>
211211
<term><option>--no-output</option></term>
212212

213-
<listitem><para>Do not print passwords to standard output.
214-
This is useful if you want to store a password in kernel
215-
keyring with <option>--keyname</option> but do not want it
216-
to show up on screen or in logs.</para></listitem>
213+
<listitem><para>Do not print passwords to standard output. This is useful if you want to store a
214+
password in kernel keyring with <option>--keyname=</option> but do not want it to show up on screen
215+
or in logs.</para></listitem>
216+
</varlistentry>
217+
218+
<varlistentry>
219+
<term><option>-n</option></term>
220+
221+
<listitem><para>By default, when writing the acquired password to standard output it is suffixed by a
222+
newline character. This may be turned off with the <option>-n</option> switch, similar to the switch
223+
of the same name of the <citerefentry
224+
project='man-pages'><refentrytitle>echo</refentrytitle><manvolnum>1</manvolnum></citerefentry>
225+
command.</para></listitem>
217226
</varlistentry>
218227

219228
<xi:include href="standard-options.xml" xpointer="help" />

src/ask-password/ask-password.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ static usec_t arg_timeout = DEFAULT_TIMEOUT_USEC;
2424
static bool arg_multiple = false;
2525
static bool arg_no_output = false;
2626
static AskPasswordFlags arg_flags = ASK_PASSWORD_PUSH_CACHE;
27+
static bool arg_newline = true;
2728

2829
STATIC_DESTRUCTOR_REGISTER(arg_message, freep);
2930

@@ -54,6 +55,8 @@ static int help(void) {
5455
" --accept-cached Accept cached passwords\n"
5556
" --multiple List multiple passwords if available\n"
5657
" --no-output Do not print password to standard output\n"
58+
" -n Do not suffix password written to standard output with\n"
59+
" newline\n"
5760
"\nSee the %2$s for details.\n",
5861
program_invocation_short_name,
5962
link,
@@ -104,7 +107,7 @@ static int parse_argv(int argc, char *argv[]) {
104107

105108
/* Note the asymmetry: the long option --echo= allows an optional argument, the short option does
106109
* not. */
107-
while ((c = getopt_long(argc, argv, "+he", options, NULL)) >= 0)
110+
while ((c = getopt_long(argc, argv, "+hen", options, NULL)) >= 0)
108111

109112
switch (c) {
110113

@@ -177,6 +180,10 @@ static int parse_argv(int argc, char *argv[]) {
177180
arg_credential_name = optarg;
178181
break;
179182

183+
case 'n':
184+
arg_newline = false;
185+
break;
186+
180187
case '?':
181188
return -EINVAL;
182189

@@ -237,8 +244,14 @@ static int run(int argc, char *argv[]) {
237244
return log_error_errno(r, "Failed to query password: %m");
238245

239246
STRV_FOREACH(p, l) {
240-
if (!arg_no_output)
241-
puts(*p);
247+
if (!arg_no_output) {
248+
if (arg_newline)
249+
puts(*p);
250+
else
251+
fputs(*p, stdout);
252+
}
253+
254+
fflush(stdout);
242255

243256
if (!arg_multiple)
244257
break;

0 commit comments

Comments
 (0)
X Tutup