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