2005-09-20 21:26:39 +08:00
/** \file wutil.h
Prototypes for wide character equivalents of various standard unix
2012-11-18 18:23:22 +08:00
functions .
2005-09-20 21:26:39 +08:00
*/
2005-10-04 23:11:39 +08:00
# ifndef FISH_WUTIL_H
# define FISH_WUTIL_H
2005-09-20 21:26:39 +08:00
2006-02-06 21:45:32 +08:00
# include <stdlib.h>
# include <stdio.h>
2005-10-04 23:11:39 +08:00
# include <wchar.h>
2005-09-20 21:26:39 +08:00
# include <dirent.h>
# include <unistd.h>
2005-10-04 23:11:39 +08:00
# include <sys/stat.h>
2005-10-06 06:37:08 +08:00
# include <sys/types.h>
2006-01-26 17:57:06 +08:00
# include <stdarg.h>
2011-12-27 11:18:46 +08:00
# include <string>
2012-12-03 15:38:38 +08:00
# include <utility>
2014-04-30 01:09:38 +08:00
# include <stdint.h>
2012-02-18 07:55:54 +08:00
# include "common.h"
2011-12-27 11:18:46 +08:00
2005-11-02 23:41:59 +08:00
/**
Call this function on startup to create internal wutil
resources . This function doesn ' t do anything .
*/
void wutil_init ( ) ;
2005-09-20 21:26:39 +08:00
/**
Call this function on exit to free internal wutil resources
*/
void wutil_destroy ( ) ;
/**
2012-03-02 16:27:40 +08:00
Wide character version of fopen ( ) . This sets CLO_EXEC .
2005-09-20 21:26:39 +08:00
*/
2012-02-19 01:11:22 +08:00
FILE * wfopen ( const wcstring & path , const char * mode ) ;
2005-09-20 21:26:39 +08:00
2012-03-02 16:27:40 +08:00
/** Sets CLO_EXEC on a given fd */
bool set_cloexec ( int fd ) ;
2005-09-20 21:26:39 +08:00
/**
Wide character version of freopen ( ) .
*/
2012-02-19 01:11:22 +08:00
FILE * wfreopen ( const wcstring & path , const char * mode , FILE * stream ) ;
2005-09-20 21:26:39 +08:00
2012-03-02 16:27:40 +08:00
/** Wide character version of open(). */
int wopen ( const wcstring & pathname , int flags , mode_t mode = 0 ) ;
/** Wide character version of open() that also sets the close-on-exec flag (atomically when possible). */
int wopen_cloexec ( const wcstring & pathname , int flags , mode_t mode = 0 ) ;
2005-09-20 21:26:39 +08:00
2013-04-08 03:40:08 +08:00
/** Mark an fd as nonblocking; returns errno or 0 on success */
int make_fd_nonblocking ( int fd ) ;
/** Mark an fd as blocking; returns errno or 0 on success */
int make_fd_blocking ( int fd ) ;
/** Wide character version of creat(). */
2012-02-19 01:11:22 +08:00
int wcreat ( const wcstring & pathname , mode_t mode ) ;
2005-09-20 21:26:39 +08:00
2012-03-02 16:27:40 +08:00
/** Wide character version of opendir(). Note that opendir() is guaranteed to set close-on-exec by POSIX (hooray). */
2012-02-19 01:11:22 +08:00
DIR * wopendir ( const wcstring & name ) ;
2005-09-20 21:26:39 +08:00
/**
Wide character version of stat ( ) .
*/
2012-02-19 01:11:22 +08:00
int wstat ( const wcstring & file_name , struct stat * buf ) ;
2005-09-20 21:26:39 +08:00
/**
Wide character version of lstat ( ) .
*/
2012-02-19 01:11:22 +08:00
int lwstat ( const wcstring & file_name , struct stat * buf ) ;
2005-09-20 21:26:39 +08:00
/**
Wide character version of access ( ) .
*/
2012-02-19 01:11:22 +08:00
int waccess ( const wcstring & pathname , int mode ) ;
2005-09-20 21:26:39 +08:00
2012-02-16 16:24:27 +08:00
/**
Wide character version of unlink ( ) .
*/
2012-02-19 01:11:22 +08:00
int wunlink ( const wcstring & pathname ) ;
2012-02-16 16:24:27 +08:00
2005-09-20 21:26:39 +08:00
/**
Wide character version of perror ( ) .
*/
2014-04-28 04:34:51 +08:00
void wperror ( const wchar_t * s ) ;
2005-09-20 21:26:39 +08:00
2013-01-10 09:06:20 +08:00
/**
Async - safe version of perror ( ) .
*/
void safe_perror ( const char * message ) ;
/**
Async - safe version of strerror ( ) .
*/
const char * safe_strerror ( int err ) ;
2005-09-20 21:26:39 +08:00
/**
Wide character version of getcwd ( ) .
*/
2012-11-19 08:30:30 +08:00
wchar_t * wgetcwd ( wchar_t * buff , size_t sz ) ;
2005-09-20 21:26:39 +08:00
/**
Wide character version of chdir ( )
*/
2012-11-19 08:30:30 +08:00
int wchdir ( const wcstring & dir ) ;
2005-09-20 21:26:39 +08:00
2012-11-18 18:23:22 +08:00
/**
Wide character version of realpath function . Just like the GNU
version of realpath , wrealpath will accept 0 as the value for the
second argument , in which case the result will be allocated using
malloc , and must be free ' d by the user .
2006-02-02 23:23:56 +08:00
*/
2012-02-19 01:11:22 +08:00
wchar_t * wrealpath ( const wcstring & pathname , wchar_t * resolved_path ) ;
2006-02-02 23:23:56 +08:00
2006-06-17 21:07:08 +08:00
/**
Wide character version of readdir ( )
*/
2012-02-20 18:13:31 +08:00
bool wreaddir ( DIR * dir , std : : wstring & out_name ) ;
bool wreaddir_resolving ( DIR * dir , const std : : wstring & dir_path , std : : wstring & out_name , bool * out_is_dir ) ;
2006-02-08 22:58:47 +08:00
2006-06-14 21:22:40 +08:00
/**
Wide character version of dirname ( )
*/
2012-11-19 08:30:30 +08:00
std : : wstring wdirname ( const std : : wstring & path ) ;
2006-06-14 21:22:40 +08:00
/**
Wide character version of basename ( )
*/
2012-11-19 08:30:30 +08:00
std : : wstring wbasename ( const std : : wstring & path ) ;
2006-06-14 21:22:40 +08:00
2006-07-20 06:55:49 +08:00
/**
Wide character wrapper around the gettext function . For historic
reasons , unlike the real gettext function , wgettext takes care of
setting the correct domain , etc . using the textdomain and
bindtextdomain functions . This should probably be moved out of
wgettext , so that wgettext will be nothing more than a wrapper
around gettext , like all other functions in this file .
*/
2012-11-19 08:30:30 +08:00
const wchar_t * wgettext ( const wchar_t * in ) ;
2006-06-14 21:22:40 +08:00
2006-08-11 22:55:28 +08:00
/**
Wide character version of getenv
*/
2012-11-19 08:30:30 +08:00
const wchar_t * wgetenv ( const wcstring & name ) ;
2006-08-11 22:55:28 +08:00
2006-09-08 22:11:28 +08:00
/**
Wide character version of mkdir
*/
2012-11-19 08:30:30 +08:00
int wmkdir ( const wcstring & dir , int mode ) ;
2006-08-11 22:55:28 +08:00
2006-10-21 06:33:47 +08:00
/**
Wide character version of rename
*/
2012-11-19 08:30:30 +08:00
int wrename ( const wcstring & oldName , const wcstring & newName ) ;
2006-10-21 06:33:47 +08:00
2012-08-05 02:07:42 +08:00
/** Like wcstol(), but fails on a value outside the range of an int */
int fish_wcstoi ( const wchar_t * str , wchar_t * * endptr , int base ) ;
2014-04-29 06:14:33 +08:00
/** Class for representing a file's inode. We use this to detect and avoid symlink loops, among other things. While an inode / dev pair is sufficient to distinguish co-existing files, Linux seems to aggressively re-use inodes, so it cannot determine if a file has been deleted (ABA problem). Therefore we include richer information. */
struct file_id_t
{
dev_t device ;
ino_t inode ;
uint64_t size ;
time_t change_seconds ;
long change_nanoseconds ;
uint32_t generation ;
bool operator = = ( const file_id_t & rhs ) const ;
bool operator ! = ( const file_id_t & rhs ) const ;
// Used to permit these as keys in std::map
bool operator < ( const file_id_t & rhs ) const ;
static file_id_t file_id_from_stat ( const struct stat * buf ) ;
} ;
2012-12-03 15:38:38 +08:00
2014-04-28 04:34:51 +08:00
file_id_t file_id_for_fd ( int fd ) ;
file_id_t file_id_for_path ( const wcstring & path ) ;
extern const file_id_t kInvalidFileID ;
2012-12-03 15:38:38 +08:00
2005-09-20 21:26:39 +08:00
# endif