Revert "Fix compilation on Android with the bionic C library"
[platform/upstream/glib.git] / gio / tests / gtlsconsoleinteraction.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2011 Collabora, Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Stef Walter <stefw@collabora.co.uk>
21  */
22
23 #include "config.h"
24
25 #include <glib.h>
26 #include <string.h>
27
28 #ifdef G_OS_WIN32
29 #include <glib/gprintf.h>
30 #include <conio.h>
31 #endif
32
33 #include "gtlsconsoleinteraction.h"
34
35 /*
36  * WARNING: This is not the example you're looking for [slow hand wave]. This
37  * is not industrial strength, it's just for testing. It uses embarassing
38  * functions like getpass() and does lazy things with threads.
39  */
40
41 G_DEFINE_TYPE (GTlsConsoleInteraction, g_tls_console_interaction, G_TYPE_TLS_INTERACTION);
42
43 #ifdef G_OS_WIN32
44 /* win32 doesn't have getpass() */
45 static gchar *
46 getpass (const gchar *prompt)
47 {
48   static gchar buf[BUFSIZ];
49   gint i;
50
51   g_printf ("%s", prompt);
52   fflush (stdout);
53
54   for (i = 0; i < BUFSIZ - 1; ++i)
55     {
56       buf[i] = _getch ();
57       if (buf[i] == '\r')
58         break;
59     }
60   buf[i] = '\0';
61
62   g_printf ("\n");
63
64   return &buf[0];
65 }
66 #endif
67
68 static GTlsInteractionResult
69 g_tls_console_interaction_ask_password (GTlsInteraction    *interaction,
70                                         GTlsPassword       *password,
71                                         GCancellable       *cancellable,
72                                         GError            **error)
73 {
74   const gchar *value;
75   gchar *prompt;
76
77   prompt = g_strdup_printf ("Password \"%s\"': ", g_tls_password_get_description (password));
78   value = getpass (prompt);
79   g_free (prompt);
80
81   if (g_cancellable_set_error_if_cancelled (cancellable, error))
82     return G_TLS_INTERACTION_FAILED;
83
84   g_tls_password_set_value (password, (guchar *)value, -1);
85   return G_TLS_INTERACTION_HANDLED;
86 }
87
88 static void
89 ask_password_with_getpass (GTask        *task,
90                            gpointer      object,
91                            gpointer      task_data,
92                            GCancellable *cancellable)
93 {
94   GTlsPassword *password = task_data;
95   GError *error = NULL;
96
97   g_tls_console_interaction_ask_password (G_TLS_INTERACTION (object), password,
98                                           cancellable, &error);
99   if (error != NULL)
100     g_task_return_error (task, error);
101   else
102     g_task_return_int (task, G_TLS_INTERACTION_HANDLED);
103 }
104
105 static void
106 g_tls_console_interaction_ask_password_async (GTlsInteraction    *interaction,
107                                               GTlsPassword       *password,
108                                               GCancellable       *cancellable,
109                                               GAsyncReadyCallback callback,
110                                               gpointer            user_data)
111 {
112   GTask *task;
113
114   task = g_task_new (interaction, cancellable, callback, user_data);
115   g_task_set_task_data (task, g_object_ref (password), g_object_unref);
116   g_task_run_in_thread (task, ask_password_with_getpass);
117   g_object_unref (task);
118 }
119
120 static GTlsInteractionResult
121 g_tls_console_interaction_ask_password_finish (GTlsInteraction    *interaction,
122                                                GAsyncResult       *result,
123                                                GError            **error)
124 {
125   GTlsInteractionResult ret;
126
127   g_return_val_if_fail (g_task_is_valid (result, interaction),
128                         G_TLS_INTERACTION_FAILED);
129
130   ret = g_task_propagate_int (G_TASK (result), error);
131   if (ret == (GTlsInteractionResult)-1)
132     return G_TLS_INTERACTION_FAILED;
133   else
134     return ret;
135 }
136
137 static void
138 g_tls_console_interaction_init (GTlsConsoleInteraction *interaction)
139 {
140
141 }
142
143 static void
144 g_tls_console_interaction_class_init (GTlsConsoleInteractionClass *klass)
145 {
146   GTlsInteractionClass *interaction_class = G_TLS_INTERACTION_CLASS (klass);
147   interaction_class->ask_password = g_tls_console_interaction_ask_password;
148   interaction_class->ask_password_async = g_tls_console_interaction_ask_password_async;
149   interaction_class->ask_password_finish = g_tls_console_interaction_ask_password_finish;
150 }
151
152 GTlsInteraction *
153 g_tls_console_interaction_new (void)
154 {
155   return g_object_new (G_TYPE_TLS_CONSOLE_INTERACTION, NULL);
156 }