adapt to recent api changes variable declaration at the beginning of a
[platform/upstream/glib.git] / tests / gio-ls.c
1
2 #include <gio/gio.h>
3
4 #define GETTEXT_PACKAGE "gio-ls"
5 #define N_(s) (s)
6 #define _(s) (s)
7
8 enum
9 {
10   SHOW_ALL,
11   SHOW_LONG
12 };
13
14 static void print_path (const gchar* path, guint32 flags);
15
16 static gboolean show_all = FALSE;
17 static gboolean show_long = FALSE;
18
19 int 
20 main (int argc, char *argv[])
21 {
22   
23   GOptionContext *context = NULL;
24   static GOptionEntry options[] =
25   {
26     {"all", 'a', 0, G_OPTION_ARG_NONE, &show_all,
27      N_("do not hide entries"), NULL },
28     {"long", 'l', 0, G_OPTION_ARG_NONE, &show_long,
29      N_("use a long listing format"), NULL },
30     { NULL }
31   };
32   GError *error = NULL;
33   int i;
34
35   g_type_init ();
36
37   context = g_option_context_new(_("[FILE...]"));
38   g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
39   
40   if (!g_option_context_parse (context, &argc, &argv, &error)) 
41     {
42       g_print ("%s", error->message);
43       g_error_free (error);
44       
45     }
46   else
47     {
48       for (i = 1; i < argc; i++) 
49         {
50           print_path (argv[i], (show_all ? SHOW_ALL : 0) | (show_long ? SHOW_LONG : 0));
51         }
52     }
53
54   g_option_context_free(context);
55   return 0;
56 }
57
58 static void 
59 print_path (const gchar* path, 
60             guint32      flags)
61 {
62   GFile *top;
63   const gchar *short_attrs = G_FILE_ATTRIBUTE_STANDARD_NAME;
64   const gchar *long_attrs = G_FILE_ATTRIBUTE_OWNER_USER "," G_FILE_ATTRIBUTE_OWNER_GROUP "," \
65                             "access:*,std:*";
66   const gchar *attrs;
67   
68   if (flags & SHOW_LONG)
69     attrs = long_attrs;
70   else
71     attrs = short_attrs;
72
73   top = g_file_new_for_path (path);
74   if (top)
75     {
76       GFileInfo *info;
77       GError *error = NULL;
78       GFileEnumerator *enumerator = g_file_enumerate_children (top, attrs, 
79                                                                G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, &error);
80       if (error)
81         {
82           g_print ("%s", error->message);
83           g_error_free (error);
84         }
85       if (!enumerator)
86         return;
87  
88       while ((info = g_file_enumerator_next_file (enumerator, NULL, NULL)) != NULL)
89         {
90           const gchar *name = g_file_info_get_name (info);
91
92           if (flags & SHOW_LONG)
93             {
94               const gchar *val;
95               
96               g_print ("%c%c%c%c ",
97                 g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY ? 'd' : '-',
98                 g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ) ? 'r' : '-',
99                 g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE) ? 'w' : '-',
100                 g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE) ? 'x' : '-');
101
102               val = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER);
103               g_print ("\t%15s", val ? val : "?user?");
104
105               val = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_GROUP);
106               g_print ("\t%15s", val ? val : "?group?");
107             }
108             
109           g_print ("\t%s\n", name ? name : "?noname?");
110
111           g_object_unref (info);
112         }
113
114       g_object_unref (top);
115     }
116 }