Add utility to attach console under windows
[moonshot-ui.git] / src / moonshot-futils.c
1 /*
2  * Copyright (c) 2011-2014, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31 */
32 #ifdef HAVE_GETPWUID
33 #include <stdlib.h>
34 #include <sys/types.h>
35 #include <pwd.h>
36 #endif
37 #ifdef OS_WIN32
38 #include <io.h>
39 #include <fcntl.h>
40 #include <stdio.h>
41 #include <windows.h>
42 #endif
43
44 const char * MoonshotGetUserName()
45 {
46 #ifdef HAVE_GETPWUID
47    struct passwd *pwd = getpwuid(getuid());
48    return pwd ? pwd->pw_name : "unknown";
49 #else
50    return "unknown";
51 #endif
52 }
53
54 const char * GetFlatStoreUsersFilePath()
55 {
56    return MOONSHOT_FLATSTORE_USERS;
57 }
58
59 #ifdef OS_WIN32
60 static int con_to_std(DWORD std_id, FILE *fp_out)
61 {
62     HANDLE con_handle;
63     int fd;
64     FILE *fp;
65     con_handle = GetStdHandle(std_id);
66     if (con_handle == INVALID_HANDLE_VALUE)
67         return 0;
68     fd = _open_osfhandle((intptr_t)con_handle, _O_TEXT);
69     if (fd == -1)
70         return 0;
71     fp = _fdopen(fd, "w" );
72     if (fp == NULL)
73         return 0;
74     *fp_out = *fp;
75     setvbuf(fp_out, NULL, _IONBF, 0 );
76     return 1;
77 }
78
79 int moonshot_attach_console()
80 {
81 #if 0
82     if (AttachConsole(ATTACH_PARENT_PROCESS) != 0) {
83         if (con_to_std(STD_OUTPUT_HANDLE, stdout) &&
84             con_to_std(STD_ERROR_HANDLE, stderr)) {
85             return 1;
86         }
87     }
88 #endif
89     if (AllocConsole() && AttachConsole(GetCurrentProcessId())) {
90         if (con_to_std(STD_OUTPUT_HANDLE, stdout) &&
91             con_to_std(STD_ERROR_HANDLE, stderr)) {
92             return 1;
93         }
94     }
95
96     return 0;
97 }
98 #endif
99