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 of the licence, 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[] = {
49 char buffer[1024 * 8 + 1];
57 in = (GInputStream *) g_file_read (file, NULL, &error);
60 print_file_error (file, error->message);
68 res = g_input_stream_read (in, buffer, sizeof (buffer) - 1, NULL, &error);
76 written = write (STDOUT_FILENO, p, res);
78 if (written == -1 && errno != EINTR)
80 print_file_error (file, "error writing to stdout");
90 print_file_error (file, error->message);
101 close_res = g_input_stream_close (in, NULL, &error);
104 print_file_error (file, error->message);
105 g_error_free (error);
113 handle_cat (int argc, char *argv[], gboolean do_help)
115 GOptionContext *context;
117 GError *error = NULL;
122 g_set_prgname ("gio cat");
123 /* Translators: commandline placeholder */
124 param = g_strdup_printf ("%s...", _("LOCATION"));
125 context = g_option_context_new (param);
127 g_option_context_set_help_enabled (context, FALSE);
128 g_option_context_set_summary (context,
129 _("Concatenate files and print to standard output."));
130 g_option_context_set_description (context,
131 _("gio cat works just like the traditional cat utility, but using GIO\n"
132 "locations instead of local files: for example, you can use something\n"
133 "like smb://server/resource/file.txt as location."));
134 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
138 show_help (context, NULL);
142 if (!g_option_context_parse (context, &argc, &argv, &error))
144 show_help (context, error->message);
145 g_error_free (error);
151 show_help (context, _("No files given"));
155 g_option_context_free (context);
158 for (i = 1; i < argc; i++)
160 file = g_file_new_for_commandline_arg (argv[i]);
162 g_object_unref (file);