2 * Copyright 2015 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 * Author: Matthias Clasen <mclasen@redhat.com>
31 #define STDOUT_FILENO 1
41 static const GOptionEntry entries[] = {
45 /* 256k minus malloc overhead */
46 #define STREAM_BUFFER_SIZE (1024*256 - 2*sizeof(gpointer))
60 in = (GInputStream *) g_file_read (file, NULL, &error);
63 print_file_error (file, error->message);
68 buffer = g_malloc (STREAM_BUFFER_SIZE);
72 res = g_input_stream_read (in, buffer, STREAM_BUFFER_SIZE, NULL, &error);
82 written = write (STDOUT_FILENO, p, res);
85 if (written == -1 && errsv != EINTR)
87 print_error ("%s", _("Error writing to stdout"));
97 print_file_error (file, error->message);
108 close_res = g_input_stream_close (in, NULL, &error);
111 print_file_error (file, error->message);
112 g_error_free (error);
122 handle_cat (int argc, char *argv[], gboolean do_help)
124 GOptionContext *context;
126 GError *error = NULL;
131 g_set_prgname ("gio cat");
132 /* Translators: commandline placeholder */
133 param = g_strdup_printf ("%s…", _("LOCATION"));
134 context = g_option_context_new (param);
136 g_option_context_set_help_enabled (context, FALSE);
137 g_option_context_set_summary (context,
138 _("Concatenate files and print to standard output."));
139 g_option_context_set_description (context,
140 _("gio cat works just like the traditional cat utility, but using GIO\n"
141 "locations instead of local files: for example, you can use something\n"
142 "like smb://server/resource/file.txt as location."));
143 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
147 show_help (context, NULL);
148 g_option_context_free (context);
152 if (!g_option_context_parse (context, &argc, &argv, &error))
154 show_help (context, error->message);
155 g_error_free (error);
156 g_option_context_free (context);
162 show_help (context, _("No locations given"));
163 g_option_context_free (context);
167 g_option_context_free (context);
170 for (i = 1; i < argc; i++)
172 file = g_file_new_for_commandline_arg (argv[i]);
174 g_object_unref (file);