]> icculus.org git repositories - taylor/freespace2.git/blob - src/platform/unix.cpp
fixed _splitpath for real this time
[taylor/freespace2.git] / src / platform / unix.cpp
1 #include <unistd.h>
2 #include <stdio.h>
3 #include <stdarg.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <ctype.h>
7 #include <errno.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10
11 #include "unix.h"
12
13 #define MAX_LINE_WIDTH 128
14
15 void strlwr (char * str)
16 {
17         while (*str) {*str = tolower (*str); str++; }
18 }
19
20 int filelength (int fd)
21 {
22         struct stat buf;
23         if (fstat (fd, &buf) == -1)
24                 return -1;
25                 
26         return buf.st_size;
27 }
28
29 unsigned long _beginthread (void (*pfuncStart)(void *), unsigned unStackSize, void* pArgList)
30 {
31         STUB_FUNCTION;
32         
33         return 0;
34 }
35
36 void Sleep (int mili)
37 {
38         usleep (mili * 1000);
39 }
40
41 void OutputDebugString (const char *str)
42 {
43         fprintf(stderr, "OutputDebugString: %s\n", str);
44 }
45
46 int WSAGetLastError()
47 {
48         return errno;
49 }
50
51 void _splitpath (const char *path, char *drive, char *dir, char *fname, char *ext)
52 {
53         if (path == NULL)
54                 return;
55
56         /* fs2 only uses fname */
57         if (fname != NULL) {
58                 const char *ls = strrchr(path, '/');
59                 if (ls != NULL) {
60                         ls++;           // move past '/'
61                 } else {
62                         ls = path;
63                 }
64         
65                 const char *lp = strrchr(path, '.');
66                 if (lp == NULL) {
67                         lp = ls + strlen(ls);   // move to the end
68                 }
69         
70                 int dist = lp-ls;
71                 if (dist > (_MAX_FNAME-1))
72                         dist = _MAX_FNAME-1;
73                 
74                 memset(fname, 0, _MAX_FNAME);   
75                 strncpy(fname, ls, dist);
76         }
77 }
78
79 int MulDiv(int a, int b, int c)
80 {
81         /* slow long long version */
82         __extension__ long long aa = a;
83         __extension__ long long bb = b;
84         __extension__ long long cc = c;
85         
86         __extension__ long long dd = aa * bb;
87         __extension__ long long ee = dd / cc;
88         
89         int retr = (int) ee;
90         
91         return retr;
92 }
93
94 /* mem debug junk */
95 #ifndef NDEBUG
96 //#define WANT_DEBUG
97 #endif
98
99 int TotalRam = 0;
100
101 #ifdef WANT_DEBUG
102 typedef struct RAM {
103         void *addr;
104         int size;
105         
106         char *file;
107         int line;
108         
109         RAM *next;
110 } RAM;
111
112 static RAM *RamTable;
113 #endif
114
115 void vm_free(void* ptr, char *file, int line)
116 {
117 #ifdef WANT_DEBUG
118         fprintf(stderr, "FREE: %s:%d addr = %p\n", file, line, ptr);
119         
120         RAM *item = RamTable;
121         RAM **mark = &RamTable;
122         
123         while (item != NULL) {
124                 if (item->addr == ptr) {
125                         RAM *tmp = item;
126                         
127                         *mark = item->next;
128                         
129                         free(tmp->addr);
130                         free(tmp);
131                         
132                         return;
133                 }
134                 
135                 mark = &(item->next);
136                 
137                 item = item->next;
138         }
139         
140         fprintf(stderr, "ERROR: vm_free caught invalid free: addr = %p, file = %s/%d\n", ptr, file, line);
141 #else   
142         free(ptr);
143 #endif
144 }
145
146 void *vm_malloc(int size, char *file, int line)
147 {
148 #ifdef WANT_DEBUG
149         fprintf(stderr, "MALLOC: %s:%d %d bytes\n", file, line, size);
150         
151         RAM *next = (RAM *)malloc(sizeof(RAM));
152         
153         next->addr = malloc(size);
154         next->size = size;
155         next->file = file;
156         next->line = line;
157         
158         next->next = RamTable;
159         RamTable = next;
160         
161         return next->addr;
162 #else
163         return malloc(size);
164 #endif  
165 }
166
167 char *vm_strdup(char const* str, char *file, int line)
168 {
169 #ifdef WANT_DEBUG
170         fprintf(stderr, "STRDUP: %s:%d\n", file, line);
171         
172         RAM *next = (RAM *)malloc(sizeof(RAM));
173         
174         next->addr = strdup(str);
175         next->size = strlen(str)+1;
176         next->file = file;
177         next->line = line;
178         
179         next->next = RamTable;
180         RamTable = next;
181         
182         return (char *)next->addr;
183 #else
184         return strdup(str);
185 #endif
186 }
187
188 void vm_dump()
189 {
190 #ifdef WANT_DEBUG
191         int i = 0;
192         int mem = 0;
193         fprintf(stderr, "\nDumping allocated memory:\n");
194         
195         RAM *ptr = RamTable;
196         while (ptr) {
197                 fprintf(stderr, "%d: file: %s:%d: addr:%p size:%d\n", i, ptr->file, ptr->line, ptr->addr, ptr->size);
198                 mem += ptr->size;
199                 ptr = ptr->next;
200                 i++;
201         }
202         
203         fprintf(stderr, "\nTotal of %d left-over bytes from %d allocations\n", mem, i);
204 #endif  
205 }
206
207 void windebug_memwatch_init()
208 {
209         TotalRam = 0;
210 }
211
212 /* error message debugging junk */
213 int Log_debug_output_to_file = 0;
214
215 void load_filter_info(void)
216 {
217 //      STUB_FUNCTION;
218 }
219
220 void outwnd_printf(char* id, char* format, ...)
221 {
222         char tmp[MAX_LINE_WIDTH*4];
223         va_list args;
224
225         va_start (args, format);
226         vsprintf (tmp, format, args);
227         va_end(args);
228         fprintf (stderr, "%s: %s\n", id, tmp);
229 }
230
231 void outwnd_printf2(char* format, ...)
232 {
233         char tmp[MAX_LINE_WIDTH*4];
234         va_list args;
235
236         va_start (args, format);
237         vsprintf (tmp, format, args);
238         va_end(args);
239         fprintf (stderr, "General: %s", tmp);
240 }
241
242 void outwnd_close()
243 {
244 //      STUB_FUNCTION;
245 }
246
247 void Warning( char * filename, int line, char * format, ... )
248 {
249         char tmp[MAX_LINE_WIDTH*4];
250         va_list args;
251
252         va_start (args, format);
253         vsprintf (tmp, format, args);
254         va_end(args);
255         fprintf (stderr, "Warning: (%s:%d): %s\n", filename, line, tmp);
256 }
257
258 void Error( char * filename, int line, char * format, ... )
259 {
260         char tmp[MAX_LINE_WIDTH*4];
261         va_list args;
262
263         va_start (args, format);
264         vsprintf (tmp, format, args);
265         va_end(args);
266         fprintf (stderr, "Error: (%s:%d): %s\n", filename, line, tmp);
267         exit (1);
268 }
269
270 void WinAssert(char * text,char *filename, int line)
271 {
272         fprintf (stderr, "Assertion: (%s:%d) %s\n", filename, line, text);
273 //      exit(1);
274 }