| skipped 11 lines |
12 | 12 | | |
13 | 13 | | static int mkdir_recursive(const char *dir) |
14 | 14 | | { |
15 | | - | char path[FILENAME_MAX]; |
| 15 | + | char path[FILENAME_MAX] = {0}; |
16 | 16 | | size_t index, size = strlen(dir); |
17 | 17 | | |
18 | 18 | | if (size + 2 > FILENAME_MAX) { |
| skipped 3 lines |
22 | 22 | | |
23 | 23 | | strcpy(path, dir); |
24 | 24 | | |
25 | | - | if (path[size - 1] != '/') { |
26 | | - | path[size] = '/'; |
27 | | - | path[size + 1] = '\0'; |
28 | | - | } |
29 | | - | |
30 | | - | for (index = path[0] == '/' ? 1 : 0; index < size; ++index) { |
31 | | - | if (path[index] == '/') { |
| 25 | + | for (index = path[0] == '/' ? 1 : 0; index < size + 1; ++index) { |
| 26 | + | if (path[index] == '/' || path[index] == '\0') { |
32 | 27 | | path[index] = '\0'; |
33 | 28 | | if (mkdir(path, 0777) < 0 && errno != EEXIST) { |
34 | 29 | | return -1; |
| skipped 6 lines |
41 | 36 | | } |
42 | 37 | | |
43 | 38 | | static int create_config_file(const char *path, const char *file_name) { |
44 | | - | char file_path [FILENAME_MAX]; |
| 39 | + | char file_path [FILENAME_MAX] = {0}; |
45 | 40 | | FILE *file; |
46 | 41 | | |
47 | 42 | | if (strlen(path) + strlen(file_name) + 1 > FILENAME_MAX) { |
| skipped 6 lines |
54 | 49 | | file = fopen(file_path, "w"); |
55 | 50 | | |
56 | 51 | | if (file == NULL) { |
57 | | - | fprintf(stderr, "Config file couldn't be created.\n"); |
| 52 | + | fprintf(stderr, "Config file couldn't be created. errno %i\n", errno); |
58 | 53 | | return -1; |
59 | 54 | | } |
60 | 55 | | |
| skipped 1 lines |
62 | 57 | | return 0; |
63 | 58 | | } |
64 | 59 | | |
65 | | - | int ziit_select_config_path(char **config_path) |
| 60 | + | int ziit_select_config_path(char *config_path) |
66 | 61 | | { |
67 | 62 | | const char *path; |
68 | 63 | | const char *file_name = "/client.cfg"; |
69 | | - | char file_path [FILENAME_MAX]; |
| 64 | + | char file_path [FILENAME_MAX] = {0}; |
70 | 65 | | char *expanded_path = NULL; |
71 | 66 | | struct stat buffer; |
| 67 | + | int status = 0; |
72 | 68 | | |
73 | 69 | | for (int i = 0; i < ZIIT_NUMBER_OF_CONFIG_PATHS; ++i) { |
74 | 70 | | path = config_paths[i]; |
| skipped 2 lines |
77 | 73 | | expanded_path = cfg_tilde_expand(file_path); |
78 | 74 | | |
79 | 75 | | if (expanded_path == NULL) { |
80 | | - | fprintf(stderr, "Out of Memory\n"); |
81 | | - | return -2; |
| 76 | + | status = -2; |
| 77 | + | goto RETURN_NO_MEMORY; |
82 | 78 | | } |
83 | 79 | | |
84 | 80 | | if (stat(expanded_path, &buffer) == 0) { |
85 | 81 | | expanded_path[strlen(expanded_path) - strlen(file_name)] = '\0'; |
86 | | - | *config_path = expanded_path; |
87 | | - | return 0; |
| 82 | + | goto RETURN_ON_SUCCESS; |
88 | 83 | | } |
89 | 84 | | |
90 | 85 | | free(expanded_path); |
| skipped 2 lines |
93 | 88 | | expanded_path = cfg_tilde_expand(config_paths[0]); |
94 | 89 | | |
95 | 90 | | if (expanded_path == NULL) { |
96 | | - | fprintf(stderr, "Out of Memory\n"); |
97 | | - | return -2; |
| 91 | + | status = -2; |
| 92 | + | goto RETURN_NO_MEMORY; |
98 | 93 | | } |
99 | 94 | | |
100 | 95 | | if (mkdir_recursive(expanded_path) == 0) { |
101 | 96 | | |
102 | 97 | | if (create_config_file(expanded_path, file_name) != 0){ |
103 | | - | free(expanded_path); |
104 | | - | return -1; |
| 98 | + | status = -1; |
| 99 | + | goto RETURN_ON_ERROR; |
105 | 100 | | } |
106 | 101 | | |
107 | | - | *config_path = expanded_path; |
108 | | - | return 0; |
| 102 | + | goto RETURN_ON_SUCCESS; |
109 | 103 | | } else { |
110 | 104 | | fprintf(stderr, "Couldn't create path %s\n", expanded_path); |
111 | | - | free(expanded_path); |
112 | | - | return -1; |
| 105 | + | status = -1; |
| 106 | + | goto RETURN_ON_ERROR; |
113 | 107 | | } |
| 108 | + | |
| 109 | + | RETURN_ON_SUCCESS: |
| 110 | + | strcpy(config_path, expanded_path); |
| 111 | + | RETURN_ON_ERROR: |
| 112 | + | free(expanded_path); |
| 113 | + | RETURN_NO_MEMORY: |
| 114 | + | return status; |
114 | 115 | | } |
115 | 116 | | |