Improved example + fixed id_ctrl_fd uninitialized

This commit is contained in:
thematdev 2023-06-15 18:15:39 +03:00
parent 815cfbbcef
commit f713a18efa
Signed by: thematdev
GPG Key ID: D12878639B090D90
2 changed files with 47 additions and 2 deletions

View File

@ -24,21 +24,64 @@ get_username()
return "";
}
void usage() {
fprintf(stderr, "Simple utility to leave comment using unix_fs_driver\n");
fprintf(stderr, "usage: <tool-name> [-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) {

View File

@ -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)) {