set both user and system registry
[swftools.git] / lib / os.c
1 /* os.c
2
3 operating system dependent functions
4
5 Part of the swftools package. 
6
7 Copyright (c) 2005 Matthias Kramm <kramm@quiss.org>
8
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.
13
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.
18
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 */
22
23 #include "os.h"
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #ifdef WIN32
28 #include <windows.h>
29 #endif
30
31 #if defined(CYGWIN)
32 static char seperator = '/';
33 #elif defined(WIN32)
34 static char seperator = '\\';
35 #else
36 static char seperator = '/';
37 #endif
38
39 #ifdef WIN32
40 char* getRegistryEntry(char*path)
41 {
42     int res = 0;
43     HKEY key;
44     long rc;
45     long size = 0;
46     DWORD type;
47     char*buf;
48     rc = RegOpenKeyEx(HKEY_CURRENT_USER, path, 0, KEY_ALL_ACCESS, &key);
49     if(rc != ERROR_SUCCESS)
50         rc = RegOpenKeyEx(HKEY_CURRENT_USER, path, 0, KEY_READ, &key);
51     if(rc != ERROR_SUCCESS)
52         rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_ALL_ACCESS, &key);
53     if(rc != ERROR_SUCCESS)
54         rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_READ, &key);
55
56     if (rc != ERROR_SUCCESS) {
57         fprintf(stderr, "RegOpenKeyEx failed\n");
58         return 0;
59     }
60     rc = RegQueryValueEx(key, NULL, 0, 0, 0, (LPDWORD)&size) ;
61     if(rc != ERROR_SUCCESS) {
62         fprintf(stderr, "RegQueryValueEx(1) failed: %d\n", rc);
63         return 0;
64     }
65     buf = (char*)malloc(size+1);
66     rc = RegQueryValueEx(key, NULL, 0, &type, (BYTE*)buf, (LPDWORD)&size);
67     if(rc != ERROR_SUCCESS) {
68         fprintf(stderr, "RegQueryValueEx(2) failed: %d\n", rc);
69         return 0;
70     }
71     if(type == REG_SZ || type == REG_EXPAND_SZ) {
72         while(size && buf[size-1] == '\0')
73           --size;
74         buf[size] = 0;
75         /* TODO: convert */
76         return buf;
77     } else if(type == REG_BINARY) {
78         return buf;
79     }
80 }
81
82 int setRegistryEntry(char*key,char*value)
83 {
84     HKEY hkey1;
85     HKEY hkey2;
86     int ret1 = 0, ret2=0;
87     ret1 = RegCreateKey(HKEY_CURRENT_USER, key, &hkey1);
88     ret2 = RegCreateKey(HKEY_LOCAL_MACHINE, key, &hkey2);
89     if(ret1 != ERROR_SUCCESS && ret2 != ERROR_SUCESS) {
90         fprintf(stderr, "registry: CreateKey %s failed\n", key);
91         return 0;
92     }
93     if(ret1==ERROR_SUCCESS)
94         ret1 = RegSetValue(hkey1, NULL, REG_SZ, value, strlen(value)+1);
95     if(ret2==ERROR_SUCCESS)
96         ret2 = RegSetValue(hkey2, NULL, REG_SZ, value, strlen(value)+1);
97     if(ret1 != ERROR_SUCCESS && ret2 != ERROR_SUCCESS) {
98         fprintf(stderr, "registry: SetValue %s failed\n", key);
99         return 0;
100     }
101     return 1;
102 }
103
104
105 #endif
106
107 //HINSTANCE me =  GetModuleHandle(NULL);
108
109 char* getInstallationPath()
110 {
111 #if defined(WIN32)
112     char* path = getRegistryEntry("Software\\quiss.org\\swftools\\InstallPath");
113     if(path)
114         return path;
115     else
116         return 0;
117 #elif defined(CYGWIN)
118     return SWFTOOLS_DATADIR;
119 #else
120     return SWFTOOLS_DATADIR;
121 #endif
122 }
123
124 char* concatPaths(const char*base, const char*add)
125 {
126     int l1 = strlen(base);
127     int l2 = strlen(add);
128     int pos = 0;
129     char*n = 0;
130     while(l1 && base[l1-1] == seperator)
131         l1--;
132     while(pos < l2 && add[pos] == seperator)
133         pos++;
134
135     n = (char*)malloc(l1 + (l2-pos) + 2);
136     memcpy(n,base,l1);
137     n[l1]=seperator;
138     strcpy(&n[l1+1],&add[pos]);
139     return n;
140 }
141
142 char* stripFilename(const char*filename, const char*newext)
143 {
144     char*last1 = strrchr(filename, '/');
145     char*last2 = strrchr(filename, '\\');
146     const char*pos = filename;
147     char*name;
148     char*dot;
149     if(last1>pos) pos = last1 + 1;
150     if(last2>pos) pos = last2 + 1;
151     name = (char*)malloc(strlen(pos)+2+(newext?strlen(newext):3));
152     strcpy(name, pos);
153     dot = strrchr(name, '.');
154     if(dot) {
155         *dot = 0;
156     }
157     if(newext)
158         strcat(name, newext);
159     return name;
160 }
161
162 static char* getTempDir()
163 {
164 #ifdef WIN32
165     char*dir = getenv("TMP");
166     if(!dir) dir = getenv("TEMP");
167     if(!dir) dir = getenv("tmp");
168     if(!dir) dir = getenv("temp");
169     if(!dir) dir = "C:\\";
170 #else
171     char* dir = "/tmp/";
172 #endif
173     return dir;
174 }
175
176 char* mktempname(char*ptr) {
177     static char tmpbuf[128];
178     char*dir = getTempDir();
179     int l = strlen(dir);
180     char*sep = "";
181     if(!ptr)
182         ptr = tmpbuf;
183     if(l && dir[l-1]!='/' && dir[l-1]!='\\') {
184 #ifdef WIN32
185         sep = "\\";
186 #else
187         sep = "/";
188 #endif
189     }
190
191  //   used to be mktemp. This does remove the warnings, but
192  //   It's not exactly an improvement.
193 #ifdef HAVE_LRAND48
194     sprintf(ptr, "%s%s%08x%08x",dir,sep,lrand48(),lrand48());
195 #else
196 #   ifdef HAVE_RAND
197         sprintf(ptr, "%s%s%08x%08x",dir,sep,rand(),rand());
198 #   else
199         static int count = 1;
200         sprintf(ptr, "%s%s%08x%04x%04x",dir,sep,time(0),(unsigned int)tmpbuf^((unsigned int)tmpbuf)>>16,count);
201         count ++;
202 #   endif
203 #endif
204      return ptr;
205 }
206