diff options
Diffstat (limited to 'programs/gvfs-cat.c')
-rw-r--r-- | programs/gvfs-cat.c | 155 |
1 files changed, 105 insertions, 50 deletions
diff --git a/programs/gvfs-cat.c b/programs/gvfs-cat.c index 0d6c9b41..351f8714 100644 --- a/programs/gvfs-cat.c +++ b/programs/gvfs-cat.c @@ -1,3 +1,5 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ + /* GIO - GLib Input, Output and Streaming Library * * Copyright (C) 2006-2007 Red Hat, Inc. @@ -28,70 +30,87 @@ #include <errno.h> #include <glib.h> +#include <glib/gi18n.h> #include <gio/gio.h> -static GOptionEntry entries[] = -{ - { NULL } +static gchar **locations = NULL; + +static GOptionEntry entries[] = { + {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &locations, "locations", NULL}, + {NULL} }; static void -cat (GFile *file) +cat (GFile * file) { GInputStream *in; - char buffer[1024*8 + 1]; + char buffer[1024 * 8 + 1]; char *p; gssize res; gboolean close_res; GError *error; - + error = NULL; - in = (GInputStream *)g_file_read (file, NULL, &error); + in = (GInputStream *) g_file_read (file, NULL, &error); if (in == NULL) { - g_printerr ("Error opening file: %s\n", error->message); + /* Translators: the first %s is the program name, the second one */ + /* is the URI of the file, the third is the error message. */ + g_printerr (_("%s: %s: error opening file: %s\n"), + g_get_prgname (), g_file_get_uri (file), error->message); g_error_free (error); return; } while (1) { - res = g_input_stream_read (in, buffer, sizeof (buffer) - 1, NULL, &error); + res = + g_input_stream_read (in, buffer, sizeof (buffer) - 1, NULL, &error); if (res > 0) - { - ssize_t written; - - p = buffer; - while (res > 0) - { - written = write (STDOUT_FILENO, p, res); - - if (written == -1 && errno != EINTR) - { - perror ("Error writing to stdout"); - goto out; - } - res -= written; - p += written; - } - } + { + ssize_t written; + + p = buffer; + while (res > 0) + { + written = write (STDOUT_FILENO, p, res); + + if (written == -1 && errno != EINTR) + { + /* Translators: the first %s is the program name, the */ + /* second one is the URI of the file. */ + g_printerr (_("%s: %s, error writing to stdout"), + g_get_prgname (), g_file_get_uri (file)); + goto out; + } + res -= written; + p += written; + } + } else if (res < 0) - { - g_printerr ("Error reading: %s\n", error->message); - g_error_free (error); - error = NULL; - break; - } + { + /* Translators: the first %s is the program name, the second one */ + /* is the URI of the file, the third is the error message. */ + g_printerr (_("%s: %s: error reading: %s\n"), + g_get_prgname (), g_file_get_uri (file), + error->message); + g_error_free (error); + error = NULL; + break; + } else if (res == 0) - break; + break; } out: - + close_res = g_input_stream_close (in, NULL, &error); if (!close_res) { - g_printerr ("Error closing: %s\n", error->message); + /* Translators: the first %s is the program name, the second one */ + /* is the URI of the file, the third is the error message. */ + g_printerr (_("%s: %s:error closing: %s\n"), + g_get_prgname (), g_file_get_uri (file), error->message); g_error_free (error); } } @@ -99,30 +118,66 @@ cat (GFile *file) int main (int argc, char *argv[]) { - GError *error; - GOptionContext *context; + GError *error = NULL; + GOptionContext *context = NULL; GFile *file; - + gchar *summary; + setlocale (LC_ALL, ""); + bindtextdomain (GETTEXT_PACKAGE, GVFS_LOCALEDIR); + bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); + textdomain (GETTEXT_PACKAGE); + g_type_init (); - - error = NULL; - context = g_option_context_new ("- output files at <location>"); + + /* Translators: this message will appear immediately after the */ + /* usage string - Usage: COMMAND [OPTION]... <THIS_MESSAGE> */ + context = + g_option_context_new (_("LOCATION... - concatenate LOCATIONS " + "to standard output.")); + + /* Translators: this message will appear after the usage string */ + /* and before the list of options. */ + summary = g_strconcat (_("Concatenate files at locations and print to the " + "standard output. Works just like the traditional " + "cat utility, but using gvfs location instead " + "local files: for example you can use something " + "like smb://server/resource/file.txt as location " + "to concatenate."), + "\n\n", + _("Note: just pipe through cat if you need its " + "formatting option like -n, -T or other."), NULL); + + g_option_context_set_summary (context, summary); + g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); g_option_context_parse (context, &argc, &argv, &error); + g_option_context_free (context); - - if (argc > 1) + g_free (summary); + + if (!locations) + { + /* Translators: the %s is the program name. This error message */ + /* means the user is calling gvfs-cat without any argument. */ + g_printerr (_("%s: missing locations"), g_get_prgname ()); + g_printerr ("\n"); + g_printerr (_("Try \"%s --help\" for more information."), + g_get_prgname ()); + g_printerr ("\n"); + return 1; + } + + int i = 0; + + do { - int i; - - for (i = 1; i < argc; i++) { - file = g_file_new_for_commandline_arg (argv[i]); - cat (file); - g_object_unref (file); - } + file = g_file_new_for_commandline_arg (locations[i]); + cat (file); + g_object_unref (file); } + while (locations[++i] != NULL); return 0; } |