Imported Upstream version 2.50.2
[platform/upstream/glib.git] / gio / gio-tool-cat.c
1 /*
2  * Copyright 2015 Red Hat, Inc.
3  *
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.
8  *
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.
13  *
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/>.
16  *
17  * Author: Matthias Clasen <mclasen@redhat.com>
18  */
19
20 #include "config.h"
21
22 #include <gio/gio.h>
23 #include <gi18n.h>
24 #include <errno.h>
25
26 #ifdef G_OS_WIN32
27 #include <io.h>
28 #endif
29
30 #ifndef STDOUT_FILENO
31 #define STDOUT_FILENO 1
32 #endif
33
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #include "gio-tool.h"
39
40
41 static const GOptionEntry entries[] = {
42   { NULL }
43 };
44
45 static gboolean
46 cat (GFile *file)
47 {
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;
55
56   error = NULL;
57   in = (GInputStream *) g_file_read (file, NULL, &error);
58   if (in == NULL)
59     {
60       print_file_error (file, error->message);
61       g_error_free (error);
62       return FALSE;
63     }
64
65   success = TRUE;
66   while (1)
67     {
68       res = g_input_stream_read (in, buffer, sizeof (buffer) - 1, NULL, &error);
69       if (res > 0)
70         {
71           gssize written;
72
73           p = buffer;
74           while (res > 0)
75             {
76               written = write (STDOUT_FILENO, p, res);
77
78               if (written == -1 && errno != EINTR)
79                 {
80                   print_file_error (file, "error writing to stdout");
81                   success = FALSE;
82                   goto out;
83                 }
84               res -= written;
85               p += written;
86             }
87         }
88       else if (res < 0)
89         {
90           print_file_error (file, error->message);
91           g_error_free (error);
92           error = NULL;
93           success = FALSE;
94           break;
95         }
96       else if (res == 0)
97         break;
98     }
99
100  out:
101  close_res = g_input_stream_close (in, NULL, &error);
102   if (!close_res)
103     {
104       print_file_error (file, error->message);
105       g_error_free (error);
106       success = FALSE;
107     }
108
109   return success;
110 }
111
112 int
113 handle_cat (int argc, char *argv[], gboolean do_help)
114 {
115   GOptionContext *context;
116   gchar *param;
117   GError *error = NULL;
118   int i;
119   gboolean res;
120   GFile *file;
121
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);
135
136   if (do_help)
137     {
138       show_help (context, NULL);
139       return 0;
140     }
141
142   if (!g_option_context_parse (context, &argc, &argv, &error))
143     {
144       show_help (context, error->message);
145       g_error_free (error);
146       return 1;
147     }
148
149   if (argc < 2)
150     {
151       show_help (context, _("No files given"));
152       return 1;
153     }
154
155   g_option_context_free (context);
156
157   res = TRUE;
158   for (i = 1; i < argc; i++)
159     {
160       file = g_file_new_for_commandline_arg (argv[i]);
161       res &= cat (file);
162       g_object_unref (file);
163     }
164
165   return res ? 0 : 2;
166 }