ftp://ftp.redhat.com/pub/redhat/linux/rawhide/SRPMS/SRPMS/gnome-vfs2-2.3.8-1.src.rpm
[gnome-vfs-httpcaptive.git] / programs / gnomevfs-ls.c
1 /* gnomevfs-ls.c - Test for open_dir(), read_dir() and close_dir() for gnome-vfs
2
3    Copyright (C) 2003, Red Hat
4
5    The Gnome Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    The Gnome Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the Gnome Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.
19
20    Author: Bastien Nocera <hadess@hadess.net>
21 */
22
23 #include <libgnomevfs/gnome-vfs.h>
24
25 char *directory;
26
27 static void show_data (gpointer item, gpointer no_item);
28 static void list (void);
29
30 static const gchar *
31 type_to_string (GnomeVFSFileType type)
32 {
33         switch (type) {
34         case GNOME_VFS_FILE_TYPE_UNKNOWN:
35                 return "Unknown";
36         case GNOME_VFS_FILE_TYPE_REGULAR:
37                 return "Regular";
38         case GNOME_VFS_FILE_TYPE_DIRECTORY:
39                 return "Directory";
40         case GNOME_VFS_FILE_TYPE_SYMBOLIC_LINK:
41                 return "Symbolic Link";
42         case GNOME_VFS_FILE_TYPE_FIFO:
43                 return "FIFO";
44         case GNOME_VFS_FILE_TYPE_SOCKET:
45                 return "Socket";
46         case GNOME_VFS_FILE_TYPE_CHARACTER_DEVICE:
47                 return "Character device";
48         case GNOME_VFS_FILE_TYPE_BLOCK_DEVICE:
49                 return "Block device";
50         default:
51                 return "???";
52         }
53 }
54
55 static void
56 show_data (gpointer item, gpointer no_item)
57 {
58         GnomeVFSFileInfo *info;
59         char *path;
60
61         info = (GnomeVFSFileInfo *) item;
62
63         path = g_strconcat (directory, "/", info->name, NULL);
64
65         g_print ("%s\t%s%s%s\t(%s, %s)\tsize %ld\tmode %04o\n",
66                         info->name,
67                         GNOME_VFS_FILE_INFO_SYMLINK (info) ? " [link: " : "",
68                         GNOME_VFS_FILE_INFO_SYMLINK (info) ? info->symlink_name
69                         : "",
70                         GNOME_VFS_FILE_INFO_SYMLINK (info) ? " ]" : "",
71                         type_to_string (info->type),
72                         info->mime_type,
73                         (glong) info->size,
74                         info->permissions);
75
76         g_free (path);
77 }
78
79 void
80 list (void)
81 {
82         GnomeVFSResult result;
83         GnomeVFSFileInfo *info;
84         GnomeVFSDirectoryHandle *handle;
85
86         result = gnome_vfs_directory_open (&handle, directory,
87                         GNOME_VFS_FILE_INFO_GET_MIME_TYPE
88                         | GNOME_VFS_FILE_INFO_FOLLOW_LINKS);
89
90         if (result != GNOME_VFS_OK)
91         {
92                 g_print("Error opening: %s\n", gnome_vfs_result_to_string
93                                 (result));
94                 return;
95         }
96
97         info = gnome_vfs_file_info_new ();
98         while ((result = gnome_vfs_directory_read_next (handle, info)) == GNOME_VFS_OK) {
99                 show_data ((gpointer) info, NULL);
100         }
101
102         gnome_vfs_file_info_unref (info);
103
104         if (result != GNOME_VFS_OK) {
105                 g_print ("Error: %s\n", gnome_vfs_result_to_string (result));
106                 return;
107         }
108 }
109
110 int
111 main (int argc, char *argv[])
112 {
113         gnome_vfs_init ();
114
115         if (argc > 1) {
116                 directory = argv[1];
117         } else {
118                 directory = g_get_current_dir ();
119         }
120
121         list ();
122
123         gnome_vfs_shutdown ();
124         return 0;
125 }