Don't assign to a proto_item * if the value won't be used: Coverity 823 & 824;
[obnox/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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 /*
26  * Indentation logic: 2-space
27  */
28
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <stdlib.h>
35 #include <string.h>
36
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40
41 #ifdef HAVE_FCNTL_H
42 #include <fcntl.h>
43 #endif
44
45 #ifdef _WIN32
46 #include <process.h>    /* getpid */
47 #endif
48
49 #include <wsutil/file_util.h>
50
51 #include <epan/filesystem.h>
52
53 #include "u3.h"
54
55
56 #define U3_DEVICE_PATH_VAR   "$U3_DEVICE_PATH"
57
58 static char *pid_file = NULL;
59 static char *u3devicepath = (char*)-1;
60 static gchar *newpath = NULL;
61
62 static char *u3_change_path(char *path, const char *old, const char *new);
63
64 gboolean u3_active()
65 {
66
67   return (
68 #ifdef _WIN32
69       getenv_utf8
70 #else
71       getenv
72 #endif
73       ("U3_HOST_EXEC_PATH") != NULL);
74
75 }
76
77 void u3_runtime_info(GString *str)
78 {
79
80   char *u3devicepath_lcl = NULL;
81   char *u3deviceproduct = NULL;
82
83   if((u3deviceproduct =
84 #ifdef _WIN32
85       getenv_utf8
86 #else
87       getenv
88 #endif
89       ("U3_DEVICE_PRODUCT")) != NULL) {
90     g_string_append(str, " from the ");
91     g_string_append(str, u3deviceproduct);
92   } else {
93     g_string_append(str, " from a ");
94   }
95
96   g_string_append(str, " U3 device");
97
98   if((u3devicepath_lcl =
99 #ifdef _WIN32
100       getenv_utf8
101 #else
102       getenv
103 #endif
104       ("U3_DEVICE_PATH")) != NULL) {
105     g_string_append(str, " in drive ");
106     g_string_append(str, u3devicepath_lcl);
107   }
108
109 }
110
111 void u3_register_pid()
112 {
113   int   pid;
114   int   pid_fd;
115   char *u3hostexecpath;
116   int   pf_size;
117
118   if((u3hostexecpath =
119 #ifdef _WIN32
120       getenv_utf8
121 #else
122       getenv
123 #endif
124       ("U3_HOST_EXEC_PATH")) != NULL) {
125
126     pid = getpid();
127
128     pf_size = (int) strlen(u3hostexecpath) + 32;
129     pid_file = g_malloc(pf_size);
130
131     g_snprintf(pid_file, pf_size, "%s\\%d.pid", u3hostexecpath, pid);
132
133     pid_fd = ws_open(pid_file, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
134
135     if(pid_fd != -1)
136       ws_close(pid_fd);
137     else {
138       g_free(pid_file);
139       pid_file = NULL;
140     }
141   }
142 }
143
144
145 void u3_deregister_pid()
146 {
147   if(pid_file) {
148     /* we don't care if we succeed or fail - u3utils may have deleted the file */
149     ws_unlink(pid_file);
150
151     g_free(pid_file);
152
153     pid_file = NULL;
154
155   }
156 }
157
158 char *u3_expand_device_path(char *path)
159 {
160   return u3_change_path(path, U3_DEVICE_PATH_VAR, NULL);
161 }
162
163
164 char *u3_contract_device_path(char *path)
165 {
166   return u3_change_path(path, NULL, U3_DEVICE_PATH_VAR);
167 }
168
169 static char *u3_change_path(char *path, const char *old, const char *new)
170 {
171
172   if(u3devicepath == (char*)-1) {
173     /* cache the device path */
174     u3devicepath =
175 #ifdef _WIN32
176       getenv_utf8
177 #else
178       getenv
179 #endif
180       ("U3_DEVICE_PATH");
181   }
182
183   if(new == NULL)
184     new = u3devicepath;
185   if(old == NULL)
186     old = u3devicepath;
187
188   if(newpath != NULL) {
189     g_free(newpath);
190     newpath = NULL;
191   }
192
193   if((path != NULL) && (u3devicepath != NULL) && (strncmp(path, old, strlen(old)) == 0)) {
194
195     newpath = g_strconcat(new, path + strlen(old), NULL);
196
197     return newpath;
198
199   }
200
201   return path;
202
203 }