2005-09-20 13:26:39 +00:00
|
|
|
/** \file wutil.c
|
2012-11-18 10:23:22 +00:00
|
|
|
Wide character equivalents of various standard unix
|
|
|
|
functions.
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <limits.h>
|
2006-06-14 13:22:40 +00:00
|
|
|
#include <libgen.h>
|
2012-02-17 23:55:54 +00:00
|
|
|
#include <pthread.h>
|
2015-07-25 15:14:25 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <features.h>
|
2011-12-27 03:18:46 +00:00
|
|
|
#include <string>
|
2012-02-24 20:13:35 +00:00
|
|
|
#include <map>
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-02-28 13:17:16 +00:00
|
|
|
#include "fallback.h"
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "common.h"
|
|
|
|
#include "wutil.h"
|
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
typedef std::string cstring;
|
|
|
|
|
2014-05-31 20:46:28 +00:00
|
|
|
const file_id_t kInvalidFileID = {(dev_t)-1LL, (ino_t)-1LL, (uint64_t)-1LL, -1, -1, (uint32_t)-1};
|
2014-04-27 20:34:51 +00:00
|
|
|
|
2006-06-20 00:50:10 +00:00
|
|
|
/**
|
|
|
|
Minimum length of the internal covnersion buffers
|
|
|
|
*/
|
2005-11-02 15:41:59 +00:00
|
|
|
#define TMP_LEN_MIN 256
|
|
|
|
|
2006-02-02 15:23:56 +00:00
|
|
|
#ifndef PATH_MAX
|
|
|
|
#ifdef MAXPATHLEN
|
|
|
|
#define PATH_MAX MAXPATHLEN
|
|
|
|
#else
|
2006-06-20 00:50:10 +00:00
|
|
|
/**
|
|
|
|
Fallback length of MAXPATHLEN. Just a hopefully sane value...
|
|
|
|
*/
|
2006-02-02 15:23:56 +00:00
|
|
|
#define PATH_MAX 4096
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2012-02-24 20:13:35 +00:00
|
|
|
/* Lock to protect wgettext */
|
|
|
|
static pthread_mutex_t wgettext_lock;
|
|
|
|
|
|
|
|
/* Maps string keys to (immortal) pointers to string values */
|
|
|
|
typedef std::map<wcstring, wcstring *> wgettext_map_t;
|
|
|
|
static std::map<wcstring, wcstring *> wgettext_map;
|
2006-07-19 22:55:49 +00:00
|
|
|
|
2005-11-02 15:41:59 +00:00
|
|
|
void wutil_init()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
void wutil_destroy()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-02-20 10:13:31 +00:00
|
|
|
bool wreaddir_resolving(DIR *dir, const std::wstring &dir_path, std::wstring &out_name, bool *out_is_dir)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
struct dirent *d = readdir(dir);
|
|
|
|
if (!d) return false;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-02-20 10:13:31 +00:00
|
|
|
out_name = str2wcstring(d->d_name);
|
2012-11-19 00:30:30 +00:00
|
|
|
if (out_is_dir)
|
|
|
|
{
|
2012-05-08 00:31:24 +00:00
|
|
|
/* The caller cares if this is a directory, so check */
|
2014-07-07 09:45:30 +00:00
|
|
|
bool is_dir = false;
|
|
|
|
|
|
|
|
/* We may be able to skip stat, if the readdir can tell us the file type directly */
|
|
|
|
bool check_with_stat = true;
|
2014-12-11 07:24:42 +00:00
|
|
|
#ifdef HAVE_STRUCT_DIRENT_D_TYPE
|
2012-11-19 00:30:30 +00:00
|
|
|
if (d->d_type == DT_DIR)
|
|
|
|
{
|
2014-07-07 09:45:30 +00:00
|
|
|
/* Known directory */
|
2012-02-20 10:13:31 +00:00
|
|
|
is_dir = true;
|
2014-07-07 09:45:30 +00:00
|
|
|
check_with_stat = false;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
else if (d->d_type == DT_LNK || d->d_type == DT_UNKNOWN)
|
|
|
|
{
|
2012-02-20 10:13:31 +00:00
|
|
|
/* We want to treat symlinks to directories as directories. Use stat to resolve it. */
|
2014-07-07 09:45:30 +00:00
|
|
|
check_with_stat = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Regular file */
|
|
|
|
is_dir = false;
|
|
|
|
check_with_stat = false;
|
|
|
|
}
|
2014-12-11 07:24:42 +00:00
|
|
|
#endif // HAVE_STRUCT_DIRENT_D_TYPE
|
2014-07-07 09:45:30 +00:00
|
|
|
if (check_with_stat)
|
|
|
|
{
|
|
|
|
/* We couldn't determine the file type from the dirent; check by stat'ing it */
|
2012-02-20 10:13:31 +00:00
|
|
|
cstring fullpath = wcs2string(dir_path);
|
|
|
|
fullpath.push_back('/');
|
|
|
|
fullpath.append(d->d_name);
|
|
|
|
struct stat buf;
|
2012-11-19 00:30:30 +00:00
|
|
|
if (stat(fullpath.c_str(), &buf) != 0)
|
|
|
|
{
|
2012-02-20 10:13:31 +00:00
|
|
|
is_dir = false;
|
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
is_dir = !!(S_ISDIR(buf.st_mode));
|
|
|
|
}
|
|
|
|
}
|
2012-02-20 10:13:31 +00:00
|
|
|
*out_is_dir = is_dir;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wreaddir(DIR *dir, std::wstring &out_name)
|
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
struct dirent *d = readdir(dir);
|
|
|
|
if (!d) return false;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-02-20 10:13:31 +00:00
|
|
|
out_name = str2wcstring(d->d_name);
|
2012-02-18 02:08:08 +00:00
|
|
|
return true;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2006-02-08 14:58:47 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
wchar_t *wgetcwd(wchar_t *buff, size_t sz)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
char *buffc = (char *)malloc(sz*MAX_UTF8_BYTES);
|
|
|
|
char *res;
|
|
|
|
wchar_t *ret = 0;
|
|
|
|
|
|
|
|
if (!buffc)
|
2012-11-18 10:23:22 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
errno = ENOMEM;
|
|
|
|
return 0;
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
res = getcwd(buffc, sz*MAX_UTF8_BYTES);
|
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
if ((size_t)-1 != mbstowcs(buff, buffc, sizeof(wchar_t) * sz))
|
|
|
|
{
|
|
|
|
ret = buff;
|
|
|
|
}
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
free(buffc);
|
|
|
|
|
|
|
|
return ret;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
int wchdir(const wcstring &dir)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
cstring tmp = wcs2string(dir);
|
2012-11-19 00:30:30 +00:00
|
|
|
return chdir(tmp.c_str());
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2012-02-18 17:11:22 +00:00
|
|
|
FILE *wfopen(const wcstring &path, const char *mode)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-03-02 08:27:40 +00:00
|
|
|
int permissions = 0, options = 0;
|
2012-03-04 06:48:21 +00:00
|
|
|
size_t idx = 0;
|
2012-11-19 00:30:30 +00:00
|
|
|
switch (mode[idx++])
|
|
|
|
{
|
2012-11-19 08:31:03 +00:00
|
|
|
case 'r':
|
|
|
|
permissions = O_RDONLY;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
permissions = O_WRONLY;
|
|
|
|
options = O_CREAT | O_TRUNC;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
permissions = O_WRONLY;
|
|
|
|
options = O_CREAT | O_APPEND;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errno = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
break;
|
2012-03-02 08:27:40 +00:00
|
|
|
}
|
|
|
|
/* Skip binary */
|
2012-03-04 06:48:21 +00:00
|
|
|
if (mode[idx] == 'b')
|
|
|
|
idx++;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-03-02 08:27:40 +00:00
|
|
|
/* Consider append option */
|
2012-03-04 06:48:21 +00:00
|
|
|
if (mode[idx] == '+')
|
2012-03-02 08:27:40 +00:00
|
|
|
permissions = O_RDWR;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-03-02 08:27:40 +00:00
|
|
|
int fd = wopen_cloexec(path, permissions | options, 0666);
|
|
|
|
if (fd < 0)
|
|
|
|
return NULL;
|
|
|
|
FILE *result = fdopen(fd, mode);
|
|
|
|
if (result == NULL)
|
|
|
|
close(fd);
|
|
|
|
return result;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
bool set_cloexec(int fd)
|
|
|
|
{
|
2012-03-02 08:27:40 +00:00
|
|
|
int flags = fcntl(fd, F_GETFD, 0);
|
2012-11-19 00:30:30 +00:00
|
|
|
if (flags < 0)
|
|
|
|
{
|
2012-03-02 08:27:40 +00:00
|
|
|
return false;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
else if (flags & FD_CLOEXEC)
|
|
|
|
{
|
2012-03-02 08:27:40 +00:00
|
|
|
return true;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-02 08:27:40 +00:00
|
|
|
return fcntl(fd, F_SETFD, flags | FD_CLOEXEC) >= 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int wopen_internal(const wcstring &pathname, int flags, mode_t mode, bool cloexec)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-03-09 07:21:07 +00:00
|
|
|
ASSERT_IS_NOT_FORKED_CHILD();
|
2011-12-27 03:18:46 +00:00
|
|
|
cstring tmp = wcs2string(pathname);
|
2015-07-20 09:34:57 +00:00
|
|
|
/* Prefer to use O_CLOEXEC. It has to both be defined and nonzero. */
|
2012-03-02 17:58:29 +00:00
|
|
|
#ifdef O_CLOEXEC
|
2015-07-20 09:34:57 +00:00
|
|
|
if (cloexec && (O_CLOEXEC != 0))
|
2012-11-19 00:30:30 +00:00
|
|
|
{
|
2012-03-02 17:58:29 +00:00
|
|
|
flags |= O_CLOEXEC;
|
|
|
|
cloexec = false;
|
|
|
|
}
|
|
|
|
#endif
|
2012-03-02 08:27:40 +00:00
|
|
|
int fd = ::open(tmp.c_str(), flags, mode);
|
2012-11-19 00:30:30 +00:00
|
|
|
if (cloexec && fd >= 0 && ! set_cloexec(fd))
|
|
|
|
{
|
2012-03-02 08:27:40 +00:00
|
|
|
close(fd);
|
|
|
|
fd = -1;
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
2012-03-02 08:27:40 +00:00
|
|
|
return fd;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-03-02 08:27:40 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-03-02 08:27:40 +00:00
|
|
|
int wopen_cloexec(const wcstring &pathname, int flags, mode_t mode)
|
|
|
|
{
|
|
|
|
return wopen_internal(pathname, flags, mode, true);
|
|
|
|
}
|
|
|
|
|
2012-02-18 17:11:22 +00:00
|
|
|
DIR *wopendir(const wcstring &name)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2014-04-27 20:34:51 +00:00
|
|
|
const cstring tmp = wcs2string(name);
|
2011-12-27 03:18:46 +00:00
|
|
|
return opendir(tmp.c_str());
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2012-02-18 17:11:22 +00:00
|
|
|
int wstat(const wcstring &file_name, struct stat *buf)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2014-04-27 20:34:51 +00:00
|
|
|
const cstring tmp = wcs2string(file_name);
|
2011-12-27 03:18:46 +00:00
|
|
|
return stat(tmp.c_str(), buf);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2012-02-18 17:11:22 +00:00
|
|
|
int lwstat(const wcstring &file_name, struct stat *buf)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2014-04-27 20:34:51 +00:00
|
|
|
const cstring tmp = wcs2string(file_name);
|
2011-12-27 03:18:46 +00:00
|
|
|
return lstat(tmp.c_str(), buf);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2012-02-18 17:11:22 +00:00
|
|
|
int waccess(const wcstring &file_name, int mode)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2014-04-27 20:34:51 +00:00
|
|
|
const cstring tmp = wcs2string(file_name);
|
2011-12-27 03:18:46 +00:00
|
|
|
return access(tmp.c_str(), mode);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2012-02-18 17:11:22 +00:00
|
|
|
int wunlink(const wcstring &file_name)
|
2012-02-16 08:24:27 +00:00
|
|
|
{
|
2014-04-27 20:34:51 +00:00
|
|
|
const cstring tmp = wcs2string(file_name);
|
2012-02-16 08:24:27 +00:00
|
|
|
return unlink(tmp.c_str());
|
|
|
|
}
|
|
|
|
|
2014-04-27 20:34:51 +00:00
|
|
|
void wperror(const wchar_t *s)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
int e = errno;
|
2014-04-27 20:34:51 +00:00
|
|
|
if (s[0] != L'\0')
|
2012-11-19 00:30:30 +00:00
|
|
|
{
|
2014-04-27 20:34:51 +00:00
|
|
|
fwprintf(stderr, L"%ls: ", s);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
fwprintf(stderr, L"%s\n", strerror(e));
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2013-04-07 19:40:08 +00:00
|
|
|
int make_fd_nonblocking(int fd)
|
|
|
|
{
|
|
|
|
int flags = fcntl(fd, F_GETFL, 0);
|
|
|
|
int err = 0;
|
2013-05-05 09:33:17 +00:00
|
|
|
if (!(flags & O_NONBLOCK))
|
2013-04-07 19:40:08 +00:00
|
|
|
{
|
|
|
|
err = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
}
|
|
|
|
return err == -1 ? errno : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int make_fd_blocking(int fd)
|
|
|
|
{
|
|
|
|
int flags = fcntl(fd, F_GETFL, 0);
|
|
|
|
int err = 0;
|
|
|
|
if (flags & O_NONBLOCK)
|
|
|
|
{
|
|
|
|
err = fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
|
|
|
|
}
|
|
|
|
return err == -1 ? errno : 0;
|
|
|
|
}
|
|
|
|
|
2013-01-10 01:06:20 +00:00
|
|
|
static inline void safe_append(char *buffer, const char *s, size_t buffsize)
|
|
|
|
{
|
|
|
|
strncat(buffer, s, buffsize - strlen(buffer) - 1);
|
|
|
|
}
|
|
|
|
|
2014-05-31 20:46:28 +00:00
|
|
|
// In general, strerror is not async-safe, and therefore we cannot use it directly
|
|
|
|
// So instead we have to grub through sys_nerr and sys_errlist directly
|
|
|
|
// On GNU toolchain, this will produce a deprecation warning from the linker (!!),
|
|
|
|
// which appears impossible to suppress!
|
2013-01-10 01:06:20 +00:00
|
|
|
const char *safe_strerror(int err)
|
|
|
|
{
|
2013-05-25 20:42:16 +00:00
|
|
|
#if defined(__UCLIBC__)
|
|
|
|
// uClibc does not have sys_errlist, however, its strerror is believed to be async-safe
|
|
|
|
// See #808
|
|
|
|
return strerror(err);
|
2014-12-08 00:43:38 +00:00
|
|
|
#elif defined(HAVE__SYS__ERRS) || defined(HAVE_SYS_ERRLIST)
|
|
|
|
#ifdef HAVE_SYS_ERRLIST
|
2013-01-10 01:06:20 +00:00
|
|
|
if (err >= 0 && err < sys_nerr && sys_errlist[err] != NULL)
|
|
|
|
{
|
|
|
|
return sys_errlist[err];
|
|
|
|
}
|
2014-12-08 00:43:38 +00:00
|
|
|
#elif defined(HAVE__SYS__ERRS)
|
|
|
|
extern const char _sys_errs[];
|
|
|
|
extern const int _sys_index[];
|
|
|
|
extern int _sys_num_err;
|
|
|
|
|
|
|
|
if (err >= 0 && err < _sys_num_err) {
|
|
|
|
return &_sys_errs[_sys_index[err]];
|
|
|
|
}
|
|
|
|
#endif // either HAVE__SYS__ERRS or HAVE_SYS_ERRLIST
|
2013-01-10 01:06:20 +00:00
|
|
|
else
|
2014-12-08 00:43:38 +00:00
|
|
|
#endif // defined(HAVE__SYS__ERRS) || defined(HAVE_SYS_ERRLIST)
|
2013-01-10 01:06:20 +00:00
|
|
|
{
|
|
|
|
int saved_err = errno;
|
2013-01-12 20:55:23 +00:00
|
|
|
|
2013-01-10 01:06:20 +00:00
|
|
|
/* Use a shared buffer for this case */
|
|
|
|
static char buff[384];
|
|
|
|
char errnum_buff[64];
|
|
|
|
format_long_safe(errnum_buff, err);
|
2013-01-12 20:55:23 +00:00
|
|
|
|
2013-01-10 01:06:20 +00:00
|
|
|
buff[0] = '\0';
|
|
|
|
safe_append(buff, "unknown error (errno was ", sizeof buff);
|
|
|
|
safe_append(buff, errnum_buff, sizeof buff);
|
|
|
|
safe_append(buff, ")", sizeof buff);
|
2013-01-12 20:55:23 +00:00
|
|
|
|
2013-01-10 01:06:20 +00:00
|
|
|
errno = saved_err;
|
|
|
|
return buff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void safe_perror(const char *message)
|
|
|
|
{
|
|
|
|
// Note we cannot use strerror, because on Linux it uses gettext, which is not safe
|
|
|
|
int err = errno;
|
2013-01-12 20:55:23 +00:00
|
|
|
|
2013-01-10 01:06:20 +00:00
|
|
|
char buff[384];
|
|
|
|
buff[0] = '\0';
|
2013-01-12 20:55:23 +00:00
|
|
|
|
|
|
|
if (message)
|
|
|
|
{
|
2013-01-10 01:06:20 +00:00
|
|
|
safe_append(buff, message, sizeof buff);
|
|
|
|
safe_append(buff, ": ", sizeof buff);
|
|
|
|
}
|
|
|
|
safe_append(buff, safe_strerror(err), sizeof buff);
|
|
|
|
safe_append(buff, "\n", sizeof buff);
|
2013-01-12 20:55:23 +00:00
|
|
|
|
2014-04-28 01:27:34 +00:00
|
|
|
write_ignore(STDERR_FILENO, buff, strlen(buff));
|
2013-01-10 01:06:20 +00:00
|
|
|
errno = err;
|
|
|
|
}
|
|
|
|
|
2006-02-02 15:23:56 +00:00
|
|
|
#ifdef HAVE_REALPATH_NULL
|
|
|
|
|
2012-02-18 17:11:22 +00:00
|
|
|
wchar_t *wrealpath(const wcstring &pathname, wchar_t *resolved_path)
|
2006-02-02 15:23:56 +00:00
|
|
|
{
|
2012-12-19 21:31:06 +00:00
|
|
|
cstring narrow_path = wcs2string(pathname);
|
|
|
|
char *narrow_res = realpath(narrow_path.c_str(), NULL);
|
2012-11-19 00:30:30 +00:00
|
|
|
|
|
|
|
if (!narrow_res)
|
2012-12-19 21:31:06 +00:00
|
|
|
return NULL;
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2012-12-19 21:31:06 +00:00
|
|
|
wchar_t *res;
|
|
|
|
wcstring wide_res = str2wcstring(narrow_res);
|
2012-11-19 00:30:30 +00:00
|
|
|
if (resolved_path)
|
|
|
|
{
|
2012-12-19 21:31:06 +00:00
|
|
|
wcslcpy(resolved_path, wide_res.c_str(), PATH_MAX);
|
2012-11-19 00:30:30 +00:00
|
|
|
res = resolved_path;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-12-19 21:31:06 +00:00
|
|
|
res = wcsdup(wide_res.c_str());
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(narrow_res);
|
|
|
|
|
|
|
|
return res;
|
2006-02-02 15:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2012-06-23 05:26:50 +00:00
|
|
|
wchar_t *wrealpath(const wcstring &pathname, wchar_t *resolved_path)
|
2006-02-02 15:23:56 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
cstring tmp = wcs2string(pathname);
|
2012-11-19 00:30:30 +00:00
|
|
|
char narrow_buff[PATH_MAX];
|
|
|
|
char *narrow_res = realpath(tmp.c_str(), narrow_buff);
|
|
|
|
wchar_t *res;
|
|
|
|
|
|
|
|
if (!narrow_res)
|
|
|
|
return 0;
|
2013-01-04 21:09:01 +00:00
|
|
|
|
2012-12-25 04:55:35 +00:00
|
|
|
const wcstring wide_res = str2wcstring(narrow_res);
|
2012-11-19 00:30:30 +00:00
|
|
|
if (resolved_path)
|
|
|
|
{
|
2012-12-25 04:55:35 +00:00
|
|
|
wcslcpy(resolved_path, wide_res.c_str(), PATH_MAX);
|
2012-11-19 00:30:30 +00:00
|
|
|
res = resolved_path;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-12-25 04:55:35 +00:00
|
|
|
res = wcsdup(wide_res.c_str());
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
return res;
|
2006-02-02 15:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2006-06-14 13:22:40 +00:00
|
|
|
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
wcstring wdirname(const wcstring &path)
|
2006-06-14 13:22:40 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
char *tmp = wcs2str(path.c_str());
|
2012-11-19 00:30:30 +00:00
|
|
|
char *narrow_res = dirname(tmp);
|
2011-12-27 03:18:46 +00:00
|
|
|
wcstring result = format_string(L"%s", narrow_res);
|
|
|
|
free(tmp);
|
|
|
|
return result;
|
2006-06-14 13:22:40 +00:00
|
|
|
}
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
wcstring wbasename(const wcstring &path)
|
2006-06-14 13:22:40 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
char *tmp = wcs2str(path.c_str());
|
2012-11-19 00:30:30 +00:00
|
|
|
char *narrow_res = basename(tmp);
|
2011-12-27 03:18:46 +00:00
|
|
|
wcstring result = format_string(L"%s", narrow_res);
|
|
|
|
free(tmp);
|
|
|
|
return result;
|
2006-06-14 13:22:40 +00:00
|
|
|
}
|
|
|
|
|
2012-02-17 23:55:54 +00:00
|
|
|
/* Really init wgettext */
|
2012-11-19 00:30:30 +00:00
|
|
|
static void wgettext_really_init()
|
|
|
|
{
|
2012-02-24 20:13:35 +00:00
|
|
|
pthread_mutex_init(&wgettext_lock, NULL);
|
2013-04-08 17:20:56 +00:00
|
|
|
fish_bindtextdomain(PACKAGE_NAME, LOCALEDIR);
|
|
|
|
fish_textdomain(PACKAGE_NAME);
|
2006-07-19 22:55:49 +00:00
|
|
|
}
|
|
|
|
|
2012-02-17 23:55:54 +00:00
|
|
|
/**
|
|
|
|
For wgettext: Internal init function. Automatically called when a translation is first requested.
|
|
|
|
*/
|
|
|
|
static void wgettext_init_if_necessary()
|
|
|
|
{
|
|
|
|
static pthread_once_t once = PTHREAD_ONCE_INIT;
|
|
|
|
pthread_once(&once, wgettext_really_init);
|
|
|
|
}
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
const wchar_t *wgettext(const wchar_t *in)
|
2006-07-19 22:55:49 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
if (!in)
|
|
|
|
return in;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
// preserve errno across this since this is often used in printing error messages
|
|
|
|
int err = errno;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-02-17 23:55:54 +00:00
|
|
|
wgettext_init_if_necessary();
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
wcstring key = in;
|
2012-02-24 20:13:35 +00:00
|
|
|
scoped_lock lock(wgettext_lock);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-02-24 20:13:35 +00:00
|
|
|
wcstring *& val = wgettext_map[key];
|
2012-11-19 00:30:30 +00:00
|
|
|
if (val == NULL)
|
|
|
|
{
|
2012-02-24 20:13:35 +00:00
|
|
|
cstring mbs_in = wcs2string(key);
|
2013-04-08 17:20:56 +00:00
|
|
|
char *out = fish_gettext(mbs_in.c_str());
|
2013-12-09 05:54:06 +00:00
|
|
|
val = new wcstring(format_string(L"%s", out)); //note that this writes into the map!
|
2012-02-24 20:13:35 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
errno = err;
|
2014-10-31 05:40:35 +00:00
|
|
|
return val->c_str(); //looks dangerous but is safe, since the string is stored in the map
|
2006-07-19 22:55:49 +00:00
|
|
|
}
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
int wmkdir(const wcstring &name, int mode)
|
2006-09-08 14:11:28 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
cstring name_narrow = wcs2string(name);
|
|
|
|
return mkdir(name_narrow.c_str(), mode);
|
2006-09-08 14:11:28 +00:00
|
|
|
}
|
2006-10-20 22:33:47 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
int wrename(const wcstring &old, const wcstring &newv)
|
2006-10-20 22:33:47 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
cstring old_narrow = wcs2string(old);
|
2012-11-19 00:30:30 +00:00
|
|
|
cstring new_narrow =wcs2string(newv);
|
|
|
|
return rename(old_narrow.c_str(), new_narrow.c_str());
|
2006-10-20 22:33:47 +00:00
|
|
|
}
|
2012-08-04 18:07:42 +00:00
|
|
|
|
|
|
|
int fish_wcstoi(const wchar_t *str, wchar_t ** endptr, int base)
|
|
|
|
{
|
|
|
|
long ret = wcstol(str, endptr, base);
|
|
|
|
if (ret > INT_MAX)
|
|
|
|
{
|
|
|
|
ret = INT_MAX;
|
|
|
|
errno = ERANGE;
|
|
|
|
}
|
|
|
|
else if (ret < INT_MIN)
|
|
|
|
{
|
|
|
|
ret = INT_MIN;
|
|
|
|
errno = ERANGE;
|
|
|
|
}
|
|
|
|
return (int)ret;
|
|
|
|
}
|
2014-04-27 20:34:51 +00:00
|
|
|
|
2014-04-28 22:14:33 +00:00
|
|
|
file_id_t file_id_t::file_id_from_stat(const struct stat *buf)
|
|
|
|
{
|
|
|
|
assert(buf != NULL);
|
|
|
|
|
|
|
|
file_id_t result = {};
|
|
|
|
result.device = buf->st_dev;
|
|
|
|
result.inode = buf->st_ino;
|
|
|
|
result.size = buf->st_size;
|
|
|
|
result.change_seconds = buf->st_ctime;
|
|
|
|
|
|
|
|
#if STAT_HAVE_NSEC
|
|
|
|
result.change_nanoseconds = buf->st_ctime_nsec;
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
result.change_nanoseconds = buf->st_ctimespec.tv_nsec;
|
|
|
|
#elif defined(_BSD_SOURCE) || defined(_SVID_SOURCE) || defined(_XOPEN_SOURCE)
|
|
|
|
result.change_nanoseconds = buf->st_ctim.tv_nsec;
|
|
|
|
#else
|
|
|
|
result.change_nanoseconds = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__APPLE__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
|
|
|
|
result.generation = buf->st_gen;
|
|
|
|
#else
|
|
|
|
result.generation = 0;
|
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-27 20:34:51 +00:00
|
|
|
file_id_t file_id_for_fd(int fd)
|
|
|
|
{
|
|
|
|
file_id_t result = kInvalidFileID;
|
|
|
|
struct stat buf = {};
|
|
|
|
if (0 == fstat(fd, &buf))
|
|
|
|
{
|
2014-04-28 22:14:33 +00:00
|
|
|
result = file_id_t::file_id_from_stat(&buf);
|
2014-04-27 20:34:51 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
file_id_t file_id_for_path(const wcstring &path)
|
|
|
|
{
|
|
|
|
file_id_t result = kInvalidFileID;
|
|
|
|
struct stat buf = {};
|
|
|
|
if (0 == wstat(path, &buf))
|
|
|
|
{
|
2014-04-28 22:14:33 +00:00
|
|
|
result = file_id_t::file_id_from_stat(&buf);
|
2014-04-27 20:34:51 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
2014-04-28 22:14:33 +00:00
|
|
|
|
|
|
|
bool file_id_t::operator==(const file_id_t &rhs) const
|
|
|
|
{
|
|
|
|
return device == rhs.device &&
|
|
|
|
inode == rhs.inode &&
|
|
|
|
size == rhs.size &&
|
|
|
|
change_seconds == rhs.change_seconds &&
|
|
|
|
change_nanoseconds == rhs.change_nanoseconds &&
|
|
|
|
generation == rhs.generation;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool file_id_t::operator!=(const file_id_t &rhs) const
|
|
|
|
{
|
|
|
|
return ! (*this == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
int compare(T a, T b)
|
|
|
|
{
|
|
|
|
if (a < b)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (a > b)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool file_id_t::operator<(const file_id_t &rhs) const
|
|
|
|
{
|
|
|
|
/* Compare each field, stopping when we get to a non-equal field */
|
|
|
|
int ret = 0;
|
|
|
|
if (! ret) ret = compare(device, rhs.device);
|
|
|
|
if (! ret) ret = compare(inode, rhs.inode);
|
|
|
|
if (! ret) ret = compare(size, rhs.size);
|
|
|
|
if (! ret) ret = compare(generation, rhs.generation);
|
|
|
|
if (! ret) ret = compare(change_seconds, rhs.change_seconds);
|
|
|
|
if (! ret) ret = compare(change_nanoseconds, rhs.change_nanoseconds);
|
|
|
|
return ret < 0;
|
|
|
|
}
|