ldns-read-zone.c

Go to the documentation of this file.
00001 /*
00002  * read a zone file from disk and prints it, one RR per line
00003  *
00004  * See the file LICENSE for the license
00005  */
00006 
00007 #include "config.h"
00008 #include <unistd.h>
00009 #include <stdlib.h>
00010 
00011 #include <ldns/ldns.h>
00012 
00013 #include <errno.h>
00014 
00015 int
00016 main(int argc, char **argv)
00017 {
00018         char *filename;
00019         FILE *fp;
00020         ldns_zone *z;
00021         int line_nr = 0;
00022         int c;
00023         bool canonicalize = false;
00024         bool sort = false;
00025         ldns_status s;
00026         size_t i;
00027 
00028         while ((c = getopt(argc, argv, "chvz")) != -1) {
00029                 switch(c) {
00030                         case 'c':
00031                                 canonicalize = true;
00032                                 break;
00033                         case 'h':
00034                                 printf("Usage: %s [-c] [-v] [-z] <zonefile>\n", argv[0]);
00035                                 printf("\tReads the zonefile and prints it.\n");
00036                                 printf("\tThe RR count of the zone is printed to stderr.\n");
00037                                 printf("\t-c canonicalize all rrs in the zone.\n");
00038                                 printf("\t-h show this text\n");
00039                                 printf("\t-v shows the version and exits\n");
00040                                 printf("\t-z sort the zone (implies -c).\n");
00041                                 printf("\nif no file is given standard input is read\n");
00042                                 exit(EXIT_SUCCESS);
00043                                 break;
00044                         case 'v':
00045                                 printf("read zone version %s (ldns version %s)\n", LDNS_VERSION, ldns_version());
00046                                 exit(EXIT_SUCCESS);
00047                                 break;
00048                         case 'z':
00049                                 canonicalize = true;
00050                                 sort = true;
00051                                 break;
00052                 }
00053         }
00054 
00055         argc -= optind;
00056         argv += optind;
00057 
00058         if (argc == 0) {
00059                 fp = stdin;
00060         } else {
00061                 filename = argv[0];
00062 
00063                 fp = fopen(filename, "r");
00064                 if (!fp) {
00065                         fprintf(stderr, "Unable to open %s: %s\n", filename, strerror(errno));
00066                         exit(EXIT_FAILURE);
00067                 }
00068         }
00069         
00070         s = ldns_zone_new_frm_fp_l(&z, fp, NULL, 0, LDNS_RR_CLASS_IN, &line_nr);
00071         if (s == LDNS_STATUS_OK) {
00072                 if (canonicalize) {
00073                         ldns_rr2canonical(ldns_zone_soa(z));
00074                         for (i = 0; i < ldns_rr_list_rr_count(ldns_zone_rrs(z)); i++) {
00075                                 ldns_rr2canonical(ldns_rr_list_rr(ldns_zone_rrs(z), i));
00076                         }
00077                 }
00078                 if (sort) {
00079                         ldns_zone_sort(z);
00080                 }
00081 
00082                 ldns_zone_print(stdout, z);
00083 
00084                 ldns_zone_deep_free(z);
00085         } else {
00086                 fprintf(stderr, "%s at %d\n", 
00087                                 ldns_get_errorstr_by_id(s),
00088                                 line_nr);
00089                 exit(EXIT_FAILURE);
00090         }
00091         fclose(fp);
00092 
00093         exit(EXIT_SUCCESS);
00094 }

Generated on Wed Feb 20 20:24:21 2008 for ldns by  doxygen 1.5.5