qments/examples/main.c
2023-06-15 01:44:02 +03:00

67 lines
1.4 KiB
C

/* Reads text from stdio and creates comment using unix fs driver in current directory */
/* TODO: add path, displayname as arguments */
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>
#include "comment.h"
#include "driver.h"
#include "drivers/unix_fs/unix_fs_driver.h"
char *
get_username()
{
uid_t uid = geteuid();
struct passwd *pw = getpwuid(uid);
if (pw) {
return pw->pw_name;
}
return "";
}
int
main(int argc, const char *argv[])
{
const Driver *driver = &unix_fs_driver;
char *path = ".";
char *user_sid = get_username();
char *user_displayname = user_sid;
int reply_id = 0;
size_t cur_buf_size = 256;
size_t idx = 0;
char *text = malloc(cur_buf_size);
int ch;
while ((ch = getchar()) != EOF) {
if (idx + 1 == cur_buf_size) {
cur_buf_size *= 2;
text = realloc(text, cur_buf_size);
}
text[idx++] = ch;
}
text[idx] = '\0';
time_t current_time;
time(&current_time);
UnixFsDriverData driver_data = { path };
CommentHeader *header = mk_comment_header(reply_id, current_time, strlen(text), user_sid, user_displayname);
int retval = driver->leave_comment(&driver_data, header, text);
free(text);
if (retval < 0) {
return retval;
} else {
return 0;
}
}