Add myself to the DOAP for GLib
[glib.git] / gio / gio-tool-cat.c
blob394748815359c50e53a600328b4ea54bc6413ff1
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 /* 256k minus malloc overhead */
46 #define STREAM_BUFFER_SIZE (1024*256 - 2*sizeof(gpointer))
48 static gboolean
49 cat (GFile *file)
51 GInputStream *in;
52 char *buffer;
53 char *p;
54 gssize res;
55 gboolean close_res;
56 GError *error;
57 gboolean success;
59 error = NULL;
60 in = (GInputStream *) g_file_read (file, NULL, &error);
61 if (in == NULL)
63 print_file_error (file, error->message);
64 g_error_free (error);
65 return FALSE;
68 buffer = g_malloc (STREAM_BUFFER_SIZE);
69 success = TRUE;
70 while (1)
72 res = g_input_stream_read (in, buffer, STREAM_BUFFER_SIZE, NULL, &error);
73 if (res > 0)
75 gssize written;
77 p = buffer;
78 while (res > 0)
80 int errsv;
82 written = write (STDOUT_FILENO, p, res);
83 errsv = errno;
85 if (written == -1 && errsv != EINTR)
87 print_error ("%s", _("Error writing to stdout"));
88 success = FALSE;
89 goto out;
91 res -= written;
92 p += written;
95 else if (res < 0)
97 print_file_error (file, error->message);
98 g_error_free (error);
99 error = NULL;
100 success = FALSE;
101 break;
103 else if (res == 0)
104 break;
107 out:
108 close_res = g_input_stream_close (in, NULL, &error);
109 if (!close_res)
111 print_file_error (file, error->message);
112 g_error_free (error);
113 success = FALSE;
116 g_free (buffer);
118 return success;
122 handle_cat (int argc, char *argv[], gboolean do_help)
124 GOptionContext *context;
125 gchar *param;
126 GError *error = NULL;
127 int i;
128 gboolean res;
129 GFile *file;
131 g_set_prgname ("gio cat");
132 /* Translators: commandline placeholder */
133 param = g_strdup_printf ("%s...", _("LOCATION"));
134 context = g_option_context_new (param);
135 g_free (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);
145 if (do_help)
147 show_help (context, NULL);
148 g_option_context_free (context);
149 return 0;
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);
157 return 1;
160 if (argc < 2)
162 show_help (context, _("No locations given"));
163 g_option_context_free (context);
164 return 1;
167 g_option_context_free (context);
169 res = TRUE;
170 for (i = 1; i < argc; i++)
172 file = g_file_new_for_commandline_arg (argv[i]);
173 res &= cat (file);
174 g_object_unref (file);
177 return res ? 0 : 2;