Remove unused cc_type parameter
[metze/wireshark/wip.git] / u3.c
1 /* u3.c
2  * u3   2006 Graeme Lunt
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 /*
26  * Indentation logic: 2-space
27  */
28
29
30 #include "config.h"
31
32 #include <stdlib.h>
33 #include <string.h>
34
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38
39 #ifdef HAVE_FCNTL_H
40 #include <fcntl.h>
41 #endif
42
43 #ifdef _WIN32
44 #include <process.h>    /* getpid */
45 #endif
46
47 #include <wsutil/file_util.h>
48
49 #include <epan/filesystem.h>
50
51 #include "u3.h"
52
53
54 #define U3_DEVICE_PATH_VAR   "$U3_DEVICE_PATH"
55
56 static char *pid_file = NULL;
57 static char *u3devicepath = (char*)-1;
58 static gchar *newpath = NULL;
59
60 static const char *u3_change_path(const char *path, const char *old, const char *new_u3devicepath);
61
62 gboolean u3_active(void)
63 {
64
65   return (
66 #ifdef _WIN32
67       getenv_utf8
68 #else
69       getenv
70 #endif
71       ("U3_HOST_EXEC_PATH") != NULL);
72
73 }
74
75 void u3_runtime_info(GString *str)
76 {
77
78   char *u3devicepath_lcl = NULL;
79   char *u3deviceproduct = NULL;
80
81   if((u3deviceproduct =
82 #ifdef _WIN32
83       getenv_utf8
84 #else
85       getenv
86 #endif
87       ("U3_DEVICE_PRODUCT")) != NULL) {
88     g_string_append(str, " from the ");
89     g_string_append(str, u3deviceproduct);
90   } else {
91     g_string_append(str, " from a ");
92   }
93
94   g_string_append(str, " U3 device");
95
96   if((u3devicepath_lcl =
97 #ifdef _WIN32
98       getenv_utf8
99 #else
100       getenv
101 #endif
102       ("U3_DEVICE_PATH")) != NULL) {
103     g_string_append(str, " in drive ");
104     g_string_append(str, u3devicepath_lcl);
105   }
106
107 }
108
109 void u3_register_pid(void)
110 {
111   int   pid;
112   int   pid_fd;
113   char *u3hostexecpath;
114   int   pf_size;
115
116   if((u3hostexecpath =
117 #ifdef _WIN32
118       getenv_utf8
119 #else
120       getenv
121 #endif
122       ("U3_HOST_EXEC_PATH")) != NULL) {
123
124     pid = getpid();
125
126     pf_size = (int) strlen(u3hostexecpath) + 32;
127     pid_file = (char *)g_malloc(pf_size);
128
129     g_snprintf(pid_file, pf_size, "%s\\%d.pid", u3hostexecpath, pid);
130
131     pid_fd = ws_open(pid_file, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
132
133     if(pid_fd != -1)
134       ws_close(pid_fd);
135     else {
136       g_free(pid_file);
137       pid_file = NULL;
138     }
139   }
140 }
141
142
143 void u3_deregister_pid(void)
144 {
145   if(pid_file) {
146     /* we don't care if we succeed or fail - u3utils may have deleted the file */
147     ws_unlink(pid_file);
148
149     g_free(pid_file);
150
151     pid_file = NULL;
152
153   }
154 }
155
156 const char *u3_expand_device_path(const char *path)
157 {
158   return u3_change_path(path, U3_DEVICE_PATH_VAR, NULL);
159 }
160
161
162 const char *u3_contract_device_path(char *path)
163 {
164   return u3_change_path(path, NULL, U3_DEVICE_PATH_VAR);
165 }
166
167 static const char *u3_change_path(const char *path, const char *old, const char *new_u3devicepath)
168 {
169
170   if(u3devicepath == (char*)-1) {
171     /* cache the device path */
172     u3devicepath =
173 #ifdef _WIN32
174       getenv_utf8
175 #else
176       getenv
177 #endif
178       ("U3_DEVICE_PATH");
179   }
180
181   if(new_u3devicepath == NULL)
182     new_u3devicepath = u3devicepath;
183   if(old == NULL)
184     old = u3devicepath;
185
186   if(newpath != NULL) {
187     g_free(newpath);
188     newpath = NULL;
189   }
190
191   if((path != NULL) && (u3devicepath != NULL) && (strncmp(path, old, strlen(old)) == 0)) {
192
193     newpath = g_strconcat(new_u3devicepath, path + strlen(old), NULL);
194
195     return newpath;
196
197   }
198
199   return path;
200
201 }