1 /*****************************************************************************
2 * filesystem.c: Windows file system helpers
3 *****************************************************************************
4 * Copyright (C) 2005-2006 the VideoLAN team
5 * Copyright © 2005-2008 Rémi Denis-Courmont
7 * Authors: Rémi Denis-Courmont <rem # videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_charset.h>
34 #include "libvlc.h" /* vlc_mkdir */
40 #include <sys/types.h>
51 static int convert_path (const char *restrict path, wchar_t *restrict wpath)
53 if (!MultiByteToWideChar (CP_UTF8, 0, path, -1, wpath, MAX_PATH))
58 wpath[MAX_PATH] = L'\0';
61 #define CONVERT_PATH(path, wpath, err) \
62 wchar_t wpath[MAX_PATH+1]; \
63 if (convert_path (path, wpath)) \
66 int vlc_open (const char *filename, int flags, ...)
68 unsigned int mode = 0;
73 mode = va_arg (ap, unsigned int);
77 /*_open translates to wchar internally on WinCE*/
78 return _open (filename, flags, mode);
81 * open() cannot open files with non-“ANSI” characters on Windows.
82 * We use _wopen() instead. Same thing for mkdir() and stat().
84 CONVERT_PATH(filename, wpath, -1);
85 return _wopen (wpath, flags, mode);
90 int vlc_openat (int dir, const char *filename, int flags, ...)
92 (void) dir; (void) filename; (void) flags;
97 int vlc_mkdir( const char *dirname, mode_t mode )
99 #if defined (UNDER_CE)
101 /* mkdir converts internally to wchar */
102 return _mkdir(dirname);
105 CONVERT_PATH (dirname, wpath, -1);
106 return _wmkdir (wpath);
111 /* Under Windows, these wrappers return the list of drive letters
112 * when called with an empty argument or just '\'. */
113 typedef struct vlc_DIR
121 DIR *vlc_opendir (const char *dirname)
123 CONVERT_PATH (dirname, wpath, NULL);
125 vlc_DIR *p_dir = malloc (sizeof (*p_dir));
126 if (unlikely(p_dir == NULL))
129 if (wpath == NULL || wpath[0] == '\0'
130 || (wcscmp (wpath, L"\\") == 0))
132 /* Special mode to list drive letters */
133 p_dir->p_real_dir = NULL;
137 p_dir->i_drives = GetLogicalDrives ();
139 return (void *)p_dir;
142 _WDIR *p_real_dir = _wopendir (wpath);
143 if (p_real_dir == NULL)
149 p_dir->p_real_dir = p_real_dir;
151 assert (wpath[0]); // wpath[1] is defined
152 p_dir->b_insert_back = !wcscmp (wpath + 1, L":\\");
153 return (void *)p_dir;
156 char *vlc_readdir (DIR *dir)
158 vlc_DIR *p_dir = (vlc_DIR *)dir;
160 if (p_dir->p_real_dir == NULL)
162 /* Drive letters mode */
163 DWORD drives = p_dir->i_drives;
165 return NULL; /* end */
168 return strdup ("\\");
171 for (i = 0; !(drives & 1); i++)
173 p_dir->i_drives &= ~(1UL << i);
177 if (asprintf (&ret, "%c:\\", 'A' + i) == -1)
183 if (p_dir->b_insert_back)
185 /* Adds "..", gruik! */
186 p_dir->b_insert_back = false;
187 return strdup ("..");
190 struct _wdirent *ent = _wreaddir (p_dir->p_real_dir);
193 return FromWide (ent->d_name);
196 int vlc_stat (const char *filename, struct stat *buf)
199 /* _stat translates to wchar internally on WinCE */
200 return _stat (filename, buf);
202 CONVERT_PATH (filename, wpath, -1);
203 return _wstati64 (wpath, buf);
207 int vlc_lstat (const char *filename, struct stat *buf)
209 return vlc_stat (filename, buf);
212 int vlc_unlink (const char *filename)
215 /*_open translates to wchar internally on WinCE*/
216 return _unlink( filename );
218 CONVERT_PATH (filename, wpath, -1);
219 return _wunlink (wpath);
223 int vlc_rename (const char *oldpath, const char *newpath)
225 CONVERT_PATH (oldpath, wold, -1);
226 CONVERT_PATH (newpath, wnew, -1);
228 /* FIXME: errno support */
229 return MoveFileW (wold, wnew) ? 0 : -1;
231 if (_wrename (wold, wnew) && errno == EACCES)
232 { /* Windows does not allow atomic file replacement */
235 errno = EACCES; /* restore errno */
238 if (_wrename (wold, wnew))
245 int vlc_dup (int oldfd)
256 #include <vlc_network.h>
258 int vlc_socket (int pf, int type, int proto, bool nonblock)
260 int fd = socket (pf, type, proto);
265 ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
269 int vlc_accept (int lfd, struct sockaddr *addr, socklen_t *alen, bool nonblock)
271 int fd = accept (lfd, addr, alen);
272 if (fd != -1 && nonblock)
273 ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });