1/* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright (C) 2009 Red Hat, Inc.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 *
18 * Author: Alexander Larsson <alexl@redhat.com>
19 */
20
21#include <config.h>
22
23#include <stdio.h>
24#include <locale.h>
25#include <errno.h>
26
27#include <glib.h>
28#include <gio/gio.h>
29
30#ifdef G_OS_UNIX
31#include <unistd.h>
32#endif
33
34#ifdef G_OS_WIN32
35#include <io.h>
36#ifndef STDOUT_FILENO
37#define STDOUT_FILENO 1
38#endif
39#endif
40
41static gchar **locations = NULL;
42static char *from_charset = NULL;
43static char *to_charset = NULL;
44static gboolean decompress = FALSE;
45static gboolean compress = FALSE;
46static gboolean gzip = FALSE;
47static gboolean fallback = FALSE;
48
49static GOptionEntry entries[] = {
50 {"decompress", 0, 0, G_OPTION_ARG_NONE, &decompress, "decompress", NULL},
51 {"compress", 0, 0, G_OPTION_ARG_NONE, &compress, "compress", NULL},
52 {"gzip", 0, 0, G_OPTION_ARG_NONE, &gzip, "use gzip format", NULL},
53 {"from-charset", 0, 0, G_OPTION_ARG_STRING, &from_charset, "from charset", NULL},
54 {"to-charset", 0, 0, G_OPTION_ARG_STRING, &to_charset, "to charset", NULL},
55 {"fallback", 0, 0, G_OPTION_ARG_NONE, &fallback, "use fallback", NULL},
56 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &locations, "locations", NULL},
57 {NULL}
58};
59
60static void
61decompressor_file_info_notify_cb (GZlibDecompressor *decompressor,
62 GParamSpec *pspec,
63 gpointer data)
64{
65 GFileInfo *file_info;
66 const gchar *filename;
67
68 file_info = g_zlib_decompressor_get_file_info (decompressor);
69 if (file_info == NULL)
70 return;
71
72 filename = g_file_info_get_name (info: file_info);
73 if (filename)
74 g_printerr (format: "Decompressor filename: %s\n", filename);
75}
76
77static void
78cat (GFile * file)
79{
80 GInputStream *in;
81 char buffer[1024 * 8 + 1];
82 char *p;
83 gssize res;
84 gboolean close_res;
85 GError *error;
86 GConverter *conv;
87 GCharsetConverter *cconv = NULL;
88
89 error = NULL;
90 in = (GInputStream *) g_file_read (file, NULL, error: &error);
91 if (in == NULL)
92 {
93 /* Translators: the first %s is the program name, the second one */
94 /* is the URI of the file, the third is the error message. */
95 g_printerr (format: "%s: %s: error opening file: %s\n",
96 g_get_prgname (), g_file_get_uri (file), error->message);
97 g_error_free (error);
98 return;
99 }
100
101 if (decompress)
102 {
103 GInputStream *old;
104 conv = (GConverter *)g_zlib_decompressor_new (format: gzip?G_ZLIB_COMPRESSOR_FORMAT_GZIP:G_ZLIB_COMPRESSOR_FORMAT_ZLIB);
105 old = in;
106 in = (GInputStream *) g_converter_input_stream_new (base_stream: in, converter: conv);
107 g_signal_connect (conv, "notify::file-info", G_CALLBACK (decompressor_file_info_notify_cb), NULL);
108 g_object_unref (object: conv);
109 g_object_unref (object: old);
110 }
111
112 if (from_charset && to_charset)
113 {
114 cconv = g_charset_converter_new (to_charset, from_charset, error: &error);
115 conv = (GConverter *)cconv;
116 if (conv)
117 {
118 GInputStream *old;
119
120 g_charset_converter_set_use_fallback (converter: cconv, use_fallback: fallback);
121
122 old = in;
123 in = (GInputStream *) g_converter_input_stream_new (base_stream: in, converter: conv);
124 g_object_unref (object: conv);
125 g_object_unref (object: old);
126 }
127 else
128 {
129 g_printerr (format: "%s: Can't convert between charsets: %s\n",
130 g_get_prgname (), error->message);
131 }
132 }
133
134 if (compress)
135 {
136 GInputStream *old;
137 GFileInfo *in_file_info;
138
139 in_file_info = g_file_query_info (file,
140 G_FILE_ATTRIBUTE_STANDARD_NAME ","
141 G_FILE_ATTRIBUTE_TIME_MODIFIED,
142 flags: G_FILE_QUERY_INFO_NONE,
143 NULL,
144 error: &error);
145 if (in_file_info == NULL)
146 {
147 g_printerr (format: "%s: %s: error reading file info: %s\n",
148 g_get_prgname (), g_file_get_uri (file), error->message);
149 g_error_free (error);
150 return;
151 }
152
153 conv = (GConverter *)g_zlib_compressor_new(format: gzip?G_ZLIB_COMPRESSOR_FORMAT_GZIP:G_ZLIB_COMPRESSOR_FORMAT_ZLIB, level: -1);
154 g_zlib_compressor_set_file_info (G_ZLIB_COMPRESSOR (conv), file_info: in_file_info);
155 old = in;
156 in = (GInputStream *) g_converter_input_stream_new (base_stream: in, converter: conv);
157 g_object_unref (object: conv);
158 g_object_unref (object: old);
159 g_object_unref (object: in_file_info);
160 }
161
162 while (1)
163 {
164 res =
165 g_input_stream_read (stream: in, buffer, count: sizeof (buffer) - 1, NULL, error: &error);
166 if (res > 0)
167 {
168 gssize written;
169
170 p = buffer;
171 while (res > 0)
172 {
173 written = write (STDOUT_FILENO, buf: p, n: res);
174
175 if (written == -1 && errno != EINTR)
176 {
177 /* Translators: the first %s is the program name, the */
178 /* second one is the URI of the file. */
179 g_printerr (format: "%s: %s, error writing to stdout",
180 g_get_prgname (), g_file_get_uri (file));
181 goto out;
182 }
183 res -= written;
184 p += written;
185 }
186 }
187 else if (res < 0)
188 {
189 g_printerr (format: "%s: %s: error reading: %s\n",
190 g_get_prgname (), g_file_get_uri (file),
191 error->message);
192 g_error_free (error);
193 error = NULL;
194 break;
195 }
196 else if (res == 0)
197 break;
198 }
199
200 out:
201
202 close_res = g_input_stream_close (stream: in, NULL, error: &error);
203 if (!close_res)
204 {
205 g_printerr (format: "%s: %s:error closing: %s\n",
206 g_get_prgname (), g_file_get_uri (file), error->message);
207 g_error_free (error);
208 }
209
210 if (cconv != NULL && fallback)
211 {
212 guint num = g_charset_converter_get_num_fallbacks (converter: cconv);
213 if (num > 0)
214 g_printerr (format: "Number of fallback errors: %u\n", num);
215 }
216}
217
218int
219main (int argc, char *argv[])
220{
221 GError *error = NULL;
222 GOptionContext *context = NULL;
223 GFile *file;
224 int i;
225
226 context =
227 g_option_context_new (parameter_string: "LOCATION... - concatenate LOCATIONS "
228 "to standard output.");
229
230 g_option_context_set_summary (context, summary: "filter files");
231
232 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
233 g_option_context_parse (context, argc: &argc, argv: &argv, error: &error);
234
235 g_option_context_free (context);
236
237 if (error != NULL)
238 {
239 g_printerr (format: "Error parsing commandline options: %s\n", error->message);
240 g_printerr (format: "\n");
241 g_printerr (format: "Try \"%s --help\" for more information.",
242 g_get_prgname ());
243 g_printerr (format: "\n");
244 g_error_free(error);
245 return 1;
246 }
247
248 if (!locations)
249 {
250 g_printerr (format: "%s: missing locations", g_get_prgname ());
251 g_printerr (format: "\n");
252 g_printerr (format: "Try \"%s --help\" for more information.",
253 g_get_prgname ());
254 g_printerr (format: "\n");
255 return 1;
256 }
257
258 i = 0;
259
260 do
261 {
262 file = g_file_new_for_commandline_arg (arg: locations[i]);
263 cat (file);
264 g_object_unref (object: file);
265 }
266 while (locations[++i] != NULL);
267
268 return 0;
269}
270

source code of gtk/subprojects/glib/gio/tests/filter-cat.c