Mercurial > repos > blastem
comparison util.c @ 762:206c449eaa81
Get "portable" builds working on Linux and add a build time check for whether /proc exists
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 28 Jun 2015 18:44:11 -0700 |
parents | fc68992cf18d |
children | 2a25ea5d94f7 2f48a3c187c6 |
comparison
equal
deleted
inserted
replaced
761:ffb8cc5845fa | 762:206c449eaa81 |
---|---|
136 | 136 |
137 char * get_exe_dir() | 137 char * get_exe_dir() |
138 { | 138 { |
139 static char * exe_dir; | 139 static char * exe_dir; |
140 if (!exe_dir) { | 140 if (!exe_dir) { |
141 char * cur; | |
142 #ifndef HAS_PROC | |
141 char * linktext = readlink_alloc("/proc/self/exe"); | 143 char * linktext = readlink_alloc("/proc/self/exe"); |
142 if (!linktext) { | 144 if (!linktext) { |
143 goto fallback; | 145 goto fallback; |
144 } | 146 } |
145 char * cur; | |
146 int linksize = strlen(linktext); | 147 int linksize = strlen(linktext); |
147 for(cur = linktext + linksize - 1; cur != linktext; cur--) | 148 for(cur = linktext + linksize - 1; cur != linktext; cur--) |
148 { | 149 { |
149 if (*cur == '/') { | 150 if (*cur == '/') { |
150 *cur = 0; | 151 *cur = 0; |
152 } | 153 } |
153 } | 154 } |
154 if (cur == linktext) { | 155 if (cur == linktext) { |
155 free(linktext); | 156 free(linktext); |
156 fallback: | 157 fallback: |
158 #endif | |
157 if (!exe_str) { | 159 if (!exe_str) { |
158 fputs("/proc/self/exe is not available and set_exe_str was not called!", stderr); | 160 fputs("/proc/self/exe is not available and set_exe_str was not called!", stderr); |
159 } | 161 } |
160 int pathsize = strlen(exe_str); | 162 int pathsize = strlen(exe_str); |
161 for(cur = exe_str + pathsize - 1; cur != exe_str; cur--) | 163 for(cur = exe_str + pathsize - 1; cur != exe_str; cur--) |
165 memcpy(exe_dir, exe_str, cur-exe_str); | 167 memcpy(exe_dir, exe_str, cur-exe_str); |
166 exe_dir[cur-exe_str] = 0; | 168 exe_dir[cur-exe_str] = 0; |
167 break; | 169 break; |
168 } | 170 } |
169 } | 171 } |
172 #ifndef HAS_PROC | |
170 } else { | 173 } else { |
171 exe_dir = linktext; | 174 exe_dir = linktext; |
172 } | 175 } |
176 #endif | |
173 } | 177 } |
174 return exe_dir; | 178 return exe_dir; |
175 } | 179 } |
176 | 180 |
177 #endif | 181 #endif |