gdbus-auth: Fix leaks in tests
[platform/upstream/glib.git] / gio / tests / filter-cat.c
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 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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include <config.h>
24
25 #include <stdio.h>
26 #include <locale.h>
27 #include <errno.h>
28
29 #include <glib.h>
30 #include <gio/gio.h>
31
32 #ifdef G_OS_UNIX
33 #include <unistd.h>
34 #endif
35
36 #ifdef G_OS_WIN32
37 #ifndef STDOUT_FILENO
38 #define STDOUT_FILENO 1
39 #endif
40 #endif
41
42 static gchar **locations = NULL;
43 static char *from_charset = NULL;
44 static char *to_charset = NULL;
45 static gboolean decompress = FALSE;
46 static gboolean compress = FALSE;
47 static gboolean gzip = FALSE;
48 static gboolean fallback = FALSE;
49
50 static GOptionEntry entries[] = {
51   {"decompress", 0, 0, G_OPTION_ARG_NONE, &decompress, "decompress", NULL},
52   {"compress", 0, 0, G_OPTION_ARG_NONE, &compress, "compress", NULL},
53   {"gzip", 0, 0, G_OPTION_ARG_NONE, &gzip, "use gzip format", NULL},
54   {"from-charset", 0, 0, G_OPTION_ARG_STRING, &from_charset, "from charset", NULL},
55   {"to-charset", 0, 0, G_OPTION_ARG_STRING, &to_charset, "to charset", NULL},
56   {"fallback", 0, 0, G_OPTION_ARG_NONE, &fallback, "use fallback", NULL},
57   {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &locations, "locations", NULL},
58   {NULL}
59 };
60
61 static void
62 decompressor_file_info_notify_cb (GZlibDecompressor *decompressor,
63                                   GParamSpec *pspec,
64                                   gpointer data)
65 {
66   GFileInfo *file_info;
67   const gchar *filename;
68
69   file_info = g_zlib_decompressor_get_file_info (decompressor);
70   if (file_info == NULL)
71     return;
72
73   filename = g_file_info_get_name (file_info);
74   if (filename)
75     g_printerr ("Decompressor filename: %s\n", filename);
76 }
77
78 static void
79 cat (GFile * file)
80 {
81   GInputStream *in;
82   char buffer[1024 * 8 + 1];
83   char *p;
84   gssize res;
85   gboolean close_res;
86   GError *error;
87   GConverter *conv;
88   GCharsetConverter *cconv = NULL;
89
90   error = NULL;
91   in = (GInputStream *) g_file_read (file, NULL, &error);
92   if (in == NULL)
93     {
94       /* Translators: the first %s is the program name, the second one  */
95       /* is the URI of the file, the third is the error message.        */
96       g_printerr ("%s: %s: error opening file: %s\n",
97                   g_get_prgname (), g_file_get_uri (file), error->message);
98       g_error_free (error);
99       return;
100     }
101
102   if (decompress)
103     {
104       GInputStream *old;
105       conv = (GConverter *)g_zlib_decompressor_new (gzip?G_ZLIB_COMPRESSOR_FORMAT_GZIP:G_ZLIB_COMPRESSOR_FORMAT_ZLIB);
106       old = in;
107       in = (GInputStream *) g_converter_input_stream_new (in, conv);
108       g_signal_connect (conv, "notify::file-info", G_CALLBACK (decompressor_file_info_notify_cb), NULL);
109       g_object_unref (conv);
110       g_object_unref (old);
111     }
112
113   if (from_charset && to_charset)
114     {
115       cconv = g_charset_converter_new (to_charset, from_charset, &error);
116       conv = (GConverter *)cconv;
117       if (conv)
118         {
119           GInputStream *old;
120
121           g_charset_converter_set_use_fallback (cconv, fallback);
122
123           old = in;
124           in = (GInputStream *) g_converter_input_stream_new (in, conv);
125           g_object_unref (conv);
126           g_object_unref (old);
127         }
128       else
129         {
130           g_printerr ("%s: Can't convert between charsets: %s\n",
131                       g_get_prgname (), error->message);
132         }
133     }
134
135   if (compress)
136     {
137       GInputStream *old;
138       GFileInfo *in_file_info;
139
140       in_file_info = g_file_query_info (file,
141                                         G_FILE_ATTRIBUTE_STANDARD_NAME ","
142                                         G_FILE_ATTRIBUTE_TIME_MODIFIED,
143                                         G_FILE_QUERY_INFO_NONE,
144                                         NULL,
145                                         &error);
146       if (in_file_info == NULL)
147         {
148           g_printerr ("%s: %s: error reading file info: %s\n",
149                       g_get_prgname (), g_file_get_uri (file), error->message);
150           g_error_free (error);
151           return;
152         }
153
154       conv = (GConverter *)g_zlib_compressor_new(gzip?G_ZLIB_COMPRESSOR_FORMAT_GZIP:G_ZLIB_COMPRESSOR_FORMAT_ZLIB, -1);
155       g_zlib_compressor_set_file_info (G_ZLIB_COMPRESSOR (conv), in_file_info);
156       old = in;
157       in = (GInputStream *) g_converter_input_stream_new (in, conv);
158       g_object_unref (conv);
159       g_object_unref (old);
160       g_object_unref (in_file_info);
161     }
162
163   while (1)
164     {
165       res =
166         g_input_stream_read (in, buffer, sizeof (buffer) - 1, NULL, &error);
167       if (res > 0)
168         {
169           gssize written;
170
171           p = buffer;
172           while (res > 0)
173             {
174               written = write (STDOUT_FILENO, p, res);
175
176               if (written == -1 && errno != EINTR)
177                 {
178                   /* Translators: the first %s is the program name, the */
179                   /* second one is the URI of the file.                 */
180                   g_printerr ("%s: %s, error writing to stdout",
181                               g_get_prgname (), g_file_get_uri (file));
182                   goto out;
183                 }
184               res -= written;
185               p += written;
186             }
187         }
188       else if (res < 0)
189         {
190           g_printerr ("%s: %s: error reading: %s\n",
191                       g_get_prgname (), g_file_get_uri (file),
192                       error->message);
193           g_error_free (error);
194           error = NULL;
195           break;
196         }
197       else if (res == 0)
198         break;
199     }
200
201  out:
202
203   close_res = g_input_stream_close (in, NULL, &error);
204   if (!close_res)
205     {
206       g_printerr ("%s: %s:error closing: %s\n",
207                   g_get_prgname (), g_file_get_uri (file), error->message);
208       g_error_free (error);
209     }
210
211   if (cconv != NULL && fallback)
212     {
213       guint num = g_charset_converter_get_num_fallbacks (cconv);
214       if (num > 0)
215         g_printerr ("Number of fallback errors: %u\n", num);
216     }
217 }
218
219 int
220 main (int argc, char *argv[])
221 {
222   GError *error = NULL;
223   GOptionContext *context = NULL;
224   GFile *file;
225   int i;
226
227   context =
228     g_option_context_new ("LOCATION... - concatenate LOCATIONS "
229                           "to standard output.");
230
231   g_option_context_set_summary (context, "filter files");
232
233   g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
234   g_option_context_parse (context, &argc, &argv, &error);
235
236   g_option_context_free (context);
237
238   if (error != NULL)
239     {
240       g_printerr ("Error parsing commandline options: %s\n", error->message);
241       g_printerr ("\n");
242       g_printerr ("Try \"%s --help\" for more information.",
243                   g_get_prgname ());
244       g_printerr ("\n");
245       g_error_free(error);
246       return 1;
247     }
248
249   if (!locations)
250     {
251       g_printerr ("%s: missing locations", g_get_prgname ());
252       g_printerr ("\n");
253       g_printerr ("Try \"%s --help\" for more information.",
254                   g_get_prgname ());
255       g_printerr ("\n");
256       return 1;
257     }
258
259   i = 0;
260
261   do
262     {
263       file = g_file_new_for_commandline_arg (locations[i]);
264       cat (file);
265       g_object_unref (file);
266     }
267   while (locations[++i] != NULL);
268
269   return 0;
270 }