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