]> granicus.if.org Git - neomutt/blob - pgpewrap.c
merge: light refactoring
[neomutt] / pgpewrap.c
1 /**
2  * @file
3  * Standalone tool to manipulate a program's command line
4  *
5  * C version by Wessel Dankers <wsl@fruit.eu.org>
6  *
7  * This code is in the public domain.
8  */
9
10 #include "config.h"
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15
16 static void print_usage(const char *progname)
17 {
18   fprintf(stderr, "Command line usage: %s [flags] -- prefix [recipients]\n", progname);
19   exit(1);
20 }
21
22 int main(int argc, char **argv)
23 {
24   char **opts = NULL, **opt = NULL;
25   char *pfx = NULL;
26
27   if (argc < 2)
28   {
29     print_usage(argv[0]);
30   }
31
32   opts = malloc((2 * argc + 1) * sizeof(*opts));
33   if (!opts)
34   {
35     perror(argv[0]);
36     exit(2);
37   }
38
39   opt = opts;
40   *opt++ = argv[1];
41   pfx = NULL;
42
43   for (int i = 2; i < argc;)
44   {
45     if (strcmp(argv[i], "--") == 0)
46     {
47       i += 2;
48       if (i > argc)
49       {
50         free(opts);
51         print_usage(argv[0]);
52       }
53       pfx = argv[i - 1];
54     }
55     if (pfx)
56       *opt++ = pfx;
57     *opt++ = argv[i++];
58   }
59   *opt = NULL;
60
61   execvp(opts[0], opts);
62   perror(argv[0]);
63   free(opts);
64   return 2;
65 }