Add a wrapper for the AddFull document portal api
[glib.git] / gio / gio-tool-cat.c
blobdcf869c3f22f5713fa12477d845373ec5a081101
1 /*
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>
20 #include "config.h"
22 #include <gio/gio.h>
23 #include <gi18n.h>
24 #include <errno.h>
26 #ifdef G_OS_WIN32
27 #include <io.h>
28 #endif
30 #ifndef STDOUT_FILENO
31 #define STDOUT_FILENO 1
32 #endif
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
38 #include "gio-tool.h"
41 static const GOptionEntry entries[] = {
42 { NULL }
45 static gboolean
46 cat (GFile *file)
48 GInputStream *in;
49 char buffer[1024 * 8 + 1];
50 char *p;
51 gssize res;
52 gboolean close_res;
53 GError *error;
54 gboolean success;
56 error = NULL;
57 in = (GInputStream *) g_file_read (file, NULL, &error);
58 if (in == NULL)
60 print_file_error (file, error->message);
61 g_error_free (error);
62 return FALSE;
65 success = TRUE;
66 while (1)
68 res = g_input_stream_read (in, buffer, sizeof (buffer) - 1, NULL, &error);
69 if (res > 0)
71 gssize written;
73 p = buffer;
74 while (res > 0)
76 written = write (STDOUT_FILENO, p, res);
78 if (written == -1 && errno != EINTR)
80 print_file_error (file, _("Error writing to stdout"));
81 success = FALSE;
82 goto out;
84 res -= written;
85 p += written;
88 else if (res < 0)
90 print_file_error (file, error->message);
91 g_error_free (error);
92 error = NULL;
93 success = FALSE;
94 break;
96 else if (res == 0)
97 break;
100 out:
101 close_res = g_input_stream_close (in, NULL, &error);
102 if (!close_res)
104 print_file_error (file, error->message);
105 g_error_free (error);
106 success = FALSE;
109 return success;
113 handle_cat (int argc, char *argv[], gboolean do_help)
115 GOptionContext *context;
116 gchar *param;
117 GError *error = NULL;
118 int i;
119 gboolean res;
120 GFile *file;
122 g_set_prgname ("gio cat");
123 /* Translators: commandline placeholder */
124 param = g_strdup_printf ("%s...", _("LOCATION"));
125 context = g_option_context_new (param);
126 g_free (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);
136 if (do_help)
138 show_help (context, NULL);
139 g_option_context_free (context);
140 return 0;
143 if (!g_option_context_parse (context, &argc, &argv, &error))
145 show_help (context, error->message);
146 g_error_free (error);
147 g_option_context_free (context);
148 return 1;
151 if (argc < 2)
153 show_help (context, _("No locations given"));
154 g_option_context_free (context);
155 return 1;
158 g_option_context_free (context);
160 res = TRUE;
161 for (i = 1; i < argc; i++)
163 file = g_file_new_for_commandline_arg (argv[i]);
164 res &= cat (file);
165 g_object_unref (file);
168 return res ? 0 : 2;