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

View File

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

View File

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