fixed segv for files with wrong font types
[swftools.git] / lib / os.c
bloba4142ff04e051750eebb14778fa6621fd2638d5d
1 /* os.c
3 operating system dependent functions
5 Part of the swftools package.
7 Copyright (c) 2005 Matthias Kramm <kramm@quiss.org>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
23 #include "os.h"
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #ifdef WIN32
28 #include <windows.h>
29 #else
30 #include <unistd.h>
31 #include <fcntl.h>
32 #endif
33 #ifdef HAVE_SYS_STAT_H
34 #include <sys/stat.h>
35 #else
36 #undef HAVE_STAT
37 #endif
38 #ifdef HAVE_SYS_MMAN_H
39 #include <sys/mman.h>
40 #else
41 #undef HAVE_MMAP
42 #endif
43 #ifdef HAVE_SYS_TYPES_H
44 #include <sys/types.h>
45 #else
46 #undef HAVE_STAT
47 #endif
49 #if defined(CYGWIN)
50 char path_seperator = '/';
51 #elif defined(WIN32)
52 char path_seperator = '\\';
53 #else
54 char path_seperator = '/';
55 #endif
57 #ifdef WIN32
58 char* getRegistryEntry(char*path)
60 int res = 0;
61 HKEY key;
62 long rc;
63 long size = 0;
64 DWORD type;
65 char*buf;
66 rc = RegOpenKeyEx(HKEY_CURRENT_USER, path, 0, KEY_ALL_ACCESS, &key);
67 if(rc)
68 rc = RegOpenKeyEx(HKEY_CURRENT_USER, path, 0, KEY_READ, &key);
69 if(rc)
70 rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_ALL_ACCESS, &key);
71 if(rc)
72 rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_READ, &key);
74 if (rc) {
75 fprintf(stderr, "RegOpenKeyEx failed\n");
76 return 0;
78 rc = RegQueryValueEx(key, NULL, 0, 0, 0, (LPDWORD)&size) ;
79 if(rc) {
80 fprintf(stderr, "RegQueryValueEx(1) failed: %d\n", rc);
81 return 0;
83 buf = (char*)malloc(size+1);
84 rc = RegQueryValueEx(key, NULL, 0, &type, (BYTE*)buf, (LPDWORD)&size);
85 if(rc) {
86 fprintf(stderr, "RegQueryValueEx(2) failed: %d\n", rc);
87 return 0;
89 if(type == REG_SZ || type == REG_EXPAND_SZ) {
90 while(size && buf[size-1] == '\0')
91 --size;
92 buf[size] = 0;
93 /* TODO: convert */
94 return buf;
95 } else if(type == REG_BINARY) {
96 return buf;
98 return 0;
101 int setRegistryEntry(char*key,char*value)
103 HKEY hkey1;
104 HKEY hkey2;
105 int ret1 = 0, ret2=0;
106 ret1 = RegCreateKey(HKEY_CURRENT_USER, key, &hkey1);
107 ret2 = RegCreateKey(HKEY_LOCAL_MACHINE, key, &hkey2);
108 if(ret1 && ret2) {
109 fprintf(stderr, "registry: CreateKey %s failed\n", key);
110 return 0;
112 if(!ret1)
113 ret1 = RegSetValue(hkey1, NULL, REG_SZ, value, strlen(value)+1);
114 if(!ret2)
115 ret2 = RegSetValue(hkey2, NULL, REG_SZ, value, strlen(value)+1);
116 if(ret1 && ret2) {
117 fprintf(stderr, "registry: SetValue %s failed\n", key);
118 return 0;
120 return 1;
124 #endif
126 //HINSTANCE me = GetModuleHandle(NULL);
128 char* getInstallationPath()
130 #if defined(WIN32)
131 char* path = getRegistryEntry("Software\\quiss.org\\swftools\\InstallPath");
132 if(path)
133 return path;
134 else
135 return 0;
136 #elif defined(CYGWIN)
137 return SWFTOOLS_DATADIR;
138 #else
139 return SWFTOOLS_DATADIR;
140 #endif
143 char* concatPaths(const char*base, const char*add)
145 int l1 = strlen(base);
146 int l2 = strlen(add);
147 int pos = 0;
148 char*n = 0;
149 while(l1 && base[l1-1] == path_seperator)
150 l1--;
151 while(pos < l2 && add[pos] == path_seperator)
152 pos++;
154 n = (char*)malloc(l1 + (l2-pos) + 2);
155 memcpy(n,base,l1);
156 n[l1]=path_seperator;
157 strcpy(&n[l1+1],&add[pos]);
158 return n;
161 char* stripFilename(const char*filename, const char*newext)
163 char*last1 = strrchr(filename, '/');
164 char*last2 = strrchr(filename, '\\');
165 const char*pos = filename;
166 char*name;
167 char*dot;
168 if(last1>pos) pos = last1 + 1;
169 if(last2>pos) pos = last2 + 1;
170 name = (char*)malloc(strlen(pos)+2+(newext?strlen(newext):3));
171 strcpy(name, pos);
172 dot = strrchr(name, '.');
173 if(dot) {
174 *dot = 0;
176 if(newext)
177 strcat(name, newext);
178 return name;
181 static char* getTempDir()
183 #ifdef WIN32
184 char*dir = getenv("TMP");
185 if(!dir) dir = getenv("TEMP");
186 if(!dir) dir = getenv("tmp");
187 if(!dir) dir = getenv("temp");
188 if(!dir) dir = "C:\\";
189 #else
190 char* dir = "/tmp/";
191 #endif
192 return dir;
195 char* mktempname(char*ptr, const char*ext) {
196 static char tmpbuf[160];
197 char*dir = getTempDir();
198 int l = strlen(dir);
199 char*sep = "";
200 if(!ptr)
201 ptr = tmpbuf;
202 if(l && dir[l-1]!='/' && dir[l-1]!='\\') {
203 #ifdef WIN32
204 sep = "\\";
205 #else
206 sep = "/";
207 #endif
210 #ifdef HAVE_LRAND48
211 unsigned int r1 = (unsigned int)lrand48();
212 unsigned int r2 = (unsigned int)lrand48();
213 #elif HAVE_RAND
214 unsigned int r1 = rand();
215 unsigned int r2 = rand();
216 #else
217 static int count = 1;
218 unsigned int r1 = time(0);
219 unsigned int r2 = (unsigned int)tmpbuf<<8^count;
220 count ++;
221 #endif
222 if(ext) {
223 sprintf(ptr, "%s%s%04x%04x.%s",dir,sep,r1,r2,ext);
224 } else {
225 sprintf(ptr, "%s%s%04x%04x",dir,sep,r1,r2);
227 return ptr;
230 memfile_t* memfile_open(const char*path)
232 memfile_t*file = malloc(sizeof(memfile_t));
233 #if defined(HAVE_MMAP) && defined(HAVE_STAT)
234 int fi = open(path, O_RDONLY);
235 if(fi<0) {
236 perror(path);
237 free(file);
238 return 0;
240 struct stat sb;
241 if(fstat(fi, &sb)<0) {
242 perror(path);
243 return 0;
245 file->len = sb.st_size;
246 file->data = mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, fi, 0);
247 #else
248 FILE*fi = fopen(path, "rb");
249 if(!fi) {
250 perror(path);
251 free(file);
252 return 0;
254 fseek(fi, 0, SEEK_END);
255 file->len = ftell(fi);
256 fseek(fi, 0, SEEK_SET);
257 file->data = malloc(file->len);
258 if(!file->data) {
259 fprintf(stderr, "Out of memory while allocating memory for file %s\n", path);
260 free(file);
261 return 0;
263 fread(file->data, file->len, 1, fi);
264 fclose(fi);
265 #endif
266 return file;
269 void memfile_close(memfile_t*file)
271 #if defined(HAVE_MMAP) && defined(HAVE_STAT)
272 munmap(file->data, file->len);
273 #else
274 free(file->data);
275 #endif
276 file->data = 0;
277 file->len = 0;
278 free(file);
281 void move_file(const char*from, const char*to)
283 int result = rename(from, to);
285 if(result==0) return; //done!
287 /* if we can't rename, for some reason, copy the file
288 manually */
289 FILE*fi = fopen(from, "rb");
290 if(!fi) {
291 perror(from);
292 return;
294 FILE*fo = fopen(to, "wb");
295 if(!fo) {
296 perror(to);
297 return;
299 char buffer[16384];
300 while(1) {
301 int bytes = fread(buffer, 16384, 1, fi);
302 if(bytes<=0)
303 return;
304 fwrite(buffer, bytes, 1, fo);
307 fclose(fo);
308 fclose(fi);