Skip to content

Commit

Permalink
make sure custom config file is malloc'd and freed accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
allinurl committed Aug 21, 2013
1 parent 4e2809a commit 020e687
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 36 deletions.
8 changes: 5 additions & 3 deletions goaccess.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ cmd_help (void)
{
printf ("\nGoAccess - %s\n\n", GO_VERSION);
printf ("Usage: ");
printf ("goaccess [-e IP_ADDRESS][-a][-r][-c][-p CONFIGFILE] -f log_file\n\n");
printf ("goaccess [-e IP_ADDRESS][-a][-r][-c][-p CONFFILE] -f log_file\n\n");
printf ("The following options can also be supplied to the command:\n\n");
printf (" -f <argument> - Path to input log file.\n");
printf (" -c - Prompt log/date configuration window.\n");
Expand Down Expand Up @@ -643,7 +643,9 @@ main (int argc, char *argv[])
conf.ifile = optarg;
break;
case 'p':
realpath( optarg, conf.iconfigfile );
if (realpath (optarg, conf.iconfigfile) == 0)
error_handler (__PRETTY_FUNCTION__, __FILE__, __LINE__,
strerror (errno));
break;
case 'e':
conf.ignore_host = optarg;
Expand All @@ -664,7 +666,7 @@ main (int argc, char *argv[])
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
return 1;
return EXIT_FAILURE;
default:
abort ();
}
Expand Down
58 changes: 26 additions & 32 deletions settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,24 @@ set_conf_vars (int key, char *val)
int
parse_conf_file ()
{
char *path = NULL, *conf_file = NULL, *user_home = NULL;
char *path = NULL, *user_home = NULL;
char *val, *c;
int key = 0;
FILE *file;
if (conf.iconfigfile != NULL){
conf_file = conf.iconfigfile;
}else{

if (conf.iconfigfile[0] != '\0')
path = alloc_string (conf.iconfigfile);
else {
user_home = getenv ("HOME");
if (user_home == NULL)
user_home = "";
user_home = "";

path = xmalloc (snprintf (NULL, 0, "%s/.goaccessrc", user_home) + 1);
path = xmalloc (snprintf (NULL, 0, "%s/.goaccessrc", user_home) + 1);
sprintf (path, "%s/.goaccessrc", user_home);
conf_file = path;
}
file = fopen (path, "r");

file = fopen (conf_file, "r");

/* could not open conf file, if so prompt conf dialog */
if (file == NULL) {
free (path);
return -1;
Expand All @@ -118,9 +117,7 @@ parse_conf_file ()
return -1;
}
for (c = val; *c; c++) {
/*
* get everything after space
*/
/* get everything after the space */
if (!isspace (c[0])) {
set_conf_vars (key, trim_str (c));
break;
Expand All @@ -136,40 +133,37 @@ parse_conf_file ()
void
write_conf_file ()
{
char *user_home;
user_home = getenv ("HOME");
if (user_home == NULL)
user_home = "";
FILE *file;
char *path = NULL, *user_home = NULL;

char *path = xmalloc (snprintf (NULL, 0, "%s/.goaccessrc", user_home) + 1);
sprintf (path, "%s/.goaccessrc", user_home);
if (conf.iconfigfile[0] != '\0')
path = alloc_string (conf.iconfigfile);
else {
user_home = getenv ("HOME");
if (user_home == NULL)
user_home = "";

path = xmalloc (snprintf (NULL, 0, "%s/.goaccessrc", user_home) + 1);
sprintf (path, "%s/.goaccessrc", user_home);
}

FILE *file;
file = fopen (path, "w");
/*
* no file available
*/
/* no file available */
if (file == NULL) {
free (path);
return;
}

/*
* color scheme
*/
/* color scheme */
fprintf (file, "color_scheme %d\n", conf.color_scheme);

/*
* date format
*/
/* date format */
if (tmp_date_format)
fprintf (file, "date_format %s\n", tmp_date_format);
else
fprintf (file, "date_format %s\n", conf.date_format);

/*
* log format
*/
/* log format */
if (tmp_log_format)
fprintf (file, "log_format %s", tmp_log_format);
else
Expand Down
4 changes: 3 additions & 1 deletion settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#ifndef SETTINGS_H_INCLUDED
#define SETTINGS_H_INCLUDED

#include <limits.h>

/* predefined log dates */
typedef struct GPreConfDate_
{
Expand Down Expand Up @@ -49,7 +51,7 @@ typedef struct GConfKeyword_
typedef struct GConf_
{
char *ifile;
char iconfigfile[200];
char iconfigfile[_POSIX_PATH_MAX];
char *ignore_host;
char *date_format;
char *log_format;
Expand Down

0 comments on commit 020e687

Please sign in to comment.