vlc | branch: master | Rémi Denis-Courmont <[email protected]> | Mon Jul 11 18:39:59 2011 +0300| [5110789433bedb1a3a495aa71bcfbe2934cefc2a] | committer: Rémi Denis-Courmont
vlc_getcwd: return current directory as UTF-8 > http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=5110789433bedb1a3a495aa71bcfbe2934cefc2a --- include/vlc_fs.h | 1 + src/libvlccore.sym | 1 + src/posix/filesystem.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/win32/filesystem.c | 11 +++++++++++ 4 files changed, 60 insertions(+), 0 deletions(-) diff --git a/include/vlc_fs.h b/include/vlc_fs.h index e3ab442..65cd4cb 100644 --- a/include/vlc_fs.h +++ b/include/vlc_fs.h @@ -43,6 +43,7 @@ VLC_API int vlc_mkdir( const char *filename, mode_t mode ); VLC_API int vlc_unlink( const char *filename ); VLC_API int vlc_rename( const char *oldpath, const char *newpath ); +VLC_API char *vlc_getcwd( void ) VLC_USED; #if defined( WIN32 ) # ifndef UNDER_CE diff --git a/src/libvlccore.sym b/src/libvlccore.sym index 4d937b9..3b6de36 100644 --- a/src/libvlccore.sym +++ b/src/libvlccore.sym @@ -467,6 +467,7 @@ vlc_stat vlc_strcasestr vlc_unlink vlc_rename +vlc_getcwd vlc_dup vlc_pipe vlc_accept diff --git a/src/posix/filesystem.c b/src/posix/filesystem.c index aadf35b..3347496 100644 --- a/src/posix/filesystem.c +++ b/src/posix/filesystem.c @@ -307,6 +307,53 @@ error: } /** + * Determines the current working directory. + * + * @return the current working directory (must be free()'d) + * or NULL on error + */ +char *vlc_getcwd (void) +{ + /* Try $PWD */ + const char *pwd = getenv ("PWD"); + if (pwd != NULL) + { + struct stat s1, s2; + /* Make sure $PWD is correct */ + if (stat (pwd, &s1) == 0 && stat (".", &s2) == 0 + && s1.st_dev == s2.st_dev && s1.st_ino == s2.st_ino) + return ToLocaleDup (pwd); + } + + /* Otherwise iterate getcwd() until the buffer is big enough */ + long path_max = pathconf (".", _PC_PATH_MAX); + size_t size = (path_max == -1 || path_max > 4096) ? 4096 : path_max; + + for (;; size *= 2) + { + char *buf = malloc (size); + if (unlikely(buf == NULL)) + break; + + if (getcwd (buf, size) != NULL) +#ifdef ASSUME_UTF8 + return buf; +#else + { + char *ret = ToLocaleDup (buf); + free (buf); + return ret; /* success */ + } +#endif + free (buf); + + if (errno != ERANGE) + break; + } + return NULL; +} + +/** * Duplicates a file descriptor. The new file descriptor has the close-on-exec * descriptor flag set. * @return a new file descriptor or -1 diff --git a/src/win32/filesystem.c b/src/win32/filesystem.c index dfcaee8..0ba5e0a 100644 --- a/src/win32/filesystem.c +++ b/src/win32/filesystem.c @@ -108,6 +108,17 @@ int vlc_mkdir( const char *dirname, mode_t mode ) #endif } +char *vlc_getcwd (void) +{ + wchar_t *wdir = _wgetcwd (NULL, 0); + if (wdir == NULL) + return NULL; + + char *dir = FromWide (wdir); + free (wdir); + return dir; +} + /* Under Windows, these wrappers return the list of drive letters * when called with an empty argument or just '\'. */ typedef struct vlc_DIR _______________________________________________ vlc-commits mailing list [email protected] http://mailman.videolan.org/listinfo/vlc-commits
