fixed pointer casting errors
[swftools.git] / lib / os.c
blobc95b7775a0a1adacbc15d41cfd13f3ae08e83d60
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 close(fi);
248 #else
249 FILE*fi = fopen(path, "rb");
250 if(!fi) {
251 perror(path);
252 free(file);
253 return 0;
255 fseek(fi, 0, SEEK_END);
256 file->len = ftell(fi);
257 fseek(fi, 0, SEEK_SET);
258 file->data = malloc(file->len);
259 if(!file->data) {
260 fprintf(stderr, "Out of memory while allocating memory for file %s\n", path);
261 free(file);
262 return 0;
264 fread(file->data, file->len, 1, fi);
265 fclose(fi);
266 #endif
267 return file;
270 void memfile_close(memfile_t*file)
272 #if defined(HAVE_MMAP) && defined(HAVE_STAT)
273 munmap(file->data, file->len);
274 #else
275 free(file->data);
276 #endif
277 file->data = 0;
278 file->len = 0;
279 free(file);
282 void move_file(const char*from, const char*to)
284 int result = rename(from, to);
286 if(result==0) return; //done!
288 /* if we can't rename, for some reason, copy the file
289 manually */
290 FILE*fi = fopen(from, "rb");
291 if(!fi) {
292 perror(from);
293 return;
295 FILE*fo = fopen(to, "wb");
296 if(!fo) {
297 perror(to);
298 return;
300 char buffer[16384];
301 while(1) {
302 int bytes = fread(buffer, 16384, 1, fi);
303 if(bytes<=0)
304 return;
305 fwrite(buffer, bytes, 1, fo);
308 fclose(fo);
309 fclose(fi);
312 char file_exists(const char*filename)
314 #ifdef HAVE_STAT
315 struct stat sb;
316 return stat(filename, &sb) >= 0;
317 #else
318 int fi = open(filename, O_RDONLY);
319 if(fi>=0) {
320 close(fi);
321 return 1;
323 return 0;
324 #endif