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