cscvs to tla changeset 2
[onak.git] / keymerge.c
1 /*
2  * keymerge.c - Takes a key on stdin, merges it and outputs the difference.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  * 
6  * Copyright 2002 Project Purple
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 #include "armor.h"
13 #include "keydb.h"
14 #include "keyid.h"
15 #include "keystructs.h"
16 #include "mem.h"
17 #include "merge.h"
18 #include "parsekey.h"
19
20 int stdin_getchar(void *ctx, size_t count, unsigned char *c)
21 {
22         int ic;
23
24         do {
25                 ic = getchar();
26                 *c = ic;
27                 c++;
28         } while ((ic != EOF) && (--count > 0));
29         return (ic == EOF);
30 }
31
32 int stdout_putchar(void *ctx, unsigned char c)
33 {
34         return (putchar(c));
35 }
36
37
38 int main(int argc, char *argv[])
39 {
40         struct openpgp_packet_list      *packets = NULL;
41         struct openpgp_packet_list      *list_end = NULL;
42         struct openpgp_publickey        *keys = NULL;
43         int                              rc = EXIT_SUCCESS;
44
45         dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
46         if (packets != NULL) {
47                 parse_keys(packets, &keys);
48                 free_packet_list(packets);
49                 packets = NULL;
50
51                 initdb();
52                 fprintf(stderr, "Got %d new keys.\n",
53                                 update_keys(&keys));
54                 cleanupdb();
55         } else {
56                 rc = 1;
57                 fprintf(stderr, "No keys read.\n");
58         }
59
60         if (keys != NULL) {
61                 flatten_publickey(keys, &packets, &list_end);
62                 free_publickey(keys);
63                 keys = NULL;
64
65                 armor_openpgp_stream(stdout_putchar, NULL, packets);
66                 free_packet_list(packets);
67                 packets = NULL;
68         } else {
69                 rc = 1;
70                 fprintf(stderr, "No changes.\n");
71         }
72
73         return rc;
74 }