Add some more cases to the app-id unit tests
[glib.git] / gio / gio-tool-cat.c
blobc0572645225d912260ae5e204cc7cb020f7c6dfd
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 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>
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 return 0;
142 if (!g_option_context_parse (context, &argc, &argv, &error))
144 show_help (context, error->message);
145 g_error_free (error);
146 return 1;
149 if (argc < 2)
151 show_help (context, _("No files given"));
152 return 1;
155 g_option_context_free (context);
157 res = TRUE;
158 for (i = 1; i < argc; i++)
160 file = g_file_new_for_commandline_arg (argv[i]);
161 res &= cat (file);
162 g_object_unref (file);
165 return res ? 0 : 2;