#include #include #include #include #include #include #include #include static void usage(int argc, char **argv, const char *message, ...) { if (message) { va_list args; va_start(args, message); vfprintf(stderr, message, args); va_end(args); } fprintf(stderr, "usage: %s canif [file.can]\n", argv[0]); exit(1); } int main(int argc, char **argv) { hexagram_can_if *can_if; hexagram_capture *capture; if (argc == 2) { if ((capture = hexagram_capture_open_fd(fileno(stdin), O_WRONLY)) == NULL) { perror("hexagram_capture_open_fd()"); goto error_capture_open; } } else if (argc == 3) { if ((capture = hexagram_capture_open_file(argv[2], O_CREAT | O_TRUNC | O_WRONLY)) == NULL) { perror("hexagram_capture_open_file()"); goto error_capture_open; } } else { usage(argc, argv, NULL); exit(1); } if ((can_if = hexagram_can_if_open(argv[1])) == NULL) { perror("hexagram_can_if_open()"); goto error_can_if_open; } if (hexagram_capture_save(capture, can_if) < 0) { goto error_capture_save; } hexagram_can_if_close(can_if); if (argc == 3) { hexagram_capture_close(capture); } else { hexagram_capture_destroy(capture); } return 0; error_capture_save: hexagram_can_if_close(can_if); error_can_if_open: if (argc == 3) { hexagram_capture_close(capture); } else { hexagram_capture_destroy(capture); } error_capture_open: return 1; }