From f713a18efaab34a72a96443ff7aabecdcdd04b23 Mon Sep 17 00:00:00 2001 From: thematdev Date: Thu, 15 Jun 2023 18:15:39 +0300 Subject: [PATCH] Improved example + fixed id_ctrl_fd uninitialized --- examples/main.c | 45 +++++++++++++++++++++++++++- src/drivers/unix_fs/unix_fs_driver.c | 4 ++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/examples/main.c b/examples/main.c index c1e5143..bb756a7 100644 --- a/examples/main.c +++ b/examples/main.c @@ -24,21 +24,64 @@ get_username() return ""; } +void usage() { + fprintf(stderr, "Simple utility to leave comment using unix_fs_driver\n"); + fprintf(stderr, "usage: [-p path] [-r reply_id] [-d displayname] [-i input_file]"); + fprintf(stderr, "-p path:\t path to comment storage \n"); + fprintf(stderr, "-r reply_id:\t ID of comment you are replying to, 0 if yours is top-level\n"); + fprintf(stderr, "-d displayname:\t Displayname to attach to comment\n"); + fprintf(stderr, "-i input_file:\t File containing comment text(defaults to stdin)\n"); +} + int -main(int argc, const char *argv[]) +main(int argc, char *argv[]) { const Driver *driver = &unix_fs_driver; char *path = "."; char *user_sid = get_username(); char *user_displayname = user_sid; + char *text_filename = NULL; int reply_id = 0; + + int opt; + while ((opt = getopt(argc, argv, "p:d:r:i:")) != -1) { + switch (opt) { + case 'p': + path = optarg; + break; + case 'd': + user_displayname = optarg; + break; + case 'r': + reply_id = strtol(optarg, NULL, 10); + if (reply_id < 0) { + fprintf(stderr, "error: reply id can't be less than 0\n"); + usage(); + exit(EXIT_FAILURE); + } + break; + case 'i': + text_filename = optarg; + break; + default: + usage(); + exit(EXIT_FAILURE); + } + } size_t cur_buf_size = 256; size_t idx = 0; char *text = malloc(cur_buf_size); + FILE *input_stream = (text_filename ? fopen(text_filename, "r") : stdin); + if (input_stream == NULL) { + fprintf(stderr, "error: cannot open input file\n"); + perror("fopen"); + exit(EXIT_FAILURE); + } + int ch; while ((ch = getchar()) != EOF) { if (idx + 1 == cur_buf_size) { diff --git a/src/drivers/unix_fs/unix_fs_driver.c b/src/drivers/unix_fs/unix_fs_driver.c index 387b2d7..7d91b10 100644 --- a/src/drivers/unix_fs/unix_fs_driver.c +++ b/src/drivers/unix_fs/unix_fs_driver.c @@ -66,6 +66,8 @@ deserialize_header(int fd, CommentHeader *header) return 0; } +/* TODO: validate driver_data_ptr */ + int unix_fs_driver_leave_comment(void *driver_data_ptr, CommentHeader *header, char *text) { @@ -80,7 +82,7 @@ unix_fs_driver_leave_comment(void *driver_data_ptr, CommentHeader *header, char id_ctrl_file_path = header_file_path = text_file_path = NULL; id_ctrl_file = NULL; - header_fd = text_fd = -1; + header_fd = text_fd = id_ctrl_fd = -1; path = ((UnixFsDriverData*) driver_data_ptr)->path; if (!is_valid_directory(path)) {