1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
#include <config.h>
#include <glib.h>
#include <locale.h>
#include <gio/gfile.h>
static char *attributes = NULL;
static gboolean show_hidden = FALSE;
static GOptionEntry entries[] =
{
{ "attributes", 'a', 0, G_OPTION_ARG_STRING, &attributes, "The attributes to get", NULL },
{ "hidden", 'h', 0, G_OPTION_ARG_NONE, &show_hidden, "Show hidden files", NULL },
{ NULL }
};
static const char *
type_to_string (GFileType type)
{
switch (type)
{
default:
return "invalid type";
case G_FILE_TYPE_UNKNOWN:
return "unknown";
case G_FILE_TYPE_REGULAR:
return "regular";
case G_FILE_TYPE_DIRECTORY:
return "directory";
case G_FILE_TYPE_SYMBOLIC_LINK:
return "symlink";
case G_FILE_TYPE_SPECIAL:
return "special";
case G_FILE_TYPE_SHORTCUT:
return "shortcut";
case G_FILE_TYPE_MOUNTABLE:
return "mountable";
}
}
static void
show_info (GFileInfo *info)
{
const char *name, *type;
goffset size;
char **attributes;
int i;
gboolean first_attr;
if ((g_file_info_get_is_hidden (info)) && !show_hidden)
return;
name = g_file_info_get_name (info);
if (name == NULL)
name = "";
size = g_file_info_get_size (info);
type = type_to_string (g_file_info_get_file_type (info));
g_print ("%s\t%"G_GUINT64_FORMAT"\t(%s)", name, (guint64)size, type);
first_attr = TRUE;
attributes = g_file_info_list_attributes (info, NULL);
for (i = 0 ; attributes[i] != NULL; i++)
{
GFileAttributeValue *val;
char *val_as_string;
if (strcmp (attributes[i], G_FILE_ATTRIBUTE_STD_NAME) == 0 ||
strcmp (attributes[i], G_FILE_ATTRIBUTE_STD_SIZE) == 0 ||
strcmp (attributes[i], G_FILE_ATTRIBUTE_STD_TYPE) == 0)
continue;
if (first_attr)
{
g_print ("\t");
first_attr = FALSE;
}
else
g_print (" ");
val = g_file_info_get_attribute (info, attributes[i]);
val_as_string = g_file_attribute_value_as_string (val);
g_print ("%s=%s", attributes[i], val_as_string);
g_free (val_as_string);
}
g_strfreev (attributes);
g_print ("\n");
}
static void
list (GFile *file)
{
GFileEnumerator *enumerator;
GFileInfo *info;
GError *error;
if (file == NULL)
return;
error = NULL;
enumerator = g_file_enumerate_children (file, attributes, 0, NULL, &error);
if (enumerator == NULL)
{
g_print ("Error: %s\n", error->message);
g_error_free (error);
error = NULL;
return;
}
while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)) != NULL)
{
show_info (info);
g_object_unref (info);
}
if (error)
{
g_print ("Error: %s\n", error->message);
g_error_free (error);
error = NULL;
}
if (!g_file_enumerator_stop (enumerator, NULL, &error))
{
g_print ("Error stopping enumerator: %s\n", error->message);
g_error_free (error);
error = NULL;
}
}
int
main (int argc, char *argv[])
{
GError *error;
GOptionContext *context;
GFile *file;
setlocale (LC_ALL, "");
g_type_init ();
error = NULL;
context = g_option_context_new ("- list files at <location>");
g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
g_option_context_parse (context, &argc, &argv, &error);
attributes = g_strconcat (G_FILE_ATTRIBUTE_STD_NAME "," G_FILE_ATTRIBUTE_STD_TYPE "," G_FILE_ATTRIBUTE_STD_SIZE,
attributes != NULL ? "," : "",
attributes,
NULL);
if (argc > 1)
{
int i;
for (i = 1; i < argc; i++) {
file = g_file_new_for_commandline_arg (argv[i]);
list (file);
g_object_unref (file);
}
}
else
{
char *cwd;
cwd = g_get_current_dir ();
file = g_file_new_for_path (cwd);
g_free (cwd);
list (file);
g_object_unref (file);
}
g_free (attributes);
return 0;
}
|