int main(int argc, char *argv[]) { uint16_t types[] = { SCAMPER_FILE_OBJ_CYCLE_START, SCAMPER_FILE_OBJ_CYCLE_STOP, SCAMPER_FILE_OBJ_PING, SCAMPER_FILE_OBJ_TRACE, SCAMPER_FILE_OBJ_DEALIAS, SCAMPER_FILE_OBJ_TBIT, }; scamper_file_t *in, *out; scamper_file_filter_t *filter; char **files = NULL; int filec; uint16_t type; void *data; int i; if((out = scamper_file_openfd(STDOUT_FILENO, NULL, 'w', "json")) == NULL) { fprintf(stderr, "could not associate stdout\n"); return -1; } filter = scamper_file_filter_alloc(types, sizeof(types)/sizeof(uint16_t)); if(filter == NULL) { fprintf(stderr, "could not allocate filter\n"); return -1; } filec = argc - 1; if(filec > 0) files = argv + 1; for(i=0; i<=filec; i++) { if(filec == 0) { if((in = scamper_file_openfd(STDIN_FILENO,"-",'r',"warts")) == NULL) { fprintf(stderr, "could not use stdin\n"); return -1; } } else if(i < filec) { if((in = scamper_file_open(files[i], 'r', NULL)) == NULL) { fprintf(stderr, "could not open %s: %s\n", files[i], strerror(errno)); return -1; } } else break; while(scamper_file_read(in, filter, &type, (void *)&data) == 0) { if(data == NULL) break; /* EOF */ if(scamper_file_write_obj(out, type, data) != 0) return -1; if(type == SCAMPER_FILE_OBJ_PING) scamper_ping_free(data); else if(type == SCAMPER_FILE_OBJ_TRACE) scamper_trace_free(data); else if(type == SCAMPER_FILE_OBJ_DEALIAS) scamper_dealias_free(data); else if(type == SCAMPER_FILE_OBJ_TBIT) scamper_tbit_free(data); } scamper_file_close(in); } scamper_file_filter_free(filter); scamper_file_close(out); return 0; }
static int check_options(int argc, char *argv[]) { char ch; int i; char *opts = "o:s?"; char *opt_outfile = NULL; while((i = getopt(argc, argv, opts)) != -1) { ch = (char)i; switch(ch) { case 'o': options |= OPT_OUTFILE; opt_outfile = optarg; break; case 's': options |= OPT_SORT; break; case '?': default: usage(argv[0], 0xffffffff); return -1; } } /* figure out how many input files there are to process */ if((infile_cnt = argc - optind) < 1) { usage(argv[0], 0); return -1; } if((infiles = malloc(sizeof(scamper_file_t *) * infile_cnt)) == NULL) { return -1; } memset(infiles, 0, sizeof(scamper_file_t *) * infile_cnt); /* open each input file */ for(i=0; i<infile_cnt; i++) { if((infiles[i] = scamper_file_open(argv[optind+i], 'r', NULL)) == NULL) { usage(argv[0], 0); fprintf(stderr, "could not open infile %s\n", argv[optind+i]); return -1; } } /* open the output file, which is a regular file */ if(options & OPT_OUTFILE) { if((outfile = scamper_file_open(opt_outfile, 'a', "warts")) == NULL) { usage(argv[0], OPT_OUTFILE); return -1; } } else { /* writing to stdout; don't dump a binary structure to a tty. */ if(isatty(1) != 0) { fprintf(stderr, "not going to dump warts to a tty, sorry\n"); return -1; } if((outfile = scamper_file_openfd(1, "-", 'w', "warts")) == NULL) { fprintf(stderr, "could not wrap scamper_file around stdout\n"); return -1; } } return 0; }