]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
ask-password: add "-n" switch for disabling trailing newline
authorLennart Poettering <lennart@poettering.net>
Wed, 23 Jun 2021 11:45:31 +0000 (13:45 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 24 Jun 2021 11:25:39 +0000 (13:25 +0200)
This is similar to the "-n" switch of the "echo" command.

man/systemd-ask-password.xml
src/ask-password/ask-password.c

index 0b1137539bba6afbbb3465362db6bb56671c8243..c3c9f573a4167a27115f9a5403f72618fc1b0418 100644 (file)
       <varlistentry>
         <term><option>--no-output</option></term>
 
-        <listitem><para>Do not print passwords to standard output.
-        This is useful if you want to store a password in kernel
-        keyring with <option>--keyname</option> but do not want it
-        to show up on screen or in logs.</para></listitem>
+        <listitem><para>Do not print passwords to standard output.  This is useful if you want to store a
+        password in kernel keyring with <option>--keyname=</option> but do not want it to show up on screen
+        or in logs.</para></listitem>
+      </varlistentry>
+
+      <varlistentry>
+        <term><option>-n</option></term>
+
+        <listitem><para>By default, when writing the acquired password to standard output it is suffixed by a
+        newline character. This may be turned off with the <option>-n</option> switch, similar to the switch
+        of the same name of the <citerefentry
+        project='man-pages'><refentrytitle>echo</refentrytitle><manvolnum>1</manvolnum></citerefentry>
+        command.</para></listitem>
       </varlistentry>
 
       <xi:include href="standard-options.xml" xpointer="help" />
index 6a09a9a35c9288c1cbad829b6d70e3c7a0826375..45305dec1f0dc39f278a35ac55b200f66c767402 100644 (file)
@@ -24,6 +24,7 @@ static usec_t arg_timeout = DEFAULT_TIMEOUT_USEC;
 static bool arg_multiple = false;
 static bool arg_no_output = false;
 static AskPasswordFlags arg_flags = ASK_PASSWORD_PUSH_CACHE;
+static bool arg_newline = true;
 
 STATIC_DESTRUCTOR_REGISTER(arg_message, freep);
 
@@ -54,6 +55,8 @@ static int help(void) {
                "     --accept-cached  Accept cached passwords\n"
                "     --multiple       List multiple passwords if available\n"
                "     --no-output      Do not print password to standard output\n"
+               "  -n                  Do not suffix password written to standard output with\n"
+               "                      newline\n"
                "\nSee the %2$s for details.\n",
                program_invocation_short_name,
                link,
@@ -104,7 +107,7 @@ static int parse_argv(int argc, char *argv[]) {
 
         /* Note the asymmetry: the long option --echo= allows an optional argument, the short option does
          * not. */
-        while ((c = getopt_long(argc, argv, "+he", options, NULL)) >= 0)
+        while ((c = getopt_long(argc, argv, "+hen", options, NULL)) >= 0)
 
                 switch (c) {
 
@@ -177,6 +180,10 @@ static int parse_argv(int argc, char *argv[]) {
                         arg_credential_name = optarg;
                         break;
 
+                case 'n':
+                        arg_newline = false;
+                        break;
+
                 case '?':
                         return -EINVAL;
 
@@ -237,8 +244,14 @@ static int run(int argc, char *argv[]) {
                 return log_error_errno(r, "Failed to query password: %m");
 
         STRV_FOREACH(p, l) {
-                if (!arg_no_output)
-                        puts(*p);
+                if (!arg_no_output) {
+                        if (arg_newline)
+                                puts(*p);
+                        else
+                                fputs(*p, stdout);
+                }
+
+                fflush(stdout);
 
                 if (!arg_multiple)
                         break;