tools/dbus-launch-win.c: strrchr -> _mbsrchr (cherry picked from commit b8b9fc300242f...
[platform/upstream/dbus.git] / tools / dbus-launch-win.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23 #include <windows.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <mbstring.h>
27 #include <assert.h>
28
29 #if defined __MINGW32__ || (defined _MSC_VER && _MSC_VER <= 1310)
30 /* save string functions version
31 */ 
32 #define errno_t int
33
34 errno_t strcat_s(char *dest, size_t size, char *src) 
35 {
36   assert(strlen(dest) + strlen(src) +1 <= size);
37   strcat(dest,src);
38   return 0;
39 }
40
41 errno_t strcpy_s(char *dest, size_t size, char *src)
42 {
43   assert(strlen(src) +1 <= size);
44   strcpy(dest,src);  
45   return 0;
46 }
47 #endif
48
49 /* TODO: use unicode version as suggested by Tor Lillqvist */
50
51 #define AUTO_ACTIVATE_CONSOLE_WHEN_VERBOSE_MODE 1
52
53 int main(int argc,char **argv)
54 {
55   char dbusDaemonPath[MAX_PATH*2+1];
56   char command[MAX_PATH*2+1];
57   char *p;
58   char *daemon_name;
59   int result;
60   int showConsole = 0;
61   char *s = getenv("DBUS_VERBOSE");
62   int verbose = s && *s != '\0' ? 1 : 0;
63   PROCESS_INFORMATION pi;
64   STARTUPINFO si;
65   HANDLE h; 
66   
67 #ifdef AUTO_ACTIVATE_CONSOLE_WHEN_VERBOSE_MODE
68   if (verbose)
69       showConsole = 1; 
70 #endif
71   GetModuleFileName(NULL,dbusDaemonPath,sizeof(dbusDaemonPath));
72
73 #ifdef _DEBUG
74       daemon_name = "dbus-daemond.exe";
75 #else
76       daemon_name = "dbus-daemon.exe";
77 #endif
78   if ((p = _mbsrchr (dbusDaemonPath, '\\'))) 
79     {
80       *(p+1)= '\0';
81       strcat_s(dbusDaemonPath,sizeof(dbusDaemonPath),daemon_name);
82     }
83   else 
84     {
85       if (verbose)
86           fprintf(stderr,"error: could not extract path from current applications module filename\n");
87       return 1;
88     } 
89   
90   strcpy_s(command,sizeof(command),dbusDaemonPath);
91   strcat_s(command,sizeof(command)," --session");
92   if (verbose)
93       fprintf(stderr,"%s\n",command);
94   
95   memset(&si, 0, sizeof(si));
96   memset(&pi, 0, sizeof(pi));
97   si.cb = sizeof(si);
98
99   result = CreateProcess(NULL, 
100                   command,
101                   0,
102                   0,
103                   TRUE,
104                   (showConsole ? CREATE_NEW_CONSOLE : 0) | NORMAL_PRIORITY_CLASS,
105                   0,
106                   0,
107                   &si,
108                   &pi);
109
110   CloseHandle(pi.hProcess);
111   CloseHandle(pi.hThread);
112
113   if (result == 0) 
114     {
115       if (verbose)
116           fprintf(stderr, "Could not start dbus-daemon error=%d",GetLastError());
117       return 4;
118     }
119    
120   return 0;
121 }