You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.4 KiB
92 lines
2.4 KiB
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "sekrits.h"
|
|
|
|
/// SEKRITS CODE
|
|
|
|
struct sekrits_node {
|
|
struct sekrits_node *next;
|
|
char *name;
|
|
char *value;
|
|
};
|
|
|
|
struct sekrits {
|
|
struct sekrits_node *root;
|
|
};
|
|
|
|
int sekrits_create_from_filename(struct sekrits **ppsekrits, const char *filename) {
|
|
FILE *stream = fopen(filename, "r");
|
|
if (stream == NULL) {
|
|
perror("couldn't open file");
|
|
return SEKRITS_ERROR;
|
|
}
|
|
int result = sekrits_create_from_file(ppsekrits, stream);
|
|
fclose(stream);
|
|
return result;
|
|
}
|
|
|
|
/* used to define how long a sekrit line is allowed to be (this includes the
|
|
* sekrit name and value) */
|
|
#define SEKRIT_MAX_LINE_LEN 8096
|
|
|
|
int sekrits_create_from_file(struct sekrits **ppsekrits, FILE *file) {
|
|
struct sekrits *out = (struct sekrits *)malloc(sizeof(struct sekrits));
|
|
if (file == NULL) {
|
|
return SEKRITS_ERROR;
|
|
}
|
|
char sekrit_buf[SEKRIT_MAX_LINE_LEN] = {0};
|
|
char *line_end;
|
|
size_t line_num = 0;
|
|
while ((line_end = fgets(sekrit_buf, SEKRIT_MAX_LINE_LEN, file)) != NULL) {
|
|
++line_num;
|
|
ssize_t total_len = line_end - sekrit_buf;
|
|
char *iter;
|
|
for (iter = sekrit_buf; iter < line_end; ++iter) {
|
|
if (*iter == '\\') {
|
|
++iter;
|
|
continue;
|
|
} else if (*iter == '=') {
|
|
*iter = '\0';
|
|
++iter;
|
|
break;
|
|
}
|
|
}
|
|
char *name = sekrit_buf;
|
|
size_t name_len = iter - sekrit_buf;
|
|
char *value = iter;
|
|
size_t value_len = line_end - iter;
|
|
// char name_buf[SEKRIT_MAX_LINE_LEN] = {0};
|
|
// char value_buf[SEKRIT_MAX_LINE_LEN] = {0};
|
|
// char terminator = 0;
|
|
// int read_bytes = 0;
|
|
// do {
|
|
// int res = sscanf(file, "%[^\\=]%n%[\=]c%s", name_buf, &read_bytes, );
|
|
// } while (res != 2);
|
|
};
|
|
*ppsekrits = out;
|
|
return SEKRITS_OK;
|
|
}
|
|
|
|
int sekrits_destroy(struct sekrits *thing) {
|
|
if (thing != NULL) {
|
|
free(thing);
|
|
}
|
|
return SEKRITS_OK;
|
|
}
|
|
int sekrits_fetch(const struct sekrits *, const char *name, char *buf, size_t len);
|
|
|
|
const char *sekrits_get(struct sekrits *secrets, const char *name) {
|
|
if (secrets == NULL) {
|
|
return NULL;
|
|
}
|
|
struct sekrits_node *curr = secrets->root;
|
|
while (curr != NULL) {
|
|
if (strcmp(curr->name, name)) {
|
|
return curr->value;
|
|
}
|
|
curr = curr->next;
|
|
};
|
|
return NULL;
|
|
}
|