cscvs to tla changeset 100
[onak.git] / splitkeys.c
1 /*
2  * splitkeys.c - Split a keyring into smaller chunks.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  * 
6  * Copyright 2003 Project Purple
7  *
8  * $Id: splitkeys.c,v 1.1 2003/09/30 21:16:14 noodles Exp $
9  */
10
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <sys/stat.h>
14 #include <sys/types.h>
15 #include <unistd.h>
16
17 #include "charfuncs.h"
18 #include "keystructs.h"
19 #include "mem.h"
20 #include "parsekey.h"
21
22 int main(int argc, char *argv[])
23 {
24         struct openpgp_packet_list      *packets = NULL;
25         struct openpgp_packet_list      *list_end = NULL;
26         struct openpgp_packet_list      *tmp = NULL;
27         int                              result = 0;
28         int                              maxkeys = 10000;
29         int                              outfd = -1;
30         int                              count = 0;
31         char                             splitfile[1024];
32
33         if (argc > 1) {
34                 maxkeys = atoi(argv[1]);
35                 if (maxkeys == 0) {
36                         fprintf(stderr,
37                                 "Couldn't parse %s as a number of keys!\n",
38                                 argv[1]);
39                         exit(1);
40                 }
41         }
42
43         do {
44                 result = read_openpgp_stream(stdin_getchar, NULL,
45                                  &packets, maxkeys);
46                 if (packets != NULL) {
47                         list_end = packets;
48                         while (list_end->next != NULL) {
49                                 tmp = list_end;
50                                 list_end = list_end->next;
51                                 if (list_end->next == NULL &&
52                                         list_end->packet->tag == 6) {
53                                         tmp->next = NULL;
54                                 }
55                         }
56
57                         snprintf(splitfile, 1023, "splitfile-%d.pgp", count);
58                         outfd = open(splitfile, O_WRONLY | O_CREAT, 0664);
59                         write_openpgp_stream(file_putchar, &outfd,
60                                         packets);
61                         close(outfd);
62                         free_packet_list(packets);
63                         packets = list_end;
64                         count++;
65                 }
66         } while (packets != NULL);
67
68         return 0;
69 }