1 /* gpgwrap.c - Wrapper to call gpg udner Windows.
2 * Copyright (C) 2007 g10 Code GmbH
4 * This file is part of Gpg4win.
6 * Gpg4win is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Gpg4win is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
32 /* Return a copy of ARGV, but with proper quoting. To release the
33 copy, you have to free argv_quoted[0] and argv_quoted. */
35 build_commandline (const char * const *argv)
44 /* We have to quote some things because under Windows the program
45 parses the commandline and does some unquoting. We enclose the
46 whole argument in double-quotes, and escape literal double-quotes
47 as well as backslashes with a backslash. We end up with a
48 trailing space at the end of the line, but that is harmless. */
49 for (i = 0; argv[i]; i++)
52 /* The leading double-quote. */
56 /* An extra one for each literal that must be escaped. */
57 if (*p == '\\' || *p == '"')
62 /* The trailing double-quote and the delimiter. */
65 /* And a trailing zero. */
68 /* Allocate a new vector. */
69 argv_quoted = malloc (sizeof (char *) * (i + 1));
80 for (i = 0; argv[i]; i++)
82 const char *argvp = argv[i];
89 if (*argvp == '\\' || *argvp == '"')
97 argv_quoted[i] = NULL;
104 main (int argc, const char * const *argv)
107 char pgm[MAX_PATH+100];
111 if (!GetModuleFileNameA (NULL, pgm, sizeof (pgm) - 1))
113 fprintf (stderr, "gpgwrap: error getting my own name: rc=%d\n",
118 /* Remove one directory part of the file name. */
119 p = strrchr (pgm, '\\');
123 p0 = strrchr (pgm, '\\');
131 /* Hack to output our own version along with the real file name
132 before the actual, we require that the --version option is given
135 && !strcmp(argv[1], "--version")
136 && !strcmp(argv[2], "--version"))
138 fputs ("gpgwrap (Gpg4win) " PACKAGE_VERSION " ;", stdout);
140 fputc ('\n', stdout);
144 argv_quoted = build_commandline (argv);
148 /* Using execv does not replace the existing program image, but
149 spawns a new one and daemonizes it, confusing the command line
150 interpreter. So we have to use spawnv. */
151 rc = _spawnv (_P_WAIT, pgm, (const char **) argv_quoted);
154 fprintf (stderr, "gpgwrap: executing `%s' failed: %s\n",
155 pgm, strerror (errno));
162 fprintf (stderr, "gpgwrap: internal error parsing my own name `%s'\n",