-
Notifications
You must be signed in to change notification settings - Fork 1
/
os.cpp
72 lines (64 loc) · 1.88 KB
/
os.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* Copyright (c) 2009, Markus Peloquin <markus@cs.wisc.edu>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED 'AS IS' AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
#include <linux/fs.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <cerrno>
#include "errors.hpp"
#include "os.hpp"
/** \throw std::system_error */
uint32_t
fluks::num_sectors(int fd) {
uint64_t sz;
int sz_sect = sector_size(fd);
if (ioctl(fd, BLKGETSIZE64, &sz) == -1)
throw_errno(errno);
return static_cast<uint32_t>(sz / sz_sect);
}
/** \throw std::system_error */
int
fluks::sector_size(int fd) {
int sz_sect;
if (ioctl(fd, BLKSSZGET, &sz_sect) == -1)
throw_errno(errno);
return sz_sect;
}
/** \throw std::system_error */
bool
fluks::term_echo() {
struct termios term;
if (tcgetattr(STDIN_FILENO, &term) == -1)
throw_errno(errno);
return term.c_lflag & ECHO;
}
/** \throw std::system_error */
bool
fluks::term_echo(bool enable) {
struct termios term;
bool old;
if (tcgetattr(STDIN_FILENO, &term) == -1)
throw_errno(errno);
old = term.c_lflag & ECHO;
if (enable)
term.c_lflag |= ECHO;
else
term.c_lflag &= ~ECHO;
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &term) == -1)
throw_errno(errno);
return old;
}