Skip to content

Commit

Permalink
Implement XDG Base Directory Specification
Browse files Browse the repository at this point in the history
Closes: #63
  • Loading branch information
bbonev committed Feb 18, 2024
1 parent b15743b commit cea6d5c
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/configfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ You should have received a copy of the GNU General Public License along with thi
#include <sys/stat.h>
#include <sys/types.h>

#define CONFIG_PATH "/.config/iotop"
#define CONFIG_DIR1 "/.config"
#define CONFIG_DIR2 "/iotop"
#define CONFIG_NAME "/iotoprc"
#define MAX_OPT 50

Expand Down Expand Up @@ -48,15 +49,25 @@ static inline void mkdir_p(const char *dir) {

static inline FILE *config_file_open(const char *mode) {
char path[PATH_MAX];
char *xdgconfig;
char *home;

xdgconfig=getenv("XDG_CONFIG_HOME");
home=getenv("HOME");
if (!home)
home="";

strcpy(path,home);
strcat(path,CONFIG_PATH);
mkdir_p(path);
if (xdgconfig) {
strcpy(path,xdgconfig);
strcat(path,CONFIG_DIR2);
mkdir_p(path);
} else {
if (home)
strcpy(path,home);
else
strcpy(path,"");
strcat(path,CONFIG_DIR1);
strcat(path,CONFIG_DIR2);
mkdir_p(path);
}
strcat(path,CONFIG_NAME);

return fopen(path,mode);
Expand Down

0 comments on commit cea6d5c

Please sign in to comment.