Mercurial > repos > blastem
comparison config.c @ 496:6fc71114d145
Extract function to determine executable directory from load_config so it can be used elsewhere
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Mon, 28 Oct 2013 21:48:46 -0700 |
parents | 39cad98d2789 |
children | 80a67be1770b |
comparison
equal
deleted
inserted
replaced
495:39cad98d2789 | 496:6fc71114d145 |
---|---|
6 #include "tern.h" | 6 #include "tern.h" |
7 #include "util.h" | 7 #include "util.h" |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <string.h> | 10 #include <string.h> |
11 | |
12 #include <sys/types.h> | |
13 #include <sys/stat.h> | |
14 #include <unistd.h> | |
15 | 11 |
16 #define MAX_NEST 30 //way more than I'll ever need | 12 #define MAX_NEST 30 //way more than I'll ever need |
17 | 13 |
18 tern_node * parse_config(char * config_data) | 14 tern_node * parse_config(char * config_data) |
19 { | 15 { |
100 fclose(config_file); | 96 fclose(config_file); |
101 open_fail: | 97 open_fail: |
102 return ret; | 98 return ret; |
103 } | 99 } |
104 | 100 |
105 char * readlink_alloc(char * path) | 101 tern_node * load_config() |
106 { | 102 { |
107 char * linktext = NULL; | 103 char * exe_dir; |
108 ssize_t linksize = 512; | |
109 ssize_t cursize = 0; | |
110 do { | |
111 if (linksize > cursize) { | |
112 cursize = linksize; | |
113 if (linktext) { | |
114 free(linktext); | |
115 } | |
116 } | |
117 linktext = malloc(cursize); | |
118 linksize = readlink(path, linktext, cursize-1); | |
119 if (linksize == -1) { | |
120 perror("readlink"); | |
121 free(linktext); | |
122 linktext = NULL; | |
123 } | |
124 } while (linksize > cursize); | |
125 return linktext; | |
126 } | |
127 | |
128 tern_node * load_config(char * expath) | |
129 { | |
130 char * linktext; | |
131 char * home = getenv("HOME"); | 104 char * home = getenv("HOME"); |
132 if (!home) { | 105 if (!home) { |
133 goto load_in_app_dir; | 106 goto load_in_app_dir; |
134 } | 107 } |
135 char * path = alloc_concat(home, "/.config/blastem/blastem.cfg"); | 108 char * path = alloc_concat(home, "/.config/blastem/blastem.cfg"); |
137 if (ret) { | 110 if (ret) { |
138 goto success; | 111 goto success; |
139 } | 112 } |
140 free(path); | 113 free(path); |
141 load_in_app_dir: | 114 load_in_app_dir: |
142 | 115 exe_dir = get_exe_dir(); |
143 linktext = readlink_alloc("/proc/self/exe"); | 116 if (!exe_dir) { |
144 if (!linktext) { | 117 goto no_config; |
145 goto link_prob; | |
146 } | 118 } |
147 char * cur; | 119 path = alloc_concat(exe_dir, "/default.cfg"); |
148 int linksize = strlen(linktext); | 120 ret = parse_config_file(path); |
149 for(cur = linktext + linksize - 1; cur != linktext; cur--) | 121 free(path); |
150 { | 122 success: |
151 if (*cur == '/') { | 123 if (ret) { |
152 *cur = 0; | 124 return ret; |
153 break; | |
154 } | |
155 } | 125 } |
156 if (cur == linktext) { | 126 no_config: |
157 goto link_prob; | |
158 } | |
159 path = alloc_concat(linktext, "/default.cfg"); | |
160 ret = parse_config_file(path); | |
161 success: | |
162 return ret; | |
163 link_prob: | |
164 if (linktext) { | |
165 free(linktext); | |
166 } | |
167 no_proc: | |
168 //TODO: Fall back to using expath if /proc is not available | |
169 fputs("Failed to find a config file in ~/.config/blastem/blastem.cfg or in the blastem executable directory\n", stderr); | 127 fputs("Failed to find a config file in ~/.config/blastem/blastem.cfg or in the blastem executable directory\n", stderr); |
170 exit(1); | 128 exit(1); |
171 } | 129 } |
172 | 130 |