removed references to ERROR_SUCCESS
[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)
50         rc = RegOpenKeyEx(HKEY_CURRENT_USER, path, 0, KEY_READ, &key);
51     if(rc)
52         rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_ALL_ACCESS, &key);
53     if(rc)
54         rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_READ, &key);
55
56     if (rc) {
57         fprintf(stderr, "RegOpenKeyEx failed\n");
58         return 0;
59     }
60     rc = RegQueryValueEx(key, NULL, 0, 0, 0, (LPDWORD)&size) ;
61     if(rc) {
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) {
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     return 0;
81 }
82
83 int setRegistryEntry(char*key,char*value)
84 {
85     HKEY hkey1;
86     HKEY hkey2;
87     int ret1 = 0, ret2=0;
88     ret1 = RegCreateKey(HKEY_CURRENT_USER, key, &hkey1);
89     ret2 = RegCreateKey(HKEY_LOCAL_MACHINE, key, &hkey2);
90     if(ret1 && ret2) {
91         fprintf(stderr, "registry: CreateKey %s failed\n", key);
92         return 0;
93     }
94     if(!ret1)
95         ret1 = RegSetValue(hkey1, NULL, REG_SZ, value, strlen(value)+1);
96     if(!ret2)
97         ret2 = RegSetValue(hkey2, NULL, REG_SZ, value, strlen(value)+1);
98     if(ret1 && ret2) {
99         fprintf(stderr, "registry: SetValue %s failed\n", key);
100         return 0;
101     }
102     return 1;
103 }
104
105
106 #endif
107
108 //HINSTANCE me =  GetModuleHandle(NULL);
109
110 char* getInstallationPath()
111 {
112 #if defined(WIN32)
113     char* path = getRegistryEntry("Software\\quiss.org\\swftools\\InstallPath");
114     if(path)
115         return path;
116     else
117         return 0;
118 #elif defined(CYGWIN)
119     return SWFTOOLS_DATADIR;
120 #else
121     return SWFTOOLS_DATADIR;
122 #endif
123 }
124
125 char* concatPaths(const char*base, const char*add)
126 {
127     int l1 = strlen(base);
128     int l2 = strlen(add);
129     int pos = 0;
130     char*n = 0;
131     while(l1 && base[l1-1] == seperator)
132         l1--;
133     while(pos < l2 && add[pos] == seperator)
134         pos++;
135
136     n = (char*)malloc(l1 + (l2-pos) + 2);
137     memcpy(n,base,l1);
138     n[l1]=seperator;
139     strcpy(&n[l1+1],&add[pos]);
140     return n;
141 }
142
143 char* stripFilename(const char*filename, const char*newext)
144 {
145     char*last1 = strrchr(filename, '/');
146     char*last2 = strrchr(filename, '\\');
147     const char*pos = filename;
148     char*name;
149     char*dot;
150     if(last1>pos) pos = last1 + 1;
151     if(last2>pos) pos = last2 + 1;
152     name = (char*)malloc(strlen(pos)+2+(newext?strlen(newext):3));
153     strcpy(name, pos);
154     dot = strrchr(name, '.');
155     if(dot) {
156         *dot = 0;
157     }
158     if(newext)
159         strcat(name, newext);
160     return name;
161 }
162
163 static char* getTempDir()
164 {
165 #ifdef WIN32
166     char*dir = getenv("TMP");
167     if(!dir) dir = getenv("TEMP");
168     if(!dir) dir = getenv("tmp");
169     if(!dir) dir = getenv("temp");
170     if(!dir) dir = "C:\\";
171 #else
172     char* dir = "/tmp/";
173 #endif
174     return dir;
175 }
176
177 char* mktempname(char*ptr) {
178     static char tmpbuf[128];
179     char*dir = getTempDir();
180     int l = strlen(dir);
181     char*sep = "";
182     if(!ptr)
183         ptr = tmpbuf;
184     if(l && dir[l-1]!='/' && dir[l-1]!='\\') {
185 #ifdef WIN32
186         sep = "\\";
187 #else
188         sep = "/";
189 #endif
190     }
191
192  //   used to be mktemp. This does remove the warnings, but
193  //   It's not exactly an improvement.
194 #ifdef HAVE_LRAND48
195     sprintf(ptr, "%s%s%08x%08x",dir,sep,lrand48(),lrand48());
196 #else
197 #   ifdef HAVE_RAND
198         sprintf(ptr, "%s%s%08x%08x",dir,sep,rand(),rand());
199 #   else
200         static int count = 1;
201         sprintf(ptr, "%s%s%08x%04x%04x",dir,sep,time(0),(unsigned int)tmpbuf^((unsigned int)tmpbuf)>>16,count);
202         count ++;
203 #   endif
204 #endif
205      return ptr;
206 }
207