Successfully write onion services in a file

This commit is contained in:
Quentin Dufour 2019-02-08 18:04:48 +01:00
parent 848263bc24
commit 1eb247d925
2 changed files with 13 additions and 10 deletions

View file

@ -4,7 +4,7 @@ void tor_os_create(struct tor_os_str* os, char* file, size_t size) {
os->size = size;
os->filled = 0;
os->file = malloc(sizeof(char*) * (strlen(file) + 1));
os->file = malloc(sizeof(char) * (strlen(file) + 1));
if (os->file == NULL) goto mem_error;
strcpy(os->file, file);
@ -17,12 +17,13 @@ mem_error:
exit(EXIT_FAILURE);
}
char* tor_os_append_cursor(struct tor_os_str* os) {
char** tor_os_append_cursor(struct tor_os_str* os) {
if (os->filled == os->size) {
return NULL;
}
char* cur = os->list[os->filled];
char** cur = os->list + os->filled;
os->filled++;
*cur = NULL;
return cur;
}
@ -35,9 +36,9 @@ void tor_os_read (struct tor_os_str* os) {
size_t len = 0;
int n = 0;
while (1) {
char* dst = tor_os_append_cursor(os);
char** dst = tor_os_append_cursor(os);
if (dst == NULL) break;
if (getline(&(os->list[n]),&len,fd) == -1) break;
if (getline(dst,&len,fd) == -1) break;
}
fclose(fd);
}
@ -52,13 +53,14 @@ void tor_os_free(struct tor_os_str* os) {
void tor_os_persist(struct tor_os_str* os) {
FILE* fd = NULL;
fd = fopen(os->file, "w+");
fd = fopen(os->file, "w");
if (fd == NULL) {
fprintf(stderr, "unable to open file for writing\n");
exit(EXIT_FAILURE);
}
for (int i = 0; i < os->filled; i++) {
printf("%s\n", os->list[i]);
fprintf(fd, "%s\n", os->list[i]);
}
@ -66,9 +68,10 @@ void tor_os_persist(struct tor_os_str* os) {
}
int tor_os_append(struct tor_os_str* os, char* entry) {
char* target = tor_os_append_cursor (os);
target = malloc(sizeof(char*) * (strlen(entry) + 1));
char** target = tor_os_append_cursor (os);
if (target == NULL) return -1;
strcpy (target, entry);
*target = malloc(sizeof(char) * (strlen(entry) + 1));
if (*target == NULL) return -1;
strcpy (*target, entry);
return 0;
}

View file

@ -13,7 +13,7 @@ struct tor_os_str {
};
void tor_os_create(struct tor_os_str* os, char* file, size_t size);
char* tor_os_append_cursor(struct tor_os_str* os);
char** tor_os_append_cursor(struct tor_os_str* os);
int tor_os_append(struct tor_os_str* os, char* entry);
void tor_os_read (struct tor_os_str* os);
void tor_os_persist(struct tor_os_str* os);