1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include "procer.h"
#include <dbg.h>
bstring Profile_read_setting(bstring path, const char *file)
{
bstring target = bformat("%s/%s", bdata(path), file);
bstring data = NULL;
FILE *test_file = NULL;
test_file = fopen(bdata(target), "r");
check(test_file, "Failed to open %s file that's required.",
bdata(target));
data = bgets((bNgetc)fgetc, test_file, '\n');
if(data) btrimws(data);
error:
// fallthrough on purpose
if(test_file) fclose(test_file);
bdestroy(target);
return data;
};
int Profile_check_setting(bstring path, const char *file)
{
bstring target = bformat("%s/%s", bdata(path), file);
int rc = access(bdatae(target, ""), R_OK);
bdestroy(target);
return rc == 0;
}
Profile *Profile_load(bstring profile_dir)
{
Profile *prof = calloc(sizeof(Profile), 1);
check_mem(prof);
prof->command = bformat("%s/run", bdata(profile_dir));
prof->pid_file = Profile_read_setting(profile_dir, "pid_file");
prof->restart = Profile_check_setting(profile_dir, "restart");
return prof;
error:
return NULL;
}
|