-
Notifications
You must be signed in to change notification settings - Fork 0
/
badness.cc
executable file
·65 lines (53 loc) · 1.38 KB
/
badness.cc
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
// This program takes path to a file as a runtime parameter. It opens the
// file, reads at most the first 40 bytes, and prints them to the console as
// a quoted string.
//
// Except that it doesn't do that because the program is buggy. It compiles
// without issues but it hangs every time it runs and never prints anything.
// What's wrong with it?
#include <iomanip>
#include <iostream>
#include <stdexcept>
#include <string>
#include <fcntl.h>
#include <unistd.h>
struct fd_t {
fd_t()
: handle(-1) {}
fd_t(int new_handle)
: handle(new_handle) {
if (new_handle < 0) {
throw std::runtime_error { "open failed" };
}
}
~fd_t() {
if (*this) {
close(handle);
}
}
explicit operator bool() const {
return handle >= 0;
}
int operator*() const {
return handle;
}
int handle;
}; // fd_t
std::string read_some(const char *path, size_t max_size) {
fd_t fd { open(path, O_RDONLY) };
std::string result(max_size, 'x');
auto actual_size =
read(*fd, const_cast<char *>(result.data()), result.size());
if (actual_size < 0) {
throw std::runtime_error { "read failed" };
}
result.resize(actual_size);
return result;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
throw std::runtime_error { "usage: badness <path>" };
}
std::cout << std::quoted(read_some(argv[1], 40).c_str()) << std::endl;
return 0;
}