diff options
| author | Adrian Kummerlaender | 2015-10-18 17:35:11 +0200 | 
|---|---|---|
| committer | Adrian Kummerlaender | 2015-10-18 17:35:11 +0200 | 
| commit | 68b69dfc92daffbe52b4eb7eb317beb2cc764fb0 (patch) | |
| tree | 1748b69328c78c80fbf327911542bec6d75f73f2 /src/io.h | |
| parent | 6a66bd864b9b95258a198e65849fa693f88032aa (diff) | |
| download | change-68b69dfc92daffbe52b4eb7eb317beb2cc764fb0.tar change-68b69dfc92daffbe52b4eb7eb317beb2cc764fb0.tar.gz change-68b69dfc92daffbe52b4eb7eb317beb2cc764fb0.tar.bz2 change-68b69dfc92daffbe52b4eb7eb317beb2cc764fb0.tar.lz change-68b69dfc92daffbe52b4eb7eb317beb2cc764fb0.tar.xz change-68b69dfc92daffbe52b4eb7eb317beb2cc764fb0.tar.zst change-68b69dfc92daffbe52b4eb7eb317beb2cc764fb0.zip  | |
Add cmake build instructions
Diffstat (limited to 'src/io.h')
| -rw-r--r-- | src/io.h | 61 | 
1 files changed, 61 insertions, 0 deletions
diff --git a/src/io.h b/src/io.h new file mode 100644 index 0000000..87778ec --- /dev/null +++ b/src/io.h @@ -0,0 +1,61 @@ +#ifndef CHANGE_IO_H_ +#define CHANGE_IO_H_ + +#include <unistd.h> +#include <dlfcn.h> +#include <fcntl.h> +#include <sys/stat.h> + +#include <cstring> + +namespace io { + +class FileDescriptorGuard { +	public: +		FileDescriptorGuard(const std::string& path) { +			this->fd = open(path.c_str(), O_WRONLY | O_APPEND); + +			if ( !this->fd ) { +				this->fd = STDERR_FILENO; +			} +		} + +		~FileDescriptorGuard() { +			close(this->fd); +		} + +		operator int() { +			return this->fd; +		} + +	private: +		int fd; + +}; + +std::string get_file_name(int fd) { +	char proc_link[20]; +	char file_name[256]; + +	snprintf(proc_link, sizeof(proc_link), "/proc/self/fd/%d", fd); +	const ssize_t name_size = readlink(proc_link, file_name, sizeof(file_name)); + +	if ( name_size > 0 ) { +		file_name[name_size] = '\0'; + +		return std::string(file_name); +	} else { +		return std::string(); +	} +} + +bool is_regular_file(int fd) { +	struct stat fd_stat; +	fstat(fd, &fd_stat); + +	return S_ISREG(fd_stat.st_mode); +} + +} + +#endif  // CHANGE_IO_H_  | 
