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>
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
},
44 show_file_listing (GFileInfo
*info
, GFile
*parent
)
46 const char *name
, *type
;
54 if ((g_file_info_get_is_hidden (info
)) && !show_hidden
)
57 name
= g_file_info_get_name (info
);
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
));
70 g_print ("%s\t%"G_GUINT64_FORMAT
"\t(%s)", print_uris
? uri
: name
, (guint64
)size
, type
);
72 g_print ("%s", print_uris
? uri
: name
);
78 attributes
= g_file_info_list_attributes (info
, NULL
);
79 for (i
= 0 ; attributes
[i
] != NULL
; i
++)
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)
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
);
110 GFileEnumerator
*enumerator
;
116 enumerator
= g_file_enumerate_children (file
,
118 nofollow_symlinks
? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
: 0,
121 if (enumerator
== NULL
)
123 print_file_error (file
, error
->message
);
124 g_error_free (error
);
129 while ((info
= g_file_enumerator_next_file (enumerator
, NULL
, &error
)) != NULL
)
131 show_file_listing (info
, file
);
132 g_object_unref (info
);
137 print_file_error (file
, error
->message
);
138 g_clear_error (&error
);
142 if (!g_file_enumerator_close (enumerator
, NULL
, &error
))
144 print_file_error (file
, error
->message
);
145 g_clear_error (&error
);
153 handle_list (int argc
, char *argv
[], gboolean do_help
)
155 GOptionContext
*context
;
157 GError
*error
= NULL
;
162 g_set_prgname ("gio list");
164 /* Translators: commandline placeholder */
165 param
= g_strdup_printf ("[%s...]", _("LOCATION"));
166 context
= g_option_context_new (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
);
180 show_help (context
, NULL
);
181 g_option_context_free (context
);
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
);
193 g_option_context_free (context
);
195 if (attributes
!= NULL
)
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
? "," : "",
209 for (i
= 1; i
< argc
; i
++)
211 file
= g_file_new_for_commandline_arg (argv
[i
]);
213 g_object_unref (file
);
220 cwd
= g_get_current_dir ();
221 file
= g_file_new_for_path (cwd
);
223 g_object_unref (file
);