Mercurial > audlegacy-plugins
annotate src/stdio/stdio.c @ 1903:5c01978673ca
fix for an undefined symbol error of __gxx_personality_v0.
author | Yoshiki Yazawa <yaz@cc.rim.or.jp> |
---|---|
date | Thu, 27 Sep 2007 12:55:56 +0900 |
parents | 761e17b23e0c |
children | 31e9908cf91e |
rev | line source |
---|---|
306 | 1 /* Audacious |
2 * Copyright (c) 2006 William Pitcock | |
3 * | |
4 * This program is free software; you can redistribute it and/or modify | |
5 * it under the terms of the GNU General Public License as published by | |
6 * the Free Software Foundation; either version 2 of the License, or | |
7 * (at your option) any later version. | |
8 * | |
9 * This program is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License | |
15 * along with this program; if not, write to the Free Software | |
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 */ | |
18 | |
19 #include <audacious/vfs.h> | |
20 #include <audacious/plugin.h> | |
1190
ed2d7787779e
more warning elimination.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
1146
diff
changeset
|
21 #include "audacious/strings.h" |
306 | 22 #include <stdio.h> |
23 | |
24 #include <unistd.h> | |
25 #include <sys/stat.h> | |
26 #include <sys/types.h> | |
27 | |
500
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
28 #include <string.h> |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
29 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
30 static gchar * |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
31 vfs_stdio_urldecode_path(const gchar * encoded_path) |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
32 { |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
33 const gchar *cur, *ext; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
34 gchar *path, *tmp; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
35 gint realchar; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
36 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
37 if (!encoded_path) |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
38 return NULL; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
39 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
40 if (!str_has_prefix_nocase(encoded_path, "file:")) |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
41 return NULL; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
42 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
43 cur = encoded_path + 5; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
44 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
45 if (str_has_prefix_nocase(cur, "//localhost")) |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
46 cur += 11; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
47 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
48 if (*cur == '/') |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
49 while (cur[1] == '/') |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
50 cur++; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
51 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
52 tmp = g_malloc0(strlen(cur) + 1); |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
53 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
54 while ((ext = strchr(cur, '%')) != NULL) { |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
55 strncat(tmp, cur, ext - cur); |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
56 ext++; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
57 cur = ext + 2; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
58 if (!sscanf(ext, "%2x", &realchar)) { |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
59 /* Assume it is a literal '%'. Several file |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
60 * managers send unencoded file: urls on drag |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
61 * and drop. */ |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
62 realchar = '%'; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
63 cur -= 2; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
64 } |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
65 tmp[strlen(tmp)] = realchar; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
66 } |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
67 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
68 path = g_strconcat(tmp, cur, NULL); |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
69 g_free(tmp); |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
70 return path; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
71 } |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
72 |
306 | 73 VFSFile * |
74 stdio_vfs_fopen_impl(const gchar * path, | |
75 const gchar * mode) | |
76 { | |
77 VFSFile *file; | |
500
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
78 gchar *decpath; |
306 | 79 |
80 if (!path || !mode) | |
81 return NULL; | |
82 | |
500
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
83 decpath = vfs_stdio_urldecode_path(path); |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
84 |
306 | 85 file = g_new(VFSFile, 1); |
86 | |
500
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
87 file->handle = fopen(decpath != NULL ? decpath : path, mode); |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
88 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
89 if (decpath != NULL) |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
90 g_free(decpath); |
306 | 91 |
92 if (file->handle == NULL) { | |
93 g_free(file); | |
94 file = NULL; | |
95 } | |
96 | |
97 return file; | |
98 } | |
99 | |
100 gint | |
101 stdio_vfs_fclose_impl(VFSFile * file) | |
102 { | |
103 gint ret = 0; | |
104 | |
105 if (file == NULL) | |
106 return -1; | |
107 | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
108 if (file->handle) |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
109 { |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
110 FILE *handle = (FILE *) file->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
111 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
112 if (fclose(handle) != 0) |
306 | 113 ret = -1; |
1228
01efa0e5b554
file->handle should be cleared on close, otherwise it can lead to double free.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
1210
diff
changeset
|
114 file->handle = NULL; |
306 | 115 } |
116 | |
117 return ret; | |
118 } | |
119 | |
120 size_t | |
121 stdio_vfs_fread_impl(gpointer ptr, | |
122 size_t size, | |
123 size_t nmemb, | |
124 VFSFile * file) | |
125 { | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
126 FILE *handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
127 |
306 | 128 if (file == NULL) |
129 return 0; | |
130 | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
131 handle = (FILE *) file->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
132 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
133 return fread(ptr, size, nmemb, handle); |
306 | 134 } |
135 | |
136 size_t | |
137 stdio_vfs_fwrite_impl(gconstpointer ptr, | |
138 size_t size, | |
139 size_t nmemb, | |
140 VFSFile * file) | |
141 { | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
142 FILE *handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
143 |
306 | 144 if (file == NULL) |
145 return 0; | |
146 | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
147 handle = (FILE *) file->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
148 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
149 return fwrite(ptr, size, nmemb, handle); |
306 | 150 } |
151 | |
152 gint | |
153 stdio_vfs_getc_impl(VFSFile *stream) | |
154 { | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
155 FILE *handle = (FILE *) stream->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
156 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
157 return getc( handle ); |
306 | 158 } |
159 | |
160 gint | |
161 stdio_vfs_ungetc_impl(gint c, VFSFile *stream) | |
162 { | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
163 FILE *handle = (FILE *) stream->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
164 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
165 return ungetc( c , handle ); |
306 | 166 } |
167 | |
168 gint | |
169 stdio_vfs_fseek_impl(VFSFile * file, | |
170 glong offset, | |
171 gint whence) | |
172 { | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
173 FILE *handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
174 |
306 | 175 if (file == NULL) |
176 return 0; | |
177 | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
178 handle = (FILE *) file->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
179 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
180 return fseek(handle, offset, whence); |
306 | 181 } |
182 | |
183 void | |
184 stdio_vfs_rewind_impl(VFSFile * file) | |
185 { | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
186 FILE *handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
187 |
306 | 188 if (file == NULL) |
189 return; | |
190 | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
191 handle = (FILE *) file->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
192 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
193 rewind(handle); |
306 | 194 } |
195 | |
196 glong | |
197 stdio_vfs_ftell_impl(VFSFile * file) | |
198 { | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
199 FILE *handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
200 |
306 | 201 if (file == NULL) |
202 return 0; | |
203 | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
204 handle = (FILE *) file->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
205 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
206 return ftell(handle); |
306 | 207 } |
208 | |
209 gboolean | |
210 stdio_vfs_feof_impl(VFSFile * file) | |
211 { | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
212 FILE *handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
213 |
306 | 214 if (file == NULL) |
215 return FALSE; | |
216 | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
217 handle = (FILE *) file->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
218 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
219 return (gboolean) feof(handle); |
306 | 220 } |
221 | |
222 gint | |
223 stdio_vfs_truncate_impl(VFSFile * file, glong size) | |
224 { | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
225 FILE *handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
226 |
306 | 227 if (file == NULL) |
228 return -1; | |
229 | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
230 handle = (FILE *) file->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
231 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
232 return ftruncate(fileno(handle), size); |
306 | 233 } |
234 | |
965 | 235 off_t |
236 stdio_vfs_fsize_impl(VFSFile * file) | |
237 { | |
238 FILE *handle; | |
239 struct stat s; | |
240 | |
241 if (file == NULL) | |
242 return -1; | |
243 | |
244 handle = (FILE *) file->handle; | |
245 | |
246 if (-1 == fstat(fileno(handle), &s)) | |
247 return -1; | |
248 | |
249 return s.st_size; | |
250 } | |
251 | |
306 | 252 VFSConstructor file_const = { |
253 "file://", | |
254 stdio_vfs_fopen_impl, | |
255 stdio_vfs_fclose_impl, | |
256 stdio_vfs_fread_impl, | |
257 stdio_vfs_fwrite_impl, | |
258 stdio_vfs_getc_impl, | |
259 stdio_vfs_ungetc_impl, | |
260 stdio_vfs_fseek_impl, | |
261 stdio_vfs_rewind_impl, | |
262 stdio_vfs_ftell_impl, | |
263 stdio_vfs_feof_impl, | |
965 | 264 stdio_vfs_truncate_impl, |
265 stdio_vfs_fsize_impl | |
306 | 266 }; |
267 | |
268 static void init(void) | |
269 { | |
270 vfs_register_transport(&file_const); | |
271 } | |
272 | |
273 static void cleanup(void) | |
274 { | |
275 #if 0 | |
276 vfs_unregister_transport(&file_const); | |
277 #endif | |
278 } | |
279 | |
1395
761e17b23e0c
added Discovery plugin type
Cristi Magherusan <majeru@atheme-project.org>
parents:
1228
diff
changeset
|
280 DECLARE_PLUGIN(stdio, init, cleanup, NULL, NULL, NULL, NULL, NULL, NULL); |