Fix getauxval error at qemu
[platform/upstream/glib.git] / gio / gio-tool-launch.c
1 /*
2  * Copyright 2020 Frederic Martinsons
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  *
19  * Author: Frederic Martinsons <frederic.martinsons@sigfox.com>
20  */
21
22 #include "config.h"
23
24 #include <gio/gio.h>
25
26 #if defined(G_OS_UNIX) && !defined(__APPLE__)
27 #include <gio/gdesktopappinfo.h>
28 #endif
29
30 #include <gi18n.h>
31
32 #include "gio-tool.h"
33
34 static const GOptionEntry entries[] = {
35   G_OPTION_ENTRY_NULL
36 };
37
38 int
39 handle_launch (int argc, char *argv[], gboolean do_help)
40 {
41   GOptionContext *context;
42   GError *error = NULL;
43 #if defined(G_OS_UNIX) && !defined(__APPLE__)
44   int i;
45   GAppInfo *app = NULL;
46   GAppLaunchContext *app_context = NULL;
47   GKeyFile *keyfile = NULL;
48   GList *args = NULL;
49   char *desktop_file = NULL;
50 #endif
51   int retval;
52
53   g_set_prgname ("gio launch");
54
55   /* Translators: commandline placeholder */
56   context = g_option_context_new (_("DESKTOP-FILE [FILE-ARG …]"));
57   g_option_context_set_help_enabled (context, FALSE);
58   g_option_context_set_summary (context,
59         _("Launch an application from a desktop file, passing optional filename arguments to it."));
60   g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
61
62   if (do_help)
63     {
64       show_help (context, NULL);
65       g_option_context_free (context);
66       return 0;
67     }
68
69   if (!g_option_context_parse (context, &argc, &argv, &error))
70     {
71       show_help (context, error->message);
72       g_error_free (error);
73       g_option_context_free (context);
74       return 1;
75     }
76
77   if (argc < 2)
78     {
79       show_help (context, _("No desktop file given"));
80       g_option_context_free (context);
81       return 1;
82     }
83
84   g_option_context_free (context);
85
86 #if !defined(G_OS_UNIX) || defined(__APPLE__)
87   print_error (_("The launch command is not currently supported on this platform"));
88   retval = 1;
89 #else
90   retval = 0;
91   desktop_file = argv[1];
92
93   /* Use keyfile api for loading desktop app in order to check for
94   *  - not existing file.
95   *  - invalid keyfile format.
96   */
97   keyfile = g_key_file_new ();
98   if (!g_key_file_load_from_file (keyfile, desktop_file, G_KEY_FILE_NONE, &error))
99     {
100       print_error (_("Unable to load ‘%s‘: %s"), desktop_file, error->message);
101       g_clear_error (&error);
102       retval = 1;
103     }
104   else
105     {
106       app = (GAppInfo*)g_desktop_app_info_new_from_keyfile (keyfile);
107       if (!app)
108         {
109           print_error (_("Unable to load application information for ‘%s‘"), desktop_file);
110           retval = 1;
111         }
112       else
113         {
114           for (i = 2; i < argc; i++)
115             {
116               args = g_list_append (args, g_file_new_for_commandline_arg (argv[i]));
117             }
118           app_context = g_app_launch_context_new ();
119           if (!g_app_info_launch (app, args, app_context, &error))
120             {
121               print_error (_("Unable to launch application ‘%s’: %s"), desktop_file, error->message);
122               g_clear_error (&error);
123               retval = 1;
124             }
125           g_list_free_full (args, g_object_unref);
126           g_clear_object (&app_context);
127         }
128       g_clear_object (&app);
129     }
130   g_key_file_free (keyfile);
131 #endif
132   return retval;
133 }