summaryrefslogtreecommitdiff
path: root/testing/toxic/configdir.c
diff options
context:
space:
mode:
authorSimon Levermann <simon@levermaenner.de>2013-08-08 16:36:16 +0200
committerSimon Levermann <simon@levermaenner.de>2013-08-08 16:36:16 +0200
commitb5f5b1a111d9e08b2b001b5d4d76629019462f39 (patch)
tree5f9718d83923b7e6d9330c5d99bd153d97a5774c /testing/toxic/configdir.c
parent97e178db3aa9485ab49c646071305164cb5a7681 (diff)
Cleanup and Error fixes
Add several frees that were missing to prevent memory leaks Replace strcpy with strdup where appropriate Replace _stat with __stat64 for building on Windows
Diffstat (limited to 'testing/toxic/configdir.c')
-rw-r--r--testing/toxic/configdir.c36
1 files changed, 13 insertions, 23 deletions
diff --git a/testing/toxic/configdir.c b/testing/toxic/configdir.c
index 7d11d020..d91d0804 100644
--- a/testing/toxic/configdir.c
+++ b/testing/toxic/configdir.c
@@ -56,10 +56,8 @@ char *get_user_config_dir(void)
56 ) 56 )
57 if (!result) return NULL; 57 if (!result) return NULL;
58 58
59 user_config_dir = malloc(strlen(appdata) + 1); 59 user_config_dir = strdup(appdata);
60 if (user_config_dir) { 60
61 strcpy(user_config_dir, appdata);
62 }
63 return user_config_dir; 61 return user_config_dir;
64 62
65 #elif defined __APPLE__ 63 #elif defined __APPLE__
@@ -78,10 +76,7 @@ char *get_user_config_dir(void)
78 #else 76 #else
79 77
80 if (getenv("XDG_CONFIG_HOME")) { 78 if (getenv("XDG_CONFIG_HOME")) {
81 user_config_dir = malloc(strlen(getenv("XDG_CONFIG_HOME")) + 1); 79 user_config_dir = strdup(getenv("XDG_CONFIG_HOME"));
82 if (user_config_dir) {
83 strcpy(user_config_dir, getenv("XDG_CONFIG_HOME"));
84 }
85 } else { 80 } else {
86 user_config_dir = malloc(strlen(getenv("HOME")) + strlen("/.config") + 1); 81 user_config_dir = malloc(strlen(getenv("HOME")) + strlen("/.config") + 1);
87 if (user_config_dir) { 82 if (user_config_dir) {
@@ -109,12 +104,10 @@ int create_user_config_dir(char *path)
109 strcat(fullpath, CONFIGDIR); 104 strcat(fullpath, CONFIGDIR);
110 105
111 mkdir_err = _mkdir(fullpath); 106 mkdir_err = _mkdir(fullpath);
112 107 struct __stat64 buf;
113 if (mkdir_err) { 108 if (mkdir_err && (errno != EEXIST || _wstat64(fullpath, &buf) || !S_ISDIR(buf.st_mode))) {
114 if(errno != EEXIST) return -1; 109 free(fullpath);
115 struct _stat buf; 110 return -1;
116 if(_wstat64(fullpath, &buf)) return -1;
117 if(!S_ISDIR(buf.st_mode)) return -1;
118 } 111 }
119 112
120 #else 113 #else
@@ -122,10 +115,8 @@ int create_user_config_dir(char *path)
122 mkdir_err = mkdir(path, 0700); 115 mkdir_err = mkdir(path, 0700);
123 struct stat buf; 116 struct stat buf;
124 117
125 if(mkdir_err) { 118 if(mkdir_err && (errno != EEXIST || stat(path, &buf) || !S_ISDIR(buf.st_mode))) {
126 if(errno != EEXIST) return -1; 119 return -1;
127 if(stat(path, &buf)) return -1;
128 if(!S_ISDIR(buf.st_mode)) return -1;
129 } 120 }
130 121
131 char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1); 122 char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1);
@@ -134,13 +125,12 @@ int create_user_config_dir(char *path)
134 125
135 mkdir_err = mkdir(fullpath, 0700); 126 mkdir_err = mkdir(fullpath, 0700);
136 127
137 if(mkdir_err) { 128 if(mkdir_err && (errno != EEXIST || stat(fullpath, &buf) || !S_ISDIR(buf.st_mode))) {
138 if(errno != EEXIST) return -1; 129 free(fullpath);
139 if(stat(fullpath, &buf)) return -1; 130 return -1;
140 if(!S_ISDIR(buf.st_mode)) return -1;
141 } 131 }
142 132
143 #endif 133 #endif
144 134
145 return 0; 135 return 0;
146} \ No newline at end of file 136}