annotate util.c @ 805:3eced113081c

Pre-release cleanup
author Michael Pavone <pavone@retrodev.com>
date Sun, 26 Jul 2015 18:29:14 -0700
parents 0b692b5d154b
children 69a6ec208111
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
495
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1 #include <string.h>
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
2 #include <stdlib.h>
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
3 #include <stdio.h>
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
4 #include <ctype.h>
768
2f48a3c187c6 Add support for reading cartridge memory map from ROM database, though without EEPROM support for now
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
5 #include <stdint.h>
805
3eced113081c Pre-release cleanup
Michael Pavone <pavone@retrodev.com>
parents: 799
diff changeset
6 #include <stdarg.h>
495
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
7
496
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
8 #include <sys/types.h>
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
9 #include <sys/stat.h>
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
10 #include <unistd.h>
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
11
792
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
12 #include "blastem.h" //for headless global
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
13 #include "render.h" //for render_errorbox
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
14
495
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
15 char * alloc_concat(char * first, char * second)
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
16 {
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
17 int flen = strlen(first);
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
18 int slen = strlen(second);
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
19 char * ret = malloc(flen + slen + 1);
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
20 memcpy(ret, first, flen);
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
21 memcpy(ret+flen, second, slen+1);
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
22 return ret;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
23 }
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
24
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
25 char * alloc_concat_m(int num_parts, char ** parts)
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
26 {
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
27 int total = 0;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
28 for (int i = 0; i < num_parts; i++) {
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
29 total += strlen(parts[i]);
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
30 }
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
31 char * ret = malloc(total + 1);
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
32 *ret = 0;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
33 for (int i = 0; i < num_parts; i++) {
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
34 strcat(ret, parts[i]);
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
35 }
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
36 return ret;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
37 }
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
38
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
39 long file_size(FILE * f)
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
40 {
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
41 fseek(f, 0, SEEK_END);
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
42 long fsize = ftell(f);
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
43 fseek(f, 0, SEEK_SET);
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
44 return fsize;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
45 }
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
46
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
47 char * strip_ws(char * text)
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
48 {
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
49 while (*text && (!isprint(*text) || isblank(*text)))
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
50 {
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
51 text++;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
52 }
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
53 char * ret = text;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
54 text = ret + strlen(ret) - 1;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
55 while (text > ret && (!isprint(*text) || isblank(*text)))
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
56 {
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
57 *text = 0;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
58 text--;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
59 }
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
60 return ret;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
61 }
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
62
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
63 char * split_keyval(char * text)
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
64 {
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
65 while (*text && !isblank(*text))
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
66 {
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
67 text++;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
68 }
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
69 if (!*text) {
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
70 return text;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
71 }
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
72 *text = 0;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
73 return text+1;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
74 }
496
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
75
768
2f48a3c187c6 Add support for reading cartridge memory map from ROM database, though without EEPROM support for now
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
76 uint32_t nearest_pow2(uint32_t val)
2f48a3c187c6 Add support for reading cartridge memory map from ROM database, though without EEPROM support for now
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
77 {
2f48a3c187c6 Add support for reading cartridge memory map from ROM database, though without EEPROM support for now
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
78 uint32_t ret = 1;
2f48a3c187c6 Add support for reading cartridge memory map from ROM database, though without EEPROM support for now
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
79 while (ret < val)
2f48a3c187c6 Add support for reading cartridge memory map from ROM database, though without EEPROM support for now
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
80 {
2f48a3c187c6 Add support for reading cartridge memory map from ROM database, though without EEPROM support for now
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
81 ret = ret << 1;
2f48a3c187c6 Add support for reading cartridge memory map from ROM database, though without EEPROM support for now
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
82 }
2f48a3c187c6 Add support for reading cartridge memory map from ROM database, though without EEPROM support for now
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
83 return ret;
2f48a3c187c6 Add support for reading cartridge memory map from ROM database, though without EEPROM support for now
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
84 }
2f48a3c187c6 Add support for reading cartridge memory map from ROM database, though without EEPROM support for now
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
85
496
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
86 static char * exe_str;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
87
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
88 void set_exe_str(char * str)
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
89 {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
90 exe_str = str;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
91 }
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
92
792
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
93 void fatal_error(char *format, ...)
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
94 {
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
95 va_list args;
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
96 va_start(args, format);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
97 if (!headless) {
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
98 //take a guess at the final size
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
99 size_t size = strlen(format) * 2;
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
100 char *buf = malloc(size);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
101 size_t actual = vsnprintf(buf, size, format, args);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
102 if (actual >= size) {
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
103 actual++;
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
104 free(buf);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
105 buf = malloc(actual);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
106 va_end(args);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
107 va_start(args, format);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
108 vsnprintf(buf, actual, format, args);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
109 }
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
110 fputs(buf, stderr);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
111 render_errorbox("Fatal Error", buf);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
112 free(buf);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
113 } else {
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
114 vfprintf(stderr, format, args);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
115 }
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
116 va_end(args);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
117 exit(1);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
118 }
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
119
794
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
120 void warning(char *format, ...)
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
121 {
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
122 va_list args;
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
123 va_start(args, format);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
124 #ifndef _WIN32
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
125 if (headless || (isatty(STDERR_FILENO) && isatty(STDIN_FILENO))) {
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
126 vfprintf(stderr, format, args);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
127 } else {
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
128 #endif
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
129 size_t size = strlen(format) * 2;
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
130 char *buf = malloc(size);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
131 size_t actual = vsnprintf(buf, size, format, args);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
132 if (actual >= size) {
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
133 actual++;
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
134 free(buf);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
135 buf = malloc(actual);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
136 va_end(args);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
137 va_start(args, format);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
138 vsnprintf(buf, actual, format, args);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
139 }
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
140 fputs(buf, stderr);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
141 render_infobox("BlastEm Info", buf);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
142 free(buf);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
143 #ifndef _WIN32
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
144 }
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
145 #endif
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
146 va_end(args);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
147 }
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
148
792
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
149 void info_message(char *format, ...)
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
150 {
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
151 va_list args;
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
152 va_start(args, format);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
153 #ifndef _WIN32
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
154 if (headless || (isatty(STDOUT_FILENO) && isatty(STDIN_FILENO))) {
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
155 vprintf(format, args);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
156 } else {
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
157 #endif
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
158 size_t size = strlen(format) * 2;
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
159 char *buf = malloc(size);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
160 size_t actual = vsnprintf(buf, size, format, args);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
161 if (actual >= size) {
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
162 actual++;
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
163 free(buf);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
164 buf = malloc(actual);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
165 va_end(args);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
166 va_start(args, format);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
167 vsnprintf(buf, actual, format, args);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
168 }
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
169 fputs(buf, stdout);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
170 render_infobox("BlastEm Info", buf);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
171 free(buf);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
172 #ifndef _WIN32
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
173 }
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
174 #endif
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
175 va_end(args);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
176 }
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
177
742
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
178 #ifdef _WIN32
795
bce97fc0bb8a Fix mingw-w64 build and cross-compilation
=?UTF-8?q?Higor=20Eur=C3=ADpedes?= <heuripedes@gmail.com>
parents: 773
diff changeset
179 #include <windows.h>
bce97fc0bb8a Fix mingw-w64 build and cross-compilation
=?UTF-8?q?Higor=20Eur=C3=ADpedes?= <heuripedes@gmail.com>
parents: 773
diff changeset
180 #include <shlobj.h>
742
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
181
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
182 char * get_home_dir()
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
183 {
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
184 static char path[MAX_PATH];
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
185 SHGetFolderPathA(NULL, CSIDL_PROFILE, NULL, 0, path);
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
186 return path;
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
187 }
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
188
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
189 char * get_exe_dir()
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
190 {
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
191 static char path[MAX_PATH];
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
192 HMODULE module = GetModuleHandleA(NULL);
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
193 GetModuleFileNameA(module, path, MAX_PATH);
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
194
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
195 int pathsize = strlen(path);
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
196 for(char * cur = path + pathsize - 1; cur != path; cur--)
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
197 {
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
198 if (*cur == '\\') {
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
199 *cur = 0;
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
200 break;
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
201 }
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
202 }
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
203 return path;
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
204 }
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
205
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
206 #else
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
207
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
208 char * get_home_dir()
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
209 {
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
210 return getenv("HOME");
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
211 }
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
212
496
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
213 char * readlink_alloc(char * path)
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
214 {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
215 char * linktext = NULL;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
216 ssize_t linksize = 512;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
217 ssize_t cursize = 0;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
218 do {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
219 if (linksize > cursize) {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
220 cursize = linksize;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
221 if (linktext) {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
222 free(linktext);
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
223 }
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
224 }
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
225 linktext = malloc(cursize);
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
226 linksize = readlink(path, linktext, cursize-1);
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
227 if (linksize == -1) {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
228 perror("readlink");
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
229 free(linktext);
559
6b248602ab84 blastem builds and almost works on OS X now
Mike Pavone <pavone@retrodev.com>
parents: 549
diff changeset
230 return NULL;
496
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
231 }
549
32da1e0d5e55 Properly null terminate string returned by readlink in util.c
Michael Pavone <pavone@retrodev.com>
parents: 496
diff changeset
232 } while ((linksize+1) > cursize);
32da1e0d5e55 Properly null terminate string returned by readlink in util.c
Michael Pavone <pavone@retrodev.com>
parents: 496
diff changeset
233 linktext[linksize] = 0;
496
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
234 return linktext;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
235 }
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
236
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
237 char * get_exe_dir()
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
238 {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
239 static char * exe_dir;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
240 if (!exe_dir) {
762
206c449eaa81 Get "portable" builds working on Linux and add a build time check for whether /proc exists
Michael Pavone <pavone@retrodev.com>
parents: 744
diff changeset
241 char * cur;
763
2a25ea5d94f7 Fix sense of HAS_PROC check
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
242 #ifdef HAS_PROC
496
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
243 char * linktext = readlink_alloc("/proc/self/exe");
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
244 if (!linktext) {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
245 goto fallback;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
246 }
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
247 int linksize = strlen(linktext);
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
248 for(cur = linktext + linksize - 1; cur != linktext; cur--)
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
249 {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
250 if (*cur == '/') {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
251 *cur = 0;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
252 break;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
253 }
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
254 }
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
255 if (cur == linktext) {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
256 free(linktext);
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
257 fallback:
762
206c449eaa81 Get "portable" builds working on Linux and add a build time check for whether /proc exists
Michael Pavone <pavone@retrodev.com>
parents: 744
diff changeset
258 #endif
496
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
259 if (!exe_str) {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
260 fputs("/proc/self/exe is not available and set_exe_str was not called!", stderr);
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
261 }
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
262 int pathsize = strlen(exe_str);
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
263 for(cur = exe_str + pathsize - 1; cur != exe_str; cur--)
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
264 {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
265 if (*cur == '/') {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
266 exe_dir = malloc(cur-exe_str+1);
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
267 memcpy(exe_dir, exe_str, cur-exe_str);
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
268 exe_dir[cur-exe_str] = 0;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
269 break;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
270 }
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
271 }
763
2a25ea5d94f7 Fix sense of HAS_PROC check
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
272 #ifdef HAS_PROC
496
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
273 } else {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
274 exe_dir = linktext;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
275 }
762
206c449eaa81 Get "portable" builds working on Linux and add a build time check for whether /proc exists
Michael Pavone <pavone@retrodev.com>
parents: 744
diff changeset
276 #endif
496
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
277 }
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
278 return exe_dir;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
279 }
742
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
280
741
80a67be1770b Initial work on Windows port
Michael Pavone <pavone@retrodev.com>
parents: 549
diff changeset
281 #endif