gjør det mulig å bruke host og user i .toml-fila

This commit is contained in:
Arindam Guha 2022-06-01 15:16:08 +02:00
parent 3542f51adb
commit 8e52ba7e6a
3 changed files with 40 additions and 14 deletions

View File

@ -31,8 +31,7 @@ extern char **get_group_names(int *numgroups);
extern int
version(void);
extern int
read_toml_file(void);
extern const char* read_toml_file(void);
/* same as strcpy, but returns a pointer to the end of dest instead of start */
extern char *strmov(char *, const char *);
@ -41,8 +40,7 @@ extern int name_isclean(char*);
#ifdef _mysql_h
extern int
dberror(MYSQL *pmysql, char *format, ...);
extern int dberror(MYSQL *pmysql, char *format, ...);
extern int
reload(MYSQL *pmysql);

View File

@ -6,7 +6,6 @@
*/
#include "config.h"
#include "mysql-admutils.h"
#include <stdio.h>
#include <string.h>
@ -17,7 +16,9 @@
#include <pwd.h>
#include <grp.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "mysql-admutils.h"
char *
strchr_whitespace(const char *s)

View File

@ -16,8 +16,8 @@
#include "toml.h"
/* defaults for configurable values */
const char* db_user = "root";
const char* db_server = "mysql.stud.ntnu.no";
const char* db_server = NULL;
const char* db_user = NULL;
const char* db_passwd = NULL ;
const char* db_name = "mysql";
@ -30,14 +30,14 @@ FILE* fp;
toml_table_t* conf;
toml_table_t* server;
const char* raw;
char* host;
char* user;
char* password;
char errbuf[200];
static char *tomlfile = SYSCONFDIR "/mysql-admutils.toml";
int
read_toml_file(void)
const char* read_toml_file(void)
{
if (0 == (fp = fopen(tomlfile, "r"))) {
perror("fopen");
@ -47,7 +47,7 @@ read_toml_file(void)
conf = toml_parse_file(fp, errbuf, sizeof(errbuf));
fclose(fp);
if (0 == conf) {
fprintf(stderr, "ERROR: %s\n", errbuf);
fprintf(stderr, "ERROR: parsing %s\n", errbuf);
exit(1);
}
@ -57,24 +57,51 @@ read_toml_file(void)
exit(1);
}
if (0 == (raw = toml_raw_in(server, "host"))) {
fprintf(stderr, "ERROR: missing 'host' in [server]\n");
toml_free(conf);
exit(1);
}
if (toml_rtos(raw, &host)) {
fprintf(stderr, "ERROR: bad value in 'host'\n");
toml_free(conf);
exit(1);
}
if (0 == (raw = toml_raw_in(server, "user"))) {
fprintf(stderr, "ERROR: missing 'user' in [server]\n");
toml_free(conf);
exit(1);
}
if (toml_rtos(raw, &user)) {
fprintf(stderr, "ERROR: bad value in 'user'\n");
toml_free(conf);
exit(1);
}
if (0 == (raw = toml_raw_in(server, "password"))) {
fprintf(stderr, "ERROR: missing 'password' in [server]\n");
toml_free(conf);
exit(1);
}
if (toml_rtos(raw, &password)) {
fprintf(stderr, "ERROR: bad value in 'host'\n");
fprintf(stderr, "ERROR: bad value in 'password'\n");
toml_free(conf);
exit(1);
}
toml_free(conf);
db_server = host;
db_user = user;
db_passwd = password;
return db_server;
return db_user;
return db_passwd;
free(host);
free(user);
free(password);
exit(1);
}