* dbus/dbus-sysdeps-util-win.c, tools/dbus-launch-win.c: msvc7.1 fixes by Jaroslaw...
[platform/upstream/dbus.git] / tools / dbus-launch-win.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-launch.c  dbus-launch utility
3  *
4  * Copyright (C) 2007 Ralf Habacker <ralf.habacker@freenet.de>
5  *
6  * Licensed under the Academic Free License version 2.1
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23 #include <windows.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <assert.h>
27
28 #if defined __MINGW32__ || (defined _MSC_VER && _MSC_VER <= 1310)
29 /* save string functions version
30 */ 
31 #define errno_t int
32
33 errno_t strcat_s(char *dest, size_t size, char *src) 
34 {
35   assert(strlen(dest) + strlen(src) +1 <= size);
36   strcat(dest,src);
37   return 0;
38 }
39
40 errno_t strcpy_s(char *dest, size_t size, char *src)
41 {
42   assert(strlen(src) +1 <= size);
43   strcpy(dest,src);  
44   return 0;
45 }
46 #endif
47
48 /* TODO: use unicode version as suggested by Tor Lillqvist */
49
50 #define AUTO_ACTIVATE_CONSOLE_WHEN_VERBOSE_MODE 1
51
52 int main(int argc,char **argv)
53 {
54   char dbusDaemonPath[MAX_PATH*2+1];
55   char command[MAX_PATH*2+1];
56   char *p;
57   char *daemon_name;
58   int result;
59   int showConsole = 0;
60   char *s = getenv("DBUS_VERBOSE");
61   int verbose = s && *s != '\0' ? 1 : 0;
62   PROCESS_INFORMATION pi;
63   STARTUPINFO si;
64   HANDLE h; 
65   
66 #ifdef AUTO_ACTIVATE_CONSOLE_WHEN_VERBOSE_MODE
67   if (verbose)
68       showConsole = 1; 
69 #endif
70   GetModuleFileName(NULL,dbusDaemonPath,sizeof(dbusDaemonPath));
71   /* check for debug version */
72   if (strstr(dbusDaemonPath,"dbus-launchd.exe"))
73       daemon_name = "dbus-daemond.exe";
74   else
75       daemon_name = "dbus-daemon.exe";
76   if ((p = strrchr(dbusDaemonPath,'\\'))) 
77     {
78       *(p+1)= '\0';
79       strcat_s(dbusDaemonPath,sizeof(dbusDaemonPath),daemon_name);
80     }
81   else 
82     {
83       if (verbose)
84           fprintf(stderr,"error: could not extract path from current applications module filename\n");
85       return 1;
86     } 
87   
88   strcpy_s(command,sizeof(command),dbusDaemonPath);
89   strcat_s(command,sizeof(command)," --session");
90   if (verbose)
91       fprintf(stderr,"%s\n",command);
92   
93   memset(&si, 0, sizeof(si));
94   memset(&pi, 0, sizeof(pi));
95   si.cb = sizeof(si);
96
97   result = CreateProcess(NULL, 
98                   command,
99                   0,
100                   0,
101                   TRUE,
102                   (showConsole ? CREATE_NEW_CONSOLE : 0) | NORMAL_PRIORITY_CLASS,
103                   0,
104                   0,
105                   &si,
106                   &pi);
107
108   CloseHandle(pi.hProcess);
109   CloseHandle(pi.hThread);
110
111   if (result == 0) 
112     {
113       if (verbose)
114           fprintf(stderr,"could not start dbus-daemon error=%d",GetLastError());
115       return 4;
116     }
117    
118   return 0;
119 }