Code cleanup for --use-askpass

* bootstrap.conf: Add xmemdup0 and strpbrk.
* src/init.c (cmd_use_askpass): Add 'const' to char *,
  remove check for file existence.
* src/main.c (run_use_askpass): C89 compat init of argv,
  added \n to error messages,
  fixed stripping of \n and \r from input,
  make run_use_askpass and use_askpass static.
This commit is contained in:
Tim Rühsen 2016-09-07 10:40:43 +02:00
parent 49af22ca94
commit 37a5257c66
3 changed files with 25 additions and 30 deletions

View File

@ -90,6 +90,7 @@ stdbool
stdint stdint
strcase strcase
strerror_r-posix strerror_r-posix
strpbrk
strptime strptime
strtok_r strtok_r
strtoll strtoll
@ -102,6 +103,7 @@ update-copyright
vasprintf vasprintf
vsnprintf vsnprintf
write write
xmemdup0
xstrndup xstrndup
" "

View File

@ -1388,18 +1388,11 @@ cmd_time (const char *com, const char *val, void *place)
static bool static bool
cmd_use_askpass (const char *com _GL_UNUSED, const char *val, void *place) cmd_use_askpass (const char *com _GL_UNUSED, const char *val, void *place)
{ {
char *env_name = "WGET_ASKPASS"; const char *env_name = "WGET_ASKPASS";
char *env; const char *env;
if (val && *val) if (val && *val)
{ return cmd_string (com, val, place);
if (!file_exists_p (val))
{
fprintf (stderr, _("%s does not exist.\n"), val);
exit (WGET_EXIT_GENERIC_ERROR);
}
return cmd_string (com, val, place);
}
env = getenv (env_name); env = getenv (env_name);
if (!(env && *env)) if (!(env && *env))
@ -1414,13 +1407,6 @@ cmd_use_askpass (const char *com _GL_UNUSED, const char *val, void *place)
exit (WGET_EXIT_GENERIC_ERROR); exit (WGET_EXIT_GENERIC_ERROR);
} }
if (!file_exists_p (env))
{
fprintf (stderr, _("%s points to %s, which does not exist.\n"),
env_name, env);
exit (WGET_EXIT_GENERIC_ERROR);
}
return cmd_string (com, env, place); return cmd_string (com, env, place);
} }

View File

@ -61,6 +61,7 @@ as that of the covered work. */
#include "version.h" #include "version.h"
#include "c-strcase.h" #include "c-strcase.h"
#include "dirname.h" #include "dirname.h"
#include "xmemdup0.h"
#include <getopt.h> #include <getopt.h>
#include <getpass.h> #include <getpass.h>
#include <quote.h> #include <quote.h>
@ -1038,7 +1039,7 @@ prompt_for_password (void)
/* Execute external application opt.use_askpass */ /* Execute external application opt.use_askpass */
void static void
run_use_askpass (char *question, char **answer) run_use_askpass (char *question, char **answer)
{ {
char tmp[1024]; char tmp[1024];
@ -1046,12 +1047,12 @@ run_use_askpass (char *question, char **answer)
int status; int status;
int com[2]; int com[2];
ssize_t bytes = 0; ssize_t bytes = 0;
char * const argv[] = { opt.use_askpass, question, NULL }; char *argv[3], *p;
posix_spawn_file_actions_t fa; posix_spawn_file_actions_t fa;
if (pipe (com) == -1) if (pipe (com) == -1)
{ {
fprintf (stderr, _("Cannot create pipe")); fprintf (stderr, _("Cannot create pipe\n"));
exit (WGET_EXIT_GENERIC_ERROR); exit (WGET_EXIT_GENERIC_ERROR);
} }
@ -1059,7 +1060,7 @@ run_use_askpass (char *question, char **answer)
if (status) if (status)
{ {
fprintf (stderr, fprintf (stderr,
_("Error initializing spawn file actions for use-askpass: %d"), _("Error initializing spawn file actions for use-askpass: %d\n"),
status); status);
exit (WGET_EXIT_GENERIC_ERROR); exit (WGET_EXIT_GENERIC_ERROR);
} }
@ -1068,15 +1069,21 @@ run_use_askpass (char *question, char **answer)
if (status) if (status)
{ {
fprintf (stderr, fprintf (stderr,
_("Error setting spawn file actions for use-askpass: %d"), _("Error setting spawn file actions for use-askpass: %d\n"),
status); status);
exit (WGET_EXIT_GENERIC_ERROR); exit (WGET_EXIT_GENERIC_ERROR);
} }
/* C89 initializer lists must be computable at load time,
* thus this explicit initialization. */
argv[0] = opt.use_askpass;
argv[1] = question;
argv[2] = NULL;
status = posix_spawnp (&pid, opt.use_askpass, &fa, NULL, argv, environ); status = posix_spawnp (&pid, opt.use_askpass, &fa, NULL, argv, environ);
if (status) if (status)
{ {
fprintf (stderr, "Error spawning %s: %d", opt.use_askpass, status); fprintf (stderr, "Error spawning %s: %d\n", opt.use_askpass, status);
exit (WGET_EXIT_GENERIC_ERROR); exit (WGET_EXIT_GENERIC_ERROR);
} }
@ -1090,19 +1097,19 @@ run_use_askpass (char *question, char **answer)
opt.use_askpass, question, strerror (errno)); opt.use_askpass, question, strerror (errno));
exit (WGET_EXIT_GENERIC_ERROR); exit (WGET_EXIT_GENERIC_ERROR);
} }
/* Set the end byte to \0, and decrement bytes */
tmp[bytes--] = '\0'; /* Make sure there is a trailing 0 */
tmp[bytes] = '\0';
/* Remove a possible new line */ /* Remove a possible new line */
while (bytes >= 0 && if ((p = strpbrk (tmp, "\r\n")))
(tmp[bytes] == '\0' || tmp[bytes] == '\n' || tmp[bytes] == '\r')) bytes = p - tmp;
tmp[bytes--] = '\0';
*answer = xmemdup (tmp, bytes + 2); *answer = xmemdup0 (tmp, bytes);
} }
/* set the user name and password*/ /* set the user name and password*/
void static void
use_askpass (struct url *u) use_askpass (struct url *u)
{ {
static char question[1024]; static char question[1024];