annotate util.c @ 794:792be135d3af

Spawn a terminal for the debugger when needed if we are not already attached to one
author Michael Pavone <pavone@retrodev.com>
date Sun, 26 Jul 2015 01:11:04 -0700
parents 724bbec47f86
children 0b692b5d154b
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>
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
6
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
7 #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
8 #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
9 #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
10
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
11 #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
12 #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
13
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
14 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
15 {
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 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
17 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
18 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
19 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
20 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
21 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
22 }
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 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
25 {
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 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
27 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
28 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
29 }
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 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
31 *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
32 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
33 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
34 }
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 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
36 }
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 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
39 {
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 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
41 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
42 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
43 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
44 }
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 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
47 {
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 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
49 {
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 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
51 }
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 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
53 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
54 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
55 {
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 *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
57 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
58 }
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 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
60 }
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 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
63 {
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 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
65 {
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 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
67 }
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 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
69 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
70 }
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 *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
72 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
73 }
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
74
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
75 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
76 {
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 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
78 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
79 {
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 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
81 }
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 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
83 }
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
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
85 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
86
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 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
88 {
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 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
90 }
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
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
92 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
93 {
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 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
95 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
96 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
97 //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
98 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
99 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
100 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
101 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
102 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
103 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
104 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
105 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
106 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
107 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
108 }
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 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
110 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
111 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
112 } 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
113 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
114 }
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 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
116 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
117 }
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
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
119 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
120 {
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 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
122 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
123 #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
124 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
125 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
126 } 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
127 #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
128 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
129 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
130 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
131 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
132 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
133 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
134 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
135 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
136 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
137 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
138 }
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 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
140 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
141 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
142 #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
143 }
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 #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
145 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
146 }
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
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
148 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
149 {
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 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
151 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
152 #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
153 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
154 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
155 } 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
156 #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
157 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
158 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
159 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
160 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
161 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
162 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
163 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
164 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
165 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
166 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
167 }
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 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
169 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
170 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
171 #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
172 }
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 #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
174 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
175 }
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
742
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
177 #ifdef _WIN32
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
178 #include "Shlobj.h"
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
179 #include "Windows.h"
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
180
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
181 char * get_home_dir()
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
182 {
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
183 static char path[MAX_PATH];
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
184 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
185 return path;
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
186 }
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 char * get_exe_dir()
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
189 {
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
190 static char path[MAX_PATH];
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
191 HMODULE module = GetModuleHandleA(NULL);
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
192 GetModuleFileNameA(module, path, MAX_PATH);
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
193
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
194 int pathsize = strlen(path);
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
195 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
196 {
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
197 if (*cur == '\\') {
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
198 *cur = 0;
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
199 break;
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
200 }
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 return path;
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
203 }
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 #else
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
206
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
207 char * get_home_dir()
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
208 {
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
209 return getenv("HOME");
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
210 }
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
211
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
212 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
213 {
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 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
215 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
216 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
217 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
218 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
219 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
220 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
221 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
222 }
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 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
225 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
226 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
227 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
228 free(linktext);
559
6b248602ab84 blastem builds and almost works on OS X now
Mike Pavone <pavone@retrodev.com>
parents: 549
diff changeset
229 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
230 }
549
32da1e0d5e55 Properly null terminate string returned by readlink in util.c
Michael Pavone <pavone@retrodev.com>
parents: 496
diff changeset
231 } while ((linksize+1) > cursize);
32da1e0d5e55 Properly null terminate string returned by readlink in util.c
Michael Pavone <pavone@retrodev.com>
parents: 496
diff changeset
232 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
233 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
234 }
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 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
237 {
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 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
239 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
240 char * cur;
763
2a25ea5d94f7 Fix sense of HAS_PROC check
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
241 #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
242 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
243 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
244 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
245 }
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 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
247 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
248 {
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 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
250 *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
251 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
252 }
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 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
255 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
256 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
257 #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
258 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
259 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
260 }
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 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
262 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
263 {
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 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
265 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
266 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
267 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
268 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
269 }
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 }
763
2a25ea5d94f7 Fix sense of HAS_PROC check
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
271 #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
272 } 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
273 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
274 }
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
275 #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
276 }
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 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
278 }
742
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
279
741
80a67be1770b Initial work on Windows port
Michael Pavone <pavone@retrodev.com>
parents: 549
diff changeset
280 #endif