Skip to content

Commit

Permalink
Merge pull request vysheng#5 from mk-pmb/env_paths
Browse files Browse the repository at this point in the history
Env paths
  • Loading branch information
koter84 committed May 10, 2014
2 parents 60658d8 + 158f579 commit a87e6c6
Showing 1 changed file with 31 additions and 22 deletions.
53 changes: 31 additions & 22 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ int sync_from_start;
int allow_weak_random;

void set_default_username (const char *s) {
if (default_username) {
if (default_username) {
tfree_str (default_username);
}
default_username = tstrdup (s);
Expand Down Expand Up @@ -129,11 +129,16 @@ void set_terminal_attributes (void) {
}
/* }}} */

int str_nonempty (char *str) {
return ((str != NULL) && (strlen(str) > 0));
}

char *get_home_directory (void) {
static char *home_directory = NULL;
if (home_directory != NULL) {
return home_directory;
}
home_directory = getenv("TELEGRAM_HOME");
if (str_nonempty (home_directory)) { return tstrdup (home_directory); }
home_directory = getenv("HOME");
if (str_nonempty (home_directory)) { return tstrdup (home_directory); }
struct passwd *current_passwd;
uid_t user_id;
setpwent ();
Expand All @@ -145,14 +150,14 @@ char *get_home_directory (void) {
}
}
endpwent ();
if (home_directory == NULL) {
home_directory = tstrdup (".");
}
if (!str_nonempty (home_directory)) { home_directory = tstrdup ("."); }
return home_directory;
}

char *get_config_directory (void) {
char *config_directory;
config_directory = getenv("TELEGRAM_CONFIG_DIR");
if (str_nonempty (config_directory)) { return tstrdup (config_directory); }
tasprintf (&config_directory, "%s/" CONFIG_DIRECTORY, get_home_directory ());
return config_directory;
}
Expand Down Expand Up @@ -206,11 +211,11 @@ void running_for_first_time (void) {
if (config_filename) {
return; // Do not create custom config file
}
tasprintf (&config_filename, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, CONFIG_FILE);
char *config_directory = get_config_directory ();
tasprintf (&config_filename, "%s/%s", config_directory, CONFIG_FILE);
config_filename = make_full_path (config_filename);

int config_file_fd;
char *config_directory = get_config_directory ();
//char *downloads_directory = get_downloads_directory ();

if (!mkdir (config_directory, CONFIG_DIRECTORY_MODE)) {
Expand All @@ -225,6 +230,7 @@ void running_for_first_time (void) {
config_file_fd = open (config_filename, O_CREAT | O_RDWR, 0600);
if (config_file_fd == -1) {
perror ("open[config_file]");
printf ("I: config_file=[%s]\n", config_filename);
exit (EXIT_FAILURE);
}
if (write (config_file_fd, DEFAULT_CONFIG_CONTENTS, strlen (DEFAULT_CONFIG_CONTENTS)) <= 0) {
Expand All @@ -238,7 +244,7 @@ void running_for_first_time (void) {
close (auth_file_fd);
printf ("[%s] created\n", config_filename);*/

/* create downloads directory */
/*if (mkdir (downloads_directory, 0755) !=0) {
perror ("creating download directory");
Expand All @@ -249,7 +255,7 @@ void running_for_first_time (void) {

#ifdef HAVE_LIBCONFIG
void parse_config_val (config_t *conf, char **s, char *param_name, const char *default_name, const char *path) {
static char buf[1000];
static char buf[1000];
int l = 0;
if (prefix) {
l = strlen (prefix);
Expand Down Expand Up @@ -277,7 +283,7 @@ void parse_config_val (config_t *conf, char **s, char *param_name, const char *d

void parse_config (void) {
config_filename = make_full_path (config_filename);

config_t conf;
config_init (&conf);
if (config_read_file (&conf, config_filename) != CONFIG_TRUE) {
Expand All @@ -299,29 +305,32 @@ void parse_config (void) {
test_dc = 0;
strcpy (buf + l, "test");
config_lookup_bool (&conf, buf, &test_dc);

strcpy (buf + l, "log_level");
long long t = log_level;
config_lookup_int (&conf, buf, (void *)&t);
log_level = t;

if (!msg_num_mode) {
strcpy (buf + l, "msg_num");
config_lookup_bool (&conf, buf, &msg_num_mode);
}

parse_config_val (&conf, &config_directory, "config_directory", CONFIG_DIRECTORY, 0);
// why not get_config_directory? -> parse_config_val (&conf, &config_directory, "config_directory", CONFIG_DIRECTORY, 0);
if (!str_nonempty(config_directory)) {
config_directory = get_config_directory ();
}
config_directory = make_full_path (config_directory);

parse_config_val (&conf, &auth_file_name, "auth_file", AUTH_KEY_FILE, config_directory);
parse_config_val (&conf, &state_file_name, "state_file", STATE_FILE, config_directory);
parse_config_val (&conf, &secret_chat_file_name, "secret", SECRET_CHAT_FILE, config_directory);
parse_config_val (&conf, &downloads_directory, "downloads", DOWNLOADS_DIRECTORY, config_directory);
parse_config_val (&conf, &binlog_file_name, "binlog", BINLOG_FILE, config_directory);

strcpy (buf + l, "binlog_enabled");
config_lookup_bool (&conf, buf, &binlog_enabled);

if (!mkdir (config_directory, CONFIG_DIRECTORY_MODE)) {
printf ("[%s] created\n", config_directory);
}
Expand Down Expand Up @@ -417,7 +426,7 @@ void args_parse (int argc, char **argv) {
binlog_enabled = 1;
break;
case 'L':
if (log_net_file) {
if (log_net_file) {
usage ();
}
log_net_file = tstrdup (optarg);
Expand Down Expand Up @@ -460,7 +469,7 @@ void print_backtrace (void) {

void sig_segv_handler (int signum __attribute__ ((unused))) {
set_terminal_attributes ();
if (write (1, "SIGSEGV received\n", 18) < 0) {
if (write (1, "SIGSEGV received\n", 18) < 0) {
// Sad thing
}
print_backtrace ();
Expand All @@ -469,7 +478,7 @@ void sig_segv_handler (int signum __attribute__ ((unused))) {

void sig_abrt_handler (int signum __attribute__ ((unused))) {
set_terminal_attributes ();
if (write (1, "SIGABRT received\n", 18) < 0) {
if (write (1, "SIGABRT received\n", 18) < 0) {
// Sad thing
}
print_backtrace ();
Expand All @@ -481,7 +490,7 @@ int main (int argc, char **argv) {
signal (SIGABRT, sig_abrt_handler);

log_level = 10;

args_parse (argc, argv);
printf (
"Telegram-client version " TG_VERSION ", Copyright (C) 2013 Vitaly Valtman\n"
Expand All @@ -502,6 +511,6 @@ int main (int argc, char **argv) {
#endif

inner_main ();

return 0;
}

0 comments on commit a87e6c6

Please sign in to comment.