ftp://ftp.redhat.com/pub/redhat/linux/rawhide/SRPMS/SRPMS/gnome-vfs2-2.3.8-1.src.rpm
[gnome-vfs-httpcaptive.git] / programs / gnomevfs-cat.c
1 /* gnomevfs-cat.c - Test for open() and read() for gnome-vfs
2
3    Copyright (C) 1999 Free Software Foundation
4    Copyright (C) 2003, Red Hat
5
6    Example use: vfscat http://host:port | mpg123 -
7
8    The Gnome Library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU Library General Public License as
10    published by the Free Software Foundation; either version 2 of the
11    License, or (at your option) any later version.
12
13    The Gnome Library is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    Library General Public License for more details.
17
18    You should have received a copy of the GNU Library General Public
19    License along with the Gnome Library; see the file COPYING.LIB.  If not,
20    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.
22
23    Author: Ettore Perazzoli <ettore@gnu.org>
24            Bastien Nocera <hadess@hadess.net>
25 */
26
27 #include <libgnomevfs/gnome-vfs.h>
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32
33 static void
34 show_result (GnomeVFSResult result, const gchar *what, const gchar *text_uri)
35 {
36         if (result != GNOME_VFS_OK) {
37                 fprintf (stderr, "%s `%s': %s\n",
38                                 what, text_uri,
39                                 gnome_vfs_result_to_string (result));
40                 exit (1);
41         }
42 }
43
44 int
45 main (int argc, char **argv)
46 {
47         GnomeVFSResult    result;
48         GnomeVFSHandle   *handle;
49         gchar             buffer[1024];
50         GnomeVFSFileSize  bytes_read;
51         GnomeVFSURI      *uri;
52         gchar            *text_uri;
53
54         if (argc != 2) {
55                 fprintf (stderr, "Usage: %s <uri>\n", argv[0]);
56                 return 1;
57         }
58
59         if (! gnome_vfs_init ()) {
60                 fprintf (stderr, "Cannot initialize gnome-vfs.\n");
61                 return 1;
62         }
63
64         uri = gnome_vfs_uri_new (argv[1]);
65         if (uri == NULL) {
66                 fprintf (stderr, "URI %s not valid.\n", argv[1]);
67                 return 1;
68         }
69
70         text_uri = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_NONE);
71
72         result = gnome_vfs_open_uri (&handle, uri, GNOME_VFS_OPEN_READ);
73         show_result (result, "open", text_uri);
74
75         while (result == GNOME_VFS_OK) {
76                 result = gnome_vfs_read (handle, buffer,
77                                 sizeof (buffer) - 1,
78                                 &bytes_read);
79
80                 /* show_result (result, "read", text_uri); */
81
82                 buffer[bytes_read] = 0;
83                 write (1, buffer, bytes_read);
84                 if(bytes_read == 0)
85                         break;
86         }
87
88         result = gnome_vfs_close (handle);
89         show_result (result, "close", text_uri);
90
91         g_free (text_uri);
92
93         return 0;
94 }
95