aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/wutil.h
diff options
context:
space:
mode:
authorGravatar David Adam <zanchey@ucc.gu.uwa.edu.au>2015-07-26 10:20:13 +0800
committerGravatar David Adam <zanchey@ucc.gu.uwa.edu.au>2015-07-26 10:20:13 +0800
commit3929e9de0e69666b37df87347d5ce15663e81347 (patch)
treeb2701c439c0260840ce1c68beaebf7de1178cc53 /src/wutil.h
parent793e1afa084982dac92c4fe19e50c25e326a79c2 (diff)
parentf4d1657c22c81a7720a91026f915b80d2d6aa6e8 (diff)
Merge branch 'master' into iwyu
Diffstat (limited to 'src/wutil.h')
-rw-r--r--src/wutil.h166
1 files changed, 166 insertions, 0 deletions
diff --git a/src/wutil.h b/src/wutil.h
new file mode 100644
index 00000000..011f3477
--- /dev/null
+++ b/src/wutil.h
@@ -0,0 +1,166 @@
+/** \file wutil.h
+
+ Prototypes for wide character equivalents of various standard unix
+ functions.
+*/
+#ifndef FISH_WUTIL_H
+#define FISH_WUTIL_H
+
+#include <stdio.h>
+#include <dirent.h>
+#include <sys/types.h>
+#include <stddef.h>
+#include <time.h>
+#include <string>
+#include <stdint.h>
+#include "common.h"
+
+/**
+ Call this function on startup to create internal wutil
+ resources. This function doesn't do anything.
+*/
+void wutil_init();
+
+/**
+ Call this function on exit to free internal wutil resources
+*/
+void wutil_destroy();
+
+/**
+ Wide character version of fopen(). This sets CLO_EXEC.
+*/
+FILE *wfopen(const wcstring &path, const char *mode);
+
+/** Sets CLO_EXEC on a given fd */
+bool set_cloexec(int fd);
+
+/** 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);
+
+/** 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 opendir(). Note that opendir() is guaranteed to set close-on-exec by POSIX (hooray). */
+DIR *wopendir(const wcstring &name);
+
+/**
+ Wide character version of stat().
+*/
+int wstat(const wcstring &file_name, struct stat *buf);
+
+/**
+ Wide character version of lstat().
+*/
+int lwstat(const wcstring &file_name, struct stat *buf);
+
+/**
+ Wide character version of access().
+*/
+int waccess(const wcstring &pathname, int mode);
+
+/**
+ Wide character version of unlink().
+*/
+int wunlink(const wcstring &pathname);
+
+/**
+ Wide character version of perror().
+*/
+void wperror(const wchar_t *s);
+
+/**
+ Async-safe version of perror().
+*/
+void safe_perror(const char *message);
+
+/**
+ Async-safe version of strerror().
+*/
+const char *safe_strerror(int err);
+
+/**
+ Wide character version of getcwd().
+*/
+wchar_t *wgetcwd(wchar_t *buff, size_t sz);
+
+/**
+ Wide character version of chdir()
+*/
+int wchdir(const wcstring &dir);
+
+/**
+ 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.
+*/
+wchar_t *wrealpath(const wcstring &pathname, wchar_t *resolved_path);
+
+/**
+ Wide character version of readdir()
+*/
+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);
+
+/**
+ Wide character version of dirname()
+*/
+std::wstring wdirname(const std::wstring &path);
+
+/**
+ Wide character version of basename()
+*/
+std::wstring wbasename(const std::wstring &path);
+
+/**
+ 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.
+*/
+const wchar_t *wgettext(const wchar_t *in);
+
+/**
+ Wide character version of mkdir
+*/
+int wmkdir(const wcstring &dir, int mode);
+
+/**
+ Wide character version of rename
+*/
+int wrename(const wcstring &oldName, const wcstring &newName);
+
+/** 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);
+
+/** 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);
+};
+
+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;
+
+
+#endif