Blame gio/gio-tool-cat.c

Packit ae235b
/*
Packit ae235b
 * Copyright 2015 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Matthias Clasen <mclasen@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <gio/gio.h>
Packit ae235b
#include <gi18n.h>
Packit ae235b
#include <errno.h>
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include <io.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifndef STDOUT_FILENO
Packit ae235b
#define STDOUT_FILENO 1
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef HAVE_UNISTD_H
Packit ae235b
#include <unistd.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include "gio-tool.h"
Packit ae235b
Packit ae235b
Packit ae235b
static const GOptionEntry entries[] = {
Packit ae235b
  { NULL }
Packit ae235b
};
Packit ae235b
Packit ae235b
/* 256k minus malloc overhead */
Packit ae235b
#define STREAM_BUFFER_SIZE (1024*256 - 2*sizeof(gpointer))
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
cat (GFile *file)
Packit ae235b
{
Packit ae235b
  GInputStream *in;
Packit ae235b
  char *buffer;
Packit ae235b
  char *p;
Packit ae235b
  gssize res;
Packit ae235b
  gboolean close_res;
Packit ae235b
  GError *error;
Packit ae235b
  gboolean success;
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  in = (GInputStream *) g_file_read (file, NULL, &error);
Packit ae235b
  if (in == NULL)
Packit ae235b
    {
Packit ae235b
      print_file_error (file, error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  buffer = g_malloc (STREAM_BUFFER_SIZE);
Packit ae235b
  success = TRUE;
Packit ae235b
  while (1)
Packit ae235b
    {
Packit ae235b
      res = g_input_stream_read (in, buffer, STREAM_BUFFER_SIZE, NULL, &error);
Packit ae235b
      if (res > 0)
Packit ae235b
        {
Packit ae235b
          gssize written;
Packit ae235b
Packit ae235b
          p = buffer;
Packit ae235b
          while (res > 0)
Packit ae235b
            {
Packit ae235b
              int errsv;
Packit ae235b
Packit ae235b
              written = write (STDOUT_FILENO, p, res);
Packit ae235b
              errsv = errno;
Packit ae235b
Packit ae235b
              if (written == -1 && errsv != EINTR)
Packit ae235b
                {
Packit ae235b
                  print_error ("%s", _("Error writing to stdout"));
Packit ae235b
                  success = FALSE;
Packit ae235b
                  goto out;
Packit ae235b
                }
Packit ae235b
              res -= written;
Packit ae235b
              p += written;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
      else if (res < 0)
Packit ae235b
        {
Packit ae235b
          print_file_error (file, error->message);
Packit ae235b
          g_error_free (error);
Packit ae235b
          error = NULL;
Packit ae235b
          success = FALSE;
Packit ae235b
          break;
Packit ae235b
        }
Packit ae235b
      else if (res == 0)
Packit ae235b
        break;
Packit ae235b
    }
Packit ae235b
Packit ae235b
 out:
Packit ae235b
 close_res = g_input_stream_close (in, NULL, &error);
Packit ae235b
  if (!close_res)
Packit ae235b
    {
Packit ae235b
      print_file_error (file, error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
      success = FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_free (buffer);
Packit ae235b
Packit ae235b
  return success;
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
handle_cat (int argc, char *argv[], gboolean do_help)
Packit ae235b
{
Packit ae235b
  GOptionContext *context;
Packit ae235b
  gchar *param;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  int i;
Packit ae235b
  gboolean res;
Packit ae235b
  GFile *file;
Packit ae235b
Packit ae235b
  g_set_prgname ("gio cat");
Packit ae235b
  /* Translators: commandline placeholder */
Packit ae235b
  param = g_strdup_printf ("%s...", _("LOCATION"));
Packit ae235b
  context = g_option_context_new (param);
Packit ae235b
  g_free (param);
Packit ae235b
  g_option_context_set_help_enabled (context, FALSE);
Packit ae235b
  g_option_context_set_summary (context,
Packit ae235b
      _("Concatenate files and print to standard output."));
Packit ae235b
  g_option_context_set_description (context,
Packit ae235b
      _("gio cat works just like the traditional cat utility, but using GIO\n"
Packit ae235b
        "locations instead of local files: for example, you can use something\n"
Packit ae235b
        "like smb://server/resource/file.txt as location."));
Packit ae235b
  g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
Packit ae235b
Packit ae235b
  if (do_help)
Packit ae235b
    {
Packit ae235b
      show_help (context, NULL);
Packit ae235b
      g_option_context_free (context);
Packit ae235b
      return 0;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!g_option_context_parse (context, &argc, &argv, &error))
Packit ae235b
    {
Packit ae235b
      show_help (context, error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
      g_option_context_free (context);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (argc < 2)
Packit ae235b
    {
Packit ae235b
      show_help (context, _("No locations given"));
Packit ae235b
      g_option_context_free (context);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_option_context_free (context);
Packit ae235b
Packit ae235b
  res = TRUE;
Packit ae235b
  for (i = 1; i < argc; i++)
Packit ae235b
    {
Packit ae235b
      file = g_file_new_for_commandline_arg (argv[i]);
Packit ae235b
      res &= cat (file);
Packit ae235b
      g_object_unref (file);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return res ? 0 : 2;
Packit ae235b
}