Trying to get buildbot Ubuntu-5.10-x86 to build again.
[obnox/wireshark/wip.git] / gtk / 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 <wiretap/file_util.h>
50 #include <epan/filesystem.h>
51
52 #include <gtk/u3.h>
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 char *u3_change_path(char *path, const char *old, const char *new);
61
62 gboolean u3_active() 
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_register_pid()
76 {
77   int   pid;
78   int   pid_fd;
79   char *u3hostexecpath;
80   int   pf_size;
81     
82   if((u3hostexecpath = 
83 #ifdef _WIN32
84       getenv_utf8
85 #else
86       getenv
87 #endif 
88       ("U3_HOST_EXEC_PATH")) != NULL) {
89
90     pid = getpid();
91
92     pf_size = strlen(u3hostexecpath) + 32;
93     pid_file = g_malloc(pf_size);
94
95     g_snprintf(pid_file, pf_size, "%s\\%d.pid", u3hostexecpath, pid);
96
97     pid_fd = eth_open(pid_file, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
98
99     if(pid_fd != -1)
100       eth_close(pid_fd);
101     else {
102       g_free(pid_file);
103       pid_file = NULL;
104     }
105   }
106 }
107
108
109 void u3_deregister_pid()
110 {
111   if(pid_file) {
112     /* we don't care if we succeed or fail - u3utils may have deleted the file */
113     eth_unlink(pid_file);
114     
115     g_free(pid_file);
116
117     pid_file = NULL;
118
119   }
120 }
121
122 char *u3_expand_device_path(char *path)
123 {
124   return u3_change_path(path, U3_DEVICE_PATH_VAR, NULL);
125 }
126
127
128 char *u3_contract_device_path(char *path)
129 {
130   return u3_change_path(path, NULL, U3_DEVICE_PATH_VAR);
131 }
132
133 static char *u3_change_path(char *path, const char *old, const char *new) 
134 {
135
136   if(u3devicepath == (char*)-1) {
137     /* cache the device path */
138     u3devicepath = 
139 #ifdef _WIN32
140       getenv_utf8
141 #else
142       getenv
143 #endif 
144       ("U3_DEVICE_PATH");
145   }
146   
147   if(new == NULL)
148     new = u3devicepath;
149   if(old == NULL)
150     old = u3devicepath;
151
152   if(newpath != NULL) {
153     g_free(newpath);
154     newpath = NULL;
155   }
156
157   if((path != NULL) && (u3devicepath != NULL) && (strncmp(path, old, strlen(old)) == 0)) {
158     
159     newpath = g_strconcat(new, path + strlen(old), NULL);
160
161     return newpath;
162
163   }
164
165   return path;
166
167 }