Initialize variable
[glib.git] / gio / gio-tool-list.c
blob202d79978c739aab425b6e52caa7da7989636a10
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.1 of the License, 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>
25 #include "gio-tool.h"
28 static char *attributes = NULL;
29 static gboolean show_hidden = FALSE;
30 static gboolean show_long = FALSE;
31 static gboolean nofollow_symlinks = FALSE;
32 static gboolean print_uris = FALSE;
34 static const GOptionEntry entries[] = {
35 { "attributes", 'a', 0, G_OPTION_ARG_STRING, &attributes, N_("The attributes to get"), N_("ATTRIBUTES") },
36 { "hidden", 'h', 0, G_OPTION_ARG_NONE, &show_hidden, N_("Show hidden files"), NULL },
37 { "long", 'l', 0, G_OPTION_ARG_NONE, &show_long, N_("Use a long listing format"), NULL },
38 { "nofollow-symlinks", 'n', 0, G_OPTION_ARG_NONE, &nofollow_symlinks, N_("Don’t follow symbolic links"), NULL},
39 { "print-uris", 'u', 0, G_OPTION_ARG_NONE, &print_uris, N_("Print full URIs"), NULL},
40 { NULL }
43 static void
44 show_file_listing (GFileInfo *info, GFile *parent)
46 const char *name, *type;
47 char *uri = NULL;
48 goffset size;
49 char **attributes;
50 int i;
51 gboolean first_attr;
52 GFile *child;
54 if ((g_file_info_get_is_hidden (info)) && !show_hidden)
55 return;
57 name = g_file_info_get_name (info);
58 if (name == NULL)
59 name = "";
61 if (print_uris) {
62 child = g_file_get_child (parent, name);
63 uri = g_file_get_uri (child);
64 g_object_unref (child);
67 size = g_file_info_get_size (info);
68 type = file_type_to_string (g_file_info_get_file_type (info));
69 if (show_long)
70 g_print ("%s\t%"G_GUINT64_FORMAT"\t(%s)", print_uris? uri: name, (guint64)size, type);
71 else
72 g_print ("%s", print_uris? uri: name);
74 if (print_uris)
75 g_free (uri);
77 first_attr = TRUE;
78 attributes = g_file_info_list_attributes (info, NULL);
79 for (i = 0 ; attributes[i] != NULL; i++)
81 char *val_as_string;
83 if (!show_long ||
84 strcmp (attributes[i], G_FILE_ATTRIBUTE_STANDARD_NAME) == 0 ||
85 strcmp (attributes[i], G_FILE_ATTRIBUTE_STANDARD_SIZE) == 0 ||
86 strcmp (attributes[i], G_FILE_ATTRIBUTE_STANDARD_TYPE) == 0 ||
87 strcmp (attributes[i], G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN) == 0)
88 continue;
90 if (first_attr)
92 g_print ("\t");
93 first_attr = FALSE;
95 else
96 g_print (" ");
97 val_as_string = g_file_info_get_attribute_as_string (info, attributes[i]);
98 g_print ("%s=%s", attributes[i], val_as_string);
99 g_free (val_as_string);
102 g_strfreev (attributes);
104 g_print ("\n");
107 static gboolean
108 list (GFile *file)
110 GFileEnumerator *enumerator;
111 GFileInfo *info;
112 GError *error;
113 gboolean res;
115 error = NULL;
116 enumerator = g_file_enumerate_children (file,
117 attributes,
118 nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS : 0,
119 NULL,
120 &error);
121 if (enumerator == NULL)
123 print_file_error (file, error->message);
124 g_error_free (error);
125 return FALSE;
128 res = TRUE;
129 while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)) != NULL)
131 show_file_listing (info, file);
132 g_object_unref (info);
135 if (error)
137 print_file_error (file, error->message);
138 g_clear_error (&error);
139 res = FALSE;
142 if (!g_file_enumerator_close (enumerator, NULL, &error))
144 print_file_error (file, error->message);
145 g_clear_error (&error);
146 res = FALSE;
149 return res;
153 handle_list (int argc, char *argv[], gboolean do_help)
155 GOptionContext *context;
156 gchar *param;
157 GError *error = NULL;
158 gboolean res;
159 gint i;
160 GFile *file;
162 g_set_prgname ("gio list");
164 /* Translators: commandline placeholder */
165 param = g_strdup_printf ("[%s...]", _("LOCATION"));
166 context = g_option_context_new (param);
167 g_free (param);
168 g_option_context_set_help_enabled (context, FALSE);
169 g_option_context_set_summary (context,
170 _("List the contents of the locations."));
171 g_option_context_set_description (context,
172 _("gio list is similar to the traditional ls utility, but using GIO\n"
173 "locations instead of local files: for example, you can use something\n"
174 "like smb://server/resource/file.txt as location. File attributes can\n"
175 "be specified with their GIO name, e.g. standard::icon"));
176 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
178 if (do_help)
180 show_help (context, NULL);
181 g_option_context_free (context);
182 return 0;
185 if (!g_option_context_parse (context, &argc, &argv, &error))
187 show_help (context, error->message);
188 g_error_free (error);
189 g_option_context_free (context);
190 return 1;
193 g_option_context_free (context);
195 if (attributes != NULL)
196 show_long = TRUE;
198 attributes = g_strconcat (G_FILE_ATTRIBUTE_STANDARD_NAME ","
199 G_FILE_ATTRIBUTE_STANDARD_TYPE ","
200 G_FILE_ATTRIBUTE_STANDARD_SIZE ","
201 G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN,
202 attributes != NULL ? "," : "",
203 attributes,
204 NULL);
206 res = TRUE;
207 if (argc > 1)
209 for (i = 1; i < argc; i++)
211 file = g_file_new_for_commandline_arg (argv[i]);
212 res &= list (file);
213 g_object_unref (file);
216 else
218 char *cwd;
220 cwd = g_get_current_dir ();
221 file = g_file_new_for_path (cwd);
222 res = list (file);
223 g_object_unref (file);
224 g_free (cwd);
227 g_free (attributes);
229 return res ? 0 : 2;